Home  >  Article  >  Web Front-end  >  How to set margin in css

How to set margin in css

藏色散人
藏色散人Original
2021-04-19 09:39:174750browse

How to set margin in css: first create an HTML sample file; then define some text content through the p tag in the body; finally set the style "p.margin {margin: 2cm 4cm 3cm 4cm}".

How to set margin in css

The operating environment of this article: Windows 7 system, Dell G3 computer, HTML5&&CSS3 version.

margin shorthand property sets all margin properties in one declaration. This attribute can have 1 to 4 values.

Description

This shorthand property sets the width of all margins of an element, or sets the width of margins on each side.

The vertically adjacent margins of block-level elements will be merged, while inline elements will not actually occupy the top and bottom margins. The left and right margins of inline elements are not merged. Likewise, the margins of floated elements are not merged. It is allowed to specify negative margin values, but use caution when using them.

Note: Negative values ​​are allowed.

Example:

<html>
<head>
<style type="text/css">
p.margin {margin: 2cm 4cm 3cm 4cm}
</style>
</head>

<body>

<p>这个段落没有指定外边距。</p>

<p class="margin">这个段落带有指定的外边距。这个段落带有指定的外边距。这个段落带有指定的外边距。这个段落带有指定的外边距。这个段落带有指定的外边距。</p>

<p>这个段落没有指定外边距。</p>

</body>

</html>

Effect;

How to set margin in css

[Recommended learning: css video tutorial]

Example 1

margin:10px 5px 15px 20px;

The top margin is 10px

The right margin is 5px

The bottom margin is 15px

The left margin is 20px

Example 2

margin:10px 5px 15px;

The top margin is 10px

The right and left margins are 5px

The bottom margin is 15px

Example 3

margin:10px 5px;

The top and bottom margins are 10px

The right and left margins are 5px

Example 4

margin:10px;

All The four margins are all 10px

The above is the detailed content of How to set margin in css. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to set font with cssNext article:How to set font with css