Width and Height of an Element

In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.

Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add padding, borders and margins. So, let's style a <div> element to have a total width (including padding, border and margin) of 350px:

HTML file: Displayed by browser:
<style> div {
    width: 320px;
    padding: 10px;
    border: 5px solid red;
    margin: 0px;
}
</style>
<body>
<img src="../html_beg/images/klematis.jpg" width="350" height="263" alt="Klematis">
<div>The picture above is 350px wide. The total width of this element is also 350px.</div>
</body>
Klematis
The picture above is 350px wide. The total width of this element is also 350px.

Back button Table of Contents Next button