CSS 边距属性用于在任何特征边界之外的组件周围留出空间。使用 CSS,您可以完全控制边距。有一些属性可用于设置组件每一侧(顶部、右侧、底部和左侧)的边缘。
CSS 具有用于指示组件每一侧的边缘的属性。
- 边距顶部
- 右边距
- 保证金底部
- 左边距
所有边距属性都可以具有以下值。
- auto:浏览器计算边距
- length:以px、pt、cm等为单位指定边距。
- %:它指定包含元素宽度的百分比的边距。
- 继承:指定边距应从父元素继承。
示例 1:在下面的示例中,HTML div用于段落的样式。边框宽度为 5px,纯蓝色。段落边距按照上边距50px,右边距50px,下边距100px,左边距100px,背景色红色。
HTML
GeeksforGeeks
Set margin of paragraph element
When compared with C++, Java codes are generally
more maintainable because Java does not allow many
things which may lead bad/inefficient programming
if used incorrectly. For example, non-primitives
are always references in Java. So we cannot pass
large objects (like we can do in C++) to functions,
we always pass references in Java. One more example, since
there are no pointers, bad memory access is also not possible.
HTML
GeeksforGeeks
Use of margin:auto
Python is a high-level, general-purpose and a
very popular programming language. Python programming
language (latest Python 3) is being used in web development,
Machine Learning applications, along with all cutting
edge technology in Software Industry. Python Programming
Language is very well suited for Beginners, also for
experienced programmers with other
programming languages like C++ and Java.
This div will be horizontally centered
because it has margin:auto;
输出:
示例2:在下面的示例中,宽度为300px,段落的边距将相同,并且它会在中间,因为它有margin: auto。边框宽度为 80 像素,紫色。
HTML
GeeksforGeeks
Use of margin:auto
Python is a high-level, general-purpose and a
very popular programming language. Python programming
language (latest Python 3) is being used in web development,
Machine Learning applications, along with all cutting
edge technology in Software Industry. Python Programming
Language is very well suited for Beginners, also for
experienced programmers with other
programming languages like C++ and Java.
This div will be horizontally centered
because it has margin:auto;
输出: