This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 2 years ago.
I'm using display: flex; to create tiles view.
I'm trying to put the third picture under the second picture.
But, whenever I do it, the third picture went under the first picture and won't come under second picture,
#media screen and (min-width: 1000px) {
.main {
height:1800px;
width: 100%;
margin: 0px 0px 0px 0px;
position: relative;
z-index: 0;
}
.parallax {
/* The image used */
background-image: url("https://i.ibb.co/r272XPt/2019-2020.png");
/* Set a specific height */
min-height: 400px;
opacity: 60%;
filter: blur(2px);
-webkit-filter: blur(2px);
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.tiles{
position:relative;
top:100px;
width:90vw;
margin-left:5vw;
display: flex;
flex-wrap: wrap;
}
.chromebookHelpdesk img{
margin-left:5vw;
width:50vw;
display:block;
}
.subhelp{
height:25vw;
}
.subhelp img{
margin-left:5vw;
width:25vw;
display:block;
}
.studentsTour img{
margin-left:5vw;
width:20vw;
margin-top:5vw;
display:block;
}
#projects img {
text-align: center;
display: block;
width: 25vw;
margin: 20px;
}
.mission_logo {
width: 200px;
display: inline-block;
float: left;
text-align: center;
}
.mission {
text-align: center;
margin-top: 100px;
font-size: 20px;
}
.ingenuity {
color: #3cba54;
font-size: 60px !important;
}
.creativity {
color: #f4c20d;
font-size: 60px !important;
}
.innovation {
color: #db3236;
font-size: 60px !important;
}
}
#media only screen and (max-width: 1000px) {
.main {
height:2500px;
width: 100%;
margin: 0px 0px 0px 0px;
position: relative;
z-index: 0;
}
.parallax {
display: none;
}
.mission_logo {
width: 60vw;
text-align: center;
}
.mission {
text-align: center;
margin-top: 100px;
font-size: 15px;
}
.ingenuity {
color: #3cba54;
font-size: 40px !important;
}
.creativity {
color: #f4c20d;
font-size: 40px !important;
}
.innovation {
color: #db3236;
font-size: 40px !important;
}
}
.tiles h1 {
text-align: center;
font-size: 50px;
color: black;
}
.follow{
position:relative;
top:100px;
text-align:center;
border-radius: 50%;
background-color: #84e3ca;
width: 50vw;
height: 50vw;
margin-left: 25vw;
opacity:70%;
}
.follow h1{
font-size:35px;
padding-top: 20vw;
}
.follow h2{
font-size:30px;
}
<div class="main">
<div class="tiles">
<div class="chromebookHelpdesk"><a href="https://sledteam.github.io/sled/chromebook"><img
src="https://github.com/sledteam/sled/blob/master/Chromebook%20Helpdesk.png?raw=true" alt="Chromebook-Helpdesk"></a></div>
<div class="subhelp"><a href="https://sledteam.github.io/sled/chromebook"><img
src="https://github.com/sledteam/sled/blob/master/Sub%20Help.png?raw=true" alt="Sub Help"></a>
</div>
<div class="studentsTour"><a href="https://sledteam.github.io/sled/chromebook"><img
src="https://github.com/sledteam/sled/blob/master/New%20Students%20Tour.png?raw=true" alt="New Students Tour"></a></div>
</div>
I'm stuck with this for a week.
I would appreciate it if anyone knows a solution for this.
Complete guide to css flexbox
.tiles {
display: flex;
flex-wrap: wrap;
flex-direction: row;
flex: 1;
}
.section {
display: flex;
flex: 1;
flex-direction: column;
}
.item {
display: flex;
flex: 1;
padding: 1rem;
margin: 4px;
background: green;
color: white;
}
<div class="tiles">
<div class="section">
<div class="item">
<p>Chromebook Helpdesk</p>
</div>
</div>
<div class="section">
<div class="item">
<p>Sub Help</p>
</div>
<div class="item">
<p>Student Tour</p>
</div>
</div>
</div>
It is simple if you understand the concept here is the example:
As per your requirement you need 2 columns with single row so you will be creating flex property, now you need 2 columns hence you make it flex:50% like 2. Now coming to your image section where you need 2 images to be underneath so you will provide the height:50%(right images) and you will give height:100%(left image).
You can keep changing the sizes as you desire. You can also add responsive design for the same. Hope it helps.
* {
box-sizing: border-box;
}
.row {
display: flex;
}
.column {
flex: 50%;
padding: 5px;
}
<div class="row">
<div class="column">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" style="width:100%" />
</div>
<div class="column">
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" style="width:50%" />
</div>
<div class="row">
<img src="http://i183.photobucket.com/albums/x312/Tiefnuker/office_02_zpsdz0zixcd.jpg" style="width:50%" />
</div>
</div>
</div>
Related
How would I make it so the image resizes properly when viewed on smaller screens. Right now, the image is over the container when viewed on smaller screens. There are also small gaps between the top/left of the container and the image. Would I have to resize the image in the media query or expand the width of my container in the media query?
.container {
width: 88%;
margin: 50px auto;
margin-top: 0px;
}
.heading {
text-align: center;
font-size: 30px;
margin-bottom: 50px;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-around;
flex-flow: wrap;
}
.card {
width: 30%;
background: #fff;
border: 1px solid #ccc;
margin-bottom: 50px;
}
.image {
width: fit-content;
height: fit-content;
}
.card-body {
padding: 30px 10px;
text-align: left;
font-size: 18px;
}
.card-body .btn {
display: block;
color: #fff;
text-align: center;
background: black;
margin-top: 30px;
text-decoration: none;
padding: 10px 5px;
}
#media screen and (max-width: 1000px) {
.card {
width: 40%;
}
.heading {
text-align: auto;
}
.card-header {
margin: auto;
}
.image {
width: fit-content;
height: fit-content;
}
}
#media screen and (max-width: 620px) {
.container {
width: 100%;
}
.heading {
padding: 20px;
font-size: 20px;
text-align: auto;
}
.card {
width: 80%;
}
.image {
width: fit-content;
height: fit-content;
}
}
<div class="container">
<div class="heading">
<h1>Latest Posts</h1>
</div>
<div class="row">
<div class="card">
<div class="image">
<img src="https://via.placeholder.com/1920x1080.jpg">
</div>
<div class="card-body">
<p>
Text Here
</p>
Read more
</div>
</div>
I typically put width: 100%; on images in my projects and height: auto this way the image will be more responsive and scale up and down. You can reduce the width for the smaller media query if you want an even smaller image (width: 85%; for example) or I would probably personally end up reducing the width of the container to get the desired result.
1st: Remove your CSS for the class .image
2nd: Add this CSS line to the base-css (not within the media queries):
img {
object-fit: contain;
width: 100%;
}
What will that do?
object-fit: contain will keep the image aspect ratio while width: 100% will cause the image to fit exactly the given space. The height is set automatically according to the width while it maintain the image aspect ratio as mentioned above.
.container {
width: 88%;
margin: 50px auto;
margin-top: 0px;
}
.heading {
text-align: center;
font-size: 30px;
margin-bottom: 50px;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-around;
flex-flow: wrap;
}
.card {
width: 30%;
background: #fff;
border: 1px solid #ccc;
margin-bottom: 50px;
}
.image {
width: fit-content;
height: fit-content;
}
.card-body {
padding: 30px 10px;
text-align: left;
font-size: 18px;
}
.card-body .btn {
display: block;
color: #fff;
text-align: center;
background: black;
margin-top: 30px;
text-decoration: none;
padding: 10px 5px;
}
img {
object-fit: contain;
width: 100%;
}
#media screen and (max-width: 1000px) {
.card {
width: 40%;
}
.heading {
text-align: auto;
}
.card-header {
margin: auto;
}
}
#media screen and (max-width: 620px) {
.container {
width: 100%;
}
.heading {
padding: 20px;
font-size: 20px;
text-align: auto;
}
.card {
width: 80%;
}
}
<div class="container">
<div class="heading">
<h1>Latest Posts</h1>
</div>
<div class="row">
<div class="card">
<div class="image">
<img src="https://via.placeholder.com/1920x1080.jpg">
</div>
<div class="card-body">
<p>
Text Here
</p>
Read more
</div>
</div>
I have one flexbox with two rows and min-height. The right row has another flex with 2 columns with justify-content space between.
It works perfectly fine in Chrome but it does not fill the min-height in IE. There are many questions about it but none of them worked for me. Please note I have different styles of this where image is on top, bottom or right. I need to expand both content and image to fill the entire card in IE.
Here is the code.
.card-wrap {
display: block;
position: relative;
}
a {
display: block;
}
.card-image-left {
flex-direction: row;
}
.card {
box-sizing: border-box;
display: flex;
border: 1px solid black;
min-height: 200px;
}
.card-content {
padding: 12px;
display: flex;
flex-direction: column;
justify-content: space-between;
flex-grow: 1;
}
.card-text {
font-size: 0.875rem;
color: #1D1D1D;
}
.card-footer {
margin-top: 12px;
}
.card-image-wrap {
overflow: hidden;
position: relative;
}
h3 {
font-size: 1.25rem;
font-weight: 500;
margin: 0 0 4px 0;
}
.card-image {
width: 100%;
background-size: cover;
width:80px;
height: 100%;
background-repeat: no-repeat;
background-position: 50% 50%;
background-image: url("https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg");
}
<div class='card-wrap'>
<a href='#'>
<div class='card card-image-left'>
<div class='image-wrap'>
<div class='card-image'>
</div>
</div>
<div class='card-content'>
<div class='card-header'>
<h3>test title</h3>
<div class='card-text'>test subtitlte</div>
</div>
<div class='card-footer'>
<div>
<div class='card-footer-monetary'>1000</div>
</div>
</div>
</div>
</div>
</a>
</div>
Image from IE
Its a bug in IE 10-11. In IE 10-11, min-height declarations on flex containers work to size the containers themselves, but their flex item children do not seem to know the size of their parents. They act as if no height has been set at all.
More info: https://github.com/philipwalton/flexbugs#flexbug-3
Workaround
Simply wrap the flex container (here it div with classname card) with another flex-container having flex direction column
.ie-fix-wrapper{
display: flex;
flex-direction:column;
}
.card-wrap {
display: block;
position: relative;
}
a {
display: block;
}
.card-image-left {
flex-direction: row;
}
.card {
box-sizing: border-box;
display: flex;
border: 1px solid black;
min-height: 200px;
}
.card-content {
padding: 12px;
display: flex;
flex-direction: column;
justify-content: space-between;
flex-grow: 1;
}
.card-text {
font-size: 0.875rem;
color: #1D1D1D;
}
.card-footer {
margin-top: 12px;
}
.card-image-wrap {
overflow: hidden;
position: relative;
}
h3 {
font-size: 1.25rem;
font-weight: 500;
margin: 0 0 4px 0;
}
.card-image {
width: 100%;
background-size: cover;
width:80px;
height: 100%;
background-repeat: no-repeat;
background-position: 50% 50%;
background-image: url("https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg");
}
<div class='card-wrap'>
<a href='#'>
<div class="ie-fix-wrapper">
<div class='card card-image-left'>
<div class='image-wrap'>
<div class='card-image'>
</div>
</div>
<div class='card-content'>
<div class='card-header'>
<h3>test title</h3>
<div class='card-text'>test subtitlte</div>
</div>
<div class='card-footer'>
<div>
<div class='card-footer-monetary'>1000</div>
</div>
</div>
</div>
</div>
</div>
</a>
</div>
The solution provided by #Soothran works well. There is also alternative solution that is quicker to implement - inherit min-height in the children. In this case of the code above:
.image-wrap {
min-height: inherit;
}
.card-content {
min-height: inherit;
}
.card-wrap {
display: block;
position: relative;
}
a {
display: block;
}
.card-image-left {
flex-direction: row;
}
.card {
box-sizing: border-box;
display: flex;
border: 1px solid black;
min-height: 200px;
}
.card-content {
min-height: inherit;
padding: 12px;
display: flex;
flex-direction: column;
justify-content: space-between;
flex-grow: 1;
}
.card-text {
font-size: 0.875rem;
color: #1D1D1D;
}
.card-footer {
margin-top: 12px;
}
.card-image-wrap {
overflow: hidden;
position: relative;
}
h3 {
font-size: 1.25rem;
font-weight: 500;
margin: 0 0 4px 0;
}
.image-wrap {
min-height: inherit;
}
.card-image {
width: 100%;
width:80px;
height: 100%;
background: black;
}
<div class='card-wrap'>
<a href='#'>
<div class='card card-image-left'>
<div class='image-wrap'>
<div class='card-image'>
</div>
</div>
<div class='card-content'>
<div class='card-header'>
<h3>test title</h3>
<div class='card-text'>test subtitlte</div>
</div>
<div class='card-footer'>
<div>
<div class='card-footer-monetary'>1000</div>
</div>
</div>
</div>
</div>
</a>
</div>
You need to wrap any flex-direction:column inside a flex-direction:row
(or just display:flex for simple)
I'm creating a grid in flexbox that has images on one side and then text on the other. The problem I'm facing is that the images aren't aligning properly in the grid.
Here is my code:
/* iPhone */
#media only screen and (max-width: 768px) {
body {
overflow-x: hidden;
}
.body-img {
background: url('background-mobile.png') no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height: 100vh;
width: 100%;
}
.search-img {
background: url('search-img-mobile.png') no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height: 100vh;
padding-bottom: 10%;
}
.logo {
width: 150px;
margin-top: 15%;
}
h1,
h2 {
font-family: 'Open Sans', sans-serif;
color: #ffffff;
font-weight: 300;
padding-top: 20%;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.5em;
padding-bottom: 15%;
}
h3 {
font-family: 'Open Sans', sans-serif;
font-size: 1.0em;
font-weight: 400;
}
.searchbar {
margin-top: 15%;
}
.btn {
background-color: #1075C1;
color: #ffffff;
border-radius: 0px;
}
#keyframes bouncing {
0% {
bottom: 0;
}
50% {
bottom: 20px;
}
100% {
bottom: 0;
}
}
.arrow {
animation: bouncing 1s infinite ease-in-out;
bottom: 0;
display: block;
height: 26px;
left: 50%;
margin-left: -25px;
position: absolute;
width: 26px;
}
.left {
background-color: #1075C1;
}
.col-md-12 {
min-height: 0px;
}
.container-flex {
display: flex;
flex-direction: column-reverse !important;
justify-content: space-between;
}
.container-flex2 {
display: flex;
flex-direction: column-reverse !important;
justify-content: space-between;
}
.searchbox {
background-color: rgba(255, 255, 255, 0.6);
padding-top: 5%;
padding-left: 5%;
padding-right: 5%;
padding-bottom: 5%;
}
.form-check-label {
font-size: 0.8em;
}
.btn-lg {
padding-left: 25%;
padding-right: 25%;
padding-top: 5%;
padding-bottom: 5%;
}
.btn-lg:hover {
background-color: #003e6c;
}
}
#media only screen and (min-width: 1024px) {
body {
overflow-x: hidden;
}
.body-img {
background: url('nathan-dumlao-609935-unsplash.png') no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height: 100vh;
width: 100%;
}
.logo {
width: 250px;
margin-top: 15%;
}
h1,
h2 {
font-family: 'Open Sans', sans-serif;
color: #ffffff;
font-weight: 300;
padding-top: 5%;
}
.searchbar {
margin-top: 8%;
}
.btn {
background-color: #1075C1;
color: #ffffff;
border-radius: 0px;
}
.arrow {
display: none;
}
.container-flex {
display: flex;
flex-direction: row !important;
justify-content: space-between;
background-color: #1075C1;
}
.container-flex2 {
display: flex;
flex-direction: row-reverse;
background-color: #1075C1;
justify-content: space-between;
}
.left {
background-color: #1075C1;
}
.description {
background-color: #1075C1;
}
}
<section id="one">
<div class="container-flex">
<div class="left">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h2>Sign up for our career guide and receive free insight into available jobs</h2>
</div>
</div>
</div>
<div class="right">
<img src="section1-mobile.png" class="img-fluid">
</div>
</div>
<div class="container-flex2">
<div class="left">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h2>Answer questions as we help you with your job search</h2>
</div>
</div>
</div>
<div class="right">
<img src="section2.png" class="img-fluid">
</div>
</div>
<div class="container-flex">
<div class="left">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h2>Get matched with companies looking for talent like you</h2>
</div>
</div>
</div>
<div class="right">
<img src="section3.png" class="img-fluid">
</div>
</div>
</section>
The end result I'm looking for.
I'm not sure what I'm doing wrong in the CSS?
There's a few things to point out here that might help you in getting things how you want them to look.
Firstly, your indentation in your markup is a wee bit skew, so, at first glance, it's a little bit tricky to figure out, sorting the indentation in both your markup and css will make it easier for you, and others, to debug.
Secondly, you've got a lot of superfluous styles / class in your markup that look like they're from bootstrap. These could potentially be affecting your own custom styles and causing conflicts.
Thirdly you've got an awful lot going on in terms nesting in your markup that makes it difficult to apply styles too.
I would suggest simplifying your markup first.
I would also suggest stripping back your css to the bare minimum needed, removing your vendor prefixes (-webkit-, -o-), the #keyframes declaration (or at the very least commenting them out). You can always add them after or put you css through a preprocessor or autoprefixer afterwards but for debugging they can make things difficult.
Flexbox can be a bit tricky at times, so I put together a pen on codepen showing how I would approach your mockup (full code below). It's just a quick draft but maybe some other community members will have some input / suggestions. In the meantime have a play around with the values and look at the outcome.
I'd also recommend this SO question regarding flexbox, the top answer is fairly concise about horizontal and vertical alignment. I'd also recommend checking out flex box froggy for honing you flex box skills.
markup:
<section class="section">
<div class="row">
<div class="content">
<span class="icon">🎨</span>
<span class="text">here is some text</span>
</div>
<img class="image" src="http://lorempixel.com/400/200" />
</div>
<div class="row">
<div class="content">
<span class="icon">🐳</span>
<span class="text">here is some other text</span>
</div>
<img class="image" src="http://lorempixel.com/400/200" />
</div>
<div class="row">
<div class="content">
<span class="icon">💰</span>
<span class="text">here is some more text</span>
</div>
<img class="image" src="http://lorempixel.com/400/200" />
</div>
</section>
css:
body {
width: 100%;
text-align: center;
font-family: sans-serif;
}
.section, .row {
width: 75vw;
background: dodgerblue;
color: #fff;
font-size: 1.5em;
}
.section {
display: inline-block;
}
.row {
display: flex;
justify-content: space-between;
flex-direction: row;
}
.row:nth-child(odd) {
flex-direction: row-reverse;
}
.content {
width: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.text {
width: 50%;
text-align: center;
}
.image {
width: 50%;
height: auto;
}
.icon {
font-size: 1.5em;
}
I hope this helps.
I have tested both snippets on screen size (1024x786) and ipad pro (1024x1366)
/* iPhone */
.left {
width: 50%;
left: 0px;
}
.right {
width: 50%;
right: 0px;
}
h2 {
font-family: arial !important;
}
#media only screen and (max-width: 768px) {
body {
overflow-x: hidden;
}
.body-img {
background: url('background-mobile.png') no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height: 100vh;
width: 100%;
}
.search-img {
background: url('search-img-mobile.png') no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height: 100vh;
padding-bottom: 10%;
}
.logo {
width: 150px;
margin-top: 15%;
}
h1, h2 {
font-family: 'Open Sans', sans-serif;
color: #ffffff;
font-weight: 300;
padding-top: 20%;
}
h1 {
font-size: 1.3em;
}
h2 {
font-size: 1.3em;
padding-bottom: 15%;
}
h3 {
font-family: 'Open Sans', sans-serif;
font-size: 1.0em;
font-weight: 400;
}
.searchbar {
margin-top: 15%;
}
.btn {
background-color: #1075C1;
color: #ffffff;
border-radius: 0px;
}
.arrow {
animation: bouncing 1s infinite ease-in-out;
bottom: 0;
display: block;
height: 26px;
left: 50%;
margin-left: -25px;
position: absolute;
width: 26px;
}
.left {
background-color: #1075C1;
}
.col-md-12 {
min-height: 0px;
}
.container-flex {
display: flex;
justify-content: space-between;
}
.container-flex2 {
display: flex;
justify-content: space-between;
}
.searchbox {
background-color: rgba(255, 255, 255, 0.6);
padding-top: 5%;
padding-left: 5%;
padding-right: 5%;
padding-bottom: 5%;
}
.form-check-label {
font-size: 0.8em;
}
.btn-lg {
padding-left: 25%;
padding-right: 25%;
padding-top: 5%;
padding-bottom: 5%;
}
.btn-lg:hover {
background-color: #003e6c;
}
.text-center {
height: 200px;
line-height: 200px;
text-align: center;
border: 2px dashed #f69c55;
}
.text-center h2 {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
}
#media only screen and (min-width: 1024px) {
body {
overflow-x: hidden;
}
.body-img {
background: url('nathan-dumlao-609935-unsplash.png') no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height: 100vh;
width: 100%;
}
.logo {
width: 250px;
margin-top: 15%;
}
h1, h2 {
font-family: 'Open Sans', sans-serif;
color: #ffffff;
font-weight: 300;
padding-top: 5%;
}
.searchbar {
margin-top: 8%;
}
.btn {
background-color: #1075C1;
color: #ffffff;
border-radius: 0px;
}
.arrow {
display: none;
}
.container-flex {
display: flex;
justify-content: space-between;
background-color: #1075C1;
}
.container-flex2 {
display: flex;
flex-direction: row-reverse;
background-color: #1075C1;
justify-content: space-between;
}
.left {
background-color: #1075C1;
}
.description {
background-color: #1075C1;
}
.text-center {
height: 500px;
line-height: 500px;
text-align: center;
border: 2px dashed #f69c55;
}
.text-center h2 {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section id="one">
<div class="container-flex">
<div class="left">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h2>Sign up for our career guide and receive free insight into available jobs</h2>
</div>
</div>
</div>
<div class="right">
<img src="https://dummyimage.com/500x500/000/fff" class="img-fluid">
</div>
</div>
<div class="container-flex2">
<div class="right">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h2>Answer questions as we help you with your job search</h2>
</div>
</div>
</div>
<div class="left">
<img src="https://dummyimage.com/500x500/000/fff" class="img-fluid">
</div>
</div>
<div class="container-flex">
<div class="left">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<h2>Get matched with companies looking for talent like you</h2>
</div>
</div>
</div>
<div class="right">
<img src="https://dummyimage.com/500x500/000/fff" class="img-fluid">
</div>
</div>
</section>
</body>
</html>
I also attached output screenshots for both size
Hope, you find your solution.
Use <img> instead of background image, because you can make them responsive more easy with
.img-responsive {
display: block;
height: auto;
max-width: 100%;
}
Therefore use big sizes for the images so that they always fill up the space.
Use flex-basis to handle width, e.g. 60% for the left side and 40% for the right side.
Make the container flex-wrap: wrap so that the columns in the container are wrapped and not in a row.
Set the column inside a container
.container > div {
display: flex;
flex-direction: column;
justify-content: center; // Center vertically
align-items: center; // Center horizontally
}
to center the content within horizontally and vertically.
Example
body {
background: black;
}
.container {
display: flex;
flex-wrap: wrap;
margin: 0 auto;
max-width: 960px;
border: 3px solid white;
background: #0F75C0;
}
.container>div {
display: flex;
flex-direction: column;
box-sizing: border-box;
justify-content: center;
align-items: center;
}
/* Columns on the left side */
.container>div:nth-child(odd) {
flex-basis: 60%;
}
/* Columns on the right side */
.container>div:nth-child(even) {
flex-basis: 40%;
}
/* Make images responsive */
.container>div>img {
display: block;
height: auto;
max-width: 100%;
}
.container .info {
max-width: 70%;
text-align: center;
padding: 15px;
font-size: 22px;
color: white;
}
#media screen and (max-width: 320px) {
.container {
flex-direction: column;
}
}
<div class="container">
<div>
<div class="info">
<img src="http://via.placeholder.com/48x48?text=icon" alt="">
<p>Sign up for our career guide</p>
</div>
</div>
<div>
<img src="http://via.placeholder.com/800x600" alt="">
</div>
<div>
<img src="http://via.placeholder.com/1000x500" alt="">
</div>
<div>
<div class="info">
<img src="http://via.placeholder.com/48x48?text=icon" alt="">
<p>Answer a few questions as we aid you in your job search</p>
</div>
</div>
<div>
<div class="info">
<img src="http://via.placeholder.com/48x48?text=icon" alt="">
<p>Get matched with companies looking for talent like you</p>
</div>
</div>
<div>
<img src="http://via.placeholder.com/800x600" alt="">
</div>
</div>
How to divide a webpage into four equal parts using flex box? Something like this website- www.rileyjshaw.com
The columns should be able to stack upon one another on smaller view ports.
Edit/ Update:
So far I've tried this-
Should I change the line height? How can I achieve distinct blocks?
/* Grid */
.column {
flex-basis: 100%;
width: 50%;
height: 50%;
line-height: 200px;
}
#media screen and (min-width: 800px) {
.row {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.column {
flex: 1;
}
._25 {
flex: 2.5;
}
._5 {
flex: 5;
}
}
/* Style */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
/* padding: 15px;
border: 1px solid #666;
margin: 5px 0;*/
background: #343436;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
You can simplify the code and gain the wanted output. Here I removed the row's and used a container. The main benefit with this structure is that you can alter the order of the column's if you find that necessary.
I also choose to use flex-basis instead of flex-grow to make them stay 50% wide no matter their content size.
On wider screens, using the media query, set the column's 50% wide and the container to display: flex; flex-wrap: wrap;.
On narrower screens, and being block elements, they stack on top of each other by default
html, body {
height: 100%;
margin: 0;
}
.container {
height: 100%;
}
.column {
height: 25%;
}
#media screen and (min-width: 600px) {
.container {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 50%;
height: 50%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.column:nth-child(1) {
background: #5c9;
}
.column:nth-child(2) {
background: #fb0;
}
.column:nth-child(3) {
background: #39f;
}
.column:nth-child(4) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="container">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
If you still need the original markup structure, here is a sample with that too
html, body {
height: 100%;
margin: 0;
}
.row {
height: 50%;
}
.column {
height: 50%;
}
#media screen and (min-width: 600px) {
.row {
display: flex;
}
.column {
flex-basis: 50%;
height: 100%;
}
}
/* general styles */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
/*margin-bottom: 70px;*/
}
.column {
padding: 15px;
/*border: 1px solid #666;*/
box-sizing: border-box;
}
.row:nth-child(1) .column:nth-child(1) {
background: #5c9;
}
.row:nth-child(1) .column:nth-child(2) {
background: #fb0;
}
.row:nth-child(2) .column:nth-child(1) {
background: #39f;
}
.row:nth-child(2) .column:nth-child(2) {
background: #f33;
}
main {
max-width: 1200px;
margin: 0 auto;
}
h1,
h2 {
text-align: center;
}
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
Updated based on a comment
Centering the column's content can be done with i.e.:
Flexbox - https://jsfiddle.net/0ns6ofcp/
.column {
height: 25%;
display: flex; /* added */
justify-content: center; /* hor. center */
align-items: center; /* ver. center */
}
Transform - https://jsfiddle.net/0ns6ofcp/1/
<div class="column">
<div>50%</div>
</div>
.column {
position: relative; /* added property */
height: 25%;
}
.column > div { /* added rule */
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
Here is something you can start with:
/* Grid */
.column {
flex-basis: 100%;
height: 50vh;
}
.row {
display: flex;
}
.row:first-child .column:first-child {
background: red;
}
.row:first-child .column:last-child {
background: blue;
}
.row:last-child .column:first-child {
background: green;
}
.row:last-child .column:last-child {
background: black;
}
/* Style */
body {
font-family: 'Lato', sans-serif;
font-size: 1.3em;
color: #ccc;
background: #000;
}
.column {
background: #343436;
}
<div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
<div class="row">
<div class="column">
50%
</div>
<div class="column">
50%
</div>
</div>
</div>
I want my images next to each other with a little margin in between. But when I do `margin-right: 10px; on each div the last image wont align with my title bar.
How can I give the divs a space in between without having a space on the right of the last div?
Note: The content is dynamic, so I cant make a div to hold the 4 divs.
There are many ways you can do, I'll just show one of them.
EDIT 1: solution for multiple rows by using nth-child
DEMO: http://jsfiddle.net/s0xLfcrx/1/
HTML:
<div class="bar"></div>
<div class="box">
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
<div>e</div>
<div>f</div>
<div>g</div>
<div>h</div>
</div>
CSS:
.bar, .box {
width: 460px;
}
.bar {
background: lime;
height: 20px;
}
.box {
text-align: center;
font-size: 0;
}
.box > div {
display: inline-block;
font-size: 16px;
background: gold;
width: 100px;
margin: 0 10px;
}
.box > div:nth-child(4n+1) {
margin-left: 0;
}
.box > div:nth-child(4n) {
margin-right: 0;
}
ORIGINAL DEMO (for only 1 row):
http://jsfiddle.net/s0xLfcrx/
You could use justify-content: space-between. This creates even spacing inbetween each of the image containers and pushes the first and last element to the edges of the parent div.
Your html:
.container {
width: 346px;
}
.title-bar {
background-color: #ccc;
width: 100%;
}
.flex-container {
padding: 0;
margin: 0;
list-style: none;
-ms-box-orient: horizontal;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -moz-flex;
display: -webkit-flex;
display: flex;
}
.space-between {
-webkit-justify-content: space-between;
justify-content: space-between;
}
.image-container {
background: #ccc;
padding: 5px;
width: 60px;
height: 50px;
margin: 0;
line-height: 50px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
<div class="container">
<div class="title-bar">
<h1>Title</h1>
</div>
<div class="flex-container space-between">
<div class="image-container">1</div>
<div class="image-container">2</div>
<div class="image-container">3</div>
<div class="image-container">4</div>
</div>
</div>
The styling for the image-containers in the code above is just an example - if you have an unknown number of divs loading you either fix the width, or make them fluid and the container width will need to be fluid (unless you want it fixed of course).
you could do something like this with variable widths:
.titlebar {
width: calc(100% - 2px);
display: block;
height: 40px;
background: blue;
}
.item25 {
width: calc(25% - 11px);
height: 80px;
display: inline-block;
background: black;
margin-right: 10px;
}
.no-margin {
margin-right: 0;
}
<div class="titlebar"></div>
<div class="item25"></div>
<div class="item25"></div>
<div class="item25"></div>
<div class="item25 no-margin"></div>
Just use simple CSS
.div { width: 25%; text-align: center; padding: 5px; box-sizing: border-box; }
.div img { display: block; margin: 0 auto; }
There are many ways to do but if you need RESPONSIVE for this then use this solution:
body{font-family:arial;}
h1{
display:block;
margin:0px;
padding:0px;
background:#0af;
color:#fff;
text-align:center;
}
.layer{
display:block;
overflow:auto;
}
.layer > div{
display:block;
float:left;
margin:10px 10px 10px 0px;
width: -moz-calc(25% - 7.5px);
width: -webkit-calc(25% - 7.5px);
width: calc(25% - 7.5px);
background-color:#000;
height:30px;
}
.layer > div:nth-child(4n) {
margin-right: 0;
background-color:#f00;
}
<h1>Title bar</h1>
<div class="layer">
<div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div>
<div></div><div></div>
</div>