2016-06-15 20 views
11

So che questa domanda è stata posta di frequente ma non riesco mai a farlo funzionare. Puoi dirmi cosa c'è che non va?Come centrare tre div orizzontalmente in un div genitore?

Ho tre div all'interno di un div #container, che voglio centrare l'uno accanto all'altro. Il #container è largo 1000px (voglio mantenerlo in questo modo). Qui è il mio codice:

#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>

Tutto l'aiuto sarebbe apprezzato volentieri.

risposta

6

Si può fare abbastanza semplice FlexBox utilizzando:

#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>

+0

Non proprio corretto, voglio dire che sta uscendo dalla larghezza dello schermo. – Shadow

+1

Ho appena messo nel centro orizzontale, dovresti rimuovere lo stile css che hai aggiunto. Lasciami aggiustarlo! –

+0

:) thax bt non è la mia domanda ma sto solo provando. – Shadow

1

rimuovere float da e aggiungere display: inline-block a tutti e tre, quindi aggiungere text-align: center; al contenitore.

+0

Lol hai preso le mie parole, vedi qui https://jsfiddle.net/jsusLxoy/ –

2

Si può usare display:table per questo ..

Si può avere div genitore con stile

display:table 

e il tuo 3 c hild Divs come

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>

3

Una soluzione utilizzando display: flex. Per saperne di più FlexBox here

  1. Applicare display: flex al contenitore
  2. Aggiungi flex: 1 a tutti del bambino che devono essere centrata orizzontalmente.

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>

1

Prova questo, e aggiungere float: left alla vostra destra, a sinistra e al centro div e ridurre il margine di a sinistra ea destra del centro div.

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

Invece di aggiungere il centro, a sinistra ea destra. Usa ul e imposta la larghezza di li in percentuale. Migliorerà il codice e ridurrà il codice css.

L'url codepen è 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> 
3

Rimuovere tutti i carri e li sostituisce con:

display: inline-block; 

Inoltre, un Per quanto io abbia provato, con quella spaziatura tra le div, non sarai in grado di mostrarle nel modo giusto. Crea lo spazio tra il #left, #center e #right div a meno di 50px, o lavora con percentuale (%).

Problemi correlati