Nested Flexbox images wrap properly - html

I'm trying to set the nested images inside gallery to:
1) Wrap thumbs horizontally, i.e. in this example, one & two on first row and then below them 3 & 4 and so on..
2) Wrap thumbs vertically as well w/ wrapping thumbs moving up to top-rt & filling downward like first column.
3) Prevent thumbs from increasing gallery wrapper height & wrap instead. I tried setting gallerry to fixed height but still thumbs overflow.
<div id="list-wrapper">
<div id="gallery">
<div><img src="images/1.png" alt="one" /></div>
<div><img src="images/2.png" alt="two" /></div>
<div><img src="images/1.png" alt="three" /></div>
<div><img src="images/2.png" alt="four" /></div>
<div><img src="images/1.png" alt="five" /></div>
<div><img src="images/2.png" alt="six" /></div>
</div>
<div id="mainimg"><img src="images/lg.png" alt="large" /></div>
</div>
#list-wrapper {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
width: 100%;
flex-wrap: wrap;
}
#list-wrapper #gallery {
flex: 1 1 20%;
background-color:red;
width:200px;
}
#list-wrapper #gallery div {
width:100px;
height:100px;
}
#list-wrapper #gallery div img {
width:100px;
height:100px;
}
#list-wrapper #mainimg {
flex: 1 1 80%;
background-color:blue;
}
#list-wrapper #mainimg img {
width:100%;
height:auto;
}
Flexbox Fiddle
Examples matching questions:
1) Horizontal:
2) Vertical:
3) Prevent overflow:

Something like this? https://jsfiddle.net/xwkr3jgL/
You can easily fix size/margin/etc. (have images in the correct ratio and sizes) to make it more pretty, but this is how you can do in like you want =)
Change the proptery flex-direction to row for
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
and to column for:
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
#list-wrapper {
width: 800px;
display: grid;
grid-template-columns: 20% 80%;
border: 10px solid #000;
}
#gallery {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 420px;
}
#gallery > div {
width: 50%;
}
#gallery img {
max-width: 100%;
}
#mainimg > img {
max-width: 100%;
display: block;
}
<div id="list-wrapper">
<div id="gallery">
<div>
<img src="https://pixel.nymag.com/imgs/daily/vulture/2016/12/15/15-star-wars-best-moments-2.w700.h700.jpg" alt="one" />
</div>
<div>
<img src="https://www.sideshowtoy.com/photo.php?sku=902537" alt="two" />
</div>
<div>
<img src="https://pixel.nymag.com/imgs/daily/vulture/2016/12/15/15-star-wars-best-moments-2.w700.h700.jpg" alt="three" />
</div>
<div>
<img src="https://www.sideshowtoy.com/photo.php?sku=902537" alt="four" />
</div>
<div>
<img src="https://pixel.nymag.com/imgs/daily/vulture/2016/12/15/15-star-wars-best-moments-2.w700.h700.jpg" alt="five" />
</div>
<div>
<img src="https://www.sideshowtoy.com/photo.php?sku=902537" alt="six" />
</div>
<div>
<img src="https://pixel.nymag.com/imgs/daily/vulture/2016/12/15/15-star-wars-best-moments-2.w700.h700.jpg" alt="seven" />
</div>
</div>
<div id="mainimg"><img src="https://shortlist.imgix.net/app/uploads/2015/12/04110243/50-of-the-best-star-wars-quotes-60-852x568.jpg?w=1200&h=1&fit=max&auto=format%2Ccompress" alt="large" /></div>
</div>

Related

How do I put elements two or three in a row?

I have to build a fluid, scalable, device-independent layout where I need to put 4 elements in a row so that they resize and stick together. Let’s say, 4 images, as they naturally are - one above another, and I just want to group them in two rows - 2 images side by side in 1 row so that I have no problems with layout or structure. Can you show me how? Thank you!
Use flexbox and make it responsive like this:
.element-container {
display: flex;
flex-direction: column;
width: 100%;
}
.element-row {
display: flex;
}
.element {
flex: 0 1 46%;
margin: 0 2% 20px 2%;
margin-bottom: 3%;
max-width: 150px;
}
.element img {
display: block;
width: 100%;
}
<div class="element-container">
<div class="element-row">
<div class="element">
<img src="http://via.placeholder.com/150x150">
</div>
<div class="element">
<img src="http://via.placeholder.com/150x150">
</div>
</div>
<div class="element-row">
<div class="element">
<img src="http://via.placeholder.com/150x150">
</div>
<div class="element">
<img src="http://via.placeholder.com/150x150">
</div>
</div>
</div>

Flexbox reserves space for svg images that have been scaled

I have a site header and I would like to use flexbox for this.
I use justify-content: space-between; to evenly divide the free space between the div's but when I add my svg and scale it down to the size of the header bar flexbox reserves the same amount of space as if the svg was displayed at 100%.
I made a example to show what I mean:
#wrapper {
width: 700px;
height: 100px;
display: flex;
justify-content: space-between;
background: blue;
}
.child {
flex: content;
background: red;
}
<div id="wrapper">
<div class="child">
<img src="https://s.cdpn.io/3/kiwi.svg" height="100%" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/200/300/?random" height="100%" alt="">
</div>
2
3
</div>
And a JSiddle:
https://jsfiddle.net/03r8vzkn/6/
Is there a way I can avoid this or should I make the svg smaller? This feels a bit hacky because I don't want to make every svg the right size; the scalability is one of its biggest advantages.
You can do it with the display: inline-flex for the .child divs, then they will only take the content's width, of course you also need to make your imgs responsive:
#wrapper {
width: 700px;
height: 100px;
display: flex;
justify-content: space-between;
background: blue;
}
.child {
display: inline-flex; /* only takes the content's width */
background: red;
}
img {
display: block; /* removes bottom margin/whitespace */
max-width: 100%; /* horizontally responsive */
max-height: 100vh; /* vertically responsive */
}
<div id="wrapper">
<div class="child">
<img src="https://s.cdpn.io/3/kiwi.svg" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/300/400/?random" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/200/300/?random" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/100/200/?random" alt="">
</div>
2
3
</div>

HTML/CSS Image center alignment

I have made a website where the homepage looks like the this :
I want to move the ficstore logo in the middle and split the bar into two halves and put one on either side , like this :
I've split my bar image into two different parts for each side and tried to put the images in different columns. But it just doesn't seem to work.
This is the snippet for the current code :
<div class="container">
<div class="row">
<div class="col-xs-8">
<h2 style="font-family:helvetica;"><span>Home Page</span></h2>
</div>
<div class="col-xs-4">
<img src="http://lorempixel.com/400/200/" class="img-responsive right" style="width:400px; z-index:1;" />
</div>
<img src="http://lorempixel.com/400/200/" class="img-responsive right" style="position: absolute; margin-top: 135px; z-index: -2;" />
</div>
<hr class="style18" />
Can someone help?
I've tried the following code as well :
<div class="container">
<div class="row">
<div class="col-xs-8">
<h2 style="font-family:helvetica;"><span>Book Info</span></h2>
</div>
<div class="flex-row">
<div>
<img src="http://lorempixel.com/400/200/" class="img-responsive right" style="width:400px; z-index:1;" />
</div>
<div>
<img src="http://lorempixel.com/400/100/" class="img-responsive right" style="position: absolute; margin-top: 135px; z-index: -2;" />
</div>
<div>
<img src="http://lorempixel.com/400/100/" class="img-responsive right" style="position: absolute; margin-top: 135px; z-index: -2;" />
</div>
</div>
</div>
<hr class="style18" />
Css :
.flex-row {
display: flex;
align-items: center;
justify-content: center;
}
This is a great time to use flex:
HTML:
<div class='flex-row'>
<div><img src="left.png"></div>
<div><img src="center.png"></div>
<div><img src="right.png"></div>
</div>
CSS:
.flex-row {
display: flex;
align-items: center;
justify-content: center;
}
This is one way to do it. I would encourage you to ditch whatever framework you are using. col-x-whatever is bad news bears.
Here's a fiddle too - with an inline-block way.
https://jsfiddle.net/sheriffderek/fcwyg0zb/
inline-block
.images {
text-align: center;
}
.images .image-w {
display: inline-block;
vertical-align: middle;
}
flexbox
.image-w img {
display: block;
width: 100%;
height: auto;
}
.one {
max-width: 200px;
}
.two {
max-width: 400px;
margin: 0 1rem;
}
.three {
max-width: 200px;
}
.images {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
<section class='images'>
<div class='image-w one'>
<img src='https://placehold.it/300x150' alt='left'>
</div>
<div class='image-w two'>
<img src='https://placehold.it/600x300' alt='center'>
</div>
<div class='image-w three'>
<img src='https://placehold.it/300x150' alt='right'>
</div>
</section>
I suggest you have to have 3 images. The bar should divided in two. Put the Fictore in the middle then add display:block.

Shrink images to fit in flexbox with wrapping rows

I want an arbitrary number of images to shrink to fit inside a fixed-width div.
It seems that flexbox is good for this purpose, but I can't seem to get it to work with wrapping multiple rows.
If I set flex-flow to row wrap and flex to 1 1 auto, the images are too big.
.container {
display: flex;
flex-flow: row wrap;
border: 1px solid black;
width: 300px;
height: 300px;
}
.image {
flex: 1 1 auto;
}
img {
max-width: 100%;
}
<div class="container">
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
</div>
If I set flex-flow to just row or flex to 1 1 0, the images don't wrap onto multiple lines:
.container {
display: flex;
flex-flow: row wrap;
border: 1px solid black;
width: 300px;
height: 300px;
}
.image {
flex: 1 1 0;
}
img {
max-width: 100%;
}
<div class="container">
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
</div>
How can I make the images expand enough to wrap as much as possible to fill the div, but not so much they break out of it?
If I set flex-flow to row wrap and flex to 1 1 auto, the images are too big.
With flex-basis: auto your item will take the size of its content (the image, in this case). That's what you're getting in your first demo: The intrinsic width of the image.
If I set flex-flow to just row or flex to 1 1 0, the images don't wrap onto multiple lines.
With flex-basis: 0 your item will ignore content size and distribute all space in the row evenly among items. That's what's happening in your second demo: It's creating three equal width items. As a result, there is no reason to wrap.
To control the size of flex items you need to define their flex-basis. Here's a rough sketch:
.container {
display: flex;
flex-flow: row wrap;
align-content: flex-start; /* pack wrapping lines to the top */
border: 1px solid black;
width: 300px;
height: 300px;
}
.image {
flex: 0 0 30%;
margin: 5px;
}
img {
max-width: 100%;
}
<div class="container">
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
<div class="image">
<img src="http://www.placebacon.net/400/300">
</div>
</div>
Just for fun.
var container1 = document.querySelector('.container1'),
container2 = document.querySelector('.container2'),
img = container2.querySelectorAll('.container2 img'),
width = 50,
stop = false;
expand();
function expand() {
for (var i = 0; i < img.length; i++) {
img[i].style.width = width + 'px';
}
if (stop) return;
if (container2.clientHeight < container1.clientHeight) {
width++;
setTimeout(function() {
expand()
}, 10);
} else {
width--;
stop = true;
expand();
}
}
.container1 {
border: 1px solid black;
height: 300px;
width: 300px;
}
.container2 {
font-size: 0;
}
<div class="container1">
<div class="container2">
<img src="http://www.placebacon.net/400/300">
<img src="http://www.placebacon.net/400/300">
<img src="http://www.placebacon.net/400/300">
</div>
</div>

Flexbox Grid with Images - Maintain Aspect Ratio / Proportions

I'm trying to use flexbox to layout something like the following:
-- ------ --
| | | |
|--| |--|
| | | |
-- ------ --
Each corner 'box' has an img within it who's aspect ratio is 1:1 (square). The center 'box' is 3:2.
The problem is for whatever reason the height of the corner boxes and the center box is larger than the hight of the responsive img within it, I'm trying to get them to align perfectly.
How can I get the flex items to line up correctly?
Here's a codepen: https://codepen.io/codybarr/pen/zNgpVK
EDIT: adding display: flex to .container .item fixed the issue in Firefox but not Chrome/Safari. Now the images stretch to be their full resolution height..weird...
img {
max-width: 100%;
}
#media (min-width: 700px) {
.main {
display: flex;
flex-direction: row;
background-color: orange;
}
.container {
display: flex;
flex-direction: column;
}
.container.second-column {
flex: 1 0 60%;
}
.container .item {
display: flex;
}
.container.second-column .item {}
.container.first-column .item:nth-child(1) {
background-color: blue;
}
.container.first-column .item:nth-child(2) {
background-color: yellow;
}
.container.third-column .item:nth-child(1) {
background-color: red;
}
.container.third-column .item:nth-child(2) {
background-color: green;
}
}
<head>
<title>Flex Testing</title>
</head>
<body>
<h1>Flex Test!</h1>
<div class="main">
<div class="container first-column">
<div class="item">
<img src="http://i-cdn.phonearena.com/images/article/90741-image/Google-touts-advanced-recipe-search-in-mobile-app.jpg" />
</div>
<div class="item">
<img src="https://lh3.googleusercontent.com/-cigJW9TQSRU/AAAAAAAAAAI/AAAAAAAAABg/Pg3e9ogcsHU/s640/photo.jpg" />
</div>
</div>
<div class="container second-column">
<div class="item">
<img src="https://www.inetsolutions.org/wp-content/uploads/2016/01/Capital-Letters-in-URLs-Do-Not-Influence-Google-Rankings.png" />
</div>
</div>
<div class="container third-column">
<div class="item">
<img src="http://www.sociallyawareblog.com/files/2016/12/GettyImages-610773752_small.jpg" />
</div>
<div class="item">
<img src="https://s-media-cache-ak0.pinimg.com/564x/b1/e2/db/b1e2dbe6c6b547a2ebc53a49fc3ffa8c.jpg" />
</div>
</div>
</div>
</body>
An initial setting of a flex container is align-items: stretch. This means that flex items will expand, by default, to cover the full length of the cross axis of the container.
Consider overriding the default with align-items: flex-start.
container .item {
display: flex;
align-items: flex-start; /* new */
}
revised codepen