终于搞懂了 CSS 中的百分比是基于什么工作的了

大家有没有对 CSS 中的百分比是如何工作的感兴趣?有没有想过,为什么它有时会乱七八糟,没啥头绪?反正我是有,所以今天分享这篇文章,对自己来说是加深理解,同时也希望对大家有所帮助。

什么百分比?

作为百分比,显然应该有一个目标作为参考源,这个参考一般是父元素。这是正确的,但并不涵盖所有情况。最正确的答案应该是包含块(containing block),即包含我们元素的块且它不必是直接的父元素。

看看下面的例子:

终于搞懂了 CSS 中的百分比是基于什么工作的了

代码:


  
    
  

.grandparent {
  position: relative;
  width: 200px;
  height: 200px;
  background: #eaeaea;
}

.parent {
  width: 100px;
  height: 100px;
  background: #aaa;
}

.child {
  position: absolute;
  width: 50%;
  height: 50%;
  top: 25%;
  left: 25%;
  background: red;
}

在上面的例子中,我创建了 3 个嵌套 p,它们是具有以下特征的3个正方形

如果百分比单位以父级为来源,则子级的大小应该是它的 1/2,但上面的不是,子级的大小实际上等于父级,也就是祖父级的 1/2。原因是祖父级 p 是子级 p 的真正包含块,因为子级具有 position: absolute ,对应于在祖父级中设置的 position:relative

因此,为了确定哪个是元素的实际包含块,它完全基于元素本身的 position 属性。

但是,对于某些属性,百分比单元的引用源既不是父块也不是包含块,而是它本身—— 自身元素

百分比的属性

width/height

如上面的例子中看到的,当一个元素为其宽度分配一个百分比值时, width 是基于包含块的width, height 是基于包含块的 height

padding

对于 padding,垂直(padding-top/padding-bottom)或水平(padding-left/padding-right)都是基于包含块的 wdith 来计算。

来个例子:

终于搞懂了 CSS 中的百分比是基于什么工作的了


 

.parent {
  background: #eaeaea;
  width: 300px;
  height: 200px;
}

.child {
  display: inline-block;
  background: red;
  padding-top: 50%;
  padding-left: 50%;
}

.parent {
  position: relative;
}

线上地址:https://codepen.io/khangnd/pen/powbjEL

在这个例子中:

最后的结果是,子元素的大小相当于父级元素 1/2宽度,也就是一个 3x3 的正方形。

margin

paddingmargin 的百分比(垂直和水平)也是相对于包含块的宽度来计算。

来个事例:

终于搞懂了 CSS 中的百分比是基于什么工作的了


  

.parent {
  background: #eaeaea;
  width: 300px;
  height: 200px;
}

.child {
  display: inline-block;
  background: red;
  width: 50px;
  height: 50px;
  margin-top: 50%;
  margin-left: 50%;
}

在这个例子中:

其结果是,子元素被定位在离父级元素的上边距和左边距3个单位的地方(父级宽度的1/2)。

top/bottom/left/right

topbottom基于包含块的height来计算,leftright 基于包含块的width来计算。

来个例子:

终于搞懂了 CSS 中的百分比是基于什么工作的了


  

.parent {
  position: relative;
  background: #eaeaea;
  width: 300px;
  height: 200px;
}

.child {
  position: absolute;
  background: red;
  width: 16.67%;
  height: 25%;
  top: 50%;
  left: 50%;
}

在这个事例中:

最终结果,子 p 被定位在离父 p 的顶部边缘 2个单位的位置(父 p 高度的 1/2),并被定位在离父 p 的左侧边缘 3 个单位的位置(父 p 宽度的 1/2)。

transform: translate()

一个用于动画/过渡的不可思议的属性,它也支持百分比值。然而,这个属性并不指其包含的块,而是指其自身。

来个例子:

终于搞懂了 CSS 中的百分比是基于什么工作的了


  

.parent {
  background: #eaeaea;
  width: 300px;
  height: 200px;
}

.child {
  background: red;
  width: 100px;
  height: 50px;
  transform: translate(50%, 50%);
}

在这个事例中:

最后结果,子 p 被定位在离父 p 的顶部边缘 0.5 个单位的位置(自身高度的 1/2),并被定位在离父 p 的左侧边缘 1 个单位的位置(自身宽度的 1/2)。

background-size

background-size 属性将百分比单元的复杂性提升到一个新的水平

此属性的百分比值指的是背景定位区域,类似于包含块,但添加了以下 3 个因素:

这三个值是由 background-origin 给出,具体看 MDN :https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-origin

来个例子:


  

.parent {
  background: #eaeaea;
  width: 300px;
  height: 200px;
}

.child {
  background-image: url(https://d2fltix0v2e0sb.cloudfront.net/dev-rainbow.png);
  background-size: 50% 50%;
  background-repeat: no-repeat;
  background-color: red;
  width: 50%;
  height: 50%;
}
终于搞懂了 CSS 中的百分比是基于什么工作的了

在这个例子中:

其结果是,背景图像被拉伸为 1.5 x 1 的大小。

background-position

background-size 类似,background-position 属性的百分比也依赖于背景定位区域。

在这个例子中:


  

css

.parent {
  background: #eaeaea;
  width: 300px;
  height: 200px;
}

.child {
  background-image: url(https://d2fltix0v2e0sb.cloudfront.net/dev-rainbow.png);
  background-size: 50% 50%;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-color: red;
  width: 50%;
  height: 50%;
}


在本例中,使用了与前面相同的图像和布局。当我们改变background-position的值时,可以看到一些变化:

注意:background-position: 0 50% 是下面的缩写

显然,这个属性的百分比背后有一些计算,而不仅仅是图像的顶部和左侧边缘与孩子的距离。通过一些研究和测试,似乎 background-position 属性在产生一个实际值之前依赖于以下计算。

offset X = (容器的宽度-图像的宽度) * background-position-x offset Y = (容器的高度-图像的高度) * background-position-y

在这种情况下:

font-size

对于 font-size ,百分比值仅指向它的直接父块。

来个例子:


  font-size: 13px
  
    font-size: 26px
    font-size: 50%
  

在这个例子中,我使用与第一个例子相同的布局,字体大小分配如下。

我们可以清楚地看到,child 的字体大小现在与 grandparent 一样,是 parent1/2

线上地址:https://codepen.io/khangnd/pen/MWoeXMO

~~ 完,最近一个礼拜都在赶项目,基本都要2点后才能睡,这篇文章是间断整理好的,现在时间 是 9/20 深夜3点,睡了,感谢大家的观看。

作者:Khang 译者:前端小智 来源:dev 原文:https://dev.to/khgnd/understanding-css-percentage-44gd

展开阅读全文

页面更新:2024-04-12

标签:百分比   比值   事例   祖父   宽度   百分   属性   例子   图像   边缘   元素   高度   大小   背景   单位   工作   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top