How to keep link at the bottom using flexbox? - html

We have to display blocks of content in many rows with max 3 columns. Each block of content contains a heading, description, and link. We are using flexbox to display the blocks in a row such that the height of the row is taken up by the tallest element. However, we are not able to always align the link at the bottom and it always seems to be place right below each description.
How can we align the link in the bottom of each block and have each block still be the height of the tallest block using flexbox?
Here is what we have tried: https://codepen.io/userrj/pen/WYXoOO
Code explained:
each block is surrounded by .bkg--grey so you can see the block is taking the height of the tallest block as expected.
border is added to each element with flex__item so you can see how much space it is taking up.
each block contains: heading, description, and link from top down (column)
Current issue:
Desired output
We are hoping to not use float or position: absolute to get this done.

You need to make article a flex container and adjust some alignment:
article {
display: flex;
}
.flex__item > div:last-child {
margin-top:auto;
}
Full code:
.flex__item {
display: flex;
justify-content: center;
align-items: stretch;
flex: 0 1 auto;
flex-direction: column;
}
.flex__col {
display: flex;
justify-content: center;
align-items: stretch;
}
.flex__wrapper {
display: flex;
align-items: stretch;
justify-content: flex-start;
flex-wrap: wrap;
flex-direction: row;
flex: 0 1 auto;
}
.flex__item {
padding: 16px;
border: 1px solid black;
}
.bkg--grey {
background-color: #ddd;
}
.col--sm-12 {
width: 100%;
}
.col--md-6 {
width: 50%;
}
.col--lg-4 {
width: 33.33%;
}
/*added this*/
article {
display: flex;
}
.flex__item > div:last-child {
margin-top:auto;
}
/**/
[class*=col--] {
box-sizing: border-box;
padding: 16px;
}
<section class="flex__wrapper">
<div class="col--sm-12 col--md-6 col--lg-4 flex__col">
<article class="bkg--grey">
<div class="flex__item">
<div>
<h4>Some Efficiently</h4>
<p>Efficiently enhance frictionless markets without distinctive deliverables. </p>
</div>
<div>Read More</div>
</div>
</article>
</div>
<div class="col--sm-12 col--md-6 col--lg-4 flex__col">
<article class="bkg--grey">
<div class="flex__item">
<div>
<h4>Some Objectively promote</h4>
<p>Objectively promote enterprise-wide strategic theme areas rather than process-centric catalysts for change. Completely procrastinate sticky best practices and corporate sources. Distinctively unleash superior metrics via go forward alignments. Uniquely reconceptualize plug-and-play e-services through collaborative solutions. Progressively maximize adaptive experiences with. </p>
</div>
<div>Read More</div>
</div>
</article>
</div>
<div class="col--sm-12 col--md-6 col--lg-4 flex__col">
<article class="bkg--grey">
<div class="flex__item">
<div>
<h4>Completely create</h4>
<p>Completely create equity invested relationships for client-focused paradigms. Distinctively benchmark exceptional information before corporate materials. Compellingly pontificate 2.0. </p>
</div>
<div>Read More</div>
</div>
</article>
</div>
<div class="col--sm-12 col--md-6 col--lg-4 flex__col">
<article class="bkg--grey">
<div class="flex__item">
<div>
<h4>Distinctively deliver</h4>
<p>Distinctively deliver one-to-one potentialities with excellent resources. Collaboratively.</p>
</div>
<div>Read More</div>
</div>
</article>
</div>
<div class="col--sm-12 col--md-6 col--lg-4 flex__col">
<article class="bkg--grey">
<div class="flex__item">
<div>
<h4>Authoritatively facilitate</h4>
<p>Authoritatively facilitate sustainable portals through cross-platform catalysts for change. Monotonectally transform e-business total linkage without front-end action items.</p>
</div>
<div>Read More</div>
</div>
</article>
</div>
</section>

Related

How can I add a x-axis scrollable slider?

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'm trying to replicate this figma design

Hey I'm new into CSS but I dont know how to make this work. Please help me on how to make this work.
The desired outcome.
My outcome.
The problem is how to make the heading come under the location tags. like in the figma design?
Here is the HTML.
import { GrLocation } from "react-icons/gr"
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="tags-colum">
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
</div>
Here is the CSS.
.container {
display: flex;
align-items: center;
justify-content: center;
}
.main-img {
height: 168px;
width: 125px;
border-radius: 5px;
}
.tags-colum {
display: flex;
align-items: center;
margin: 20px 20px;
}
.container-text {
display: block;
}
.underline-text {
text-decoration: underline;
}
Your problem is .tags-column is display: flex, so you cannot group all 3 of those elements together. Because the default flexbox is row-based style which means it will align all elements on the same row
For the fix,
Create a group of that left image and all content elements (.container)
Separate the location icon and JAPAN to another group with flexbox (.tags-colum)
Put Mount Fuji separately (.container-text)
Note that, .new-group is just an alias name which I'm using for demonstration, and it has no specific styles
import { GrLocation } from "react-icons/gr"
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="new-group">
<div className="tags-colum">
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
</div>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
</div>
The issue is that you have used display flex to the .tags-colum ( which is the outermost parent) to fix this you can use flex-direction: column, yes adding this will make everything stacked up, so what's the solution?
group your elements like this
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="tags-colum">
<div className='gp1'>
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
</div>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
.tags-colum {
display: flex;
align-items: flex-start;
margin: 20px 20px;
flex-direction: column;
}
.gp1 {
display: flex;
align-items: center;
}
here is an example https://codesandbox.io/s/homepage-forked-g18lgm?file=/public/index.html:56-126
or you can completely remove the display flex from the tags-colum

How to set identical gap between elements when one image is longer than rest

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>

Creating flexbox row with equal layouts *per line* [duplicate]

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

Divs with same class adjust to different content

I have three divs in a row next to each other. They have the same class and same structure, but their content is different, particularly in length.
My problem is, that all three divs take the height of the one with the "longest" content instead of adjusting to their individual content.
Here's my HTML:
<main class="content">
<div class="column last-match">
<div class="image-wrapper">
<img src="default_thumbnail.jpg"/>
</div>
<div class="text-wrapper">
<a class="heading" href="#"><p class="heading">A Heading</p></a>
<p class="post-excerpt">Some text</p>
Read More
</div>
</div>
<div class="column last-match">
<div class="image-wrapper">
<img src="default_thumbnail.jpg"/>
</div>
<div class="text-wrapper">
<a class="heading" href="#"><p class="heading">A Heading</p></a>
<p class="post-excerpt">Some text that's longer</p>
Read More
</div>
</div>
<div class="column next-matches">
<div class="image-wrapper">
<img src="next_matches.jpg"/>
</div>
<div class="text-wrapper">
<p class="heading">List Heading</p>
<ul class="match-list">
<li class="match">some list item</li>
<li class="match">some list item</li>
</ul>
</div>
</div>
</main>
And the basic CSS:
.content {
display: flex;
flex-wrap: nowrap;
}
.column {
display: flex;
flex-direction: column;
margin: 60px 20px;
min-height: 100px;
width: 33%;
}
.image-wrapper {
width: 100%;
height: 260px;
overflow: hidden;
}
.image-wrapper img {
width: 100%;
height: auto;
}
.post-excerpt {
margin-bottom: 20px;
}
.button {
display: block;
float: left;
margin-bottom: 20px;
padding: 10px;
}
.match {
font-size: 1em;
padding-top: 15px;
}
Here's my code on JSFiddle: https://jsfiddle.net/4h0g73sp/5/
As you will see, the columns will take the height of the one with the most text in it. What I want is them to adjust to their content, so that they have different heights if necessary.
I don't know if it's noteworthy, that I'm working with Wordpress, so these columns are originally inside of some php. If that's important I'm happy to share the code with you, so let me know :)
The issue is because align-items on the flex container defaults to stretch so the flex items takes the height (rows) of the tallest item.
Simply set it to flex-start
.content {
display: flex;
align-items: flex-start;
}
https://jsfiddle.net/4h0g73sp/9/
I have also removed flex-wrap from .content because the default is no wrap so there is no need to declare it
Check https://css-tricks.com/snippets/css/a-guide-to-flexbox/ it's very useful and demonstrates flex fairly well