¿Cómo centrar tres divs horizontalmente dentro de un div principal? [duplicate]

7 minutos de lectura

avatar de usuario de nil338
nil338

Sé que esta pregunta se ha hecho con frecuencia, pero parece que nunca puedo hacer que funcione. ¿Puedes decirme qué está mal?

Tengo tres divs dentro de un div #container, que quiero centrar uno al lado del otro. El #contenedor tiene 1000 px de ancho (quiero mantenerlo así). Aquí está mi código:

#container {
  margin-top: 500px;
  position: absolute;
  width: 1000px;
}
.related-article {
  background-color: #D6A400;
  display: inline-block;
  width: 230px;
  height: 300px;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
  width: 200px;
  height: 150px;
  border-radius: 15px;
  margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
  text-decoration: none;
}
#right {
  float: right;
}
#left {
  float: left;
}
#center {
  margin-left: 385px;
  margin-right: 385px;
}
<div id="container">
  <h2>Here's what you'll do!</h2>
  <div id="left">
    <a href="#" class="related-article first" align="middle">
      <img src="download.jpeg" alt="T-rex">
      <h3>Constructing amazing fossils you never even dreamed of</h3>
    </a>
  </div>
  <div id="center">
    <a href="#" class="related-article second" align="middle">
      <img src="fossil-fish-10-lg.jpg" alt="Fish">
      <h3>Studying ancient marine biology</h3>
    </a>
  </div>
  <div id="right">
    <a href="#" class="related-article third" align="middle">
      <img src="fossil.turtle2.jpg" alt="Turtle">
      <h3>Uncovering the world's greatest mysteries</h3>
    </a>
  </div>
</div>

Toda ayuda sería apreciada con mucho gusto.

Avatar de usuario de Teuta Koraqi
Teuta Koraqi

Puedes hacerlo bastante simple usando flexbox:

#container {
/*     margin-top: 500px; */
  width: 1000px;
  margin: 0 auto;
}
.related-article {
  background-color: #D6A400;
  display: inline-block;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
    width: 200px;
    height: 150px;
    border-radius: 15px;
    margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
    text-decoration: none;
}

}
#container {
    width: 1000px;
}

.related-article {
  background-color: #D6A400;
  display: inline-block;
  width: 230px;
  height: 300px;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
    width: 200px;
    height: 150px;
    border-radius: 15px;
    margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
    text-decoration: none;
}
.box {
  margin-right: 10px;
    width: 230px;
  height: 300px;
  }

.flex-container {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div id="container">
        <h2>Here's what you'll do!</h2>
        <div class="flex-container">
        <div id="left" class="box">
          <a href="#" class="related-article first" align="middle">
            <img src="download.jpeg" alt="T-rex">
            <h3>Constructing amazing fossils you never even dreamed of</h3>
          </a>
          </div>
          <div id="center"  class="box">
          <a href="#" class="related-article second" align="middle">
            <img src="fossil-fish-10-lg.jpg" alt="Fish">
            <h3>Studying ancient marine biology</h3>
          </a>
          </div>
          <div id="right"  class="box">
          <a href="#" class="related-article third" align="middle">
            <img src="fossil.turtle2.jpg" alt="Turtle">
            <h3>Uncovering the world's greatest mysteries</h3>
          </a>
          </div>
          </div>
        </div>

Una solución usando display: flex. Leer más sobre flex box aquí

  1. Solicitar display: flex al contenedor
  2. Agregar flex: 1 a todos los del niño que se van a centrar horizontalmente.
h2 {
  margin-top: 500px;
}
#container {
  position: absolute;
  width: 1000px;
  display: flex;
  text-align: center;
}
#container div {
  flex: 1;
}
.related-article {
  background-color: #D6A400;
  display: inline-block;
  width: 230px;
  height: 300px;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
  width: 200px;
  height: 150px;
  border-radius: 15px;
  margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
  text-decoration: none;
}
<h2>Here's what you'll do!</h2>
<div id="container">
  <div id="left">
    <a href="#" class="related-article first" align="middle">
      <img src="download.jpeg" alt="T-rex">
      <h3>Constructing amazing fossils you never even dreamed of</h3>
    </a>
  </div>
  <div id="center">
    <a href="#" class="related-article second" align="middle">
      <img src="fossil-fish-10-lg.jpg" alt="Fish">
      <h3>Studying ancient marine biology</h3>
    </a>
  </div>
  <div id="right">
    <a href="#" class="related-article third" align="middle">
      <img src="fossil.turtle2.jpg" alt="Turtle">
      <h3>Uncovering the world's greatest mysteries</h3>
    </a>
  </div>
</div>

Retire todos los flotadores y reemplácelos con:

display: inline-block;

Además, por mucho que lo haya intentado, con ese espacio entre divs, no podrá mostrarlos de la manera correcta. Haga que el espacio entre las divisiones #left, #center y #right sea inferior a 50 px, o trabaje con un porcentaje (%).

Puedes utilizar display:table para eso..

Puedes tener Parent div con estilo

display:table

y tus 3 hijos se dividen como

display:table-cell 
#container {
  margin-top: 500px;
  position: absolute;
  width: 1000px;
}
.related-article {
  background-color: #D6A400;
  display: inline-block;
  width: 230px;
  height: 300px;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
  width: 200px;
  height: 150px;
  border-radius: 15px;
  margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
  text-decoration: none;
}
#container {
  margin-top: 500px;
  position: absolute;
  width: 1000px;
  display: table;
}
.related-article {
  background-color: #D6A400;
  display: inline-block;
  width: 230px;
  height: 300px;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
  width: 200px;
  height: 150px;
  border-radius: 15px;
  margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
  text-decoration: none;
}
#left,
#right,
#center {
  display: table-cell;
}
#center {
  margin-left: 385px;
  margin-right: 385px;
}
h2 {
  display: table-row;
}
<div id="container">
  <h2>Here's what you'll do!</h2>
  <div id="left">
    <a href="#" class="related-article first" align="middle">
      <img src="download.jpeg" alt="T-rex">
      <h3>Constructing amazing fossils you never even dreamed of</h3>
    </a>
  </div>
  <div id="center">
    <a href="#" class="related-article second" align="middle">
      <img src="fossil-fish-10-lg.jpg" alt="Fish">
      <h3>Studying ancient marine biology</h3>
    </a>
  </div>
  <div id="right">
    <a href="#" class="related-article third" align="middle">
      <img src="fossil.turtle2.jpg" alt="Turtle">
      <h3>Uncovering the world's greatest mysteries</h3>
    </a>
  </div>
</div>

retirar float de y agregar display: inline-block a los tres, luego agregue text-align: center; al contenedor.

  • Lol, tomaste mis palabras, mira aquí. jsfiddle.net/jsusLxoy

    – Leo el león

    15 de junio de 2016 a las 9:25

avatar de usuario de frnt
frente

Prueba esto y agrega flotador izquierdo a su derecha, izquierda y centro div y reducir su margen izquierdo y derecho del centro div.

#right {
float: left;
}
#left {
float: left;
}
#center {
margin-left: 85px;
margin-right: 85px;
float:left;
}

  • Lol, tomaste mis palabras, mira aquí. jsfiddle.net/jsusLxoy

    – Leo el león

    15 de junio de 2016 a las 9:25

Avatar de usuario de SESN
SESN

En lugar de agregar centro, izquierda y derecha. Use ul y establezca el ancho de li en porcentaje. Mejorará el código y reducirá el código css.

La URL de codepen es http://codepen.io/SESN/pen/pbbjrb

CSS

#container { width: 100%; }
.ulContainer { margin: 0px auto; list-style: none; width: 80%; }
.ulContainer li { width: 33.33%; float: left; }

HTML

<div id="container">
        <h2>Here's what you'll do!</h2>
  <ul class="ulContainer">
  <li>
    <a href="#" class="related-article first" align="middle">
            <img src="download.jpeg" alt="T-rex">
            <h3>Constructing amazing fossils you never even dreamed of</h3>
          </a>
  </li>
    <li>
    <a href="#" class="related-article second" align="middle">
            <img src="fossil-fish-10-lg.jpg" alt="Fish">
            <h3>Studying ancient marine biology</h3>
          </a>
    </li>
    <li>
    <a href="#" class="related-article third" align="middle">
            <img src="fossil.turtle2.jpg" alt="Turtle">
            <h3>Uncovering the world's greatest mysteries</h3>
          </a>
    </li>
  </ul>
        </div>

¿Ha sido útil esta solución?