It is completely feasible to achieve this without relying on jQuery. The key challenge lies in accommodating the varying heights of the elements.
You can find a helpful reference here: http://jsfiddle.net/uqZgt/1/
Here is the HTML structure:
<div class="container">
<div class="box-1">
This box contains a lot of text. This box contains a lot of text. This box contains a lot of text.
</div>
<div class="box-2">
This box has less content compared to box-1. This box has less content compared to box-1. This box has less content compared to box-1. This box contains more text than box-1. This box contains more text than box-1. This box contains more text than box-1.
</div>
</div>
The corresponding CSS styling:
.container {
width: 242px;
}
.container div {
width: 100px;
background: #ff0000;
float: left;
padding: 10px;
border-right: 2px solid #000
}
.box-1 + .box-2 {
border-right: none;
border-left: 2px solid #000
}
.box-1 ~ .box-2 {
margin-left: -2px
}
In this example, all divs within the .container
have a 2px solid black border on their right side. However, if an element with class box-2
immediately follows an element with box-1
, it will have a 2px solid black border-left and no right border. This setup creates a 3px border gap between the two elements.
The selector .box-1 ~ .box-2
targets any instance of box-1
that directly precedes a box-2
and adjusts its margin-left to -2px. This action shifts its neighboring element by two pixels to the left, essentially causing their borders to overlap.
The total width of the .container
div equals the combined width of both elements (200px), along with padding (10px on both sides = 20px) and the width of one border (2px). This sums up to 242px, ensuring a perfect fit for the two elements.
Regardless of which div contains more text, the border will visually extend to match the height of the taller div.