line-height as a percentage not working

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

I am having an issue with line-height that I cannot quite get my head around.

The following code will center an image within a div:

CSS

.bar {     height: 800px;     width: 100%;     line-height:800px;     background-color: #000;     text-align: center;  }  .bar img {     vertical-align: middle;    } 

HTML

Foo Image

However, if I change the line height to 100%, then the line height does not take effect (or at least does not become 100% of the div).

Example jsfiddle

Does anyone know what I am doing wrong?

回答1:

line-height: 100% means 100% of the font size for that element, not 100% of its height. In fact, the line height is always relative to the font size, not the height, unless its value uses a unit of absolute length (px, pt, etc).



回答2:

I know this question is old, but I found what for me is the perfect workaround.

I add this css to the div that I want to center:

div:before {   content: "";   display: inline-block;   height: 100%;   vertical-align: middle; } 

This works every time and it is clean.

Edit: Just for completion's sake, I use scss and I have a handy mixin that I include on every parent who's direct children I want to have vertically centered:

@mixin vertical-align($align: middle) {   &:before {     content: "";     display: inline-block;     height: 100%;     vertical-align: $align;     // you can add font-size 0 here and restore in the children to prevent     // the inline-block white-space to mess the width of your elements     font-size: 0;   }   & > * {     vertical-align: $align;     // although you need to know the font-size, because "inherit" is 0     font-size: 14px;   } } 

Full explanation: div:before will add an element inside the div, but before any of its children. When using :before or :after we must use a content: declaration otherwise nothing will happen, but for our purpose, the content can be empty. Then we tell the element to be as tall as its parent, as long as its parent's height is defined and this element is at least inline-block. vertical-align defines the vertical position of self related to parent, as opposed to text-align that works differently.

The @mixin declaration is for sass users and it would be used like this:

div {     @include vertical-align(middle) } 


回答3:

When you use percentage as the line-height it is not based on the div container, rather its based on the font-size.



回答4:

line-height: 100% would be an easy way to vertically center elements, if it was calculated in relation to the container, but that would be too easy, hence it doesn't work.

So instead, it is just another way of saying line-height: 1em (right?)

Another way of vertically centering an element would be:

.container {      position:relative; } .center {      position:absolute;      top:0; left:0; bottom:0; right:0;      margin: auto;      /* for horiz left-align, try "margin: auto auto auto 0" */ } 


回答5:

might not be pretty, but it's working, and its semantic;

Foo Image

display: table-cell gives an element the unique table ablillity to align verticaly (atleast i think its unique)



回答6:

This is a very late answer, however in modern browsers, assuming that the parent element is 100% of the screen height as well, you can use the vh viewport unit. FIDDLE

line-height: 100vh; 

Browser support



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!