In div, img is centered based on different resolutions. The excess part is hidden. divimg

Source: Internet
Author: User

In div, img is centered based on different resolutions. The excess part is hidden. divimg

The problems encountered when I was working on banner's residence, I knew that I could use the background image to display them in the center, but I wanted to go deep into it. So I found several ways to collect them. The next two are really amazing.

Next two places

Https://www.zhihu.com/question/39742237

Https://www.v2ex.com/t/187544

 

4. Parent element position: relative, child element

{Transform: translate (-50%,-50% );
Position: absolute;
Top: 50%;
Left: 50% ;}

The width of the Parent and Child elements can be undefined. The translate percentage is based on its own height and width. The problem only occurs when the UC browser and overflow: hidden are superimposed.

5. Extend the width and height of the parent element with the p element and the hidden img element.

Http://nec.netease.com/library/141122

<div class="m-demo">    <p>                    </p></div><div class="m-demo">    <p>                    </p></div><div class="m-demo">    <p>                    </p></div>
/* Hide the image center overflow */. m-demo {position: relative; width: 300px; height: 300px; overflow: hidden; border: 1px solid # ddd ;}. m-demo p {position: absolute; top: 50%; left: 50%; margin: 0; padding: 0 ;}. m-demo img {position: absolute; top:-50%; left:-50%; display: block ;}. m-demo img. hidden {visibility: hidden; position: static ;}

6. Set a to top:-100%; bottom:-100%; left:-100%; right:-100%; to three times the original size, and then text-align: center; center

Http://dabblet.com/gist/e191e05066016cb040d4

<div class="banner">    <a href="#"></a></div><div class="banner">        <a href="#"></a></div>
.banner { width: 300px; height: 200px; overflow: hidden; position: relative; }.banner > a { position: absolute; top: -100%; bottom: -100%; left: -100%; right: -100%; text-align: center; }.banner > a:before { content: ""; display: inline-block; vertical-align: middle; height: 100%; }.banner > a > img { vertical-align: middle; }

 

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.