The width
and height
properties determine the size of the element. Default property values are 'auto
'; however, different HTML element types (block elements vs. inline elements) show different behavior. You can set the width
and height
properties for block elements, but you cannot set them for inline elements. You can also set the width and height property for inline-block elements which will be explained later in this course.
As a default setting, they (the width and height) are calculated from one edge to the other edge of content (content box) but you can extend them to the border edge using the box-sizing
property. We'll explain box-sizing
at the end of this chapter.
You can specify width and height using one of the lengths or keywords.
px
em
rem
%
vw
vh
auto
(default)initial
inherit
When you use initial
as a property value, CSS sets the property to its default value. When you use inherit
as a property value, the property inherits its value from its parent element. These property values are available for all properties. Although we may not mention these keyword options in all topics, you can still use these keywords as a property value.
The default value of the width
property is auto
. In the case of the block element, the element will be stretched out until its width becomes the width of its parent element. For block elements, you can also customize the width using the width
property. Usually, you should specify the value within the width of its parent element. You can set the width of the value larger than the parent element's width, but this will result in the element overflowing beyond the parent element.
In the case of the inline element , width is determined by the length of its content, and you cannot change the width of the inline element using the width
property.
!
and hit tab or enter to create an html template.<title>
section to 10. CSS: Sizing and Spacing.<title>
tag, add a link to the practice.css file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/practice.css">
<title>10. CSS: Sizing and Spacing</title>
</head>
<body>
<h1>Chapter 10. CSS: Sizing and Spacing</h1>
</body>
</html>
Add the code below in the body section of the HTML file.
We added the size-object
class in all elements below the <h2>
element to add styles.
<h2>Width</h2>
<div class="size-object">div element: Original</div>
<div class="size-object" style="width:300px">div element: Width 300px</div>
<p class="size-object">p element: Original</p>
<p class="size-object" style="width:300px">p element: Width 300px</p>
<a class="size-object">a element: Original</a>
<a class="size-object" style="width:300px">a element: Width 300px</a>
<span class="size-object">span element: Original</span>
<span class="size-object" style="width:300px">span element: Width 300px</span>
<hr>
Open the practice.css file and add new code to add color and margin to the size-object
class to make the results visible.
We haven't explained the background-color
, margin
and border
properties yet. For now, just copy and paste the code to your file.
For a code readability point of view, add a comment upfront.
/* Chapter 10. CSS: Sizing and Spacing */
.size-object{
background-color: #55D0DC;
margin:10px;
border:1px dotted white;
}
<div>
and <p>
are stretched out horizontally.<a>
and <span>
have widths determined by their content lengths.You can also check the sample result here (Demo Site ).
The default value of the height
property is auto
. Height is determined by the height of each element's content. You can customize the height of block elements; however, you cannot set the height of inline elements.
The height of inline elements is determined by their content size. Even the line-height
property won't change the height of the content area. When you change the font-size
of the element, you'll see that the height of the element follows the size of the font.
Add the code below to the chapter10.html file.
<h2>Height</h2>
<div class="size-object">div element: Original</div>
<div class="size-object" style="height:100px">div element: Height 100px</div>
<p class="size-object">p element: Original</p>
<p class="size-object" style="height:100px">p element: Height 100px</p>
<a class="size-object">a element: Original</a>
<a class="size-object" style="height:100px">a element: Height 100px</a>
<span class="size-object">span element: Original</span>
<span class="size-object" style="height:100px">span element: Height 100px</span<
<hr>
You can also check the sample result here (Demo Site ).
The width
and height
properties determine the size of the element. Default property values are 'auto
'; however, different HTML element types (block elements vs. inline elements) show different behavior. You can set the width
and height
properties for block elements, but you cannot set them for inline elements. You can also set the width and height property for inline-block elements which will be explained later in this course.
As a default setting, they (the width and height) are calculated from one edge to the other edge of content (content box) but you can extend them to the border edge using the box-sizing
property. We'll explain box-sizing
at the end of this chapter.
You can specify width and height using one of the lengths or keywords.
px
em
rem
%
vw
vh
auto
(default)initial
inherit
When you use initial
as a property value, CSS sets the property to its default value. When you use inherit
as a property value, the property inherits its value from its parent element. These property values are available for all properties. Although we may not mention these keyword options in all topics, you can still use these keywords as a property value.
The default value of the width
property is auto
. In the case of the block element, the element will be stretched out until its width becomes the width of its parent element. For block elements, you can also customize the width using the width
property. Usually, you should specify the value within the width of its parent element. You can set the width of the value larger than the parent element's width, but this will result in the element overflowing beyond the parent element.
In the case of the inline element , width is determined by the length of its content, and you cannot change the width of the inline element using the width
property.
!
and hit tab or enter to create an html template.<title>
section to 10. CSS: Sizing and Spacing.<title>
tag, add a link to the practice.css file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/practice.css">
<title>10. CSS: Sizing and Spacing</title>
</head>
<body>
<h1>Chapter 10. CSS: Sizing and Spacing</h1>
</body>
</html>
Add the code below in the body section of the HTML file.
We added the size-object
class in all elements below the <h2>
element to add styles.
<h2>Width</h2>
<div class="size-object">div element: Original</div>
<div class="size-object" style="width:300px">div element: Width 300px</div>
<p class="size-object">p element: Original</p>
<p class="size-object" style="width:300px">p element: Width 300px</p>
<a class="size-object">a element: Original</a>
<a class="size-object" style="width:300px">a element: Width 300px</a>
<span class="size-object">span element: Original</span>
<span class="size-object" style="width:300px">span element: Width 300px</span>
<hr>
Open the practice.css file and add new code to add color and margin to the size-object
class to make the results visible.
We haven't explained the background-color
, margin
and border
properties yet. For now, just copy and paste the code to your file.
For a code readability point of view, add a comment upfront.
/* Chapter 10. CSS: Sizing and Spacing */
.size-object{
background-color: #55D0DC;
margin:10px;
border:1px dotted white;
}
<div>
and <p>
are stretched out horizontally.<a>
and <span>
have widths determined by their content lengths.You can also check the sample result here (Demo Site ).
The default value of the height
property is auto
. Height is determined by the height of each element's content. You can customize the height of block elements; however, you cannot set the height of inline elements.
The height of inline elements is determined by their content size. Even the line-height
property won't change the height of the content area. When you change the font-size
of the element, you'll see that the height of the element follows the size of the font.
Add the code below to the chapter10.html file.
<h2>Height</h2>
<div class="size-object">div element: Original</div>
<div class="size-object" style="height:100px">div element: Height 100px</div>
<p class="size-object">p element: Original</p>
<p class="size-object" style="height:100px">p element: Height 100px</p>
<a class="size-object">a element: Original</a>
<a class="size-object" style="height:100px">a element: Height 100px</a>
<span class="size-object">span element: Original</span>
<span class="size-object" style="height:100px">span element: Height 100px</span<
<hr>
You can also check the sample result here (Demo Site ).