Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does text-align not only center text, but images, too?

Tags:

css

Why does text-align center text and images?

enter image description here

CSS:

#SupplierContainer {
    width: 100%;
    margin: 0 auto;

    background-color: Blue;

}
#SupplierContainerContentHolder {
    background-color: Yellow;
    text-align: center;
}

HTML:

<div id="SupplierContainer">
    <div id="SupplierContainerContentHolder">
        <img src="~/Shared/Suppliers/Default" alt="Suppliers: [name removed]." />
        <br />

        <p>View a complete list of our Suppliers.</p>
    </div>
</div>
like image 893
Arrow Avatar asked Oct 27 '12 06:10

Arrow


People also ask

Why is my text-align center not working word?

Select the text that you want to center, and then click Paragraph on the Format menu. On the Indents and Spacing tab, change the setting in the Alignment box to Centered, and then click OK.

Does text-Align Center work on images?

In general, you cannot use the CSS text-align property to center an image, as it works only on block elements, and the <img> is an inline one.

Why is text-align center not working in HTML?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.

How do you center text perfectly?

Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center. In the Apply to box, click Selected text, and then click OK.


2 Answers

From W3,

This property describes how inline-level content of a block container is aligned.

Since <img> is an inline-block element, this property applies to <img> as well.

Have a look at the <img> tag, where it is stated :

The IMG element has no content; it is usually replaced inline by the image designated by the src attribute, the exception being for left or right-aligned images that are "floated" out of line.

like image 162
Pranav 웃 Avatar answered Sep 22 '22 14:09

Pranav 웃


Because text-align is used to align inline elements (not just text) inside the content area of an element. If you want the image not to be affected by the "text-align", float it then:

#SupplierContainerContentHolder img { float:left }
like image 24
dpp Avatar answered Sep 19 '22 14:09

dpp