How to merge two divs into one with CSS

How to Merge the Borders of Two Adjacent Divs into One with CSS

In web page layouts, we often encounter situations where we need to display two div elements next to each other. However, when two div elements are displayed side by side, there will be a gap between them. This gap is caused by the borders of the two div elements. If we want to merge these two borders into one, we can use some CSS techniques to achieve this effect. This article will introduce several methods to achieve this effect.

Method 1: Use negative margins

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Two Divs Side by Side</title> 
<style> 
.container { 
display: flex; 
} 
.box { 
width: 50%; 
border: 1px solid black; 
margin-right: -1px; 
} 
</style> 
</head> 
<body> 
<div class="container"> 
<div class="box">Div 1</div> 
<div class="box">Div 2</div> 
</div> 
</body> 
</html> 

Output:

How to merge the borders of two adjacent divs into one in CSS


In the example above, we use flex layout and add a 1px border to each div element. To make the two div elements appear side by side, we add the style margin-right: -1px; to the first div element, which combines the two borders into one.

Method 2: Use pseudo-elements

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Two Divs Side by Side</title> 
<style> 
.container { 
display: flex; 
} 
.box { 
width: 50%; 
position: relative; 
} 
.box::after { 
content: ''; 
position: absolute; 
top: 0; 
right: -1px; 
bottom: 0; 
width: 1px; 
background-color: black; } 
</style> 
</head> 
<body> 
<div class="container"> 
<div class="box">Div 1</div> 
<div class="box">Div 2</div> 
</div> 
</body> 
</html> 

Output:

CSS How to merge the borders of two divs into one

In this example, we use the pseudo-element ::after to create a vertical black border, thereby merging the borders of two div elements into one. By setting right: -1px;, the pseudo element overlaps with the border of the first div element, achieving the effect of border merging.

Method 3: Use outline

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Two Divs Side by Side</title> 
<style> 
.container { 
display: flex; 
} 
.box { 
width: 50%; 
outline: 1px solid black; 
} 
</style> 
</head> 
<body> 
<div class="container"> 
<div class="box">Div 1</div> 
<div class="box">Div 2</div> 
</div> 
</body> 
</html> 

Output:

CSS two divs adjacent to each other how to merge the two adjacent borders into one

In this example, we use the outline attribute to add a 1px black border to each div element. Since the outline attribute does not affect the element’s layout, there is no gap between the two div elements, thus achieving a merged border effect.

Through the above three methods, we can achieve the effect of merging the borders of two div elements into one when they are displayed next to each other. In actual projects, you can choose the appropriate method to achieve this effect based on your specific needs.

Leave a Reply

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