margin
and padding
attributes The percentage values in the vertical direction are calculated relative to the width, which is different from the percentage values of attributes such as top
and bottom
.
<p>The reasons for this design will be explained in my new book (which should be published in a few months), so I won’t go into it here.
<p>For the padding
attribute, the percentage in any direction padding
is calculated for the width, allowing us to easily implement a fixed-proportion block-level container. For example, Suppose there is now a <p>
element:
p { padding: 50%; }
p { padding: 100% 0 0; }
p { padding-bottom: 100%; }
<p>
element is a square with a width and height of 1:1 , no matter what the width of its parent container is, this <p>
element can always maintain the same proportion. <p>What is the function of this feature that can fix the proportion? <p>For most layouts, we do not require that the ratio must be fixed, but there is one exception, and that is the picture, because the original size of the picture is fixed. In the traditional fixed-width layout, we will set specific width and height values for the image to ensure that our image occupies a stable area; but on the mobile terminal or in the case of responsive development, the final width of the image is very small It may be uncertain. For example, for a banner advertisement on the mobile phone, the width is 375 under iPhone 7, 414 under iPhone 7 Plus, and 360. In this case, what is needed is not a fixed size setting for the image, but a proportion setting. <p>Usually have the following implementations: <p><p><p>1. Fix a height, and then use the background-size
attribute to control, As follows: .banner { height: 40px; background-size: cover; }
vw
, as follows: .banner { height: 15.15vw; background-size: cover; }
vw
is also a good idea, at least it will be easier to understand. <p>However, if our image is not a banner, but needs to be 1rem
distance from the left and right sides, at this time, our CSS code will be a bit verbose, and if we want to maintain perfect proportions, Just use CSS3 calc()
to calculate: .banner { height: calc(0.1515 * (100vw - 2rem)); background-size: cover; }
calc()
is also stretched, but it is just the ordinary padding
attribute that has great compatibility and adaptability. <p>3. Use percentage padding
, as follows: .banner { padding: 15.15% 0 0; background-size: cover; }
<img>
, percentagepadding
can also be easily dealt with. The search routine is relatively fixed. A fixed-proportion container element is required outside the picture element, such as the following HTML structure: <br/>
# The ##
<p class="banner"> <img src=""banner.jpg> </p>
element is also responsible for controlling the proportion, and then the image is filled with the
.banner element. The CSS code is as follows:
.banner { padding: 15.15% 0 0; position: relative; } .banner > img { position: absolute; width: 100%; height: 100%; left: 0; top: 0; }
, because of the existence of
vw units, after all Understanding
vw seems to be simpler, so I haven’t introduced related techniques. However, as more and more image-related layouts are processed, I found that the practical value of percentage
padding is greater than imagined, and it is applicable to more scenes than
vw units, and is more compatible. Better (percentage feature IE6+ support, pictures 100% coverage IE8+ support).
For complex layouts, if the width of the picture is not fixed and adaptive, we usually think of a trick, which is to only set the width of the picture, for example: <p>img { width: 100%; }
0
到计算高度的图片变化,视觉上会有明显的元素跳动,代码层面会有布局重计算。<p>所以对图片高宽进行同时约定还是有必要的,但是同时要保证宽度自适应,似乎有点难度。记住,如果遇到这种需求场景,没有比百分比padding
布局更好的做法!<p>缩小浏览器宽度可以看到不同宽度下的布局效果,Gif效果截图如下:<p><p>此demo难点就是图片自适应同时保持比例,以及页面刷新的时候没有布局稳固不晃动,其核心HTML和CSS代码如下:<br/>
<p class="works-item-t"> <img src="./150x200.png"> </p>
.works-item-t { padding-bottom: 133%; position: relative; } .works-item-t > img { position: absolute; width: 100%; height: 100%; }
padding
值只使用padding-bottom
表示的时候,如果没有text-align
属性干扰,<img>
元素的left:0;top:0
是可以省略的。<p>对于这种图片宽度100%容器,高度按比例的场景,padding-bottom
的百分比值大小就是图片元素的高宽比,就这么简单。<p>但,有时候,图片宽度并不是100%容器的,例如,图片宽度50%容器宽度,图片高宽比4:3
,此时,CSS垂直方向百分比就666了,如下:.img-box { padding: 0 50% 66.66% 0; }
The above is the detailed content of Detailed explanation of CSS percentage padding to create image adaptive layout. For more information, please follow other related articles on the PHP Chinese website!