How to set the outer spacing of css? How to set css outer spacing (with examples)

寻∝梦
Release: 2018-09-10 13:40:59
Original
4463 people have browsed it

There are four elements in the css box model, namely inner spacing, outer spacing, content and borders. These four define the use of the box model. Today we will talk about the use of the outer spacing of the css box model.

How to set the outer spacing of css?

Our outer spacing is one of the css box models, so now let’s take a look at how to set the outer spacing.

margin: It is a shorthand attribute that can set all margin attributes in one statement.

Let’s look at an example of complete margins:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文网</title>
<style type="text/css">
.diyi{border:solid #D91C1F thin; width: 400px;}
p{border: double;}
</style>
</head>
<body>
<div class="diyi">
<p>这是一个p标签,是用来使用外边距的</p>
</div>
</body>
</html>
Copy after login

This is an example of a simple HTML code, with a border added inside, so what we see The effect is as shown in the figure:

How to set the outer spacing of css? How to set css outer spacing (with examples)

#This is the effect of the string of code just now.

Now let’s take a look at the effect of using margins:

<style type="text/css">
.diyi{border:solid #D91C1F thin; width: 400px;}
p{border: double; margin-top: auto}
</style>
Copy after login

The above is using css style to set the distance between the top of the p tag and the middle of the div tag I canceled , now look at the picture:

How to set the outer spacing of css? How to set css outer spacing (with examples)

Is the effect the same as mentioned above?

Now let’s see what the effect will be if we cancel all the four margins of the p tag. Let’s see:

<style type="text/css">
.diyi{border:solid #D91C1F thin; width: 400px;}
p{border: double; margin: auto}
</style>
Copy after login

Does the code feel like it? The same as above, but it is also obvious that this is canceling the upward elements inside, so now the margins on all four sides are zero. Let’s take a look at the effect:

How to set the outer spacing of css? How to set css outer spacing (with examples)

Isn’t it obvious that all the outer margins are gone.

But what should we do if we want to set margins?

It’s also very simple. Let’s take a look at the example:

<style type="text/css">
.diyi{border:solid #D91C1F thin; width: 400px;}
p{border: double; margin:20px}
</style>
Copy after login

This is to set all the margins to 20. You can see it, so let’s take a look. Rendering:

How to set the outer spacing of css? How to set css outer spacing (with examples)

You can clearly see that the surrounding borders are all 20px.

You can also set margins in one direction. For example, we can look at the following example:

<head>
<meta charset="utf-8">
<title>PHP中文网</title>
<style type="text/css">
.diyi{border:solid #D91C1F thin; width: 400px;}
p{border: double; margin-right:20px}
h1{border:double; margin-bottom: 50px}
</style>
</head>
<body>
<div class="diyi">
<h1>这里是PHP中文网</h1>
sdfahsdkjfhksjdhf
<p>这是一个p标签,是用来使用外边距的</p>
</div>
</body>
Copy after login

This example adds the bottom margin to the h1 tag, and the margin The margin distance is 50px, the p tag adds a right margin, and the margin distance is 20px. Let’s take a look at the rendering:

How to set the outer spacing of css? How to set css outer spacing (with examples)

Is it obvious? This is how margins are used. You can also set several margins in one attribute. Everyone can try it out, it’s not difficult at all.

Okay, the above is the entire content of this article. If you want to see more, come to the css Learning Manual column of the PHP Chinese website to learn. If you have any questions, you can ask below.

【Editor’s Recommendation】

Do you know how to use the border of the css box model? Here is an introduction to the composition of the css box model

What are the css pseudo-elements? Detailed introduction to css pseudo-elements (with examples included)

The above is the detailed content of How to set the outer spacing of css? How to set css outer spacing (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!