尺寸样式属性介绍
| height |
auto:自动,浏览器会自动计算高度length:使用px定义高度%:基于包含它的块级对象的百分比高度 |
设置元素高度 |
| width |
同上 |
设置元素的宽度 |
height属性和width属性
- 接下来让我们进入
height属性、width属性实践,笔者用class属性值为.box,给div标签设置宽度和高度以及div标签背景颜色。
代码块
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>尺寸样式属性</title> <style> .box{ width: 100px; height: 100px; background-color: red; } </style></head><body> <div class="box"> </div></body></html>

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>尺寸样式属性</title> <style> .box{ height: 100px; background-color: red; } </style></head><body> <div class="box"> </div></body></html>

- 为什么会这样呢?其实是这样的如果我们没有给
div标签设置宽度,div标签将会自动的占用父元素的百分百的宽度,注意的是:高度和宽度是不一样的哈,高度不会自动占用父元素的任何位置。
- 只有块级元素才可以设置宽高度。