Padding elements of a fixed width - html

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.

Related

div content shifts to left as screen size gets reduced

I've provided the html and css. The is modified by the script.js file which is why it's empty. I want the text to appear at the center even when screen width is reduced. Please help.
HTML:
<section id="page1">
<div class="welcome">
<div class="box">
<img class="mypic" src="img/me.jpg" alt="" srcset="" />
<h1 id="welcomeText1"></h1>
</div>
</div>
</section>
CSS:
#page1, #page2, #page3, #page4 {
min-height: 100vh;
}
.box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 2rem;
margin-left: 2rem;
}
.mypic {
width: 15rem;
height: 15rem;
border-radius: 500px;
}
I've provided screenshots below.
Absolute beginner here. =)
At normal width
On decreasing width
When you do justify-content: center; it will only align the div/element which is the child of your flexbox.
But to align the text/content in that particular element you need to use text-align: center;
If I correctly understand, you just need to add CSS property to welcomeText1:
#page1, #page2, #page3, #page4 {
min-height: 100vh;
}
.box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 2rem;
margin-left: 2rem;
}
.mypic {
width: 15rem;
height: 15rem;
border-radius: 500px;
}
#welcomeText1{
text-align: center;
border: 3px solid green;
}
<section id="page1">
<div class="welcome">
<div class="box">
<img class="mypic" src="img/me.jpg" alt="" srcset="" />
<h1 id="welcomeText1">Welcome to stackoverflow <br> Welcome</h1>
</div>
</div>
</section>
(I had you a border to show you)
You can use text-align: center for the text.
#welcomeText1 {
text-align: center;
}
Or if you want to keep the text in that structure, you could decrease the font size when using small screens with media queries.
#welcomeText1 {
font-size: 1.4rem;
}
#media (max-width: 720px) {
#welcomeText1 {
font-size: 1.2rem;
}
}

Flexbox centering 2 images and scale image when viewport gets too small

I am fiddling with this simple layout for a while now.
The goal is to utilize flexbox to center 2 images that resize when the screen gets to small because the images themself are rather large.
Desktop:
Mobile:
Also the body itself stays above the Footer element, but the content in the body overlaps the footer, do someone knows how to fix this maybe?
The HTML looks like this:
<div class="row">
<div class="imagecontainer">
<img class="prijsimage" src="../../assets/images/example1.png">
<img class="prijsimage" src="../../assets/images/example2.png">
</div>
</div>
The CSS looks like this:
.imagecontainer{
display: flex;
justify-content: center;
flex-wrap: wrap;
}
I also tried this code:
<div class="row">
<div class="imagecontainer">
<img class="prijsimage" src="../../assets/images/prijslijst_noback.png">
</div>
<div class="imagecontainer">
<img class="prijsimage" src="../../assets/images/pijslijst_noback_wax.png">
</div>
</div>
.imagecontainer{
flex: 50%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
margin-top: 5%;
}
.prijsimage{
max-height: 100%;
vertical-align: bottom;
}
.row{
display: flex;
flex-direction: row;
}
#media (max-width: 480px) {
.row {
flex-direction: row;
}
.imagecontainer {
height: auto;
width: 100%;
}
.prijsimage {
width: 100%;
max-height: 75vh;
min-width: 0;
}
}
I really hopes that brings clarity of my issue. Can someone help me out on this one?
You need to set the flex-direction: column; to the container in your media query. I cleaned up your code and made it as vanilla as possible so you can copy and paste into your project.
.block {
width: 100%
}
.container {
display: flex;
flex-wrap: wrap;
}
.container>div {
padding: 1em;
flex: 0 50%;
box-sizing: border-box;
border: red solid;
}
/* Changed the max-width for testing purposes */
#media (max-width: 600px) {
.container {
flex-direction: column;
}
}
<div class="container">
<div class="block">content 1</div>
<div class="block">content 2</div>
<div class="block">content 3</div>
<div class="block">content 4</div>
</div>

Why are my divs disappearing when I use flex-direction?

I am currently working on my webpage's responsiveness and have implemented flexbox for the positioning of my page's elements. The issue that I am experiencing is that when I use 'flex-direction: column' in my media query, the divs disappear upon the browser being resized. What am I doing wrong here?
#blue {
background-color: #57afb5;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#dark-green {
background-color: #29914c;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#green {
background-color: #91e3ad;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#orange {
background-color: #c98a32;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#top {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
#bottom {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
/* responsive web design*/
#media only screen and (max-width: 960px) {
#top {
display: flex;
flex-direction: column;
}
#bottom {
display: flex;
flex-direction: column;
}
/*end of responsive web design*/
<div class='main-content'>
<div id='Navbar_Link-Toggle' style='font-size: 20px'>
<i id='main' class='fas fa-bars'></i>
</div>
<div class='container'>
<div class='Navbar'>
<a class='links' href=''>FOOD</a>
<a class='links' href=''>FUN</a>
<img id='center-logo' src='img/SAMO.png'>
<a class='links' href=''>HISTORY</a>
<a class='links' href=''>LOCATION</a>
</div>
</div>
<div class='header'>
<img id='food' src='img/food.jpg'>
</div>
</div>
<div id='top'>
<div id='blue'></div>
<div id='dark-green'></div>
</div>
<div id='bottom'>
<div id='green'></div>
<div id='orange'></div>
</div>
</div>
The #top and #bottom divs have no height. You could give them a hight. I.E #top, #bottom { hight: 700px; }
Alternatively, use display: block; as illustrated in the example below.
Side note: When I have missing elements, I'll sometimes throw a border: solid red 2px; on them to better visualize what is happening. Hope that helps for next time!
#blue {
background-color: #57afb5;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#dark-green {
background-color: #29914c;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#green {
background-color: #91e3ad;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#orange {
background-color: #c98a32;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#top {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
#bottom {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
/* responsive web design*/
#media only screen and (max-width: 960px) {
#top,
#bottom {
display: block;
}
}
/*end of responsive web design*/
<div class='main-content'>
<div id='Navbar_Link-Toggle' style='font-size: 20px'>
<i id='main' class='fas fa-bars'></i>
</div>
<div class='container'>
<div class='Navbar'>
<a class='links' href=''>FOOD</a>
<a class='links' href=''>FUN</a>
<img id='center-logo' src='img/SAMO.png'>
<a class='links' href=''>HISTORY</a>
<a class='links' href=''>LOCATION</a>
</div>
</div>
<div class='header'>
<img id='food' src='img/food.jpg'>
</div>
</div>
<div id='top'>
<div id='blue'></div>
<div id='dark-green'></div>
</div>
<div id='bottom'>
<div id='green'></div>
<div id='orange'></div>
</div>

How can I create slider layout with css flex boxes?

I'm trying to create a Slider Layout with flex boxes, like this photo :
Here I've big photo at right side and thumbnail items on the left. I want to align left side and thumbnails wrapper with big photo height. But unfortunately It's not possible only with flex boxes and I should check big photo height with JavaScript and align left side with that.
For example check this code:
main{
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper{
width: 500px;
overflow: hidden;
background: gray;
display: flex;
align-items: flex-start;
justify-content: center;
}
.right{
width: calc(100% - 150px);
height: 450px;
background: red;
}
.left{
width: 150px;
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
}
.item{
width: 100%;
height: 50px;
color: white;
background: green;
display: flex;
align-items: center;
justify-content: center;
}
<main>
<div class="wrapper">
<div class="left">
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
</div>
<div class="right"></div>
</div>
</main>
In sample code I've not image and I handled this issue with 450px height in CSS.
So how can I align the left side with out JS and only with CSS? I want to use space-between mode to show all items in this height. Consider it, height:100% didn't work for this issue.
As per my comment, in order for your left column to extend to the height of your right column, all you need to do is remove the align items from your wrapper:
main{
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper{
width: 500px;
overflow: hidden;
background: gray;
display: flex;
/* align-items: flex-start; -- remove this */
justify-content: center;
}
.right{
/* width: calc(100% - 150px); I would swap this for flex grow, then you don't need hard values */
flex-grow: 1;
height: 450px;
background: red;
}
.left{
width: 150px;
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
}
.item{
width: 100%;
height: 50px;
color: white;
background: green;
display: flex;
align-items: center;
justify-content: center;
}
<main>
<div class="wrapper">
<div class="left">
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
</div>
<div class="right"></div>
</div>
</main>
This solution uses CSS Grid layouts - use display: grid on your wrapper that lays out your left and right sections using grid-template-columns: 150px 1fr (remove the width definitions in right and left elements.).
Now add height: 100% to left and then for the items flex: 1 for the flexbox items to occupy the dynamic height coming from the right section - see demo below:
main {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.wrapper {
width: 500px;
overflow: hidden;
background: gray;
display: grid; /* make this a grid container */
grid-template-columns: 150px 1fr; /* 150px for left and rest for right*/
align-items: flex-start;
justify-content: center;
}
.right {
/* width: calc(100% - 150px);*/
height: 450px;
background: red;
}
.left {
/*width: 150px;*/
display: flex;
align-items: center;
justify-content: space-between;
flex-direction: column;
height: 100%; /* ADDED */
}
.item {
width: 100%;
height: 50px;
color: white;
background: green;
display: flex;
align-items: center;
justify-content: center;
flex: 1; /* ADDED */
}
<main>
<div class="wrapper">
<div class="left">
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
<div class="item">
<strong>Item</strong>
</div>
</div>
<div class="right"></div>
</div>
</main>
Something like this can be done with flexbox, if you set the height of the container to be the height you want all your sliders to be:
.container {
display: flex;
flex-flow: column wrap;
height: 250px;
width: 250px;
}
.container > * {
display: flex;
flex-flow: row wrap;
flex: 1 100%;
}
.thumbs {
flex-direction: row;
margin-right: 5px;
}
.thumb {
background: red;
flex: 1 100%;
margin: 5px 0;
}
.large {
background: blue;
margin: 5px;
}
<div class="container">
<div class="thumbs">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
<div class="large"></div>
</div>

Vertically center images with flexbox

How do I vertically center these pairs of images?
I've searched everywhere and tried the align-items: center property with no success.
.first {
display: flex;
justify-content: center;
align-items: center;
}
.first img {
width: 580px;
height: 50vh;
}
.second {
display: flex;
justify-content: center;
}
.second img {
padding-top: 2px;
width: auto;
height: 290px;
}
<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="https://68.media.tumblr.com/64123ccb4f9358207ae32b94646138ca/tumblr_ni9bysrHCt1sh9i3lo1_1280.jpg" alt="">
</div>
https://jsfiddle.net/jsxor8a1/
EDIT:
I don't know if this is the right way but it fixed it. I added a margin-top: 25vh; to the top of the first image and that positioned it perfectly in the center.
.first {
display: flex;
justify-content: center;
align-items: center;
}
.first img {
margin-top: 25vh;
width: 580px;
height: 25vh;
}
.second {
display: flex;
justify-content: center;
}
.second img {
padding-top: 2px;
width: 290px;
height: 25vh;
}
<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="https://68.media.tumblr.com/64123ccb4f9358207ae32b94646138ca/tumblr_ni9bysrHCt1sh9i3lo1_1280.jpg" alt="">
</div>