HTML CSS Dimensions
Height and Width are used to control the size of an element in CSS. This is calculated by the web browser the viewer is using. Dimensions can be calculated either by using length values such as pixels (px= picture elements) or centimeters (cm), or by percentages (%). Height and Width do not set the border or the padding or the margins. They determine the content area inside of the border, padding and margins. Here is a link to the lesson on the CSS Box Model in case you need a refresher.
Here is a demonstration. The Div element has a 50% width and a 200px height. Here is the code;
I then just create a <div class="demo"> </div> set of tags to surround the text. The 50% selector is based on the area I am using for text, within my Style sheet declarations for this page, not the screen width. In the style sheet I am using to style this page, the content division is 800px wide, thus 50% would be 400px. You can see the width by looking at the horizontal rules I'm using. I don't use the whole page as this makes it easier to read. Here is the code for my content style;
div.demo {
width: 50%;
height: 200px;
border: ridge 5px blue;
}
div.content {
width: 800px;
margin-top: 25px;
margin-left: 20px;
font-size: 15px;
line-height: 160%;
}
- Dimension values
- height Sets height in px, cm or %
- width Sets width in px, cm or %
- max-height Limits possible height
- max-width Limits possible width
- min-height Sets minimum height
- min-width Sets minimum width
Example; min-width If you want to over-ride the values set by the parent style width
As you can see, the box becomes wider than the parent style.
An issue here is if the browser window is smaller than the Div element. Try miminizing your browser so the Box is smaller than the window and you should see some scroll bars appear so you can see the entire box.
div.demoMinW {
min-width: 999px;
height: 200px;
border: ridge 5px red;
}
Example; max-width
As you can see, the box does not become become wider than the parent style.
div.demoMaxW {
max-width: 999px;
height: 200px;
border: ridge 5px green ;
}
line-height: value;
Controls height between lines
- normal default
- number number is multiplied time current font size to set line height.
- length Fixed length in px, cm, pt.
- percentage % of current font size.
- inherit inherits line height from parent.
Examples
<div class="big"> This is a 300% line height </div>
<div class="small"> This is 50% line height </div>
div.big{
line-height:300%;
}
div.small{
line-height:50%;
}
I hope you will join us for the next lesson Lesson 15) Borders
Additional Resources