Boxes with space between (different width/height) - html

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

Related

CSS Grid Specify First Column and set Fractional Unit For Unknown Remaining

I am trying to learn the CSS grid, but having trouble with what seems like it should be a basic repeat. Consider the following; I have 3 div elements in a header tag. I want the first div to be at least 450px and the remaining div elements (which could be 2, 3 or 4) to all be equal parts of the remaining space.
I assumed the following would work, but it seems it's not as straightforward as I thought. What am I missing?
header {
display: grid;
grid-template-columns: minmax(450px, 1fr) repeat(auto-fill, 1fr);
grid-gap: 1rem;
}
<header>
<div>
<p>450px</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
</header>
You can use auto-fit instead of auto-fill:
auto-fit
Behaves the same as auto-fill, except that after placing the grid items any empty repeated tracks are collapsed. An empty track is one with no in-flow grid items placed into or spanning across it. (This can result in all tracks being collapsed, if they’re all empty.)
header {
display: grid;
grid-template-columns: minmax(450px, 1fr) repeat(auto-fit, minmax(0, 1fr));
grid-gap: 1rem;
}
<header>
<div>
<p>450px</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
</header>
<header>
<div>
<p>450px</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
</header>
You need a column flow for this:
header {
display: grid;
grid-auto-flow:column; /* column flow */
grid-template-columns: minmax(450px, 1fr); /* first column */
grid-auto-columns:1fr; /* all remaining equal width */
grid-gap: 1rem;
}
<header>
<div>
<p>450px</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
</header>
<header>
<div>
<p>450px</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
<div>
<p>1fr</p>
</div>
</header>

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>

Auto column widths with images inside

I am currently trying to achieve the following type of grid/column but I am not able to.
Some images have bigger width than others so they need to take up more space.
I will have around 30ish images that I will loop through so i can't just put a set width to each column.
I am not sure how to tackle this problem.
I have the following and I want to have 3 per row it is giving me somewhat okey results but not exactly.
https://codepen.io/BlooDyBG/pen/OJVxYEj
<div class="flex-container">
<div class="flex-item">
<a href="https://ibb.co/CBBLGnp"><img src="https://i.ibb.co/WnnCJcN/Image-2.png" alt="Image-2"
border="0"></a>
</div>
<div class="flex-item">
<a href="https://imgbb.com/"><img src="https://i.ibb.co/HqdrqQB/Image-3.png" alt="Image-3" border="0">
</a>
</div>
<div class="flex-item">
<img src="https://i.ibb.co/r0YZVVw/Image-4.png" alt="Image-4" border="0">
</div>
<div class="flex-item">
<img src="https://i.ibb.co/7pSqfZn/Image-5.png" alt="Image-5" border="0">
</div>
<div class="flex-item">
<img src="https://i.ibb.co/R6Vd1WH/Image-6.png" alt="Image-6" border="0">
</div>
<div class="flex-item">
<img src="https://i.ibb.co/HqdrqQB/Image-3.png" alt="Image-3" border="0">
</div>
</div>
CSS
.flex-container {
display : flex;
flex-wrap: wrap;
padding : 20px;
}
.flex-item {
flex: 1 1 auto;
padding : 20px;
}
img {
width : 100%;
height : 100%;
}
The third part of the flex shorthand is flex-basis, which informs the browser how to treat sizing of the flexible items. In this case, you've set it to auto which instructs the browser to use the width of the content to determine the size.
Instead, you should use something like flex: 1 1 30% to ensure that all of the items have a consistent size.
Alternately, you can create this type of layout with CSS Grid. That would look something like this:
.flex-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}

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 }

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;