关于CSS Padding那些你意想不到的用法示例

2020-05-02 07:41:49易采站长站整理

padding百分比值的特殊用法

从W3C标准中我们可以看到他是这么描述的:规定基于父元素的宽度的百分比的内边距。注意这里是基于父元素的宽度。
对于移动端来说我们考虑响应式的时候可以做一个正方行或者固定比例的区域用于填充背景。

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″/>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge,chrome=1″>
<title>DEMO</title>
<style>
*{padding: 0;margin: 0;}
.test {width: 500px;margin: auto;}
.test div {padding: 50%;background: blue;}
</style>
</head>
<body>
<div class=”test”>
<div></div>
</div>
</body>
</html>

效果图

padding绘制小图标

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″/>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge,chrome=1″>
<title>DEMO</title>
<style>
*{padding: 0;margin: 0;}
.icon1 {width: 50px;height: 50px;border-radius: 50%;padding: 10px;border: 10px solid #000;background: #000;background-clip: content-box;float: left;}
.icon2 {width: 50px;height: 10px;padding: 10px 0;border-top: 10px solid #000;border-bottom: 10px solid #000;background: #000;background-clip: content-box;float: left;margin:20px 20px;}
</style>
</head>
<body>
<div class=”icon1″></div>
<div class=”icon2″></div>
</body>
</html>

效果图

当然padding的用法肯定还有很多,像什么三栏两栏或者等比例自适应布局等等。这些都是大家常用的方法我就不单个列举了。有兴趣的同学可以去研究一下。希望这篇文章对大家的学习和工作能带来一定的帮助。