In this tutorial, you will learn-

CSS Height and Width

Height and Width in CSS are used to set the height and width of boxes. It’s value can be set using length, percentage or auto.

The CSS height and width properties are used to set the height and width of an element.

The CSS max-width property is used to set the maximum width of an element.


CSS Setting height and width

The height and width properties are used to set the height and width of an element.

The height and width properties do exclude padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.


CSS height and width Values

The height and width properties might have the accompanying values:

• auto – This is default. The browser calculates the height and width

• length – Defines the height/width in px, cm and so on

• % – Defines the height/width in percent of the containing block

• initial – Sets the height/width to its default value

• inherit – The height/width will be inherited from its parent value

Example
Set the height and width of a <div> element:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  height: 200px;
  width: 50%;
  background-color: powderblue;
}
</style>
</head>
<body>

<h2>Set the height and width of an element</h2>

<div>This div element has a height of 200px and a width of 50%.</div>

</body>
</html>

Example
Set the height and width of another <div> element:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  height: 100px;
  width: 500px;
  background-color: powderblue;
}
</style>
</head>
<body>

<h2>Set the height and width of an element</h2>

<div>This div element has a height of 100px and a width of 500px.</div>

</body>
</html>

• Note: Remember that the height and width properties do exclude padding, borders, or margins! They set the height/width of the area inside the padding, border, and margin of the element!


Setting max-width

• The max-width property is used to set the maximum width of an element.

• The max-width can be determined in length values, similar to px, cm, and so on, or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).

• The issue with the <div> above happens when the browser window is smaller than the width of the element (500px). The browser then, at that point add a horizontal scrollbar to the page.

• Using max-width instead, in the present circumstance, will work on the browser’s handling of small windows.

• Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!

Note: If you for some reason use both the width property and the max-width property on a similar element, and the value of the width property is larger than the max-width property; the max-width property will be used (and the width property will be disregarded).

Example

This <div> element has a height of 100 pixels and a max-width of 500 pixels:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  max-width: 500px;
  height: 100px;
  background-color: powderblue;
}
</style>
</head>
<body>

<h2>Set the max-width of an element</h2>

<div>This div element has a height of 100px and a max-width of 500px.</div>

<p>Resize the browser window to see the effect.</p>

</body>
</html>


All CSS Dimension Properties

PropertyDescription
heightSets the height of an element
max-heightSets the maximum height of an element
max-widthSets the maximum width of an element
min-heightSets the minimum height of an element
min-widthSets the minimum width of an element
widthSets the width of an element

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your projects with us.


0 Comments

Leave a Reply

Avatar placeholder