CSSGrid repeating, dynamic, horizontal div elements - html

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;

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; */
}

How to set 3 images on bootstrap left, center and right, As I am using loop on the images

All the images are coming at the center, I Even tried with Justify-content-between, But if from the backend only two images come to the edges and the center space is empty.
<div class="mm-mkt-BadgeRow row mx-auto">
<sly data-sly-list.awardimages = "${aboutlo.Award_Image_LINK}">
<div class="col-4 pl-3">
<img src="${awardimages}" class="mm-mkt-BadgeOne" id="mm-mkt-badgeOne">
</div>
</sly>
</div>
.grid-row {
display: grid ;
justify-content: stretch;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
}
.grid-row div:nth-child(3n+2){text-align:center; }
.grid-row div:nth-child(3n+3){text-align:right; }
<div class="grid-row">
<div class"item1">1</div>
<div class"item2">2</div>
<div class"item3">3</div>
<div class"item1">1</div>
<div class"item2">2</div>
<div class"item3">3</div>
<div class"item1">1</div>
<div class"item2">2</div>
<div class"item3">3</div>
</div>

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>

Boxes with space between (different width/height)

I'm trying to build an element for a magazine website, where the first element consists of post promotion boxes with different widths and heights like on the following pic:
I'm using ZURB Foundation
I need a gutter between the boxes like on the pic
Using collapse and appropriate image sizes, I can get the desired result, but without gutters between the pics.
When I define custom foundation gutters manually, the height of the left pic changes for some pixels
Here is my approach:
<div class="row collapse">
<div class="column medium-6"><!-- Left Pic 50% width --></div>
<div class="column medium-6">
<div class="row collapse">
<div class="column medium-12"><!-- Right Top Pic 50% width, 50% height --></div>
<div class="column medium-6"><!-- Right Bottom Left Pic 25% width, 50% height --></div>
<div class="column medium-6"><!-- Right Bottom Right Pic 25% width, 50% height --></div>
</div>
</div>
</div>
It seems a good scenario for Grid layout
The basic idea is to create a template of the desired grid using grid-template property on the parent container and later to assign each area to the children elements with grid-area property.
Codepen demo
Markup
<section class="grid">
<div class="item first">
<img src="https://placekitten.com/g/600/500" />
</div>
<div class="item second">
<img src="https://placekitten.com/g/300/150" />
</div>
<div class="item third">
<img src="https://placekitten.com/g/149/100" />
</div>
<div class="item fourth">
<img src="https://placekitten.com/g/149/100" />
</div>
</section>
CSS
.grid {
width: 600px;
height: 250px;
display: grid;
grid-column-gap: 3px;
grid-row-gap: 3px;
grid-template:
"first first second second" 1fr
"first first second second" 1fr
"first first second second" 1fr
"first first third fourth" 1fr
"first first third fourth" 1fr / 1fr 1fr 1fr 1fr;
}
.grid .first { grid-area: first; }
.grid .second { grid-area: second; }
.grid .third { grid-area: third; }
.grid .fourth { grid-area: fourth; }
Images can be added either as regular img elements or as backgrounds, as you prefer.
Result