Horizontal border across entire row of CSS GRID - html

I need to use a grid layout but also need a horizontal line separating each row.
The only thing I've been able to find is applying a border to each cell, but this only works if there are enough cells to fill each row.
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
}
.box {
border-bottom: 2px solid #ffa94d;
padding: 1em;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>
Is there a way to fix the above so that the entire row has a border?

Add a grid-gap equal to the width of your border then consider gradient to achieve this:
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
grid-row-gap:2px;
background:
repeating-linear-gradient(to bottom,
transparent 0,
transparent 100px,
#ffa94d 100px,
#ffa94d 102px /*+2px here*/
);
}
.box {
padding: 1em;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>
Another idea is to consider a pseudo-element that you add to the 1st,4th,7th .. (3n + 1)th element:
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
overflow:hidden;
}
.box {
position:relative;
padding: 1em;
}
.box:nth-child(3n + 1)::after {
content:"";
position:absolute;
bottom:0px;
left:0;
width:100vw;
height:2px;
background:#ffa94d;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box">Four</div>
</div>

Imagine your table as a collection of cells (much like an excel spreadsheet). You can create a simple cell class that you append to each of your grid items to manipulate the cells without affecting the table data itself. Consider:
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
grid-row-gap: 20px;
}
.cell {
position: relative;
border-bottom: 2px solid #ffa94d;
}
<div class="wrapper">
<!-- Here is your first row -->
<div class="cell">One</div>
<div class="cell">Two</div>
<div class="cell">Three</div>
<!-- Here is your second row -->
<div class="cell">Four</div>
<!-- You can extend the line by the number of cells per row -->
<div class="cell"></div>
<div class="cell"></div>
<!-- Organize your html into row groups to easily visualize them -->
<!-- This will produce a blank row with no line -->
<div></div>
<div>-- blank row --</div>
<div></div>
<!-- You can also control where the line begins and ends -->
<div class="box cell">First Column Only</div>
<div></div> <!-- No cells here.. We just want to underline the first column -->
<div></div>
<!-- 2nd and 3rd columns only -->
<div></div>
<div class="cell">Second Column</div>
<div class="cell">Third Column</div>
</div>
Note that I only used a grid-row-gap. If you introduce a grid-gap, or a grid-column-gap, your lines will be broken at the column gaps (this may be the desired effect in some cases).
It is true that this is a more involved method of controlling the horizontal lines separating the grid and less "programmatic" and more micro-management-esque but, it does provide great control over introducing the lines into your grid.
The other answers were great options too! I just wanted to provide my two cents.

This can be achieved with a pseudo element, absolutely positioned. It will override the grid-gap. You will have to set it to a wide width, which is only a little hacky, then set the overflow on the container to hidden.
body {
margin:0;padding:0;
}
.products {
display:grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
grid-gap:20px;
overflow:hidden;
border-bottom:1px solid black;
}
.card-product {
position:relative;
text-align:center;
}
.card-product:after {
content:'';
position:absolute;
border-bottom:1px solid black;
top:-20px;left:0;
height:1px;
width:1000%;
}
<section class="products">
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
<article class="card-product">
<h3 class="card-product__title">
Product Title
</h3>
<h4 class="card-product__sub">
Product Category
</h4>
</article>
</section>

The box class puts a border in the grid children, and you can make a grid children grow to fill any number of columns; below there's an example, where definig the class span3, the fourth grid child spans for 3 columns.
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
}
.box {
border-bottom: 2px solid #ffa94d;
padding: 1em;
}
.span3 {
grid-column-end: span 3;
}
<div class="wrapper">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
<div class="box span3">Four</div>
</div>

Related

Why Can't i create columns with grid?

I've been having problems to create multiple columns in my site using html and CSS with grid, for some reason the command grid-template-rows:2 does not create a second column, but when I try with flexboxes it does create multiple columns.
.first-container {
display: grid;
grid-template-rows: 2;
}
<form>
<div class="first-container">
<!--<div class = "tittle"><h1>Controle Digital Web 1.0</h1></div>-->
<div class="boxes-box-1">
<div class="subtittles">
<h3>Subtittle 1:</h3>
</div>
</div>
<div class="boxes-box-2">
<div class="subtittles">
<h3>Subtittle 2:</h3>
</div>
</div>
<div class="boxes-box-3">
<div class="subtittles">
<h3>Subtittle 3:</h3>
</div>
</div>
<div class="boxes-box-4">
<div class="subtittles">
<h3>Subtittle 4:</h3>
</div>
</div>
</div>
</form>
Results with the code:
img-grid
Results changing the code to display with flexboxes:
img-flex
You could try using this code:
.first-container {
display: grid;
grid-template-columns: auto auto auto auto;
/* grid-template-rows: 0px 0px; */
}

Put a text beneath an image

I am currently working on a website and what i want to do is I want four pictures in a square and I want a text underneath them.
I already managed to put the four images but once i try to put a text it goes everywhere but not underneath the images
HTML:
<section class="section-2">
<div class="item">
<img src="#">
<p>text</p>
</div>
<div class="item">
<img src="#">
<p>text</p>
</div>
<div class="item">
<img src="#">
<p>text</p>
</div>
<div class="item">
<img src="#">
<p>text</p>
</div>
</section>
CSS:
.section-2 {
margin: 200px 30px 30px 30px;
width:100%;
float:right;
min-height:1000px;
height:100%;
}
img {
float:left;
margin: 0px 100px 200px 150px;
width: 30%;
height: 30%;
The easiest and shortes way would be to simply align the cards in a grid. For that use display: grid;. To have 2 cards aligned horizontally you need to add: grid-template-columns: repeat(2, 1fr); you can change the number 2 with the numebr of cards you want to have aligned next to each other. To seperate the cards from each other, you can use grid-gap: with a value of the gap you want to have.
.section-2 {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 15px;
}
.section-2 div img {
width: 100%;
}
<section class="section-2">
<div>
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg">
<p>I'm a Syrian Hamster</p>
</div>
<div>
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0736.jpg">
<p>I like to eat watermelons</p>
</div>
<div>
<img src="https://www.tacoshy.de/Images/Areno/IMAG0865.jpg">
<p>I love to burrow tunnels and caves</p>
</div>
<div>
<img src="https://www.tacoshy.de/Images/Areno/IMAG0863.jpg">
<p>And I really enjoy sleeping in my self digged caves</p>
</div>
</section>

Flexbox - How to prevent long text from shifting divs to the right? [duplicate]

This question already has an answer here:
Why is a flex item limited to parent size?
(1 answer)
Closed 3 years ago.
I've got a layout that I'm working on that uses a combination of CSS Grid and Flexbox. The grid is setting up the structure (left hero image and 4 rows of content on the right).
Within each content row, I've got a flexbox that handles placement of a thumbnail image on the right, and three lines of text on the left - date, title and author.
However, when any of the lines of text are too long, they start bumping up the thumbnail image, as in Row 2 here:
Obviously I want the thumbnail to stay fixed in place and the text to just wrap before they touch it. I've tried a whole bunch of potential strategies - use of overflows, floats instead of flexbox, but can't find the trick. Any ideas?
Here's the code, Row 3 and Row 4 omitted for brevity, and a Codepen link:
https://codepen.io/anon/pen/dBWyzb
.wrapper {
width: 1200px;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(2, minmax(100px, auto));
grid-template-areas:
"hero row1"
"hero row2"
"hero row3"
"hero row4";
grid-gap: 10px;
}
.hero {grid-area: hero;}
.row1 {grid-area: row1;}
.row2 {grid-area: row2;}
.flex {
display: flex;
background-color: lightblue;
align-items: center;
}
.thumbnail {
width: 100px;
height: 100px;
margin-left: auto;
}
img {
object-fit: cover;
height: 100%;
width: 100%;
}
<div class="wrapper">
<div class="grid">
<div class="hero">
<img src="https://dummyimage.com/600x400/000/fff"/>
</div>
<!-- Row 1 -->
<div class="row1">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>Title</h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div> <!-- end flex -->
</div> <!-- end row -->
<!-- Row 2 -->
<div class="row2">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>A really long title seems to be shifting the right-hand image up, which is not ideal! Any ideas, internet? ------> </h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div> <!-- end flex -->
</div> <!-- end row -->
</div>
</div>
Add flex: 1; to the .text div, so the flexbox can manage how much space it can take:
.wrapper {
width: 1200px;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(2, minmax(100px, auto));
grid-template-areas: "hero row1" "hero row2" "hero row3" "hero row4";
grid-gap: 10px;
}
.hero {
grid-area: hero;
}
.row1 {
grid-area: row1;
}
.row2 {
grid-area: row2;
}
.flex {
display: flex;
background-color: lightblue;
align-items: center;
}
.text {
flex: 1;
}
.thumbnail {
width: 100px;
height: 100px;
margin-left: auto;
}
img {
object-fit: cover;
height: 100%;
width: 100%;
}
<div class="wrapper">
<div class="grid">
<div class="hero">
<img src="https://dummyimage.com/600x400/000/fff" />
</div>
<!-- Row 1 -->
<div class="row1">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>Title</h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div>
<!-- end flex -->
</div>
<!-- end row -->
<!-- Row 2 -->
<div class="row2">
<div class="flex">
<div class="text">
<div class="date">12 JULY 2019</div>
<h3>A really long title seems to be shifting the right-hand image up, which is not ideal! Any ideas, internet? ------> </h3>
<div class="author">Author Name</div>
</div>
<div class="thumbnail">
<img src="https://dummyimage.com/600x400/000/fff"></img>
</div>
</div>
<!-- end flex -->
</div>
<!-- end row -->
</div>
</div>
The default setting on flex items is flex-shrink: 1. This means the flex items are allowed to shrink in order to remain inside the container.
In your case, just disable flex-shrink. Add this to your code:
.thumbnail { flex-shrink: 0 }

Set element to be 2fr instead of 1fr CSS Grid Column

I'm trying to create a flexible CSS grid to display some cards. I've set my CSS code for the cards to repeat and auto-fill to a minimum of 330px and a max of 1fr. Everything is fine, but now I have a card that I require to be a little bigger, at 2fr. The problem is very simple, but I can find a way to make this one card to be 2fr instead of 1fr.
Cards container
.cards-row{
display: grid;
grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
grid-column-gap: 15px;
}
<div class="cards-row" style="margin-top: 30px;">
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
</div>
This is basically what I want to achieve
Make the last element to span 2 columns:
.cards-row {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-gap: 15px;
}
.card-wrap {
height: 100px;
background: red;
}
.card-wrap:last-child {
grid-column: span 2;
}
<div class="cards-row" style="margin-top: 30px;">
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
<div class="card-wrap">
</div>
</div>

CSSGrid repeating, dynamic, horizontal div elements

I am pretty new on CSS/Html/JS and want to create a series of Boxes (loaded from a json file) and display them horizontally. Something like This:
I tried to achieve this with the following code:
<style>
.wrapper {
display: grid;
grid-template-columns: auto auto;
}
.Product {
display: grid;
grid-template-columns: auto 1fr;
background-color: rgb(2, 121, 61);
padding: 10px;
}
</style>
<div class"Wrapper">
<div class="Product">
<div>Pos: </div><div id="pos">test1</div>
<div>Artikel: </div><div id="article">test2</div>
<div>Bezeichnung: </div><div id="name">test3</div>
<div>Menge: </div><div id="stock">test4</div>
<div>Einheit:</div><div id="einheit">test5</div>
<div>Lagerplatz:</div><div id="shelf">test6</div>
<div>Intern:</div><div id="barcode">test7</div>
</div>
<div class="Product">
<div>Pos: </div><div id="pos">test1</div>
<div>Artikel: </div><div id="article">test2</div>
<div>Bezeichnung: </div><div id="name">test3</div>
<div>Menge: </div><div id="stock">test4</div>
<div>Einheit:</div><div id="einheit">test5</div>
<div>Lagerplatz:</div><div id="shelf">test6</div>
<div>Intern:</div><div id="barcode">test7</div>
</div>
</div>
But the result looks like this:
As you can see the divs are not horizontal and the width fills the screen. I want the boxes to be horizontally aligned and not to stop at the screen end. If I could put the whole element into a horizontal scroll view I would be even happier. Thanks for your time.
In your product class use inline-grid...
.wrapper {
display: grid;
grid-template-columns: auto auto;
}
.Product {
display: inline-grid;
grid-template-columns: auto 1fr;
background-color: rgb(2, 121, 61);
padding: 10px;
}
<div class"Wrapper">
<div class="Product">
<div>Pos: </div><div id="pos">test1</div>
<div>Artikel: </div><div id="article">test2</div>
<div>Bezeichnung: </div><div id="name">test3</div>
<div>Menge: </div><div id="stock">test4</div>
<div>Einheit:</div><div id="einheit">test5</div>
<div>Lagerplatz:</div><div id="shelf">test6</div>
<div>Intern:</div><div id="barcode">test7</div>
</div>
<div class="Product">
<div>Pos: </div><div id="pos">test1</div>
<div>Artikel: </div><div id="article">test2</div>
<div>Bezeichnung: </div><div id="name">test3</div>
<div>Menge: </div><div id="stock">test4</div>
<div>Einheit:</div><div id="einheit">test5</div>
<div>Lagerplatz:</div><div id="shelf">test6</div>
<div>Intern:</div><div id="barcode">test7</div>
</div>
</div>
You specify the wrapper class wrong. You should put wrapper not Wrapper in the main div class. If you want a space between columns you can use grid-column-gap: 20px;