CSS 中的 margin 简写属性
CSS margin 简写属性用于定义元素的 margin 区域。它沿顺时针方向设置值,即 margin-top、margin-right、margin-bottom 然后是 margin-left。
语法
CSS margin 属性的语法如下 −
Selector {
margin: /*value*/
}
上述值可以是 −
margin-top
margin-right
margin-bottom
margin-left
以下示例说明了 CSS margin 简写属性 −
具有所有值 margin 属性
具有所有值的 margin 属性为顶部、右侧、底部和左侧属性设置值 −
margin: 7% auto -3% 25%;
示例
让我们看一个示例 −
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 7% auto -3% 25%;
width: 40px;
height: 40px;
padding: 0.9rem;
box-shadow: inset 0 0 50px turquoise;
border: thin solid;
}
div > div {
border-top-right-radius: 100px;
border-bottom-right-radius: 500px;
border-top-left-radius: 30px;
box-shadow: inset 0 0 6px navy;
}
div > div > div {
padding: 0.3em;
margin: 2px -40px;
box-shadow: inset 0 0 16px orange;
border-radius: 50%;
}
#one {
padding: 50px;
border-radius: 10px;
}
</style>
</head>
<body>
<div id="one">
<div>
<div></div>
</div>
</div>
</body>
</html>
具有单个值的 margin 属性
具有单个值的 margin 属性为顶部、底部、左侧和右侧的所有属性设置相同的值 −
p.demo {
margin: 30px;
}
示例
让我们看一个示例 −
<!DOCTYPE html>
<html>
<head>
<style>
p.demo {
margin: 30px;
}
</style>
</head>
<body>
<h1>Demo Heading</h1>
<p>This is a demo text.</p>
<p class="demo">This is another demo text.</p>
<p>Another demo text</p>
<h2>Demo Heading2</h2>
<p>Demo text</p>
</body>
</html>
具有两个值的 margin 属性
具有两个值的 margin 属性,即顶部和底部 margin 为 2em 以下。左侧和右侧属性为 1em −
margin: 2em 1em;
示例
让我们看一个示例 −
<!DOCTYPE html>
<html>
<head>
<style>
article {
margin: 2em 1em;
background-color: bisque;
}
span {
margin: -23% 83%;
border-left: dashed;
background-image: linear-gradient(to right, lightgreen, forestgreen);
font-size: 1.4em;
font-style: italic;
}
</style>
</head>
<body>
<h2>What is Spring Framework?</h2>
<article>
Spring framework is an open source Java platform. It was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.<span> Spring is lightweight when it comes to size and transparency. The basic version of Spring framework is around 2MB.</span>
</article>
</body>
</html>
具有三个值的 margin 属性
使用单个值的边距属性将上边距设置为 35px,左右边距设置为 70px。底部边距设置为 50px -
p.demo {
margin: 35px 70px 50px;
}
示例
让我们看一个示例 −
<!DOCTYPE html>
<html>
<head>
<style>
p.demo {
margin: 35px 70px 50px;
}
</style>
</head>
<body>
<h1>Demo Heading</h1>
<p>This is a demo text.</p>
<p class="demo">This is another demo text.</p>
<p>Another demo text</p>
<h2>Demo Heading2</h2>
<p>Demo text</p>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP