简体   繁体   English

填充高度/边距/边框的百分比可以比em更好吗?

[英]Can using percentages for height of padding/margin/border be better than em?

What are good and recommended uses of percentage values for vertical CSS declarations? 垂直CSS声明的百分比值的优点和建议用途是什么?

In other words, under responsive design, are we overlooking something where % would be beneficial over em? 换句话说,在响应式设计下, 我们是否会忽略%对em有益的东西?


Because it seems that for most situations (except for cases where you want all sides equal; credit ), em would be better served than % , consider: 因为在大多数情况下(除了你希望所有方面都相等; 信用的情况 ), em会比%更好地服务,考虑:

Using percentages for the horizontal values of padding, margin or border of elements in CSS is fairly standard — especially in responsive web design. 使用填充的水平值的百分比,CSS中元素的边距或边框是相当标准的 - 尤其是在响应式网页设计中。 For example, take margin-left: 7.2% and padding: 0 5% . 例如,取margin-left: 7.2%padding: 0 5% It also makes sense: the wider the screen, then the space will increase proportionally. 它也是有道理的:屏幕越宽,那么空间就会按比例增加。

One can do the same for the vertical values: 人们可以对垂直值做同样的事情:

margin-top: 5%;
padding: 10% 0;
border: 1% 0 2% 0;

As expected, an increasing viewport width will increase the corresponding vertical spaces. 正如所料,增加的视口宽度将增加相应的垂直空间。

However, in the cases I've come across, it can look a bit odd — unfitting to the design. 然而,在我遇到的情况下,它看起来有点奇怪 - 不适合设计。 It seems that em values may be better served.* But, on the other hand, where would it be beneficial to use percentages? 似乎可以更好地服务于em值。*但是,另一方面,使用百分比在哪里是有益的?


* Since these won't increase with the width of the screen, but will increase according to the font size of the page. *由于这些不会随着屏幕的宽度而增加,而是会根据页面的字体大小而增加。

I don't think there's any right or wrong answers to this question. 我不认为这个问题有任何正确或错误的答案。 It really does depend on your design. 它确实取决于您的设计。

As you noted, % values, even on vertical-based properties on margin & padding, are still relative to the width of the document. 如您所述,即使在边距和填充的基于垂直的属性上,%值仍然相对于文档的宽度 So if your design requires even padding, then % values all round are great. 因此,如果您的设计需要均匀填充,那么全面的%值都很棒。

But, if the design is content oriented, and you're still using % values on the horizontal properties for responsive design, % might not be the best for the vertical properties. 但是,如果设计是面向内容的,并且您仍然在响应式设计的水平属性上使用%值,则%可能不是垂直属性的最佳值。 You may, for example, want the padding-top to be exactly the height of 1 line of text. 例如,您可能希望padding-top恰好是1行文本的高度。 So you'd use ems . 所以你要用ems

But I digress; 但我离题了; it really does depend on your design and personal preference. 它确实取决于您的设计和个人喜好。

Yes, depending on the situation just like any other css practice. 是的,视任何其他css练习而定的情况。

Say you have a container div that uses 100% of the screen height and you have a header you want to appear at the top of your div. 假设您有一个使用100%屏幕高度的容器div,并且您想要显示在div顶部的标题。 You could say margin-top: 15px on your header, which works, but then if I come and view it on my phone it will look very squished. 你可以说在你的标题上有一个上margin-top: 15px ,但是如果我在手机上查看它会看起来非常挤压。

So instead you say margin-top: 10% then no matter what screen I come and view your site on your header is always 10% from the top of the div. 所以相反,你说margin-top: 10%然后无论我来什么屏幕,你的标题上的网站总是从div的顶部10%。 which means the relative flow of your layout will always be the same. 这意味着您的布局的相对流量将始终相同。

The general rule is this: For any valid css you can write there is a use-case where it would be the best way to go about achieving your design goal. 一般规则如下:对于任何有效的CSS,您可以编写一个用例,它是实现设计目标的最佳方式。 Forget anyone who says "Never use negative margins" or "always avoid absolute positioning" or any of the other crap they throw around. 忘掉那些说“绝不使用负边距”或“总是避免绝对定位”或任何其他废话的人。

I have been pondering this question as well recently and after reading around the internet the 'rule of thumb' I'm beginning to lean on is as follows. 我最近一直在思考这个问题,在浏览互联网之后,我开始依赖的“经验法则”如下。 First, the reason why % is good responsive design for the horizontal axis is because as we all know the width of your browser can vary greatly depending whether the user is on a phone or computer. 首先,%为横轴的良好响应设计的原因是因为众所周知,浏览器的宽度可能会有很大差异,具体取决于用户是在手机还是计算机上。 The vertical axis is different however because while it can also vary like the horizontal axis, many webpages are created for a vertical scroll and the user is expecting to scroll down. 垂直轴是不同的,因为虽然它也可以像水平轴一样变化,但是为垂直滚动创建了许多网页,并且用户期望向下滚动。 In such cases a little more vertical scrolling due to less responsive ems is fine. 在这种情况下,由于响应性较低的ems,可以进行更多的垂直滚动。

To answer your question based on that assumption, a time in which you would use % for the vertical margin is when you have a design where you don't want to make the user scroll much to see a part of the page. 要根据该假设回答您的问题,您将使用%作为垂直边距的时间是,当您有一个设计时,您不希望让用户滚动太多以查看页面的一部分。 Specific examples might be a single-page web app where you don't want any scrolling or a header or initial page content such as a picture that you would want the user to see in its entirety without having to scroll down. 具体示例可能是单页Web应用程序,您不需要任何滚动或标题或初始页面内容,例如您希望用户完整查看而无需向下滚动的图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM