How to use flexbox to align two under one - html

How do I use flexbox to align one box at the top with two under like on my screenshot
So far I can align three in a row with this
.flex-container {
display: flex;
justify-content: center;
align-items: center;
}
<div className="flex-container">
<div />
<br />
<div />
<div />
</div>
I tried with a br but it did not work

Just providing a different approach if you want a single container; you would just have to target the right elements and apply the right flex properties so the behave how you need them to.
.flex {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.flex div:first-child {
width: 100%;
margin-bottom: 20px;
}
.flex div:not(:first-child) {
flex: 1 0 50%;
}
img {
margin: 0 auto;
display: block;
}
<div class="flex">
<div>
<img src="https://via.placeholder.com/150" />
</div>
<div>
<img src="https://via.placeholder.com/150" />
</div>
<div>
<img src="https://via.placeholder.com/150" />
</div>
</div>

Better to do this with grid:
.flex-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 100px;
}
.flex-container>img {
justify-self: center;
border-radius: 50%;
}
.flex-container>img:nth-child(1) {
grid-column: 1/3;
}
<div class="flex-container">
<img src="http://placekitten.com/200/200" />
<img src="http://placekitten.com/200/200" />
<img src="http://placekitten.com/200/200" />
</div>

You can use flex-wrap
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
}
.box1 {
width: 100%;
display: flex;
justify-content: center;
}
<div class="container">
<div class="box1">
<div>
Box 1
</div>
</div>
<div>Box 2</div>
<div>Box 3</div>
</div

Related

Center align content of a grid cell

I want to center align content inside a grid item, it's centered horizontally but not vertically. see on JSfiddle: http://jsfiddle.net/6zs8ydhf/
.container {
display: grid;
grid-template-columns: 200px auto;
grid-template-rows: 400px 200px;
}
.item {
border: 1px solid #111;
}
.item2 h1 {
display: flex;
align-items: center;
justify-content: center;
}
<div class="container">
<div class="item item1">
<h1>1</h1>
</div>
<div class="item item2">
<h1>2 Should be at center</h1>
</div>
<div class="item item3">
<h1>3</h1>
</div>
</div>
here what you need, to do:
.item {
border: 1px solid #111;
display: flex;
align-items: center;
justify-content: center;
}

Padding elements of a fixed width

I'm creating an adaptable row of images that will break on to another row if needed when there are too many images for the size. At the moment there's 4 in a row on desktop and 2 in a row on mobile.
All of the images are set to 25% or 50% in width. I want to have them spaced out slightly and evenly.
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
#media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
<div class="images-row">
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="100%">
</div>
</div>
This works fine and all of the images are touching. I feel like some space would look better visually. Is the best approach to pad the width of the main container? I've tried making the images smaller, like this;
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
#media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
<div class="images-row">
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
<div class="image-container">
<img src="https://via.placeholder.com/500x500" width="95%">
</div>
</div>
Which looks fine on desktop but the bottom of the images still touches the top of the ones below them. Doing something like this:
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
#media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
.image-padding {
padding: 5px
}
.image {
width: 100%
}
<div class="images-row">
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
</div>
Works fine for padding also but runs into the issue that they don't then stack on mobile. I feel like I'm close but I'm not sure how to get this to work.
EDIT
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
text-align: center;
justify-content: center;
justify-content: space-evenly;
}
.image-container {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
}
#media only screen and (max-device-width: 500px) {
.image-container {
width: 50%;
display: flex;
align-items: center;
justify-content: center;
}
}
.image-padding {
padding: 5px
}
.image {
width: 100%
}
<div class="images-row">
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
<div class="image-container">
<div class="image-padding">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
</div>
<style>
</style>
This would be a perfect case to use a ready-made grid system, there's many out there you could use. That being said, you were pretty close but your html is a bit too complex for what you're trying to accomplish. My guess is that you're fighting against how the box-model works: if you add padding, the container gets wider. To avoid that I usually use ´box-siting: border-box´, that way the padding goes inside the container. It just makes everything much more logic. In all of my projects I start with:
* {box-sizing: border-box;}
As for your specific case, here's my solution, as you can see I simplified your html a little and change the css for it:
.images-row {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-top: 15px;
padding-bottom: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
flex-wrap: wrap;
}
.image-container {
width: 25%;
padding: 5px;
box-sizing: border-box;
}
#media only screen and (max-width: 500px) {
.image-container {
width: 50%;
}
}
.image {
width: 100%;
display: block;
}
<div class="images-row">
<div class="image-container">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
<div class="image-container">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
<div class="image-container">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
<div class="image-container">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
</div>
Your code can really be simplified, you don't need all those containers. By keeping it simple, it will also be clearer to identify problems.
.images-row {
width: 100%;
margin-bottom: 15px;
display: flex;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
}
img {
width: calc(25% - 10px);
}
#media only screen and (max-device-width: 500px) {
img {
width: calc(50% - 10px);
}
}
<div class="images-row">
<img class="image" src="https://via.placeholder.com/500x500">
<img class="image" src="https://via.placeholder.com/500x500">
<img class="image" src="https://via.placeholder.com/500x500">
<img class="image" src="https://via.placeholder.com/500x500">
</div>
For mobile, the problem may come from #media only screen and (max-device-width: 500px): there are some more adapted to detect mobile, #media (orientation: portrait) for instance.

Prevent CSS Flex From Stretching on Both Axis

Is there a way to prevent stretching on both axis of CSS Flex? In the example below, I want the the option and box layers to adjust to the width and height of the children. Additionally, I want the box to be in the middle of the page.
I am aware of flex-grow and basis, but my understanding is it only manages the space of the main axis.
.container {
background-color: red;
display: flex;
justify-content: center;
}
.box {
display: flex;
flex-basis: 0;
background-color: green;
justify-content: center;
}
.option {
display: flex;
flex-direction: column;
align-items: center;
background-color: yellow;
}
.img-container {
background-color: blue;
}
.img-container img {
max-width: 100%;
}
<div class="container">
<div class="box">
<label class="option">
Option 1
<div class="img-container">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=640:*" />
</div>
</label>
<label class="option">
Option 2
<div class="img-container">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=640:*" />
</div>
</label>
</div>
</div>

How to align the flex items in three different ways?

So, I want to align items inside flex-box like in the above picture.
I have html as follows:
<div class="flex-row">
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
</div>
css as
.flex-row {
min-height: 400px;
display: flex;
align-items: flex-start;
}
.flex-item:nth-child(2) {
align-items: center;
}
.flex-item:last-child {
align-items: flex-end;
}
Use align-self not align-items
.flex-row {
min-height: 300px;
display: flex;
width: 90%;
margin: 1em auto;
border: 1px solid red;
justify-content: space-between;
}
.flex-item {
height: 50px;
background: lightblue;
width: 100px;
}
.flex-item:nth-child(2) {
align-self: center;
}
.flex-item:last-child {
align-self: flex-end;
}
<div class="container flex-row">
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
Like so?
You need flex-wrap: wrap; In order to wrap the items need to be wide enough (100%). Since 100% flex item is always justified to the full width, you need text-align, not align-items which is the wrong property in the first place.
align-items is the alignment at the cross axis, not the main axis which would be justify-content
.flex-row {
min-height: 400px;
display: flex;
align-items: flex-start;
flex-wrap: wrap;
}
.flex-item {
flex-basis: 100%;
}
.flex-item:nth-child(2) {
text-align: center;
}
.flex-item:last-child {
text-align: right;
}
<div class="flex-row">
<div class="flex-item">
<img src="/default.png" width="100" height="50">
</div>
<div class="flex-item">
<img src="/default.png" width="100" height="50">
</div>
<div class="flex-item">
<img src="/default.png" width="100" height="50">
</div>
</div>
Look at this code
display:flex;
flex-direction: row;
justify-content: flex-start;
align-items: centre;
With flex-direction you specify le principal alignment (horizontal with row , vertical with column). Depending on what you choose here the following properties will have a different effect.
Then with justify-content you specify how your items inside your elements should align on the principal axis you just defined (vertical or horizontal).
In this case flex-start means at the beginning of my axis.
And with align-items you specify the alignment for the secondary axe.The vertical one because we choose flex-direction: row; and not flex-direction: column;. So in the vertical axis items will align at centre
And don't forget to take look at the available properties like space-between or flex-end
NOTE!
to set height to column change the px here:flex: 0 1 50px; or remove it!
Using margin-right/left:auto as:
.flex-row {
min-height: 400px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.flex-item {
flex: 0 1 50px;
}
.flex-item:nth-child(2) {
margin-right: auto;
margin-left: auto;
}
.flex-item:last-child {
margin-left: auto;
}
<div class="flex-row">
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
</div>
2. using align-self
.flex-row {
min-height: 400px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.flex-item {
flex: 0 1 50px;
align-self: flex-start;
}
.flex-item:nth-child(2) {
align-self: center;
}
.flex-item:last-child {
align-self: flex-end;
}
<div class="flex-row">
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
</div>

Flexbox centeres items with same height

I ceated a flexbox grid and tried to make each item the same hight which is not working. Basically all those blue containers shall have the same height.
HTML:
<div class="outer">
<div class="item">
<h1>Contact</h1>
</div>
<div class="item">
<h3>Mail:</h3>
<a>john#doe.com</a>
<br>
<a>PGP</a>
</div>
<div class="item">
<h3>Telegram:</h3>
<a>www.t.me/doe</a>
</div>
</div>
CSS:
.outer {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
height: 100vh;
width: 100vw;
}
.item {
background-color: aqua;
flex: 1;
}
Photo:
I would add another element that is the only flex child of .outer, centered vertically, with the background applied to it, then make that element the flex parent that holds your 3 sections and aligns them.
.outer, .inner {
display: flex;
align-items: center;
}
.outer {
height: 100vh;
width: 100vw;
}
.inner {
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
flex: 1 0 0;
background-color: aqua;
}
.item {
flex: 1;
}
<div class="outer">
<div class="inner">
<div class="item">
<h1>Contact</h1>
</div>
<div class="item">
<h3>Mail:</h3>
<a>john#doe.com</a>
<br>
<a>PGP</a>
</div>
<div class="item">
<h3>Telegram:</h3>
<a>www.t.me/doe</a>
</div>
</div>
</div>