Position DIV in the center of the page

Asked

Viewed 38,304 times

3

How do I align the div with black and transparent background where it contains the buttons in the center of the page independent of the monitor resolution?

inserir a descrição da imagem aqui

The code I’m using is this:

.fundo{
    background-color: rgba(10,23,55,0.7);
    padding: 20px;
    margin: 200px auto;
}

<div class="col-md-12 fundo" align="center"> 
    <button class="btn btn-primary">ACESSO CONSULTOR</button>
    <button class="btn btn-primary">ACESSO GERENTE</button>
 </div>
  • I don’t understand your doubt, it’s already centered

  • Hello Rafael. But I would like to adjust the code so that it is compatible in other resolutions. I’m not very good at CSS and I don’t know if the code I’m using is right.

  • Are you using any front-end framework?

  • Hi Lukas. I’m using Bootstrap, but the div containing the buttons is right after the body tag

  • maybe this will help you a little bit, https://answall.com/questions/8602/comor- we are divs-responsive

  • Center anything: https://answall.com/a/252419/8063

Show 1 more comment

3 answers

8

Modern times

It is currently possible to do this much more simply using flex-box, example:

.flex-box {
  display: flex;
  align-items: center;
  justify-content: center;
}

.container-box {
  background-color: yellow;
  height: 300px;
}

.content-box {
  background-color: black;
  color: white;
  text-align: center;
  width: 200px;
}
<div class="flex-box container-box">
  <div class="content-box">Conteúdo alinhado<br> verticalmente</div>
</div>


Old times

To align an element vertically in the center of the screen, only with CSS it needs to have fixed height, example:

#centro {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align:center;
  background-color: rgba(10,23,55,0.7);
  color: white;
  
  /* pura mágica */
  position: absolute;
  top: 50%; /* posiciona na metade da tela */
  margin-top: -25px; /* e retrocede metade da altura */
}
<div id="centro">Hello World</div>

To align horizontally the logic is the same:

#centro {
  width: 200px;
  height: 50px;
  line-height: 50px;
  text-align:center;
  background-color: rgba(10,23,55,0.7);
  color: white;
  
  /* pura mágica */
  position: absolute;
  top: 50%; /* posiciona na metade da tela */
  margin-top: -25px; /* e retrocede metade da altura */
  left: 50%; /* posiciona na metade da tela */
  margin-left: -100px; /* e retrocede metade da largura */
}
<div id="centro">Hello World</div>

4

There are several techniques, the two that I like the most are the ones I will quote because they do not depend on fixed size.

Technique 1 with transform:translate

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    background: url(https://www.placecage.com/c/500/500) no-repeat center;
    background-size: cover;
}
.centro {
    height: 100px;
    width: 100%;
    background-color: rgba(0, 0, 0, .5);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
.conteudo {
    height: 50px;
    width: 50px;
    background-color: red;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
<div class="centro">
    <div class="conteudo"></div>
</div>

Technique 2, I think this is very good, because it uses display:flex and you can do the margin:auto function both in width and height!

html, body {
    width: 100%;
    height: 100%;
    display: flex;
    margin: 0;
    padding: 0;
    background: url(https://www.placecage.com/c/500/500) no-repeat center;
    background-size: cover;
}
.centro {
    margin: auto;
    background-color: rgba(0, 0, 0, .5);
    height: 100px;
    width: 100%;
    display: flex;
}
.conteudo {
    height: 50px;
    width: 50px;
    background-color: red;
    margin: auto;
}
<div class="centro">
    <div class="conteudo"></div>
</div>

[]s

  • hugocsl, I’m a fan. In CSS it’s you and @Sam.

  • 1

    @Jason-Theinternship. Haha was worth my young man, but I’ll tell you something... There are some monsters hidden here on the site that know even more than we rss. []’s

  • hugocsl, without wanting to abuse. But I’m applying styling on a page in print mode (media print). I wonder if there is a way I can use two stylizations, one for portrait mode and another for landscape mode (In this case, use a media query for each maybe. Or another mode). It would be for graphic display. If you can help me, or refer me to a question that has an answer from you that answers my question, you could link it here. Or I open a question on the subject. Thank you!

  • 1

    @Jason-Theinternship. without more details I can not help you much, but take a look at these two answers, there I speak of how to pick up the orientation of the device if it is in Portrait or Landscape https://answall.com/questions/396278/howto detectlargura-da-tela-menor-que-a-altura-com-css/396282#396282 and here tb https://answall.com/questions/292233/blockr-modo-paisagem-via-css-na-visualiza%C3%A7%C3%a3o-pelo-celular/292236#292236 this is the list of everything ká answered here about printing and CSS https://en.stackoverflow.com/search?q=user%3A97477+print sometimes helps you tb

  • 1

    You helped me out there, man. Thanks a lot!

0

First, in this background photo, set its size in width 100%.

Next:

.fundo{
background-color: rgba(10,23,55,0.7);
margin: auto;
}

<div class="col-md-12 fundo" align="center"> 
<button class="btn btn-primary">ACESSO CONSULTOR</button>
<button class="btn btn-primary">ACESSO GERENTE</button>
</div>

Browser other questions tagged

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