Categories: CSS教程

CSS 尺寸 (Dimension) | CSS教程

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

所有CSS 尺寸 (Dimension)属性

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

大家看完上面的CSS属性后,我们来了解下具体的实例吧!

1、设置不同元素的高度。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
img.normal
{
 height:auto;
}
img.big
{
 height:120px;
}
p.ex
{
 height:100px;
 width:100px;
}
</style>
</head>

<body>
<img class="normal" src="/wp-content/uploads/2022/02/CSS.jpg" width="95" height="84" /><br>
<img class="big" src="/wp-content/uploads/2022/02/CSS.jpg" width="95" height="84" />
<p class="ex">这个段落的高和宽是 100px.</p>
<p>这是段落中的一些文本。这是段落中的一些文本。
这是段落中的一些文本。这是段落中的一些文本。
这是段落中的一些文本。这是段落中的一些文本.</p>
</body>
</html>

2、使用百分比值设置元素的高度。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title>  
<style>
html {height:100%;}
body {height:100%;}
img.normal {height:auto;}
img.big {height:50%;}
img.small {height:10%;}
</style>
</head>

<body>
<img class="normal" src="/wp-content/uploads/2022/02/CSS.jpg" width="95" height="84" /><br>
<img class="big" src="/wp-content/uploads/2022/02/CSS.jpg" width="95" height="84" /><br>
<img class="small" src="/wp-content/uploads/2022/02/CSS.jpg" width="95" height="84" />
</body>
</html>

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
img {width:200px;}
</style>
</head>
<body>

<img src="/wp-content/uploads/2022/02/CSS.jpg" width="95" height="84" />

</body>
</html>

4、设置元素的最大高度。

<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<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>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<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、设置元素的最小高度。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
p
{
 min-height:100px;
 background-color:yellow;
}
</style>
</head>

<body>
<p>这段的最小高度设置为100 px。</p>
</body>
</html>

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
p
{
 min-width:150px;
 background-color:yellow;
}
</style>
</head>

<body>
<p>这个段落的最小宽度设置为 150px。</p>
</body>
</html>

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

6 天 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

1 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

2 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

3 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

4 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago