CSS尺寸

CSS尺寸解析

CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度。同样,它允许你增加行间距。

实例

1、设置元素的高度

<!DOCTYPE html>
<html>
<head>
<style>
img.normal
{
height:auto;
}
img.big
{
height:120px;
}
p.ex
{
height:100px;
width:100px;
}
</style>
</head>

<body>
<img class="normal" src="/images/online_demo/logocss.gif" width="95" height="84" /><br>
<img class="big" src="/images/online_demo/logocss.gif" width="95" height="84" />
<p class="ex">The height and width of this paragraph is 100px.</p>
<p>This is some text in a paragraph. This is some text in a paragraph.
This is some text in a paragraph. This is some text in a paragraph.
This is some text in a paragraph. This is some text in a paragraph.</p>
</body>
</html>

效果:
css元素高度

2、使用百分比设置图像的高度

<style>
html {height:100%;}
body {height:100%;}
img.normal {height:auto;}
img.big {height:50%;}
img.small {height:10%;}
</style>

效果:

图像高度

3、使用像素值来设置元素的宽度

<head>
<style>
img {width:200px;}
</style>

效果:
元素宽度

4、设置元素的最大高度

<html>
<head>
<style type="text/css">
p
{
max-height:50px;
background-color:yellow;
}
</style>
</head>

<body>
<p>本段落的最大高度被设置为 50px。 本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。本段落的最大高度被设置为 50px。</p>
</body>
</html>

效果:

元素最大宽度

5、使用百分比来设置元素的最大宽度

<!DOCTYPE html>
<html>
<head>
<style>
p
{
max-width:20%;
background-color:yellow;
}
</style>
</head>
<body>

<p>This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.</p>

</body>
</html>

效果:
元素百分比最大宽度

6、设置元素的最低高度

<html>
<head>
<style>
p
{
min-height:100px;
background-color:yellow;
}
</style>
</head>

<body>
<p>The minimum height of this paragraph is set to 100px.</p>
</body>
</html>

效果:
元素最小高度

7、使用像素值设置元素的最小宽度

<html>
<head>
<style>
p
{
min-width:150px;
background-color:yellow;
}
</style>
</head>

<body>
<p>The minimum width of this paragraph is set to 150px.</p>
</body>
</html>

元素最小宽度

所有CSS 尺寸 (Dimension)属性

属性	描述
height	设置元素的高度。
line-height	设置行高。
max-height	设置元素的最大高度。
max-width	设置元素的最大宽度。
min-height	设置元素的最小高度。
min-width	设置元素的最小宽度。
width	设置元素的宽度。