如何将边距或填充设置为父容器高度的百分比?
我一直在绞尽脑汁地使用以下代码在 css 中创建垂直对齐
.base{
background-color:green;
width:200px;
height:200px;
overflow:auto;
position:relative;
}
.vert-align{
padding-top:50%;
height:50%;
}
<!-- and used the following div structure. -->
<div class="base">
<div class="vert-align">
Content Here
</div>
</div>
虽然这似乎适用于这种情况,但令我惊讶的是,当我增加或减少基础 div 的宽度时,垂直对齐会突然发生变化。我原以为当我设置 padding-top 属性时,它会将内边距视为父容器高度的百分比,在我们的例子中这是基础,但上面的 50% 值是按父容器高度的百分比计算的。宽度。 :(
有没有办法将填充和/或边距设置为高度的百分比,而无需使用 JavaScript?
I had been racking my brains over creating a vertical alignment in css using the following
.base{
background-color:green;
width:200px;
height:200px;
overflow:auto;
position:relative;
}
.vert-align{
padding-top:50%;
height:50%;
}
<!-- and used the following div structure. -->
<div class="base">
<div class="vert-align">
Content Here
</div>
</div>
While this seemed to work for this case, i was surprised that when i increased or decreased the width of my base div, the vertical alignment would snap. I was expecting that when I set the padding-top property, it would take the padding as a percentage of the height of the parent container, which is base in our case, but the above value of 50 percent is calculated as a percentage of the width. :(
Is there a way to set the padding and/or margin as a percentage of the height, without resorting to using JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
解决方法是,垂直内边距和边距与宽度相关,但
顶部
和底部
不是。因此,只需将一个 div 放在另一个 div 中,然后在内部 div 中使用类似
top:50%
的内容(记住position
很重要,如果它仍然不起作用)The fix is that yes, vertical padding and margin are relative to width, but
top
andbottom
aren't.So just place a div inside another, and in the inner div, use something like
top:50%
(rememberposition
matters if it still doesn't work)一个稍微不同的问题的答案:您可以使用
vh
单位将元素填充到视口的中心:An answer to a slightly different question: You can use
vh
units to pad elements to the center of the viewport:这里有两个选项可以模拟所需的行为。这不是通用的解决方案,但在某些情况下可能会有所帮助。这里的垂直间距是根据外部元素的大小计算的,而不是其父元素的大小,但该大小本身可以相对于父元素,这样间距也将是相对的。
第一种选择:使用伪元素,这里垂直和水平间距是相对于外部的。 演示
将水平间距移动到外部元素使其相对于外部元素的父级。 演示
第二个选项:使用绝对定位。 演示
Here are two options to emulate the needed behavior. Not a general solution, but may help in some cases. The vertical spacing here is calculated on the basis of the size of the outer element, not its parent, but this size itself can be relative to the parent and this way the spacing will be relative too.
First option: use pseudo-elements, here vertical and horizontal spacing are relative to the outer. Demo
Moving the horizontal spacing to the outer element makes it relative to the parent of the outer. Demo
Second option: use absolute positioning. Demo
要使子元素相对于其父元素绝对定位,您需要在父元素上设置相对位置和子元素上的绝对位置。
那么子元素的“top”是相对于父元素的高度。因此,您还需要将孩子向上“平移”其自身高度的 50%。
还有另一种使用弹性盒的解决方案。
您会发现两者的优点/缺点。
To make the child element positioned absolutely from its parent element you need to set relative position on the parent element AND absolute position on the child element.
Then on the child element 'top' is relative to the height of the parent. So you also need to 'translate' upward the child 50% of its own height.
There is another a solution using flex box.
You will find advantages/disavantages for both.
这可以通过
writing-mode
属性。如果将元素的
writing-mode
设置为垂直书写模式,例如vertical-lr
,则其后代在两个维度上的内边距和边距百分比值将变为相对值高度而不是宽度。来自规范:
内联大小的定义:
例如,对于可调整大小的元素,其中水平边距相对于宽度,垂直边距相对于高度。
当您希望元素的纵横比保持不变,但希望其大小根据其高度而不是宽度进行缩放时,使用垂直书写模式特别有用。
This can be achieved with the
writing-mode
property. If you set an element'swriting-mode
to a vertical writing mode, such asvertical-lr
, its descendants' percentage values for padding and margin, in both dimensions, become relative to height instead of width.From the spec:
The definition of inline size:
Example, with a resizable element, where horizontal margins are relative to width and vertical margins are relative to height.
Using a vertical writing mode can be particularly useful in circumstances where you want the aspect ratio of an element to remain constant, but want its size to scale in correlation to its height instead of width.
将一行文本居中的其他方法是:
或者只是
Other way to center one line text is:
or just
这是一个非常有趣的错误。 (在我看来,无论如何,这是一个错误)很好的发现!
关于如何设置它,我会推荐Camilo Martin的答案。但至于为什么,如果你们不介意的话,我想解释一下。
在CSS规范中我发现:
……这很奇怪,但没关系。
因此,使用父
width: 210px
和子padding-top: 50%
,我得到padding-top: 96.5px< 的计算/计算值/code> – 这不是预期的
105px
。这是因为在 Windows 中(我不确定其他操作系统),常见滚动条的大小默认为
17px × 100%
(或水平滚动条的100% × 17px
酒吧)。在计算50%
之前,先减去这些17px
,因此193px 的 50% = 96.5px
。This is a very interesting bug. (In my opinion, it is a bug anyway) Nice find!
Regarding how to set it, I would recommend Camilo Martin's answer. But as to why, I'd like to explain this a bit if you guys don't mind.
In the CSS specs I found:
… which is weird, but okay.
So, with a parent
width: 210px
and a childpadding-top: 50%
, I get a calculated/computed value ofpadding-top: 96.5px
– which is not the expected105px
.That is because in Windows (I'm not sure about other OSs), the size of common scrollbars is per default
17px × 100%
(or100% × 17px
for horizontal bars). Those17px
are substracted before calculating the50%
, hence50% of 193px = 96.5px
.带有空行的 CSS 网格
这种方法可能只有在您已经在相关容器中使用 css-grid 时才有意义,但是如果您是这样,您可以创建一个空行,其百分比(因为它是一行)将是身高的百分比。
CSS Grid with empty row
This approach probably only makes sense if you're already using css-grid for the container in question, but if you are you can create an empty row with a percentage that (because it is a row) will be a percentage of the height.
50% 的填充不会使您的孩子居中,而是将其置于中心下方。我想你确实想要 25% 的 padding-top。也许随着您的内容变高,您的空间不足了?您还尝试过设置 margin-top 而不是 padding-top 吗?
编辑:没关系,w3schools 网站说
所以也许它总是使用宽度?我从来没有注意到。
您正在做的事情可以使用 display:table 来实现(至少对于现代浏览器)。 此处解释了该技术。
A 50% padding wont center your child, it will place it below the center. I think you really want a padding-top of 25%. Maybe you're just running out of space as your content gets taller? Also have you tried setting the margin-top instead of padding-top?
EDIT: Nevermind, the w3schools site says
So maybe it always uses width? I'd never noticed.
What you are doing can be acheived using display:table though (at least for modern browsers). The technique is explained here.
您可以使用
grid
或inline-grid
设置相对填充,如下所示:You can set relative padding using
grid
orinline-grid
like following: