I've been trying to align my boxes horizontally, but haven't been able to resolve the issue. Each time I try to, they become stacked horizontally. Rather than centered straight across. Or will just remain stacked in a column(vertically).
HTML for box divs:
<div class="section2">
<h1 class="s2h1">This is Random Info</h1>
<div class="boxes">
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">Placeholder Text</h5>
</div>
</div>
</div>
</div>
</div>
CSS for box div
.b1container {
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
background-color: #ffdeab;
border-radius: 10px;
transition: 0.5s;
cursor: pointer;
}
.b1container:hover {
width: 250px;
height: 250px;
}
.s2boxImg {
width: 100px;
height: 100px;
border-radius: 5px;
border: 3px dotted #553c2b;
transition: 0.5s;
}
.s2boxImg:hover {
width: 250px;
height: 250px;
border: 3px dotted #f36dcb;
transition: 0.5s;
}
.boxes {
display: flex;
justify-content: space-evenly;
justify-content: center;
margin-top: 30px;
}
Stacked Boxes
I have tried to use flex direction row, I used justify content to make sure everything was centered. I attempted to move the divs around to make sure everything was within the correct div. I also tried to space-evenly the div box itself labeled as "box".
You have nested all .box elements a few levels deep inside .boxes, that is why they are stacked. Make all .box elements direct decendants of .boxes and it will work.
The flexbox mechanism only work one level deep: a parent flexbox container element (.boxes) contains flexed child elements (.box). Any element inside a .box is unaffected, or better, unhandled by the flexbox mechanism. That is why the main structure in your case should be:
<div class="boxes">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
I also added flex-flow: row wrap to make .box elements wrap to the next line when required.
.b1container {
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
background-color: #ffdeab;
border-radius: 10px;
transition: 0.5s;
cursor: pointer;
}
.b1container:hover {
width: 250px;
height: 250px;
}
.s2boxImg {
width: 100px;
height: 100px;
border-radius: 5px;
border: 3px dotted #553c2b;
transition: 0.5s;
}
.s2boxImg:hover {
width: 250px;
height: 250px;
border: 3px dotted #f36dcb;
transition: 0.5s;
}
.boxes {
display: flex;
flex-flow: row wrap; /* ADDED to make content wrap */
justify-content: space-evenly;
justify-content: center;
margin-top: 30px;
}
<div class="section2">
<h1 class="s2h1">This is Random Info</h1>
<div class="boxes">
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg">
</div>
<h5 class="b1Info">Placeholder Text</h5>
</div>
</div>
</div>
Try adding this CSS rule:
.box {
display: flex;
flex-wrap: nowrap;
}
You're making the div boxes flex, but the div which really contains the boxes is the box div.
just add the flex-direction which take either row or column
b1container {
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
flex-direction :row;
background-color: #ffdeab;
border-radius: 10px;
transition: 0.5s;
cursor: pointer;
}
here is a link that explains flexbox really well
Basic concepts of flexbox
If you don't want the items to wrap when the width gets smaller you could use flex-wrap
which you can give nowrap attripute flex-wrap:nowrap;
the .box is not flex, and is he that is holding the boxes
try this:
.box {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
}
this code will put your boxes aligned
if you want to do add a prefix size to each box you can try:
.box > * {
flex: 0 0 25%;
width: 100%;
max-width: 25%;
}
with this you can set the box class to wrap, and you have 4 columns per row.
I think your CSS styling is correct. However, the HTML is structured incorrectly. It is logical that the div.boxes include a bunch of div.box. And each div.box has box contents; like in your case div.b1container and an h5 tag. You, however, have nested div.box inside div.box instead of div.boxes.
Here's a figure of how it should be structured:
Here's the corrected HTML file:
<div class="section2">
<h1 class="s2h1">This is Random Info</h1>
<div class="boxes">
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg" />
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg" />
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg" />
</div>
<h5 class="b1Info">PlaceHolder Text</h5>
</div>
<div class="box">
<div class="b1container">
<img src="" alt="Placeholder" class="s2boxImg" />
</div>
<h5 class="b1Info">Placeholder Text</h5>
</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>
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>
I have several columns of images of different sizes. As the sizes are unknown, one column will be the tallest. I now want to stretch out the other (smaller) columns to match that height by increasing the gaps between the images accordingly. Here is an example image:
And here is a jsfiddle of this example that I set up with flexbox.
#main {
width: 50%;
display: flex;
justify-content: space-between;
}
.column {
background-color: lightpink;
margin-right: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column:last-child {
margin-right: 0;
}
.column img {
width: 100%;
align-self: center;
}
<div id="main">
<div class="column">
<img src="http://placekitten.com/g/200/300">
<img src="http://placekitten.com/200/300">
<img src="http://placekitten.com/g/200/400">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/200">
<img src="http://placekitten.com/200/280">
<img src="http://placekitten.com/g/200/250">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/400">
<img src="http://placekitten.com/200/220">
<img src="http://placekitten.com/g/200/260">
</div>
</div>
However in my specific case, I cannot use flexbox (as I need to absolute position some children), so I am now looking for a way to achieve the same thing without flexbox. Is there any way to get this vertical "space-between" distribution without flexbox?
Based on the comment regarding absolute positioning:
I have tried to get the absolute positioning to work without any success. Basically I am trying to place captions underneath each images, however this captions should not be part of the flow, so the gaps should keep the same with as if there were no captions. When I tried to place the captions underneath, I ended up with all captions on the bottom of the entire column.
The solution is to rust wrap the images and captions in a div (or better still a figure) and give that position relative...then position your captions absolutely.
Like so:
#main {
max-width: 80%;
margin: auto;
display: flex;
justify-content: space-between;
}
.column {
background-color: lightpink;
margin-right: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.holder {
position: relative;
}
.column img {
display: block;
max-width: 100%;
height: auto;
}
.caption {
position: absolute;
bottom: 0;
text-align: center;
background: rgba(255, 255, 255, 0.5);
width: 100%;
}
<div id="main">
<div class="column">
<div class="holder">
<img src="http://placekitten.com/g/200/300">
</div>
<div class="holder"> <img src="http://placekitten.com/200/300"></div>
<div class="holder">
<img src="http://placekitten.com/g/200/400">
</div>
</div>
<div class="column">
<div class="holder">
<img src="http://placekitten.com/g/200/200">
<div class="caption">My Caption</div>
</div>
<div class="holder"> <img src="http://placekitten.com/200/280"></div>
<div class="holder">
<img src="http://placekitten.com/g/200/250">
<div class="caption">My Caption</div>
</div>
</div>
<div class="column">
<div class="holder">
<img src="http://placekitten.com/g/200/400">
<div class="caption">Superduper long Caption In Here</div>
</div>
<div class="holder"> <img src="http://placekitten.com/200/220"></div>
<div class="holder">
<img src="http://placekitten.com/g/200/260">
</div>
</div>
</div>
fix image height and use "object-fit:cover;"
#main {
width: 50%;
display: flex;
justify-content: space-between;
}
.column {
background-color: lightpink;
margin-right: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.column:last-child {
margin-right: 0;
}
.column img {
width: 100%;
align-self: center;
height:100px;
object-fit: cover;
}
<div id="main">
<div class="column">
<img src="http://placekitten.com/g/200/300">
<img src="http://placekitten.com/200/300">
<img src="http://placekitten.com/g/200/400">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/200">
<img src="http://placekitten.com/200/280">
<img src="http://placekitten.com/g/200/250">
</div>
<div class="column">
<img src="http://placekitten.com/g/200/400">
<img src="http://placekitten.com/200/220">
<img src="http://placekitten.com/g/200/260">
</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/