CSS two blocks increase distance

Increasing the distance between two CSS blocks

Increasing the distance between two CSS blocks

In web design, we often need to adjust the distance between page elements to make the page look more beautiful and neat. The margin and padding properties in CSS can help achieve this. This article will detail how to use CSS to increase the distance between two blocks.

1. Margin Property

1.1 What is the margin property?

In CSS, margin refers to the distance between an element and its surrounding elements. By setting margins, you can control the outer margins of an element, thereby adjusting the distance between the element and its surrounding elements.


1.2 How to set the margin property

You can set the margin property in the following ways:

.element {
margin: 10px; /* Set the top, bottom, left, and right margins of the element to 10px */
}

You can also set the margins in the top, right, bottom, and left directions separately:

.element {
margin-top: 10px; /* Set the top margin to 10px */
margin-right: 20px; /* Set the right margin to 20px */
margin-bottom: 15px; /* Set the bottom margin to 15px */
margin-left: 30px; /* Set the left margin to 30px */ 
} 

1.3 Example

<!DOCTYPE html> 
<html lang="en"> 
<head> <meta charset="UTF-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Margin example</title> 
<style> 
.block1 { 
width: 100px; 
height: 100px; 
background-color: red; 
margin-right: 20px; 
} 
.block2 { 
width: 100px; 
height: 100px; 
background-color: blue; 
} 
</style> 
</head> 
<body> 
<div class="block1"></div> 
<div class="block2"></div> 
</body> 
</html> 

In the example above, the distance between block1 and block2 is 20px because we set a right margin of 20px for block1.

2. Padding Property

2.1 What is the padding property?

In CSS, padding refers to the inner margin of an element, that is, the distance between the content and the border of an element. By setting padding, you can adjust the distance between the content and the border of an element.

2.2 How to Set the Padding Property

You can set the padding property in the following ways:

.element {
padding: 10px; /* Set the top, bottom, left, and right padding of the element to 10px */ 
}

You can also set the padding in the top, right, bottom, and left directions separately:

.element {
padding-top: 10px; /* Top padding is 10px */
padding-right: 20px; /* Right padding is 20px */
padding-bottom: 15px; /* Bottom padding is 15px */
padding-left: 30px; /* Left padding is 30px */
}

2.3 Example

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Padding example</title> 
<style> 
.block1 { 
width: 100px; 
height: 100px; 
background-color: red; 
padding-right: 20px; 
} 
.block2 { 
width: 100px; 
height: 100px; 
background-color: blue; 
} 
</style> 
</head> 
<body> 
<div class="block1"></div>
<div class="block2"></div>
</body>
</html>

In the example above, the distance between block1 and block2 is also 20px because we set a right padding of 20px for block1.

3. Differences between Margin and Padding

Although both margin and padding can be used to increase the distance between elements, there are some differences between the two:

  • Margin controls the distance between an element and its surrounding elements, while padding controls the distance between an element’s content and its border.
  • Margin affects the distance between an element and its surrounding elements, but does not affect the element’s own size; padding affects the element’s own size.
  • When margin is set, the distance between elements is added together; when padding is set, the distance between an element’s content and its border is not added together.

In summary, we can choose the appropriate method to adjust the distance between elements based on specific needs.

Through the introduction in this article, I believe readers have a clearer understanding of how to use CSS to increase the distance between two blocks.

Leave a Reply

Your email address will not be published. Required fields are marked *