css17 CSS Height, Width
https://www.w3schools.com/css/css_dimension.asp
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.
<!DOCTYPE html> <html> <head> <style> div { height: 50px; width: 100%; border: 1px solid #4CAF50; } </style> </head> <body> <h2>CSS height and width properties</h2> <div>This div element has a height of 50 pixels and a width of 100%.</div> </body> </html>
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 not include 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 may have the following values:
auto
- This is default. The browser calculates the height and widthlength
- Defines the height/width in px, cm, etc.%
- Defines the height/width in percent of the containing blockinitial
- Sets the height/width to its default valueinherit
- The height/width will be inherited from its parent value
CSS height and width Examples

Example
Set the height and width of a <div> element:
div {
height:
200px;
width: 50%;
background-color: powderblue;
}
<!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:
div {
height:
100px;
width: 500px;
background-color: powderblue;
}
<!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 not include 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 specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).
The problem with the <div>
above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page.
Using max-width
instead, in this situation, will improve 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 the same 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 ignored).
Example
This <div> element has a height of 100 pixels and a max-width of 500 pixels:
div {
max-width: 500px;
height:
100px;
background-color: powderblue;
}
<!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>
Try it Yourself - Examples
Set the height and width of elements
This example demonstrates how to set the height and width of different elements.
Set the height and width of an image using percent
This example demonstrates how to set the height and width of an image using a percent value.
Set min-width and max-width of an element
This example demonstrates how to set a minimum width and a maximum width of an element using a pixel value.
Set min-height and max-height of an element
This example demonstrates how to set a minimum height and a maximum height of an element using a pixel value.
All CSS Dimension Properties
Property | Description |
---|---|
height | Sets the height of an element |
max-height | Sets the maximum height of an element |
max-width | Sets the maximum width of an element |
min-height | Sets the minimum height of an element |
min-width | Sets the minimum width of an element |
width | Sets the width of an element |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2022-05-29 Word 内存或磁盘空间不足,保存失败
2015-05-29 jQuery plugins
2011-05-29 似然 似然函数 最大似然估计
2011-05-29 聚合酶链式反应简称PCR
2011-05-29 遗传图谱 genetic map
2011-05-29 什么是DNA微阵列技术?
2011-05-29 基因选择