How to position the Text of a span below a Spinner

Asked

Viewed 86 times

2

How do I position a centralized tag text below the spinner? It is positioned at the top left corner of the page...

Thank you and a hug to all!

inserir a descrição da imagem aqui

<div id="cover-spin">
    <span>Carregando...</span>
</div>

#cover-spin {
    position: fixed;
    width: 100%;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #f5f5f5;
    z-index: 9999;
    display: none;
}


#cover-spin::after {
    content: '';
    display: block;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 60px;
    height: 60px;
    margin: 0px auto;
    border-style: solid;
    border-color: black;
    border-top-color: transparent;
    border-width: 4px;
    border-radius: 50%;
    -webkit-animation: spin .8s linear infinite;
    animation: spin .8s linear infinite;
    -webkit-animation: rotation .6s infinite linear;
    -moz-animation: rotation .6s infinite linear;
    -o-animation: rotation .6s infinite linear;
    animation: rotation .6s infinite linear;
    border-left: 6px solid rgba(0,174,239,.15);
    border-right: 6px solid rgba(0,174,239,.15);
    border-bottom: 6px solid rgba(0,174,239,.15);
    border-top: 6px solid rgba(0,174,239,.8);
    border-radius: 50%;
}

@-webkit-keyframes rotation {
    from {
        -webkit-transform: rotate(0deg);
    }

    to {
        -webkit-transform: rotate(359deg);
    }
}

@-moz-keyframes rotation {
    from {
        -moz-transform: rotate(0deg);
    }

    to {
        -moz-transform: rotate(359deg);
    }
}

@-o-keyframes rotation {
    from {
        -o-transform: rotate(0deg);
    }

    to {
        -o-transform: rotate(359deg);
    }
}

@keyframes rotation {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(359deg);
    }
}

3 answers

1

Your Spinner isn’t exactly centered because you used it left and top 50%, my suggestion is to put display:flex in the container father and sets in the center with align-items and justify-content center. This will align the text and the Spiner exactly in the center of the page. Then just you give a top in the span same height as Spinner, and that’s it.

body {
margin: 0;
}
#cover-spin {
  position: fixed;
  width: 100%;
  background-color: #f5f5f5;
  z-index: 9999;
  /* display: none; */
  margin: auto;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
#cover-spin span {
  position: relative;
  top: 60px;
}


#cover-spin::after {
  content: '';
  display: block;
  position: absolute;
  width: 60px;
  height: 60px;
  border-style: solid;
  border-color: black;
  border-top-color: transparent;
  border-width: 4px;
  border-radius: 50%;
  -webkit-animation: spin .8s linear infinite;
  animation: spin .8s linear infinite;
  -webkit-animation: rotation .6s infinite linear;
  -moz-animation: rotation .6s infinite linear;
  -o-animation: rotation .6s infinite linear;
  animation: rotation .6s infinite linear;
  border-left: 6px solid rgba(0,174,239,.15);
  border-right: 6px solid rgba(0,174,239,.15);
  border-bottom: 6px solid rgba(0,174,239,.15);
  border-top: 6px solid rgba(0,174,239,.8);
  border-radius: 50%;
}

@-webkit-keyframes rotation {
  from {
    -webkit-transform: rotate(0deg);
  }

  to {
    -webkit-transform: rotate(359deg);
  }
}

@-moz-keyframes rotation {
  from {
    -moz-transform: rotate(0deg);
  }

  to {
    -moz-transform: rotate(359deg);
  }
}

@-o-keyframes rotation {
  from {
    -o-transform: rotate(0deg);
  }

  to {
    -o-transform: rotate(359deg);
  }
}

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(359deg);
  }
}
<div id="cover-spin">
  <span>Carregando...</span>
</div>

0


Apply the following properties to #cover-spin:

text-align: center; ALINHA O TEXTO AO CENTRO HORIZONTALMENTE
line-height: 100vh; ALINHA O TEXTO AO CENTRO VERTICALMENTE
padding-top: 60px;  MOVE O TEXTO PARA BAIXO DO SPIN

And change the margin of #cover-spin::after to center horizontally and vertically, in this way:

margin: -36px auto 0 -36px;

The -36px is the half of the sum of the dimension of the spin + the edges, which gives a total of 72px.

Behold:

#cover-spin {
    position: fixed;
    width: 100%;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #f5f5f5;
    z-index: 9999;
    /* display: none; */
    text-align: center;
    line-height: 100vh;
    padding-top: 60px;
}


#cover-spin::after {
    content: '';
    display: block;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 60px;
    height: 60px;
    margin: -36px auto 0 -36px;
    border-style: solid;
    border-color: black;
    border-top-color: transparent;
    border-width: 4px;
    border-radius: 50%;
    -webkit-animation: spin .8s linear infinite;
    animation: spin .8s linear infinite;
    -webkit-animation: rotation .6s infinite linear;
    -moz-animation: rotation .6s infinite linear;
    -o-animation: rotation .6s infinite linear;
    animation: rotation .6s infinite linear;
    border-left: 6px solid rgba(0,174,239,.15);
    border-right: 6px solid rgba(0,174,239,.15);
    border-bottom: 6px solid rgba(0,174,239,.15);
    border-top: 6px solid rgba(0,174,239,.8);
    border-radius: 50%;
}

@-webkit-keyframes rotation {
    from {
        -webkit-transform: rotate(0deg);
    }

    to {
        -webkit-transform: rotate(359deg);
    }
}

@-moz-keyframes rotation {
    from {
        -moz-transform: rotate(0deg);
    }

    to {
        -moz-transform: rotate(359deg);
    }
}

@-o-keyframes rotation {
    from {
        -o-transform: rotate(0deg);
    }

    to {
        -o-transform: rotate(359deg);
    }
}

@keyframes rotation {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(359deg);
    }
}
<div id="cover-spin">
    <span>Carregando...</span>
</div>

If you want to add more text to the spin, lower the padding-top: 60px.

0

You can also use this very easy to use jquery plugin.

$("#cover-spin").LoadingOverlay("show", {
    background  : "rgba(165, 190, 100, 0.5)",
    text        : "Carregando...",
    size: 25

});
#cover-spin { width:600px; height:200px; font-size:10px }
<div id="cover-spin"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/loadingoverlay.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.