The height of the div or img image is adaptive to the width, and the divimg width is adaptive.

Source: Internet
Author: User

The height of the div or img image is adaptive to the width, and the divimg width is adaptive.

1. You can use js to determine the image width to obtain a specific value, and then use js to set the Image Height (I will not elaborate on it here ).

One disadvantage of using js is that the image height can be adjusted only when the page is refreshed. It cannot be adaptive as the browser window size changes.

II. I am using css to achieve Adaptive Image Height this time.

The following is the required code.

(This method is used to vertically center text on the top of an image. If you do not need it, you can select the code at the bottom to be concise)

<Div class = "box"> <span> vertical center of row elements </span> <div class = "img-box">  </div> </div>

. Box {width: 50%; margin: 50px auto ;}. img-box {width: 100%; position: relative; z-index: 1 ;}. img-box img {position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 100%; margin: auto; z-index:-1; * zoom: 1 ;}. img-box: before {content: ""; display: inline-block; padding-bottom: 100%; width: 0.1px;/* A value is required, otherwise, the height cannot be supported */vertical-align: middle ;}


1. the attribute padding-bottom is described here. When its value is a percentage, it is calculated based on the width of the element. So when it is set to 100%, its height is equal to its own width, forming a square. Of course, this value can be adjusted based on actual conditions.

2. Next, we need to note that the referenced image is laid out by absolute positioning, so that the image can adapt to the size change of its parent element.


Another simple method is to directly add padding-bottom to the parent element of the img.

<div class="img-box"></div>

.img-box{padding-bottom:100%;}.img-box img{position:absolute;top:0;bottom:0;left:0;right:0;width:100%;margin:auto;}

 



Copyright Disclaimer: This article is an original article by the blogger. If you need to reprint it, please leave a message in the comment area.

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.