I want a div that have same width as content which is done by display: inline-block;
but when I use this,text-align: center
not working anymore.
so my content just go left and inline-block apply to it.
like this:
#div1 {
position: relative;
bottom: 10px;
left: auto;
text-decoration: overline underline;
display: inline;
text-align: center;
}
it doesn't center the text of div anymore.
how I can get both effects of this two?
Inline block divs can be centered by applying text-align:center to their parent.
Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.
Inline Elements To center an inline element like a link or a span or an img, all you need is text-align: center . For multiple inline elements, the process is similar. It's possible by using text-align: center .
You can center inline-block (and inline) elements by setting text-align: center on a parent element.
Give the property text-align:center to any other element, and use that element inside your div.
eg.
#div1 {
position: relative;
bottom: 10px;
left: auto;
text-decoration: overline underline;
display: inline;
}
#p1 {
text-align: center;
}
and then
<div id="div1">
<p id="p1">blah blah blah</p>
</div>
That should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With