CSS Flexbox - Aligning by card description - css

I have a simple card layout and I want to align it by the card description.
Can anyone help me, please? :)
Here is a Pen: CODEPEN
Here is my HTML structure:
<div class="card-container">
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">A veeeeery very very very large title</h4>
<p class="card-desc">omg, that description have broken :(</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
</div>

By changing the .card elements to
display: flex;
flex-direction: column;
and adding margin-top: auto; to the descriptions. This will cause the description element to be pushed to the bottom of the card. Because the cards are stretched vertically, the alignment works on the horizontal axis. This would not be the case if you changed align-items on the .card-container to be something other than stretch.
.card {
display: flex;
flex-direction: column;
text-align: center;
flex: 50%;
padding: 15px 0;
}
.card-img img {
width: 60px;
padding: 15px;
}
.card-title {
font-weight: 700;
text-transform: uppercase;
margin-bottom: 10px;
font-size: 1.2rem;
}
.card-desc {
font-weight: 400;
margin: 0;
margin-top: auto;
line-height: 1rem;
}
.card-container {
background: rgba(255, 0, 0, 0.432);
display: flex;
flex-wrap: wrap;
justify-content: center;
flex: 100%;
}
#media screen and (min-width: 500px) {
.card {
flex: 1;
}
.card-img img {
width: 50px;
}
.card-title {
font-weight: 700;
text-transform: uppercase;
margin-bottom: 10px;
font-size: 1.2rem;
}
.card-desc {
font-weight: 400;
line-height: 1rem;
}
.card-container {
background: rgba(255, 0, 0, 0.432);
display: flex;
flex-wrap: wrap;
justify-content: center;
flex: 100%;
}
}
<div class="card-container">
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">A veeeeery very very very large title</h4>
<p class="card-desc">omg, that description have broken :(</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
<div class="card">
<div class="card-img"><img src="https://via.placeholder.com/500x500"></div>
<h4 class="card-title">Just a title</h4>
<p class="card-desc">A simple description</p>
</div>
</div>

Related

why my <div></div> is overlaying on another div on small screen

Whenever I check the responsiveness and check it on the small screen my one div starts overlaying another div. I have tried the ```position : relative and overflow: hidden as well but it makes no sense call you please tell me what I have to do to stop it from being overlaying another div
my HTML code is :
<div class="container-grid">
<div class="grid_upper">
<h1 class="grid_Heading">My recent projects</h1>
<div class="grid_para">
Ask me a Question if you are unable to solve it.
</div>
</div>
<div class="cardContainer1">
<div class="container">
<div class="row">
<div class="col-sm">
<div class="card h-200">
<img
src="./istockphoto-1074239826-170667a.jpg"
class="card-img-top"
alt="..."
/>
<div class="card-footer">
<small class="card_text_1">Business</small>
<small class="text-muted"> 3 mins</small>
</div>
<div class="card-body">
<h5 class="card-title">Card </h5>
<p class="card-text">
This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit
longer.
</p>
</div>
<div class="card-footer footerNo2">
<img class="shortImg" src="./istockphoto-1074239826-170667a.jpg" alt="">
<div class="FooterInsideContainer">
<small class="text"> Micheal Corleone</small>
<small class="text-muted"> Senior web developer</small>
</div>
</div>
</div>
</div>
<div class="col-sm">
<div class="card h-200">
<img
src="./istockphoto-1272685152-612x612.jpg"
class="card-img-top"
alt="..."
/>
<div class="card-footer">
<small class="card_text_1">Business</small>
<small class="text-muted"> 3 mins</small>
</div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit
longer.
</p>
</div>
<div class="card-footer footerNo2">
<img class="shortImg" src="./istockphoto-1074239826-170667a.jpg" alt="">
<div class="FooterInsideContainer">
<small class="text"> Micheal Corleone</small>
<small class="text-muted"> Senior web developer</small>
</div>
</div>
</div>
</div>
<div class="col-sm">
<div class="card h-200">
<img
src="./photo-1473090826765-d54ac2fdc1eb.jpg"
class="card-img-top"
alt="..."
/>
<div class="card-footer">
<small class="card_text_1">Business</small>
<small class="text-muted"> 3 mins</small>
</div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit
longer.
</p>
</div>
<div class="card-footer footerNo2">
<img class="shortImg" src="./istockphoto-1074239826-170667a.jpg" alt="">
<div class="FooterInsideContainer">
<small class="text"> Micheal Corleone</small>
<small class="text-muted"> Senior web developer</small>
</div>
</div>
</div>
</div>
</div>
<div class="row my-3">
<div class="col-sm-4">
<div class="card h-200">
<img
src="./creative-book-cover-design-vintage-260nw-1115305040.webp"
class="card-img-top"
alt="..."
/>
<div class="card-footer">
<small class="card_text_1">Business</small>
<small class="text-muted"> 3 mins</small>
</div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit
longer.
</p>
</div>
<div class="card-footer footerNo2">
<img class="shortImg" src="./istockphoto-1074239826-170667a.jpg" alt="">
<div class="FooterInsideContainer">
<small class="text"> Micheal Corleone</small>
<small class="text-muted"> Senior web developer</small>
</div>
</div>
</div>
</div>
<div class="col-sm-8" style="background-color: white;">
<div class="card card2 h-200" >
<img style="width: 100%;"
src="./wide-view-image-young-woman-paddling-sup-board-calm-morning-sea-water-rear-view_254268-2063.jpg"
class="card-img-top"
alt="..."
/>
<div class="card-footer laster">
<small class="card_text_1">Business</small>
<small class="text-muted"> 3 mins</small>
</div>
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">
This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit
longer.
</p>
</div>
<div class="card-footer footerNo2_laster">
<img class="shortImg" src="./istockphoto-1074239826-170667a.jpg" alt="">
<div class="FooterInsideContainer">
<small class="text" style="color: black;"> Micheal Corleone</small>
<small class="text-muted"> Senior web developer</small>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="Approach">
<div class="app_head">
<h1 class="main_approach_Heading">My approach</h1>
</div>
<div class="container">
<div class="row row-cols-1 row-cols-md-3 g-4">
<div class="col">
<div class="card h-100">
<!-- <img src="..." class="card-img-top" alt="..."> -->
<h1 class="pic_char no1">1</h1>
<div class="card-body">
<h5 class="card-title">We will sit and talk drinking coffee</h5>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
<div class="card-footer approach_footer">
<div class="triangle arrow-up"></div>
</div>
</div>
</div>
<div class="col">
<div class="card h-100">
<!-- <img src="..." class="card-img-top" alt="..."> -->
<h1 class="pic_char no2">2</h1>
<div class="card-body">
<h5 class="card-title">We will sketch some ugly Frameworks</h5>
<p class="card-text">This card has supporting text below as a natural lead-in to additional content text below as a natural lead-in to additional content.</p>
</div>
<div class="card-footer approach_footer">
<div class="app_circle"> </div>
</div>
</div>
</div>
<div class="col">
<div class="card h-100">
<!-- <img src="..." class="card-img-top" alt="..."> -->
<h1 class="pic_char no3">3</h1>
<div class="card-body">
<h5 class="card-title">You will pay alot for my services</h5>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
</div>
<div class="card-footer approach_footer">
<div class="triangle arrow-up"></div>
</div>
</div>
</div>
</div>
</div>
</div>
and the CSS code is :
.grid_upper {
display: flex;
flex-direction: column;
width: 67%;
margin: auto;
}
.grid_Heading {
font-size: clamp(2em, 2.5vw, 7em);
padding: 0.5em 0.21em 0em 0em;
color: white;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
.grid_para {
color: white;
}
.container-grid {
display: flex;
position: relative;
height: 160vh;
overflow: visible;
flex-wrap: wrap;
background-color: #3e4245;
}
.cardContainer1 {
position: relative;
display: flex;
flex-wrap: wrap;
padding: 2em 0em;
width: 70%;
margin: auto;
}
.cardContainer2 {
position: relative;
display: flex;
flex-wrap: wrap;
padding: 2em 0em;
width: 70%;
margin: auto;
}
.card {
background-color: #252728;
border: none;
border-radius: 20px;
height: 100%;
}
.card2 {
background-color: white;
}
.card2 .card-title {
color: black;
}
.card2 .card-text {
color: grey;
}
.card-title {
color: white;
}
.card-text {
color: rgba(255, 255, 255, 0.522);
}
.card-footer {
display: flex;
align-items: center;
justify-content: space-between;
color: rgba(255, 255, 255, 0.522);
background-color: #252728;
/* background-color: red; */
margin: none;
border: none;
}
.card_text_1 {
color: rgba(255, 255, 0, 0.659);
}
.card-img-top {
width: 100%;
height: 25vh;
object-fit: cover;
}
.shortImg {
height: 40px;
width: 40px;
border-radius: 50%;
}
.footerNo2 {
display: flex;
align-items: flex-start;
justify-content: flex-start;
/* flex-direction: column; */
padding: 1em 1em;
/* background-color: aqua; */
}
.footerNo2_laster {
display: flex;
align-items: flex-start;
justify-content: flex-start;
background-color: white;
}
.FooterInsideContainer {
display: flex;
flex-direction: column;
padding: 0em 1em;
}
.laster {
background-color: white;
}
.laster .card_text_1 {
color: skyblue;
}
.Approach {
position: relative;
/* overflow: hidden; */
height: 100vh;
width: 100%;
margin-top: 10em;
}
.app_head {
padding: 7em 5em;
display: flex;
font-weight: bold;
/* background-color: red; */
}
.main_approach_Heading {
font-size: clamp(1.5rem, 10.5vw, 4rem);
font-family: "Roboto", sans-serif;
margin: auto;
text-align: center;
}
.pic_char {
background-repeat: repeat;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
/* margin-top: 200px; */
text-align: center;
font-weight: bold;
text-transform: uppercase;
/* font-family: 'Times New Roman', Times, serif; */
font-weight: 800;
-webkit-font-smoothing: antialiased;
}
.no1 {
background-image: url("./istockphoto-1288917639-170667a.jpg");
font-family: "Montserrat Alternates", sans-serif;
font-size: 13em;
}
.no2 {
background-image: url("./polygonal-square-background-black-blue-purple-vector-21357114.jpg");
/* font-family: 'Anton', sans-serif; */
font-size: 12em;
}
.no3 {
background-image: url("./android-marshmallow-6-hd-orange-yellow-and-gray-blocks-illustrarion-wallpaper-preview.jpg");
font-size: 12em;
}
.Approach .card {
display: flex;
background-color: white;
color: black;
/* width: 50%; */
}
.Approach .card-body {
display: flex;
flex-direction: column;
text-align: center;
align-items: center;
justify-content: center;
padding: 0em 4em;
/* background-color: pink; */
}
.Approach .card-title {
color: black;
font-weight: 700;
font-size: 1.4em;
font-feature-settings: "vrt2";
}
.Approach .card-text {
color: grey;
text-align: center;
}
.approach_footer {
background-color: white;
display: flex;
align-items: center;
justify-content: center;
color: grey;
}
.arrow-up {
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 20px solid white;
box-shadow: -1px -1px 10px -2px rgba(0, 0, 0, 0.5);
}
.app_circle{
width: 25px;
height: 25px;
border-radius: 50%;
background-color: white;
box-shadow: -1px -1px 10px -2px rgba(0, 0, 0, 0.5);
}

Keep left margin of a div inline with another div when resizing

the problem that I'm having is that when I resize the browser the left margins of both the and my card divs aren't aligning. Is there some sort of css property or maybe some JS that will make them stay aligned when resizing?
here's my sandbox
https://codesandbox.io/s/stupefied-christian-tosys?file=/src/styles.css:0-
Add the .filter margin margin: 0 80px; to your .row. and then remove justify-content: center;. It will loose the centered. But will be align on the left. To aviod the overflow from the body, you can set width: calc(100% - 160px);to your .row:
/* ADDED BELOW */
margin: 0 80px;
justify-content: unset;
width: calc(100% - 160px);
After if you want to keep your card more center, either you use justify-content: space-between; on row, either you use margin: 10px auto; on your card.
DEMO:
body {
margin: 0 auto;
max-width: 1200px;
}
.row {
display: flex;
justify-content: center;
width: 100%;
flex-wrap: wrap;
/*ADDED BELOW */
margin: 0 80px;
justify-content: unset;
width: calc(100% - 160px);
}
.filter {
display: flex;
margin: 0 80px;
}
.filter select {
display: inline;
width: 15rem;
margin: 10px;
border-radius: 5px;
}
.card {
width: 15rem;
margin: 10px;
border-radius: 5px;
border: 1px solid rgba(0, 0, 0, 0.125);
}
.info {
padding: 15px;
}
.img-container {
padding-top: 0;
position: relative;
}
.card img {
width: 100%;
object-fit: cover;
}
.price {
font-weight: bold;
}
<div id="root">
<div class="App">
<div class="filter-container">
<div class="product-listing-wrap">
<div class="filter">
<select class="custom-select" id="priceGroup">
<option value="1">Under $50</option>
<option value="2">$50 to $100</option>
<option value="3">$100 to $250</option>
<option value="4">Over $250</option>
</select>
</div>
<div class="row" style="
margin: 0 80px;
padding: 10px 0;
width: calc(100% - 160px);
">
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="1"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="2"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="3"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="4"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="5"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="6"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="7"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
<div class="card">
<div class="img-container"><img src="https://images.pexels.com/photos/593171/pexels-photo-593171.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="8"></div>
<div class="info">
<p class="info-title"><span><a>placeholder...</a></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
try it:
.row {
display: flex;
justify-content: space-between;
margin: 0 80px;
max-width: 1040px;
flex-flow: wrap;
}

Display items in one line with scroll CSS

I am trying to display images in a single line horizontal with scroll but I can't figure how to I would appreciate it if anyone could help me.
CSS
.img {
max-width: 400px;
position: relative;
}
.animal {
float: left;
background-color: #fff;
}
.image-grid{
flex-wrap: wrap;
width: 1400px;
margin: 0 auto;
}
.pet-photo {
padding: 10px;
display: block;
box-sizing: border-box;
flex-basis: 100%;
float: left;
padding: 20px;
width: 200px;
height: 300px;
display: block;
}
js
<div class ="image-grid">
<div class="animal">
<img class="pet-photo " src="${pet.photo}" >
<div class="olay" onclick="location.href='${pet.href}';" style="cursor: pointer;">
<div id="mydiv">
<h2 class="pet-name">${pet.name}
<h1 class="species">${pet.species}
</div>
<div></div></div>
</div>
</div>
That's all thank you for reading :) really appreciate it.
solution is display: flex; with flex-wrap: nowrap;
.container-items {
overflow: auto;
width: 100%;
}
.items {
display: flex;
flex-wrap: nowrap;
}
.items .item {
min-width: 150px;
text-align: center;
padding: 5px;
}
.items .item h2,
.items .item h3 {
margin: 0;
}
.items .item img {
width: 100%;
}
<div class ="container-items">
<div class ="items">
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3>here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
<div class="item">
<img src="https://cdn.mos.cms.futurecdn.net/otjbibjaAbiifyN9uVaZyL-320-80.jpg">
<h2>here title</h2>
<h3 class="species">here text</h3>
</div>
</div>
</div>

Why would the overflow property on a flex item affect its flex-shrink behavior? [duplicate]

This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Why is a flex item limited to parent size?
(1 answer)
Closed 2 years ago.
I am creating a layout that contains three parts: sidebar (left), page (center), and summary (right). The left will remain fixed at the width of its inner content when the viewport width decreases, which is the expected behavior.
However, once I add overflow-y: auto to the left container, it loses respect for its inner content width, and completely shrinks along with the viewport width.
How does overflow-y: auto affect an item's flex-shrink behavior? How do I apply an overflow-y while allowing it to continue respecting its inner content width?
.root {
display: flex;
}
.root .sidebar {
border: 8px solid red;
flex: 2;
background: #f3f3f5;
height: 100vh;
display: flex;
justify-content: flex-end;
padding-top: 80px;
position: sticky;
top: 0;
/*
enable this and shrink the viewport
notice how the sidebar shrinks completely
and no longer respects the inner content's fixed width
*/
/*overflow-y: auto;*/
}
.root .page {
border: 8px solid green;
flex: 8;
max-width: 810px;
padding: 80px 80px 160px 80px;
}
#media only screen and (max-width: 1024px) {
.root .page {
padding-right: 40px;
padding-left: 40px;
}
}
.root .summary {
border: 8px solid blue;
flex: 2;
padding-top: 160px;
position: sticky;
top: 0;
align-self: flex-start;
}
.root .actions {
border: 8px solid orange;
position: fixed;
bottom: 0;
padding: 10px;
width: 100%;
background: white;
display: flex;
justify-content: center;
align-items: center;
}
.root .actions .inner {
display: flex;
justify-content: space-between;
align-items: center;
flex-basis: 650px;
}
.root .stepper {
border: 4px solid #333;
flex-basis: 320px;
}
.root .stepper .step {
display: flex;
align-items: center;
margin: 0px 70px 40px 40px;
white-space: nowrap;
}
.root .stepper .step .circle {
background: #ffc2d4;
border-radius: 50%;
width: 28px;
height: 28px;
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
}
.root .form-stuff {
margin-bottom: 80px;
}
.root .card {
border: 4px solid #333;
align-self: flex-start;
border-radius: 8px;
padding: 10px;
max-width: 280px;
min-width: 180px;
white-space: nowrap;
}
.root .card .title {
margin: 20px 0;
}
.root .card .subtitle {
margin: 20px 0;
font-size: 14px;
}
.root .button {
background: #ffc2d4;
padding: 10px 20px;
}
* {
box-sizing: border-box;
}
<div class="root">
<div class="sidebar">
<div class="stepper">
<div class="step">
<div class="circle">
1
</div>
<div class="title">Step 1</div>
</div>
<div class="step">
<div class="circle">
2
</div>
<div class="title">Step 2</div>
</div>
<div class="step">
<div class="circle">
3
</div>
<div class="title">Step 3</div>
</div>
<div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div><div class="step">
<div class="circle">
4
</div>
<div class="title">Step 4</div>
</div>
</div>
</div>
<div class="page">
<div class="form">
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
<div class="form-stuff">
form stuff
</div>
</div>
</div>
<div class="summary">
<div class="card">
<div class="title">Summary</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
<div class="subtitle">foo</div>
</div>
</div>
<div class="actions">
<div class="inner">
<div class="button">Back</div>
<div class="button">Next</div>
</div>
</div>
</div>

How to set width of a flexbox div

Please look my code in the JSFiddle link.
Now, the output looks like this:
Please help me change the code so it looks like this:
CSS:
.card {
display: contents;
}
.card > div {
width: 150px;
height: 120px;
margin: 10px;
}
.breakLine::after {
content: '';
width: 100%;
}
.hiddenCard {
background-color: red;
}
.cardList {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.MemoryGame {
display: flex;
align-items: center;
flex-direction: column;
}
.controlSection {
display: flex;
justify-content: space-between;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
HTML:
<body>
<div id="root">
<div>
<div class="MemoryGame">
<h1>Memory Game !</h1>
<div class="gameWrapper">
<div class="controlSection">
<h2>score: 5</h2>
<h2>level: 3</h2>
</div>
<div class="cardList">
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
.card {
display: contents;
}
.card > div {
width: 150px;
height: 120px;
margin: 10px;
}
.breakLine::after {
content: '';
width: 100%;
}
.hiddenCard {
background-color: red;
}
.cardList {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.gameWrapper{
max-width: 90%;
}
.MemoryGame {
display: flex;
align-items: center;
flex-direction: column;
}
.controlSection {
display: flex;
justify-content: space-between;
margin: 0 2rem;
}
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
Try with the following CSS for the controlSection
.controlSection {
width: 660px;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
I just set the width to the width of the boxes (150*4 width + margin 10*6 = 660px) and aligned the container to the middle with margin: 0 auto;
This is not really responsive though.
Good luck
You can try this: justify-content:space-around;
.controlSection {
display: flex;
justify-content: space-around;
align-items: center;
flex-direction: row;
}
.card {
display: contents;
}
.card > div {
width: 150px;
height: 120px;
margin: 10px;
}
.breakLine::after {
content: '';
width: 100%;
}
.hiddenCard {
background-color: red;
}
.cardList {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.MemoryGame {
display: flex;
align-items: center;
flex-direction: column;
}
.controlSection {
display: flex;
justify-content: space-around;
align-items: center;
flex-direction: row;
text-align:center;
}
<div class="MemoryGame">
<h1>Memory Game !</h1>
<div class="gameWrapper">
<div class="controlSection">
<h2>score: 5</h2>
<h2>level: 3</h2>
</div>
<div class="cardList">
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
</div>
</div>
</div>
If this does not work for you, you may need to set boundaries by assigning width to the parent container. Like so,
.controlSection {
display: flex;
justify-content: space-between;
align-items: center;
flex-direction: row;
width:65%;
margin: 0 auto;
}
.card {
display: contents;
}
.card > div {
width: 150px;
height: 120px;
margin: 10px;
}
.breakLine::after {
content: '';
width: 100%;
}
.hiddenCard {
background-color: red;
}
.cardList {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.MemoryGame {
display: flex;
align-items: center;
flex-direction: column;
}
.controlSection {
display: flex;
justify-content: space-between;
align-items: center;
flex-direction: row;
width:65%;
margin: 0 auto;
}
<div class="MemoryGame">
<h1>Memory Game !</h1>
<div class="gameWrapper">
<div class="controlSection">
<h2>score: 5</h2>
<h2>level: 3</h2>
</div>
<div class="cardList">
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
</div>
</div>
</div>
The justify-content property is a sub-property of the Flexible Box Layout module.
space-between items are evenly distributed in the line; the first item is on the start line, the last item on the end line.
space-around items are evenly distributed in the line with equal space around them.
Change the justify-content: space-between to space-around. The empty space before the first and after the last item equals half of the space between each pair of adjacent items.
.card {
display: contents;
}
.card > div {
width: 150px;
height: 120px;
margin: 10px;
}
.breakLine::after {
content: '';
width: 100%;
}
.hiddenCard {
background-color: red;
}
.cardList {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.MemoryGame {
display: flex;
align-items: center;
flex-direction: column;
}
.controlSection {
display: flex;
justify-content: space-around;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>React App</title>
</head>
<body>
<div id="root">
<div>
<div class="MemoryGame">
<h1>Memory Game !</h1>
<div class="gameWrapper">
<div class="controlSection">
<h2>score: 5</h2>
<h2>level: 3</h2>
</div>
<div class="cardList">
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card ">
<div class="hiddenCard"></div>
</div>
<div class="card breakLine">
<div class="hiddenCard"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You could add an extra div, to wrap it up all together
<!-- the whole game area, including the white area -->
<div id="main-div">
<!-- only the area where stuff should be displayed -->
<!-- the title div and the header div will align accordingly with the width of the game div -->
<div id="game-wrapper">
<div id="title">
<p>memory game</p>
</div>
<div id="header">
<p>score: 5</p>
<p>level: 3</p>
</div>
<div id="game">
<!-- actual game -->
</div>
</div>
</div>

Resources