Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css inline-block with aligned center

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?

like image 359
mpower Avatar asked Oct 16 '13 14:10

mpower


People also ask

How do you center align items in inline block?

Inline block divs can be centered by applying text-align:center to their parent.

How do you center align a block in CSS?

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.

How do I center an inline element in CSS?

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 .

How do I center an inline block button?

You can center inline-block (and inline) elements by setting text-align: center on a parent element.


1 Answers

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.

like image 106
Shisa Avatar answered Sep 16 '22 11:09

Shisa