I am struggling with my code. I need 8 boxes (2 columns with 4 rows) and the content shall be centered (horizontally and vertically). The content will be either an image or a text. The boxes shall have 50% of the width and there shall be space between the two boxes on one line. I tried different ways, but I wasn't successful.
My current code looks like this:
.desc {
display: grid;
grid-template-columns: 50% 50%;
padding-bottom: 30px;
text-align: center;
}
.image {
height: 200px;
max-height: 300px;
max-width: 500px;
object-fit: cover;
object-position: center;
}
.text {
text-align: center;
vertical-align: middle;
}
<div class="desc">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
If you check the below example, there are 8 boxes which are aligned in 2 columns and 4 rows with contents are vertically and horizontally centred. Check it and let me know if this is what you are looking for
* {
box-sizing: border-box;
}
section {
display: flex;
flex-wrap: wrap;
border: 1px solid red;
}
div {
flex: 1;
display: inline-flex;
max-width: 50%;
min-width: 50%;
border: 1px solid #ddd;
justify-content: center;
align-items: center;
text-align: center;
}
.bg-image {
background-image: url('https://i.ytimg.com/vi/k-POG1-Cp1k/maxresdefault.jpg');
background-size: cover;
min-height: 200px;
}
<section>
<div>1</div>
<div>2</div>
<div>3sdass asd assadas asd asdasadas asd asdasdassdaasasd sd as as</div>
<div>4</div>
<div class='bg-image'>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</section>
EDIT
Have updated the snippet with an item with a background-image, the image will automatically resize.
Flex Implementation. An another way.
If you want the texts in the tesxt section to be in two seperate lines, use flex-direction: column; for .text class
.desc {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.image, .text {
display: flex;
flex: 1 1 50%;
align-items: center;
justify-content: center;
padding: 5px;
box-sizing: border-box;
border: 1px dashed black;
flex-direction: column;
}
.image img {
max-width: 100%;
}
<div class="desc">
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
<div class="image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
.desc,
.desc_inner {
width: 99%;
overflow: hidden;
margin: 0px auto;
}
.desc_inner {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 2px dashed #f69c55;
}
.text {
width: 100%;
text-align: center;
}
<!-- today -->
<div class="desc">
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
<div class="desc_inner">
<div class="desc image">
<img src="https://homepages.cae.wisc.edu/~ece533/images/mountain.png">
</div>
<div class="text">
<b>Text</b>
<p>Text</p>
</div>
</div>
</div>
Related
I'm trying to implement horizontal scrolling of fixed width images which are wrapped in divs. The entire layout is wrapped in flex with a left right layout.
However, I'm not able to keep the parent width of the boxes from overflowing. I need the children boxes to scroll horizontally and its parent contained in a flex.
Link to fiddle: https://jsfiddle.net/wn8zd2t6/43/
.dash {
display:flex;
width:100%;
}
.left {
width: 380px;
height: 100vh;
}
.right {
flex: 1 1 0%;
display:flex;
flex-direction: column;
}
.b {
border:1px solid black;
}
.ig {
display:inline-block;
height:100px;
width:180px;
object-fit: cover;
}
.scrollable {
white-space: nowrap;
overflow-x:scroll;
}
.box {
display: inline-block;
}
<div class="dash">
<div class="left b">
left
</div>
<div class="right b">
<div>
top section
</div>
<div class="container">
<!-- necessary -->
<div>
scrollable section title
</div>
<!-- need this to be scroll -->
<div class="scrollable">
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
</div>
</div>
</div>
</div>
The flex-basis: 0% isn't enough to define a fixed width on the images container, therefore the overflow function doesn't have a break point.
Instead of flex: 1 1 0% use width: calc(100% - 380px) (the 380px being the fixed width of the other column).
This is all you need:
.right {
/* flex: 1 1 0%; */
width: calc(100% - 380px); /* new */
}
revised jsfiddle
.dash {
display: flex;
width: 100%;
}
.left {
width: 380px;
height: 100vh;
}
.right {
/* flex: 1 1 0%; */
width: calc(100% - 380px); /* new */
display: flex;
flex-direction: column;
}
.b {
border: 1px solid black;
}
.ig {
display: inline-block;
height: 100px;
width: 180px;
object-fit: cover;
}
.scrollable {
white-space: nowrap;
overflow-x: scroll;
}
.box {
display: inline-block;
}
<div class="dash">
<div class="left b">
left
</div>
<div class="right b">
<div>
top section
</div>
<div class="container">
<!-- necessary -->
<div>
scrollable section title
</div>
<!-- need this to be scroll -->
<div class="scrollable">
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
<div class="box">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" class="ig" />
<div>
img caption
</div>
</div>
</div>
</div>
</div>
</div>
I simplified your flex code into the following. I added border styling to the image divs for visual clarity of what's going on.
It's important to remember that to constrain a child element's width to a parent with flex view, the parent flex container must have a constraint on it's width, even if it's width: 100%;
You can see this within the .right class and that controls how the rest of that column's children behave with their flex-grow.
/* Utility */
.border-settings {
border: 1px solid black;
}
.scrollable {
display: flex;
flex-direction: row;
overflow-x: scroll;
padding: 10px;
}
/* Container Settings */
.dashboard {
display: flex;
flex-direction: row;
width: 100%;
height: 100vh;
}
.left {
display: flex;
flex-direction: column;
flex-grow: 1;
}
.right {
display: flex;
flex-direction: column;
flex-grow: 1;
height: 100%;
max-width: 50%;
}
.container {
display: flex;
flex-direction: column;
flex-grow: 1;
}
.card {
margin: 10px;
padding: 10px;
border-style: solid;
border-color: black;
border-width: 1px;
border-radius: 10px;
}
/* Element Settings */
img {
height: 100px;
width: 180px;
}
<div class="dashboard">
<div class="left border-settings">
left Section
</div>
<div class="right border-settings">
<div class="top">
top section
</div>
<div class="container">
<!-- necessary -->
<div>
scrollable section title
</div>
<!-- need this to be scroll -->
<div class="scrollable">
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/150/FF0000/FFFFFF" />
<p>img caption</p>
</div>
</div>
</div>
</div>
</div>
How to align display: inline-block cards horizontally with an equal top border, with CSS?
Why is there a unequal top boder for each card when you open the following snippet in Full page width?
I used a fixed height and width for each .card element so I expected that it would be aligned.
Note: I also tried with display: table-cell but then I lost the fact that it's fluid and that the cards auto-adapt to the browser width (I'd like to keep an automatic number of cards per row, fitting the browser width, without having a horizontal scrollbar).
#main { width: 100%; }
.cell { display: inline-block; }
.card { border: 1px solid black; margin: 3em; width: 15em; height: 30em; }
.card img { width: 15em; height: 20em; }
.container { padding: 2px 16px; height: 10em; width: 100%; box-sizing: border-box; }
<div id="main">
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla</p>
</div>
</div>
</div>
</div>
#main { width: 100%; display: grid; grid-template-columns: repeat(auto-fill, 320px); justify-content: center; }
.card { border: 1px solid black; margin: 3em; width: 15em; height: 30em; }
.card img { width: 15em; height: 20em; }
.container { padding: 2px 16px; height: 10em; width: 100%; box-sizing: border-box; }
<div id="main">
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla</p>
</div>
</div>
</div>
</div>
do you mean something around these lines ?
#main {
display: flex;
gap: 40px;
flex-wrap: wrap;
width: 100%;
}
.cell { display: inline-block; }
.card { border: 1px solid black; width: 15em; height: 30em; }
.card img { width: 15em; height: 20em; }
.container { padding: 2px 16px; height: 10em; width: 100%; box-sizing: border-box; }
<div id="main">
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla blablabla</p>
</div>
</div>
</div>
<div class="cell">
<div class="card">
<img src="">
<div class="container">
<h4><b>Abc</b></h4>
<p>blablabla</p>
</div>
</div>
</div>
</div>
You could add vertical-align: middle to your cell class.
.cell { display: inline-block; vertical-align: middle;}
Super easy with flex and grid altogether. Flex is good for self center and adjustment. Grid will help us for the whole page margins and alignment:
.container {
display: grid;
align-items: center;
justify-content: center;
height: 100%;
}
.card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 200px;
height: 200px;
background-color: lightblue;
padding: 20px;
text-align: center;
}
You can adjust the width and height of the card to fit your needs, and also modify the padding and text alignment as desired.
P.S: Also, if you demand scrollbars to work, don't use flexbox. Change it to grid:
.card {
display: flex;
}
Here is the coding block. Will be my first time so behold:)
.container {
display: grid;
align-items: center;
justify-content: center;
height: 100%;
}
.card {
display: grid;
flex-direction: column;
align-items: center;
justify-content: center;
width: 200px;
height: 200px;
background-color: lightblue;
padding: 20px;
text-align: center;
}
.topnav {
white-space: nowrap;
display: grid;
grid-auto-flow: column;
overflow-x: scroll;
width: 20rem;
}
.topnav a,
.topnav button {
float: left;
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
<div class="container">
<div class="twitter-card">
<p>Here is the card</p>
<textarea cols="40" rows="10" ></textarea>
<div class="topnav">
<button>BIST </button>
<button>BIST Fav</button>
<button>EMTIA </button>
<button>Other</button>
<button>Other</button>
<button>Other </button>
<button>Other </button>
</div>
</div>
</div>
I have read all answers I think to this SO question, and changed max-height to every different combination, but it still bothers me;
I have the following
<div class="content-section">
<div class="products-container">
{% for p in current_lists %}
<div class="item">
<div class="image-wrapper">
<img src="{{p.image_url}}" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
{% endfor %}
</div>
</div>
and CSS
* {
box-sizing: border-box;
}
.products-container{
display: flex;
align-items: center;
background-color: blue;
}
.products-container .item{
max-width: 30%;
max-height: 50%;
margin: 10px;
background-color: blueviolet;
}
img{
max-width: 100%;
max-height: 100%;
}
The issue is, that the image expands outside the blue-box
I have tried changing max-height in img and in products-container .item down to 10%, but it does not scale the big image at all.
If height is the issue then I would say:
width:100%;
height:auto;
I added your bits into a codepen and didn't have the issue:
https://codepen.io/liam88/pen/zYwYNVN
<div class="content-section">
<div class="products-container">
<div class="item">
<div class="image-wrapper">
<img src="https://source.unsplash.com/user/erondu/600x600" alt="Image of product added"/>
</div>
<div class="total-price">£12.99</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://source.unsplash.com/collection/190727/600x600" alt="Image of product added"/>
</div>
<div class="total-price">£12.99</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://source.unsplash.com/daily" alt="Image of product added"/>
</div>
<div class="total-price">£12.99</div>
</div>
</div>
</div>
* {
box-sizing: border-box;
}
.products-container{
display: flex;
align-items: center;
background-color: blue;
}
.products-container .item{
max-width: 30%;
max-height: 50%;
margin: 10px;
background-color: blueviolet;
}
img{
width: 100%;
height: auto;
}
There is a possible solution:
Setting the container height to auto, and manipulating the width. I also used align-self: flex-end to make sure everything is aligned to the bottom of the blue-box. Using proportional images will make it all of them the same height, after auto-resizing.
* {
box-sizing: border-box;
}
.products-container{
display: flex;
align-items: center;
background-color: blue;
}
.products-container .item{
width: 10%;
height: auto;
margin: 10px;
background-color: blueviolet;
align-self: flex-end;
}
.image-wrapper img{
max-width: 100%;
max-height: 100%;
}
<div class="content-section">
<div class="products-container">
<div class="item">
<div class="image-wrapper">
<img src="https://cdn-images.farfetch-contents.com/14/66/46/09/14664609_23855407_1000.jpg" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://images-na.ssl-images-amazon.com/images/I/41DIxdHUYCL._AC_UL1020_.jpg" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
<div class="item">
<div class="image-wrapper">
<img src="https://assets.burberry.com/is/image/Burberryltd/2d6345e1d1fe0353ef7f95098c4d890c0a76ebb1.jpg?$BBY_V2_SL_1x1$&wid=2800&hei=2800" alt="Image of product added" />
</div>
<div class="total-price">{{price}}
</div>
</div>
</div>
</div>
In my current desktop markup, I'm trying to add a 800px media query. In the desktop version, I have 2 rows of cards, that in the 800px bp, I want 2 columns of 4 cards each. How can I achieve this?
.sub-section .blank-containers {
width: 250px;
height: 250px;
background: rgb(197, 179, 196);
margin: 5px;
display: flex;
justify-content: space-evenly;
border-radius: 10px;
border: 1px solid;
}
.sub-section {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
margin-top: 10px;
border: 1px solid;
}
#media (max-width: 800px) {
section.sub-section {
flex-direction: column;
flex-wrap: wrap;
}
}
<section class="sub-section">
<div class="img1">
<div class="blank-containers"></div>
<p>image One</p>
</div>
<div class="img2">
<div class="blank-containers"></div>
<p>image Two</p>
</div>
<div class="img3">
<div class="blank-containers"></div>
<p>image Three</p>
</div>
..........
</div>
</section>
</section>
Add a common class with img1, img2, img3, ..... which will take 50% of the parent container width.
like
.img {
width: 50%;
display: flex;
justify-content: center;
}
And then
<section class="sub-section">
<div class="img1 img">
<div class="blank-containers"></div>
<p>image One</p>
</div>
<div class="img2 img">
<div class="blank-containers"></div>
<p>image Two</p>
</div>
<div class="img3 img">
<div class="blank-containers"></div>
<p>image Three</p>
</div>
..........
</div>
</section>
</section>
Hope it will work for you.
https://jsfiddle.net/joel081112/6yud4bvo/3/
I have this fiddle which shows my current situation. I cant get my text to always be aligned in the middle of the div next to the image. At the minute the text stays level with the top of the div.
I have tried vertical-aligned and top: 50% but I must be missing something easy
.homeInfo2 {
padding: 20px;
height: auto;
text-align: center;
float: left;
}
.homeIm1 {
height: auto;
float: right;
}
.item {
display: flex;
width: 100%;
}
.item:nth-child(odd) {
flex-direction: row-reverse;
}
.item > div {
width: 50%;
}
<article class="item">
<div class="homeInfo2">
some text 1
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
That code is the crux of what I'm using
Since your .item element is a flexbox set align-items: center on the item:
.item {
display: flex;
width: 100%;
align-items: center;
}
.item:nth-child(odd) {
flex-direction: row-reverse;
}
.item>div {
width: 50%;
}
.homeIm1 {
height: auto;
}
.homeInfo2 {
padding: 20px;
text-align: center;
}
img {
max-width: 100%;
height: auto;
}
#media (max-width: 500px) {
.item,
.item:nth-child(odd) {
flex-direction: column;
}
.item>div {
width: 100%;
}
}
<div class="container">
<article class="item">
<div class="homeInfo2">
some text 1
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text 2
</div>
<div class="image">
some image 2
</div>
</article>
<hr>
<article class="item">
<div class="homeInfo2">
some 1 text blah
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
</div>
Try using flex.
You have to create a div next to the image and then justify-content-center and align-items-center. For more information use google :) Especially css-tricks would be helpful! CSS-Tricks
Just add align-items: center; in your item class.
.item {
display: flex;
width: 100%;
align-items: center;
}
.item:nth-child(odd) {
flex-direction: row-reverse;
}
.item>div {
width: 50%;
}
.homeIm1 {
height: auto;
float: right;
}
.homeInfo2 {
padding: 20px;
height: auto;
text-align: center;
float: left;
}
img {
max-width: 100%;
height: auto;
}
#media (max-width: 500px) {
.item,
.item:nth-child(odd) {
flex-direction: column;
}
.item>div {
width: 100%;
}
}
<div class="container">
<article class="item">
<div class="homeInfo2">
some text 1
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text 2
</div>
<div class="image">
some image 2
</div>
</article>
<hr>
<article class="item">
<div class="homeInfo2">
some 1 text blah
</div>
<div class="homeIm2">
<img src="https://cdn1.imggmi.com/uploads/2019/10/11/13fad36a93db836e4eaa1906b8f16433-full.jpg">
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
<hr>
<article class="item">
<div class="text">
some text
</div>
<div class="image">
some image
</div>
</article>
</div>