2017-07-13 3 views
0

In einem div, ich habe zwei mit verschiedenen Stil, und ich möchte die beiden als eine in div zu zentrieren.Wie zentriere ich zwei Inline-Block p in einem Div-Container?

Wenn nur eine p, ich nur width: 100% und text-align: center zu p.

Aber wie zentriert man zwei genau wie einzelne p in div?

div { 
 
    width: 200px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
} 
 
p { 
 
    display: inline-block; 
 
} 
 
.first { 
 
    color: red; 
 
} 
 
.second { 
 
    color: blue; 
 
}
<div> 
 
    <p class="first">one</p> 
 
    <p class="second">two</p> 
 
</div>

Das Ergebnis wie folgt aus:

enter image description here

+2

'text-align: center;' auf div wird die Arbeit – Chiller

+0

Dank all Sie tun. – tomfriwel

Antwort

3

hinzufügen text-align: center; div-Tag

div { 
 
    width: 200px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
    text-align: center; 
 
} 
 
p { 
 
    display: inline-block; 
 
} 
 
.first { 
 
    color: red; 
 
} 
 
.second { 
 
    color: blue; 
 
}
<div> 
 
    <p class="first">one</p> 
 
    <p class="second">two</p> 
 
</div>

+0

Wow, ich denke, das ist der einfachste Weg, dies zu lösen. Vielen Dank für Ihre Hilfe. – tomfriwel

+0

Immer willkommen @tom .. – Santhoshkumar

+0

Wenn ich die Lücke zwischen zwei 'p' nicht brauche, scheint die Flexbox besser. – tomfriwel

0

Mit einem Flexbox und Auto margin für die div

div { 
 
    width: 200px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
    display: flex; 
 
    justify-content: center; 
 
    margin: 0 auto; 
 
} 
 
p { 
 
    display: inline-block; 
 
} 
 
.first { 
 
    color: red; 
 
} 
 
.second { 
 
    color: blue; 
 
}
<div> 
 
    <p class="first">one</p> 
 
    <p class="second">two</p> 
 
</div>

0

div { 
 
    width: 200px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
} 
 
p { 
 
    display: inline-block; 
 
} 
 
.first { 
 
    color: red; 
 
} 
 
.second { 
 
    color: blue; 
 
} 
 

 
div { 
 
    display: flex; 
 
    justify-content: center; 
 
}
<div> 
 
    <p class="first">one</p> 
 
    <p class="second">two</p> 
 
</div>

0

A flexbox Ansatz ...

div { 
 
    width: 200px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 

 
.first { 
 
    color: red; 
 
} 
 

 
.second { 
 
    color: blue; 
 
}
<div> 
 
    <p class="first">one</p> 
 
    <p class="second">two</p> 
 
</div>

0

Ich denke, das ist das, was Sie suchen.

div { 
 
    width: 200px; 
 
    height: 50px; 
 
    border: 1px solid black; 
 
    text-align:center; 
 
    display: inline-block; 
 
    } 
 
p { 
 
    display: inline-block; 
 
} 
 
.first { 
 
    color: red; 
 
} 
 
.second { 
 
    color: blue; 
 
}
<div> 
 
    <p class="first">one</p> 
 
    <p class="second">two</p> 
 
</div>

+0

Ich habe den Code überprüft, ob es für Sie funktioniert.! – weBBer

Verwandte Themen