I'm using css flexbox to place an unknown number of items in rows, wrapping around to additional rows if needed.
My question is, is it possible to have a horizontal line between each of the rows?
Here is a simple example of what I have. If you open the codepen you will see the items wrap into two lines (it could be more than two; or only one - this depends on the exact number of elements and the width of the display). I would like to have a horizontal line between the rows.
<div>
<span>First item</span>
<span>Second item</span>
<span>Third item</span>
<span>Fourth item</span>
<span>Fifth item</span>
<span>Sixth item</span>
</div>
With the following CSS:
div {
border: 1px solid black;
width:20%;
display: flex;
flex-flow: row wrap;
}
span {
border: 1px solid blue;
margin: 5px;
}
HTML <hr> Tag. The <hr> tag in HTML stands for horizontal rule and is used to insert a horizontal rule or a thematic break in an HTML page to divide or separate document sections. The <hr> tag is an empty tag, and it does not require an end tag. Used to specify the alignment of the horizontal rule.
There are two ways to add a horizontal line between two divs. First, put a <hr> tag between the two divs, and the second is to place any block-level element such as a div, p, etc, between the two divs and apply a bottom or top border on it. Either way, you can achieve the same task.
The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked.
There is a way to add a horizontal line underneath each row using the "flex-grow" property. However, if you wanted to keep exactly 5px between each item - I don't know of a way to accomplish this. If not, here is how you would do it:
Here is a JFiddle of this working.
Here is the code I used:
<style>
div.flexbox {
border-left: 1px solid black;
border-top: 1px solid black;
border-right: 1px solid black;
width:50%;
display: flex;
flex-flow: row wrap;
align-items: stretch;
}
span {
border: 1px solid blue;
margin: 5px;
}
.wrap {
border-bottom: 1px solid black;
padding: 5px;
flex-grow: 1;
}
#last {
flex-grow: 1000;
}
</style>
<div class="flexbox">
<div class="wrap"><span>First item</span></div>
<div class="wrap"><span>Second item</span></div>
<div class="wrap"><span>Third item</span></div>
<div class="wrap"><span>Fourth item</span></div>
<div class="wrap"><span>Fifth item</span></div>
<div class="wrap"><span>Sixth item</span></div>
<div class="wrap"><span>Seventh item</span></div>
<div class="wrap"><span>Eigth item</span></div>
<div class="wrap"><span>Nineth item</span></div>
<div class="wrap"><span>tenth item</span></div>
<div id="last" class="wrap"><span>Eleventh item</span></div>
</div>
If you don't mind the last row being evenly spaced, then you can ignore the part about adding an ID to the last element with a larger flex-grow number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With