Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make text height 100% of div?

I'm trying to make the text 100% height of a div but it doesn't work.
It just becomes 100% of the body { font-size:?; }.

Is there any way to make it follow the div height?
The div height is 4% of the whole page and I wan't the text to follow it when you resize/change resolution.

like image 448
Daniel Avatar asked May 23 '12 11:05

Daniel


People also ask

How do I make my height 100% in HTML?

Try setting the height of the html element to 100% as well. Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well. However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.

What does height 100% do CSS?

When your element is nested inside another element, the browser will use the parent element's height to calculate a value for 100%. So, if your element is inside another element that has a height of 100px, and you set the child element's height to 100%. The child element will be 100px high.

What does HTML height 100% mean?

For years, the answer was the following: html { height: 100%; } body { min-height: 100%; } This allows the HTML element to reference the parent viewport and have a height value equal to 100% of the viewport value.


1 Answers

To get the result I wanted, I had to use this code:

// Cache the div so that the browser doesn't have to find it every time the window is resized.
var $div = $('li.leaf.item');

// Run the following when the window is resized, and also trigger it once to begin with.
$(window).resize(function () {
   // Get the current height of the div and save it as a variable.
   var height = $div.height();
   // Set the font-size and line-height of the text within the div according to the current height.
   $div.css({
      'font-size': (height/2) + 'px',
      'line-height': height + 'px'
   })
}).trigger('resize');​

Thank you joshuanhibbert@css-tricks for the help!

like image 175
Daniel Avatar answered Oct 18 '22 11:10

Daniel