高度与线高样式
当处理永远不会超过一行的文本时,使用这两种方法有什么区别?从我所看到的关于元素顶部或底部的元素来看,它们都可以在屏幕上产生类似的结果。如果是的话为什么还要使用行高呢?使用高度会更有意义。
编辑:一个示例是从内部带有文本的 div 创建的风格化按钮。这永远不会是多行的。那么出于兼容性原因是否需要行高?或者有什么我不知道的事情?
谢谢!
What is the difference between using these two when dealing with text that will never be more than a single line? They both can produce similar results on the screen from what I can see in regards to the elements on top or on the bottom of the element. Why use line-height at all if so? Height would make more sense to use.
Edit: An example of this a stylized button created from a div with text inside. This will never be multiline. So would line-height be needed for compatibility reasons? or anything I don't know about?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
height
是容器的垂直测量值。line-height
是第一行文本顶部到顶部的距离第二个。
如果仅与一行文本一起使用,我希望它们在大多数情况下会产生类似的结果。
当我想显式设置容器大小和排版布局的行高时,我使用高度,如果用户调整文本大小,它可能会相关。
height
is the vertical measurement of the container.line-height
is the distance from the top of the first line of text to the topof the second.
If used with only one line of text I'd expect them to produce similar results in most cases.
I use
height
when I want to explicitly set the container size andline-height
for typographic layout, where it might be relevant if the user resizes the text.如果将文本包裹在 div 中,给 div 指定高度,并且文本将增长为 2 行(可能是因为在手机等小屏幕上查看它),则文本将与其下方的元素重叠。另一方面,如果您为 div 指定了行高,并且文本增长到 2 行,则 div 将扩展(假设您没有为 div 指定高度)。
这是一个 fiddle 演示了这一点。
If you wrap the text in a div, give the div a height, and the text grows to be 2 lines (perhaps because it is being viewed on a small screen like a phone) then the text will overlap with the elements below it. On the other hand, if you give the div a line-height and the text grows to 2 lines, the div will expand (assuming you don't also give the div a height).
Here is a fiddle that demonstrates this.
假设文本小于容器:
设置容器上的行高指定容器内行框的最小高度。对于 1 行文本,这会导致文本在容器内垂直居中。
如果您在容器上设置高度,则容器将垂直增长,但其中的文本将从其中的第一行(顶行)开始。
Assuming the text is smaller than the container:
Setting the line-height on the container specifies the minimum height of line-boxes inside it. For 1 line of text, this results in the text vertically centered inside the container.
If you set height on the container then the container will grow vertically, but the text inside it will start on the first (top) line inside it.
高度 = 行高 + 顶部内边距 + 底部内边距
height = line-height + padding-top + padding-bottom
看看这个例子就很容易理解了。
Easily understand by seeing this example.
出于实际目的,在您引用的情况下(不要换行超过一行)行高将使文本垂直居中。但要小心这个假设,因为用户最终控制字体大小。
For practical purposes in the case you cite (never wrapping to more than one line) line-height will vertically center the text. Be careful about that assumption though as the user ultimately controls the font-size.