Center <div>

Asked

Viewed 63 times

0

Hello, I’d like to know the reason for my <div> not being centered in the middle of the page, it is in the extreme right as in the example I created below:

div#pagina-principal {
  position: absolute;
  width: 1000px;
  background: rgba(255, 255, 255, 0.5);
  margin: 0px auto 0px auto;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, .5);
  left: 50%;
}
<div id="pagina-fundo">
  <div id="fundo">
    <img src="_imagens/background.jpg" alt="" />
  </div>
</div>

<div id="pagina-principal">
  <header id="cabecalho">

    <nav id="navegacao">
      <ul>
        <a href="principal.html"><img src="_imagens/simbolo.webp" alt=""></a>
      </ul>
    </nav>
    <p>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
  </header>
</div>

inserir a descrição da imagem aqui

  • Take a look at this page I believe that this may be your problem: The left property added to the position property added to the margin property of the element, is what is causing the positioning error. https://www.w3schools.com/cssref/pr_pos_left.asp

1 answer

1


Your div is the direct daughter of body, and is with position: absolute, then when you put left: 50% in that div means that the div will align from 50% do body, and not centered on body as shown below

inserir a descrição da imagem aqui

One of the ways to centralize how position: absolute, the one I point out in this particular case is to put left/right with 0 and margin: auto

inserir a descrição da imagem aqui

Run the code in full screen!

div#pagina-principal {
  position: absolute;
  width: 1000px;
  background: rgba(255, 255, 255, 0.5);
  box-shadow: 0px 0px 10px rgba(0, 0, 0, .5);
  
  margin: 0px auto 0px auto;
  left: 0;
  right: 0;
}
<div id="pagina-fundo">
  <div id="fundo">
    <img src="_imagens/background.jpg" alt="" />
  </div>
</div>

<div id="pagina-principal">
  <header id="cabecalho">

    <nav id="navegacao">
      <ul>
        <a href="principal.html"><img src="_imagens/simbolo.webp" alt=""></a>
      </ul>
    </nav>
    <p>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
  </header>
</div>

Browser other questions tagged

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