Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set line-height as a percentage relative to the parent element

Tags:

css

I have a responsive element where it's width and height will both scale. Inside this I have some text which I want to center vertically.

How can I set the text's line-height to be the same as it's parent if I don't know the parent's height?

line-height: 100% is relative to the font's regular height so this doesn't help...

like image 743
smilly92 Avatar asked Oct 02 '12 07:10

smilly92


People also ask

How do you calculate line height percentage?

Sets line height to be equal to a multiple of the font size. If your font size is 10px, your line height will be 10px, 18px, and 20px, respectively. Sets line height as a percentage of the font size of the element. If your font size is 10px, your line height will be 3px, 5px, and 11px respectively.

How do I make my Div 100% height of my parents?

container div has two parent elements: the <body> and the <html> element. And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

Can we give height in percentage in CSS?

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto .


1 Answers

Here's another way to center an element vertically. I came across this technique some time ago. Basically it uses a pseudo element and vertical-align: middle.

.block::before {   content: '';   display: inline-block;   height: 100%;   vertical-align: middle;   margin-right: -0.25em; /* Adjusts for spacing */ }  /* The element to be centered, can    also be of any width and height */  .centered {   display: inline-block;   vertical-align: middle;   width: 300px; } 
like image 79
Mr. 14 Avatar answered Sep 18 '22 18:09

Mr. 14