Flex Box Divs Stacking instead of being horizontal - html

So I am trying to create a flexbox that has several divs inside of it. The divs have an image and a button that should stack but then I would like them to align in a row. I cannot seem to get them to actually go into a row instead they are stacking. Here is the HTML and CSS:
.block2 {
display: flex;
flex-direction: row;
}
.tan {
background: #FFEFD5
}
.kite1 img {
height: 150px;
width: 130px;
padding: 30px;
}
.kite1button img {
height: 50px;
width: 125px;
}
.kite2 img {
height: 150px;
width: 130px;
padding: 30px;
}
.kite2button img {
height: 50px;
width: 125px;
}
<div class="block2 tan">
<div class="kite1">
<img src="kite1.png" />
<div class="kite1button">
<img src="deltasbutton.png" />
</div>
</div>
<div class="kite2">
<img src="diamond-kite.png" />
<div class="kite2button">
<img src="diamondsbutton.png" />
</div>
</div>
<div class="kite3">
<img src="specialty-kite.png" />
<div class="kite3button">
<img src="specialty-button.png" />
</div>
</div>
</div>

Change kite1, kite2, and kite3, to kite.
Then style the kite class like so:
.kite {
display: flex;
flex-direction: column;
}
P.S. If you're going to be using an identifier (i.e. class= or id= or data-whatever=, etc) once, use id. If you plan on using an identifier on multiple elements, always use class. ids can only be used on one element, whereas class can be used on an indefinite number of elements.
.block2 {
display: flex;
}
.kite {
display: flex;
flex-direction: column;
}
<div class="block2 tan" >
<div class="kite">
<img src="kite1.png" />
<div class="kite1button">
<img src="deltasbutton.png"/>
</div>
</div>
<div class="kite">
<img src="diamond-kite.png"/>
<div class="kite2button" >
<img src="diamondsbutton.png"/>
</div>
</div>
<div class="kite">
<img src="specialty-kite.png"/>
<div class="kite3button">
<img src="specialty-button.png"/>
</div>
</div>
</div>

Related

How can I change size of pictures in a flexbox?

I try to develop website for my portfolio. I want to do something like that :
But I've some problem with the 3 icons for Twitter/Instagram and Deviantart. To put the icons like that, I use the flexbox in html. Currently, I've that :
#conteneur {
display: flex;
justify-content: space-between;
}
<div id="conteneur">
<div class="element1">
<img src="https://zupimages.net/up/21/29/vbxh.png">
</div>
<div class="element2">
<img src="https://zupimages.net/up/19/51/4gl0.png">
</div>
<div class="element3">
<img src="https://zupimages.net/up/21/29/dbtc.png">
</div>
</div>
Test with CodePen here : https://codepen.io/lucbenedet/pen/yLbPMgK
But as you can see, the pictures are too large and when I try to put the pictures to a size of 35px like that :
#conteneur
{
display: flex;
justify-content: space-between;
width: 35px;
}
or
.element1
{
width: 35px;
}
The resize doesn't works...
Someone to show how to do that ?
You can update your CSS with following code.
#conteneur {
display: flex;
justify-content: space-between;
width: 150px;
padding: 10px;
}
#conteneur div img {
width: 35px;
height: 35px;
}
<div id="conteneur">
<div class="element1">
<img src="https://zupimages.net/up/21/29/vbxh.png">
</div>
<div class="element2">
<img src="https://zupimages.net/up/19/51/4gl0.png">
</div>
<div class="element3">
<img src="https://zupimages.net/up/21/29/dbtc.png">
</div>
</div>
You can have some space between icons when you maximize the width of the container
you can reset width on the flex-container to max-content (3 icones shouldnot overflow) and use gap to set a distance in between them.
You could use em for the gap and icon's width to have a coherent render.
Possible example:
#conteneur {
display: flex;
width: max-content;
align-items: center;
margin: auto;
gap: 1.6em;
}
#conteneur div img {
width: 3em;
}
<div id="conteneur">
<div class="element1">
<img src="https://zupimages.net/up/21/29/vbxh.png">
</div>
<div class="element2">
<img src="https://zupimages.net/up/19/51/4gl0.png">
</div>
<div class="element3">
<img src="https://zupimages.net/up/21/29/dbtc.png">
</div>
</div>
possible example from your codepen https://codepen.io/gc-nomade/pen/qBmVjBV

Resize flex items to fit container (prevent items from overflowing)

I've been struggling with an issue that i think will have a pretty simple solution, but i just can't see it right now. I have a nested flexbox layout. The first level of flex items are divs that are display:flex themselves, and depending on class they can have a flex-directon: of row or column. Both the first and second level of these nested flex items should grow to fill up the remaining parent size, and shrink if new elements are added, never overflowing.
My Problem: When adding new flex items to the "first level", this seems to work fine. However, adding new flex items to the second level seems to always overflow the parent container height.
As i'm not really good at describing stuff like this, here's a picture of what i need:
The purple box is the parent container (not flex)
The orange box is the flex container with flex-direction: row, with the red lines being its flex items (each item is a flexbox as well).
The blue lines are the nested flex items.
Nested Flexbox layout
Adding new red items works and expands or shrinks as needed, but adding new blue items, overflows the container.
https://jsfiddle.net/t40x7or8/
Here's my code sample:
CSS
width: 1800px;
height: 500px;
border: 4px blue solid;
}
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.flex-item {
flex-grow: 1;
flex-basis: 0;
display: flex;
}
.flex-item > div {
flex-grow: 1;
border: 1px solid orange;
}
.height-1 {
flex-direction: column;
max-height: 100%;
}
.height-2 {
max-height: 100%;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
HTML
<div class="flex-container">
<div class="height-2 flex-item">
<div class="img-div">
<img src="https://picsum.photos/1080/600" />
</div>
</div>
<div class="height-1 flex-item">
<div class="img-div">
<img src="https://picsum.photos/1080/601" />
</div>
<div class="img-div">
<img src="https://picsum.photos/1080/602" />
</div>
</div>
</div>
</div>
Sorry for the chaotic description, i'm quite bad at these kind of things.
Thanks for the help
I think we should use flex: 1 1 0 for the fix the layout of the flex items.
And using width: 100% to control the size of the inner image.
https://jsfiddle.net/ramseyfeng/kxf46bju/
.page {
border: 3px solid green;
width: 600px;
height: 300px;
}
.flex {
display: flex;
}
.cols {
display: flex;
flex-direction: row;
}
.rows {
display: flex;
flex-direction: column;
}
.flex-1-1-0 {
flex: 1 1 0;
}
.img-div {
flex: 1 1 0;
display: flex;
}
.img-div img {
width: 100%;
}
<div class="page cols">
<div class="flex-1-1-0 rows">
<div class="img-div">
<img src="https://picsum.photos/1080/600" />
</div>
</div>
<div class="flex-1-1-0 rows">
<div class="img-div">
<img src="https://picsum.photos/1080/601" />
</div>
<div class="img-div">
<img src="https://picsum.photos/1080/602" />
</div>
</div>
<div class="flex-1-1-0 rows">
<div class="img-div">
<img src="https://picsum.photos/1080/607" />
</div>
<div class="img-div">
<img src="https://picsum.photos/1080/608" />
</div>
</div>
<div class="flex-1-1-0 rows">
<div class="img-div">
<img src="https://picsum.photos/1080/605" />
</div>
<div class="img-div">
<img src="https://picsum.photos/2000/1000" />
</div>
<div class="img-div">
<img src="https://picsum.photos/2000/1001" />
</div>
</div>
</div>

Element alignment: Remove margin with modifier class not working

I have a wrapper with two boxes in it. The boxes are each to other. In the boxes there is a title (optional) and a content. I have two cases. First case: One of the boxes has a title the other hasn't. Second case: Both boxes have a title. If you take a look on the example for the second case (both titles), the boxes and titles are aligned on the bottom of the wrapper, and the titles are also aligned. There is also an example for the first case (one box without title). Because of the missing title, the alignment isn't correct yet. Here is a screenshot of the problem:
So what I tried is, to figure out the missing space for the first case, which was 21px. After this I select in CSS the second box and add the missing space. For the second case with both title, I tried to add a modifier class on the box and remove the margin-top. So default there would be a space on the second box if no title (this title is optional) and if both titles are avalable, a class should remove it again. Thats the part of the code:
.box + .box {
margin-top: 21px;
}
.box--with-title {
margin-top: 0;
}
Now with my idea it doesn't work. The first case is solved, but my class is not removing the margin. So now the alignment for the second case is wrong. Any ideas how to solve that the class is removing the margin or is there a bether way to do this with pure CSS? Hope this is clear enough.
.wrapper {
display: flex;
justify-content: space-betweet;
width: 500px;
background-color: lightgray;
;
margin-top: 36px;
}
.box {
display: flex;
flex-direction: column;
width: 50%;
}
.box__content {
width: 120px;
height: 100px;
background-color: lightcoral;
}
.box+.box {
margin-top: 21px;
}
.box--with-title {
margin-top: 0;
}
<div class="wrapper">
<div class="box">
<h3>I have a title!</h3>
<div class="box__content"></div>
</div>
<div class="box">
<h3></h3>
<div class="box__content"></div>
</div>
</div>
<div class="wrapper">
<div class="box">
<h3>I have a title!</h3>
<div class="box__content"></div>
</div>
<div class="box box--with-title">
<h3>I have also a title!</h3>
<div class="box__content"></div>
</div>
</div>
Simply use align-items:flex-end on the container and no need to consider margin:
flex-end
The cross-end margin edge of the flex item is flushed with
the cross-end edge of the line. ref
.wrapper {
display: flex;
justify-content: space-between;
align-items:flex-end;
width: 500px;
background-color: lightgray;
;
margin-top: 36px;
}
.box {
display: flex;
flex-direction: column;
width: 50%;
}
.box__content {
width: 120px;
height: 100px;
background-color: lightcoral;
}
<div class="wrapper">
<div class="box">
<h3>I have a title!</h3>
<div class="box__content"></div>
</div>
<div class="box">
<div class="box__content"></div>
</div>
</div>
<div class="wrapper">
<div class="box">
<h3>I have a title!</h3>
<div class="box__content"></div>
</div>
<div class="box box--with-title">
<h3>I have also a title!</h3>
<div class="box__content"></div>
</div>
</div>
By the way your intial code was almost good, you are simply facing a specificity issue which make the rule of .box--with-title not being considered. You may do something like this instead:
.wrapper {
display: flex;
justify-content: space-betweet;
width: 500px;
background-color: lightgray;
;
margin-top: 36px;
}
.box {
display: flex;
flex-direction: column;
width: 50%;
}
.box__content {
width: 120px;
height: 100px;
background-color: lightcoral;
}
.box+.box {
margin-top: 21px;
}
.box.box--with-title {
margin-top: 0;
}
<div class="wrapper">
<div class="box">
<h3>I have a title!</h3>
<div class="box__content"></div>
</div>
<div class="box">
<h3></h3>
<div class="box__content"></div>
</div>
</div>
<div class="wrapper">
<div class="box">
<h3>I have a title!</h3>
<div class="box__content"></div>
</div>
<div class="box box--with-title">
<h3>I have also a title!</h3>
<div class="box__content"></div>
</div>
</div>

Cropping to the Shortest Image Height in a Single Row of Images?

Is it possible to crop images to the shortest height of an image in a single row of images? If so, can this be done solely with CSS?
Here is an example of what I'd like to accomplish:
And here is the code I am currently working on:
.container {
width: 100%;
margin: 0 auto;
}
.row {
width: 100%;
background: black;
display: flex;
align-items: center;
overflow: auto;
}
.row .item {
width: 33.33333%;
}
.row img {
display: block;
object-fit: cover;
width: 100%;
}
<div class="container">
<div class="row">
<div class="item">
<img src="https://78.media.tumblr.com/708bb6dcdaf359fd2ea83d11a0b5b4b8/tumblr_oyslstg5xk1unhdoco3_r1_1280.jpg" />
</div>
<div class="item">
<img src="https://78.media.tumblr.com/c49ef0b40483c35ad3b6898c0e037e11/tumblr_oyslstg5xk1unhdoco2_r1_1280.jpg" />
</div>
<div class="item">
<img src="https://78.media.tumblr.com/737456a69c07da1a9a2784506f62dce9/tumblr_oyslstg5xk1unhdoco9_r1_1280.jpg" />
</div>
</div>
</div>
Any solutions or advice?
It's probably best to use JS for this, unless you can process the heights on a backend script and set a height on the container as it is output.
Anyways, here is a little bit of JS that should achieve what you want:
const items = [...document.querySelectorAll('.item')];
const row = document.querySelector('.row');
function normalizeImages(images, container) {
const shortestItem = images.reduce((smallest, element) => {
return element.clientHeight < smallest.clientHeight ? element : smallest;
});
container.style.height = `${shortestItem.clientHeight}px`;
}
normalizeImages(items, row);
window.addEventListener('resize', () => normalizeImages(items, row));
.container {
width: 100%;
margin: 0 auto;
}
.row {
width: 100%;
background: black;
display: flex;
align-items: center;
overflow: hidden;
}
.row .item {
flex-basis: 33.33333%;
}
.row img {
display: block;
width: 100%;
}
<div class="container">
<div class="row">
<div class="item">
<img src="https://78.media.tumblr.com/708bb6dcdaf359fd2ea83d11a0b5b4b8/tumblr_oyslstg5xk1unhdoco3_r1_1280.jpg" />
</div>
<div class="item">
<img src="https://78.media.tumblr.com/c49ef0b40483c35ad3b6898c0e037e11/tumblr_oyslstg5xk1unhdoco2_r1_1280.jpg" />
</div>
<div class="item">
<img src="https://78.media.tumblr.com/737456a69c07da1a9a2784506f62dce9/tumblr_oyslstg5xk1unhdoco9_r1_1280.jpg" />
</div>
</div>
</div>
Note that a small amount of CSS has changed to hide overflow on the .row element

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.