CSS dig pit crawl pit div aspect equal

Source: Internet
Author: User
Target effect

For such an avatar, the outside is a rounded div inside is an IMG, for the outer div I want to make him height equal to the width.

Discover problems

At the beginning of the set width of 100% and then the height of 100%, the width of the words can be filled with the width of the page, but the height of the parent node will be set to the height of the parent node height, not set the height of the parent node will not have a height.

Solve the problem

Finding the solution on the web is to set the width of the div and then not set the height, and then set his after margin-top to 100%; Because a property similar to margin has a percentage relative to the parent element width

And then on the code

    . head{      position:relative;      width:100%;      border-radius:50%;      Overflow:hidden;      &:after {        content: ';        Display:block;        margin-top:100%; Margin percentage is calculated relative to the parent element width      }      img{        height:100%;        width:100%;        Position:absolute;        top:0;        left:0;      }    }

  

CSS dig pit crawl pit div aspect equal

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.