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>
Related
I am trying to figure out how to make a section of divs on my page responsive so that as the page is minimized, the divs begin stacking on top of each other. I've tried flex-direction: column but that doesn't seem to be working. I have attached photos of what I am trying to achieve.
Here is my HTML:
<div>
<!-- <section> -->
<center>
<div class="container">
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3">
<image-tag src="https://></image-tag>
</div>
<div class="item3 idk">
<image-tag src="https://></image-tag>
</div>
<div class="item3 idk">
<image-tag src="https://></image-tag>
</div>
</div>
</center>
<!-- </section> -->
</div>
CSS
.container {
display: flex;
flex-direction: row;
}
Photos of what I am trying to achieve
Desktop
Tablet
Mobile
FYI I have taken some code out for confidentiality purposes.
try flex-wrap: wrap;
use .item3{ flex: 1 } if needed
A flex-box solution is hard because it is meant for 1 directional layouts where as your desired layout is 2 directional based on screen width. However, you can come close to the desired layout behavior by using flex-wrap which depends on either the width of your flex-items or flex-container to force the items to a new line. Try the below and adjust either the items' or container's width
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: start;
}
You can give max-width: 1200px; to your body element then
margin:0 auto to center vertically.
Than create a flexbox inside container class and make flex-wrap: wrap; to make flexbox create new row else all the images will always stay in same row.
You can also add padding: 0 1rem; for always having empty spaces when images comes to limit of wrapping.
/* RESET */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background-color: antiquewhite;
min-height: 100vh;
max-width: 1200px;
margin: 0 auto;
}
img {
max-width: 100%;
display: block;
}
/* RESET ENDS */
.container {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
gap: 1rem;
padding: 0 1rem;
}
<div class="container">
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
<div class="item">
<img src="https://source.unsplash.com/random/375x375" alt="" />
</div>
</div>
Does anyone know how to set multiple divs side-by-side that are centered and have at least 10 pixels gap between each other? I managed to do it but the following divs went right under the first 2.
Here is an example of what outcome I am expecting: https://imgur.com/a/p1ilgCu
And here is the outcome I made: https://imgur.com/a/JI0beTk
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 50px;
height: 50%;
width: 33.33%;
text-align: center;
transform: translate(50px, 20px);
}
.column1 {
background-color: whitesmoke;
margin-top: 10px;
border-radius: 20px;
}
<div class="container">
<div class="column1">
<img src="profilepiclink256x" style="width:50%">
<h1>text name</h1>
</div>
<div class="column1">
<img src="profilepiclink256x" style="width:50%">
<h1>text name</h1>
</div>
<div class="column1">
</div>
<div class="column1">
</div>
</div>
You can use flex to place them side by side. If you want them to fill the width of your parent element, use min-width on the children calculate 100% by the amount of divs and subtract the column gap. min-width: calc(100% / var(--num-cols) - var(--col-gap)); i used css variables.
:root {
--num-cols: 4;
--col-gap: 10px;
}
.container {
display: flex;
/* centers content when flex axis is set as row */
justify-content: center;
}
.column1 {
background-color: whitesmoke;
margin-top: 10px;
border-radius: 20px;
/* will fill parents width with children including gap
/* can also use flex-grow: 1; to fill parents width with children */
/* min-width: calc(100% / var(--num-cols) - var(--col-gap)); */
flex-grow: 1; /* remove if you do not want to spread entire width of parent width */
}
.column1~.column1 {
/* adds a gap of 10px to all but first col element */
margin-left: var(--col-gap);
}
<div class="container">
<div class="column1">
<img src="profilepiclink256x" style="width:50%">
<h1>text name</h1>
</div>
<div class="column1">
<img src="profilepiclink256x" style="width:50%">
<h1>text name</h1>
</div>
<div class="column1">
<img src="profilepiclink256x" style="width:50%">
<h1>text name</h1>
</div>
<div class="column1">
<img src="profilepiclink256x" style="width:50%">
<h1>text name</h1>
</div>
</div>
Use flex element in your css:
.container {
display: flex;
}
First things first, a fiddle: https://jsfiddle.net/2v1axtkz/3/
I have a flex grow box with a floating right image and am trying to add underneath a text box and contain them both within the parent box.
Reason for this is that there is an onclick event for the parent box, so I need both the image box and the text box contained within the same parent.
Right now it's not happening and I can't figure out why.
The markup is:
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
As you can see from the image, the text is somehow outside the parent container even though it's inside of the div:
How do I fix this?
You have height: 100% on the .sections__container__box. That essentially pushes out the title container. I'm assuming you want the red in the background, so just removing that won't work.
You need to remove the float from the image and make that .sections__container__box display flex and then use the justify-content property to position the image.
.sections__container {
margin: 0 auto;
max-width: 630px;
display: flex;
flex-wrap: wrap;
align-content: stretch;
justify-content: space-between;
}
.sections__container__box {
background-color: #FF0030;
flex: 1 0 100%;
text-align: center;
width: 100% !important;
text-align: right;
display: flex;
justify-content: flex-end;
}
.sections__container__outside {
width: 40%;
flex-grow: 1;
margin: 10px;
}
.image {
display: block;
}
.sections__container__box__title {
margin-bottom: 20px;
}
<div class="sections__container">
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
<div class="sections__container__outside" onclick="action();">
<div class="sections__container__box">
<img src="https://via.placeholder.com/150" class="image">
</div>
<div class="sections__container__box__title">Title</div>
</div>
</div>
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>
I'm trying to have 1 image on the top with 2 images below it spanning half the width of the image on top. The problem I'm encountering are aligning issues with the 2 images in which they don't fit nicely below the top image but spaced perfectly to the sides. I'm trying to have the 2 images be below the top image perfectly aligned in the center with no spaces below. As I shrink the window, that is when it looks like I want it to on a full screen except without the spaces. I also want them perfectly aligned in the center of the screen.
https://jsfiddle.net/voryuja8/
.first {
display: flex;
}
.first img {
margin: auto;
max-width: 800px;
}
.second {
display: flex;
}
.second img {
margin: auto;
flex: 1;
max-width: 400px;
}
<div class="first">
<img src="https://sarahannephoto.files.wordpress.com/2015/01/devyn_015.jpg?w=1008" alt="">
</div>
<div class="second">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
Just remove margin:auto from the images and use justify-content: center in the flex containers.
.first {
display: flex;
justify-content: center;
}
.first img {
max-width: 800px;
}
.second {
display: flex;
justify-content: center;
}
.second img {
flex: 1;
max-width: 400px;
}
<div class="first">
<img src="https://sarahannephoto.files.wordpress.com/2015/01/devyn_015.jpg?w=1008" alt="">
</div>
<div class="second">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
This might be a bit better, as it doesn't rely on setting pixel values for anything: https://jsfiddle.net/voryuja8/1/
HTML:
<div class="second">
<div class="flex-child">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
<div class="flex-child">
<img src="http://preen.inquirer.net/files/2016/05/preen-emily-oberg-complex-e1463660455785.jpg" alt="">
</div>
</div>
CSS:
img {
max-width: 100%;
}
.second {
display: flex;
}
.flex-child {
flex-grow: 1;
flex-shrink: 1;
}