Css achieves the effect of placing images in div

Source: Internet
Author: User
Tags relative

Use the margin attribute of the image to center the image horizontally, and use the padding attribute of the div to center the image vertically.

The structure code is the same as above;

The css code is as follows:

Div {width: 300px; height: 150px; background-color: # eee; padding-top: 50px; border: #000 1px solid ;}

Img {display: block; margin: 0 auto ;}

Note:

Img is an inline element. To set its margin attribute to center it, you need to convert it into a block element display: block, and then use margin: 0 auto; to realize horizontal center of the image; (Some designers add a div label to the image and use the margin of the div label to center the image.


Idea: use the text-align attribute to center the image horizontally and set the value of padding-top to center it vertically.

The structure is as follows:

The code is as follows: Copy code

<Style type = "text/css">
Div {
Width: 180px;
Height: 180px;
Border: 1px solid #000;
Position: relative;
Display: table-cell;
Text-align: center;
Vertical-align: middle;
}
Div p {
Position: static;
+ Position: absolute;
Top: 50%;
}
Div img {
Position: static;
+ Position: relative;
Top:-50%;
Left:-50%;
}
</Style>

<Div> <p> </p> </div> <br>
<Div> <p> </p> </div>


Method 2

The code is as follows: Copy code

<Div>

</Div>

The css style is as follows:

The code is as follows: Copy code

Div {width: 300px; height: 150px; background-color: # ccc; border: #000 1px solid; text-align: center; padding-top: 50px ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.