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>
Related
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>
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>
This question already has an answer here:
Change div order with CSS depending on device-width
(1 answer)
Closed 3 years ago.
I have a div that shows an image and text and i load the content with a foreach and i need to invert the order div in second row of foreach.
First div shows:
Image - Text
Second div shows:
Text - Image
For example:
.content
{
width:100%;
height: 200px;
}
.content .image, .content .text
{
width:50%;
float:left;
}
.content .image img
{
width:100%;
}
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text1</span>
</div>
</div>
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text2</span>
</div>
</div>
Thank you
You can do this with at least 2 options
1. using flexbox see below
.content {
width: 100%;
height: 200px;
display: flex;
}
.content .image,
.content .text {
width: 50%;
float: left;
}
.content .image img {
width: 100%;
}
.content:nth-child(even) {
flex-direction: row-reverse
}
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text1</span>
</div>
</div>
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text2</span>
</div>
</div>
2. using float:right see below
.content {
width: 100%;
height: 200px;
}
.content .image,
.content .text {
width: 50%;
float: left;
}
.content .image img {
width: 100%;
}
.content:nth-child(even) .image{
float:right;
}
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text1</span>
</div>
</div>
<div class="content">
<div class="image">
<img src="https://www.w3schools.com/css/trolltunga.jpg" />
</div>
<div class="text">
<span>text2</span>
</div>
</div>
Trying to align the image vertically, and the text centered according to image. The whole card is about 300px * 200px. I want the image to be on the left hand side with text on the right.
<div class="card center-block">
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="photo">
<img class="rounded" src="../im/3.jpg" width="130" height="130" style="min-height:50px;" />
}
</div>
</div>
<div class="col-sm-6" style="height: 250px; align-items:center">
<div class="content">
<h5 class="left-align"><strong>John Doe</strong></h5>
<p class="left-align">Software Developer</p>
</div>
</div>
</div>
</div>
CSS:
.card {
min-width: 350px;
max-width: 350px;
min-height: 200px;
max-height: 200px;
}
.photo {
padding-top: 35px;
margin-right: 10px;
display:inline-block;
height: 100%;
float: left;
}
.content{
height: 100%;
}
You can add a class to the row and use flexbox positioning. align-items: center on the row that holds the 2 columns, then also use flex on the .content element and create a flex column with justify-content: center
.card {
min-width: 350px;
max-width: 350px;
min-height: 200px;
max-height: 200px;
}
.photo {
margin-right: 10px;
display:inline-block;
height: 100%;
float: left;
}
.special-row {
display: flex;
align-items: center;
}
.special-row .content {
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card center-block">
<div class="container"><div class="row special-row">
<div class="col-sm-6">
<div class="photo">
<img class="rounded" src="http://androidandme.com/wp-content/uploads/2015/12/Google-Now-on-Tap.jpg"width="130" height="130" style="min-height:50px;" />
</div>
</div>
<div class="col-sm-6 details" style="height: 250px; align-items:center">
<div class="content">
<h5 class="left-align"><strong>John Doe</strong></h5>
<p class="left-align">Software Developer</p>
</div>
</div>
</div>
</div>
Please see my HTML structure below - I am trying to have the .prod divs to be to the right of the logo, and the logo to be the full height of the .row div
I know this can be done using tables and floats but I want to try and avoid using those.
Here's my structure:
.row {
width: 100%;
}
.row > div {
display: inline-block;
}
.row .image {
height: 100%;
width: 24%;
}
.row .prod {
width: 75%;
height: auto;
}
.prod > div {
display: inline-block;
width: Calc(50% - 4px);
}
div {
border: solid 1px black;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="" />
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
Wrap the additional info inside another div and give it the remaining width.
I have given it 74% because of the extra space from inline-block elements. Adjust it to your requirement. I would prefer flexbox if you are implementing it for modern browsers.
.row {
width: 100%;
}
.row > div {
display: inline-block;
}
.row .image {
height: 100%;
width: 24%;
vertical-align: top; /* Default to baseline, align to the top */
}
.row .product_info {
width: 74%; /* Remaining width */
}
.row .product_info .prod {
/* width: 75% */ Remove
height: auto;
}
.prod > div {
display: inline-block;
width: Calc(50% - 4px);
}
div {
border: solid 1px black;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="" />
</div>
<div class="product_info">
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
</div>
flexbox can do that.
.row {
display: flex;
}
.image,
.prod {
flex: 1;
background: lightblue;
text-align: center;
height: 75px;
}
.image {
flex: 0 0 auto;
background: orange;
display: flex;
flex-direction: column;
}
.image img {
height: 100%;
width: auto;
}
<div class="row">
<div class="image">
<img alt="Full Height Logo" src="http://lorempixel.com/output/food-q-c-50-50-1.jpg" />
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
<div class="prod">
<div class="prod_image">
<img alt="Product Image" src="" />
</div>
<div class="prod_info">
Prod Info
</div>
</div>
</div>
Flexbox?
I'm still pretty new to flexbox myself, but you could use the align-items: stretch property on your container to cause your items to stretch, but give the .prod div's a max-height so that they only stretch so much. You can also set various widths properties using the flex property on your flex items so that you get the width your after.
For more information: https://css-tricks.com/snippets/css/a-guide-to-flexbox/