This question already has answers here:
Equal height children of flex items
(2 answers)
Closed 3 years ago.
I'm trying to achieve a flexbox, where the row will have the titles all lined up. Requirements:
The images won't always be the same height
The description won't always be the same height
The title could be 1 row, or 3 (depending on the length)
Here is a simple fiddle:
https://jsfiddle.net/youradds/r56j4uLe/6/
As you can see this is what you get:
This is more what I'm after:
My SCSS is:
#item-wrapper {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
.item-block {
background: yellow;
flex-grow: 0;
width: 350px;
margin: 2rem 1rem;
display: flex;
flex-direction: column;
justify-content: space-between;
align-content: center;
align-items: center;
.what-logo {
img {
max-height: 100%;
}
}
.text-info {
flex-grow: 1;
.desc {
padding: 1rem;
}
h2 {
flex-grow: 0;
}
}
.action-button {
margin: 0 auto;
}
}
}
.pure-img {
max-width: 100%;
height: auto;
display: block;
}
..and test HTML:
<div id="item-wrapper">
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/3/3-1562951826-3.png" class="pure-img">
</div>
<div class="text-info">
<h2>Zydrunas Savickas Seminar</h2>
<div class="desc">Bodywise Gym and Studios are proud to announce we are bringing the greatest strongman Zydrunas Savickas (Big Z) to Horsham for a seminar.</div>
</div>
Find out more »
</div>
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/7/7-1562936970-7.jpg" class="pure-img">
</div>
<div class="text-info">
<h2>Class for mums</h2>
<div class="desc">Join Quick HIIT</div>
</div>
Find out more »
</div>
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/6/6-1562936464-6.png" class="pure-img">
</div>
<div class="text-info">
<h2>Gratitude Day</h2>
<div class="desc">To spread the positivity you can bring a training partner along to workout with you or to attend one of our classes.</div>
</div>
Find out more »
</div>
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/4/4-1562951950-4.jpg" class="pure-img">
</div>
<div class="text-info">
<h2>The experience of non-duality</h2>
<div class="desc">Yoga & meditation workshop with Indian Spiritual Master Acharya Shree Shankar </div>
</div>
Find out more »
</div>
</div>
Important note: This is a responsive design, so I can't set a height on the image div (i.e min-height 600px) because while this kind of sorts it on wider screens:
...but then on smaller screens, it scales down to 1 element per row - and this then means we have a silly amount of padding between the image and the title on the entries with smaller images:
you could look for a visual compromise.
flex children do not align with flex children from another flex parent.
You may try centering tex-info and what-logo and add an average min-height on .desc
Demo below, play it in full page to test behavior and visual.
#item-wrapper {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
#item-wrapper .item-block {
background: yellow;
max-width: 350px;
margin: 2rem 1rem;
padding:2px;/* see me */
display: flex;
flex-direction: column;
align-items: center;
}
#item-wrapper .item-block .what-logo img {
max-height: 100%;
}
#item-wrapper .item-block .text-info,
#item-wrapper .item-block .what-logo {/* update */
margin-top: auto;
}
#item-wrapper .item-block .text-info .desc {
padding: 1rem;
min-height: 4em;/* 3 lines , average */
}
#item-wrapper .item-block .text-info h2 {text-align:center}
#item-wrapper .item-block .action-button {
margin: 0 auto;
}
.pure-img {
max-width: 100%;
height: auto;
display: block;
}
<div id="item-wrapper">
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/3/3-1562951826-3.png" class="pure-img">
</div>
<div class="text-info">
<h2>Zydrunas Savickas Seminar</h2>
<div class="desc">Bodywise Gym and Studios are proud to announce we are bringing the greatest strongman Zydrunas Savickas (Big Z) to Horsham for a seminar.</div>
</div>
Find out more »
</div>
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/7/7-1562936970-7.jpg" class="pure-img">
</div>
<div class="text-info">
<h2>Class for mums</h2>
<div class="desc">Join Quick HIIT</div>
</div>
Find out more »
</div>
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/6/6-1562936464-6.png" class="pure-img">
</div>
<div class="text-info">
<h2>Gratitude Day</h2>
<div class="desc">To spread the positivity you can bring a training partner along to workout with you or to attend one of our classes.</div>
</div>
Find out more »
</div>
<div class="item-block">
<div class="what-logo">
<img src="https://bodywisegym.co.uk/2018/images/events/4/4-1562951950-4.jpg" class="pure-img">
</div>
<div class="text-info">
<h2>The experience of non-duality</h2>
<div class="desc">Yoga & meditation workshop with Indian Spiritual Master Acharya Shree Shankar </div>
</div>
Find out more »
</div>
</div>
forked fiddle
Related
I'm trying to add a x-axis scrollable slider, but cant seems to figure it out.
Here, I'm trying to do a horizontal card scroll. If I add padding to the card Wrapper it extends itself out of the body. which I don't want to happen. Therefore I removed the padding and added overflow-x: scroll . I thought it will solve the issue and I will be able to scroll left/right.
Please tell me how to add the scroll without going out of the body.
Thanks in advance.
.services {
display: flex;
flex-direction: column;
width: 100%;
overflow-x: visible;
height: fit-content;
align-items: center;
justify-content: space-between;
padding: 30px 0;
}
.cardWrapper {
display: flex;
background: red;
overflow-x: scroll;
flex-direction: row;
justify-content: space-around;
}
.cardContainer {
width: 500px;
height: 500px;
background: blue;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
}
.cardContainer .cardsImage {
width: 60%;
}
.cardContainer .cardsImage img {
width: 100%;
}
.cardContainer .cardContents {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
<div class="services">
<h1>Our Services</h1>
<div class="cardWrapper">
<div class="cardContainer">
<div class="cardsImage">
<img src="https://via.placeholder.com/300" alt="Qa Services">
</div>
<div class="cardContents">
<h2>Qa Services</h2>
<p>Our Qa engineers don't just test,they make your software application successful ensuring quality delivery with expert manual and automated testing services.</p>
</div>
</div>
<div class="cardContainer">
<div class="cardsImage">
<img src="https://via.placeholder.com/300" alt="Qa Services">
</div>
<div class="cardContents">
<h2>Qa Services</h2>
<p>Our Qa engineers don't just test,they make your software application successful ensuring quality delivery with expert manual and automated testing services.</p>
</div>
</div>
<div class="cardContainer">
<div class="cardsImage">
<img src="https://via.placeholder.com/300" alt="Qa Services">
</div>
<div class="cardContents">
<h2>Qa Services</h2>
<p>Our Qa engineers don't just test,they make your software application successful ensuring quality delivery with expert manual and automated testing services.</p>
</div>
</div>
</div>
</div>
You need to have a simple wrapper that acts as a boundary for your scrollbar. Then inside you need the wrapper for your flex contents.
In addition, simple using a width on elements within a flex context is not enough. You need to provide flex as well
.services {
padding: 30px 0;
}
.cardWrapperOuter {
overflow-x: scroll;
}
.cardWrapper {
display: flex;
background: red;
}
.cardContainer {
flex: 0 0 500px;
width: 500px;
height: 500px;
background: blue;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
}
.cardContainer .cardsImage {
width: 60%;
}
.cardContainer .cardsImage img {
width: 100%;
}
.cardContainer .cardContents {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
<div class="services">
<h1>Our Services</h1>
<div class="cardWrapperOuter">
<div class="cardWrapper">
<div class="cardContainer">
<div class="cardsImage">
<img src="./assets/images/QaServices.svg" alt="Qa Services">
</div>
<div class="cardContents">
<h2>Qa Services</h2>
<p>Our Qa engineers don't just test,they make your software application successful ensuring quality delivery with expert manual and automated testing services.</p>
</div>
</div>
<div class="cardContainer">
<div class="cardsImage">
<img src="./assets/images/QaServices.svg" alt="Qa Services">
</div>
<div class="cardContents">
<h2>Qa Services</h2>
<p>Our Qa engineers don't just test,they make your software application successful ensuring quality delivery with expert manual and automated testing services.</p>
</div>
</div>
<div class="cardContainer">
<div class="cardsImage">
<img src="./assets/images/QaServices.svg" alt="Qa Services">
</div>
<div class="cardContents">
<h2>Qa Services</h2>
<p>Our Qa engineers don't just test,they make your software application successful ensuring quality delivery with expert manual and automated testing services.</p>
</div>
</div>
</div>
</div>
</div>
I will put here some code for better understanding my need.
.wrap{
display:flex;
flex-direction:row;
align-items: center;
justify-content: flex-start;
width: 100%;
}
.img{
width: 30px;
height: 20px;
background: red;
}
.img1{
width:40px;
}
<div class='wrap'>
<div class='img img1'>
</div>
<p>
Xxxxxxxxxxxxx
</p>
</div>
<div class='wrap'>
<div class='img'>
</div>
<p>
yyyyyyyyy
</p>
</div>
<div class='wrap'>
<div class='img'>
</div>
<p>
zzzzzz
</p>
</div>
So as u can see, we have 3 div & p elements. I want to separate this elements with identical gap between them. I want separate red div from text. As u notice, first element is longer than rest. I wouldn't like to use static margin or padding value. Exist better solution?
You need a container that defines the width of all children. Then, you can set justify-content: space-between on .wrap, so the text and the red rectangle will get away from each other.
Then, add a gap property to .wrap, so you'll have a basic gap between the elements to start with.
Here's your snippet with the new adjustments:
.container {
display: flex;
flex-direction: column;
max-width: fit-content;
}
.wrap {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
gap: 1em;
width: 100%;
}
.img {
width: 30px;
height: 20px;
background: red;
}
.img1 {
width: 40px;
}
<div class="container">
<div class='wrap'>
<div class='img img1'>
</div>
<p>
Xxxxxxxxxxxxx
</p>
</div>
<div class='wrap'>
<div class='img'>
</div>
<p>
yyyyyyyyy
</p>
</div>
<div class='wrap'>
<div class='img'>
</div>
<p>
zzzzzz
</p>
</div>
</div>
Trying to create a 2 column, 3 row flexbox container for a food menu. The product elements (which should be 2 per row) do not wrap when shrunk. I'm looking for a way to create a wrapping layout using flexbox. Also, what would be the best way to use Media Queries for the product title to be displayed when the layout is shrunk for mobile-size?
I'm attaching my jsfiddle code:
https://jsfiddle.net/5ksd34nf/#&togetherjs=Ix1LEBTca6
(keep in mind without the images, the design changes so I'm attaching photos)
The HTML is:
`
<section class="menu-page" id="Menu">
<div class="TitleWrapper">
<h1 class="title">Menu</h1>
</div>
<div class="menu-list">
<div class="product">
<div class="imgwrapper">
<img src="images/burger.jpg" alt="Burger" class="food-image">
</div>
<div class="text">
<div class="product-content">
<h3 class="name">Burgers</h3>
<h3 class="price">10 €</h3>
</div>
<div class="ptags">
<p class="allergens">Allergens:</p>
<p class="info">Milk, Gluten</p>
</div>
</div>
</div>
How can I space out the Price from the Name using flexbox?
Basically, you can do something like this:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-item {
border: 1px solid gray;
width: 100%;
margin-bottom: 20px;
}
.flex-sub-item {
display: flex;
justify-content: space-between;
}
#media screen and (min-width: 400px) {
.flex-item {
width: calc((100% - 20px) / 2);
margin-right: 20px;
}
.flex-item:nth-child(2n) {
margin-right: 0;
}
}
<div class="flex-container">
<div class="flex-item">
<div class="flex-sub-item">
<div>Title</div>
<div>Price</div>
</div>
</div>
<div class="flex-item">
<div class="flex-sub-item">
<div>Title</div>
<div>Price</div>
</div>
</div>
</div>
I am looking to achieve the following layout:
Here is how I'm picturing it (with grids):
Black bar is the nav (we can ignore this)
A title and subtitle (purple) - these should be aligned and take up approx 70% of width - I think I've done this
A form which has 3 columns (should take up 70ish percent of the 70%, I don't want inputs to be too wide)
Column 1: Heading + text pairs
Column 2: it will have some icon/character - these must be perfectly aligned
Column 3: Heading + input boxes - these must be the same width
Here is my starting HTML:
.title-container {
display: flex;
justify-content: center;
background: red;
}
.title-item {
flex-basis: 75%;
}
.data-container {
display: flex;
justify-content: center;
background: blue;
}
.column-items {
flex-basis: 70%;
display: flex;
flex-direction: row;
}
.column-1-item {
background: green;
flex-grow: 0.5;
}
.column-2-item {
background: yellow;
flex-grow: 0.1;
align-self: center;
}
.column-3-item {
background: orange;
flex-grow: 1;
}
<div class="title-container">
<div class="title-item">
<h2>Title</h2>
<p>This is some text</p>
</div>
</div>
<div class="data-container">
<div class="column-items">
<div class="column-1-item">
<p>Heading1</p>
<p>SomeText</p>
</div>
<div class="column-2-item">
<p>--></p>
</div>
<div class="column-3-item">
<p>Heading1</p>
<input type="text" name="lname">
</div>
</div>
</div>
I have tried to expand on this, but no matter what I try, I end up further away from my design making me think there is something wrong with my initial design (and flex understanding). If I add additional 'row', it breaks my layout. I also think my data-container is wrongly setup, since this will take up far more space than I want it to
Here is a code pen.
Could someone help get me closer to my design?
I would wrap your entire html in a wrapper class so that you can get the general layout of the page like so:
<div class="wrapper">
<div class="title-container">
<h2>Title</h2>
<p>
Subtitle should be aligned with title
</p>
</div>
<div class="form-container">
<div class="item">
<div class="column">
<p>Heading1</p>
<p>Some Text</p>
</div>
<div class="column">
<p>-></p>
</div>
<div class="column">
<p>Heading1</p>
<p>[ input textfield ]</p>
</div>
</div>
<div class="item">
<div class="column">
<p>Heading3</p>
<p>Some Text</p>
</div>
<div class="column">
<p>-></p>
</div>
<div class="column">
<p>Heading2</p>
<p>[ input textfield ]</p>
</div>
</div>
<div class="item">
<div class="column">
<p>Heading3</p>
<p>Some Text</p>
</div>
<div class="column">
<p>-></p>
</div>
<div class="column">
<p>Heading3</p>
<p>[ input textfield ]</p>
</div>
</div>
<div class="item">
<div class="column"></div>
<div class="column"></div>
<div class="column submit-button">
<p>[ Button ]</p>
</div>
</div>
</div>
</div>
Then you can specify the width for the title-container and form-container with the width property. Making each of the item classes in the form container have a display: flex property lets you format the children column classes to have flex-grow: 1 so they can fill up the available space. The css then looks like:
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
outline: 1px solid red;
}
.title-container {
width: 70%;
outline: 1px solid red;
}
.form-container {
width: 50%;
outline: 1px solid red;
}
.item {
display: flex;
outline: 1px solid red;
}
.column {
/* flex-grow: 1; */
flex: 1 1 0px;
outline: 1px solid red;
}
.submit-button {
display: flex;
align-items: flex-end;
justify-content: flex-end;
}
Alternately you can remove the flex-grow: 1 property from the column class and add justify-content: space-between to the item class to get a result similar to your example.
Here is the codepen.
Your .data-container just needs a flex-direction: column; because you want the .column-items to stack.
I'm currently working on my code where i tried making a responsive image gallery with a hover effect. At my first code I created, the effects are working great. However, it is not responsive as it stays in 4 rows always. Please see this Codepen 1
When I tried to change some of the contents of my style.css using flexbox, i was able to make it responsive and change its size depending on the window size. However, whenever you hover to the image, the hover box does not align with the container itself. Please see this Codepen2
The codes are written on the codepen itself. The only code i changed in the 1st code to the 2nd one is this (I commented the previous code instead of removing it to remember which part i changed:
.container
{
/*width: 1280px;
margin: 70px auto 0;
display: flex;
flex-direction: row;
flex-wrap: wrap;*/
margin: .5vw;
font-size: 0;
display: -ms-flexbox;
-ms-flexbox-wrap : wrap;
-ms-flexbox-direction: column;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
}
.container .box
{
/*position: relative;
width: 300px;
height: 300px;
background: #ff0;
margin: 10px;
box-sizing: border-box;
display: inline-block;*/
-webkit-box-flex: auto;
-ms-flex: auto;
flex: auto;
width: 300px;
margin: .5vw;
}
.container .box .imgBox img
{
/*max-width: 100%;*/
width: 100%;
height: auto; /*added this*/
transition: transform 2s;
}
Please do help me figure out why the hover position is not working. Thanks
You could modify the width's of the container so that it size is defined by relative positioning, this will solve your issue.
The only change that is to be made is to the div with class container, shown below.
.container {
width: 80%;
margin: 0px auto;
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
}
Please check the below example.
body {
margin: 0;
padding: 0;
background: #262626;
font-family: sans-serif
}
.container {
width: 80%;
margin: 0px auto;
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
}
.container .box {
position: relative;
width: 300px;
height: 300px;
background: #ff0;
margin: 10px;
box-sizing: border-box;
display: inline-block;
}
.container .box .imgBox {
position: relative;
overflow: hidden;
}
.container .box .imgBox img {
max-width: 100%;
transition: transform 2s;
}
.container .box:hover .imgBox img {
transform: scale(1.2);
}
.container .box .details {
position: absolute;
top: 10px;
left: 10px;
bottom: 10px;
right: 10px;
background: rgba(0, 0, 0, .8);
transform: scaleY(0);
transition: transform .5s;
}
.container .box:hover .details {
transform: scaleY(1);
}
.container .box .details .content {
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
padding: 15p;
color: #fff;
}
.container .box .details .content h1 {
margin: 0;
padding: 0;
font-size: 20px;
color: #ff0;
}
.container .box .details .content p {
margin: 10px 0 0;
padding: 0;
}
<div class="container">
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/1.jpg">
</div>
<div class="details">
<div class="content">
<h1> Snow Queen Elsa </h1>
<p>Elsa is the daughter of Agnarr and Iduna, older sister of Anna, and queen of Arendelle. Elsa was born with the powers to manipulate ice and snow and used them to entertain her sister. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/2.jpg">
</div>
<div class="details">
<div class="content">
<h1> The Little Mermaid Ariel </h1>
<p>Ariel as she appears in her mermaid form in Disney's The Little Mermaid. Ariel is a fictional character and the title character of Walt Disney Pictures' 28th animated film The Little Mermaid (1989). She is often rebellious, and in the first film,
she longs to be a part of the human world. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/3.jpg">
</div>
<div class="details">
<div class="content">
<h1> Sleeping Beauty Aurora </h1>
<p>Princess Aurora, also known as Sleeping Beauty or Briar Rose, is a fictional character who appears in Walt Disney Pictures' animated feature film Sleeping Beauty (1959). Originally voiced by singer Mary Costa, Aurora is the only daughter of King
Stefan and Queen Leah. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/4.jpg">
</div>
<div class="details">
<div class="content">
<h1> Aladdin's Jasmine </h1>
<p>Princess Jasmine is the deuteragonist of Disney's 1992 animated feature film, Aladdin. She is from the Middle Eastern kingdom of Agrabah where her father, the Sultan, rules. Jasmine was born into a role and society that treats her as an object
and a tool, rather than a person </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/5.jpg">
</div>
<div class="details">
<div class="content">
<h1> Rupunzel </h1>
<p>Princess Rapunzel (voiced by Mandy Moore) is more assertive in character, and was born a princess. Her long blonde hair has magical healing and restoration powers. A woman named Mother Gothel (voiced by Donna Murphy) kidnaps Rapunzel for her magical
hair which would help maintain her youth. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/5.jpg">
</div>
<div class="details">
<div class="content">
<h1> Megara </h1>
<p>In Greek mythology, Megara was the oldest daughter of Creon, king of Thebes. Megara was offered by her father to Hercules because he defended Thebes. She had two children, a boy and a girl, but was killed with both of them by Hercules in excess
of madness caused by Hera. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="c">
</div>
<div class="details">
<div class="content">
<h1> Belle The Beauty </h1>
<p>Belle is a fictional character who appears in Walt Disney Pictures' animated feature film Beauty and the Beast (1991). Originally voiced by American actress and singer Paige O'Hara, Belle is the non-conforming daughter of an inventor who yearns
to abandon her predictable village life in return for adventure. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/1.jpg">
</div>
<div class="details">
<div class="content">
<h1> Mulan The Great Warrior Of China </h1>
<p>Mulan (full name Fa Mulan) is a character, inspired by an actual historic figure, who appears in Walt Disney Pictures' animated feature film Mulan (1998), as well as its sequel Mulan II (2004). </p>
</div>
</div>
</div>
</div>