I am having a problem while trying to use Flexbox for some markup.
I have made this codepen to demonstrate my problem: http://codepen.io/anon/pen/QjbzJY?editors=110
I want the image in the left(.item1 - The multicolor image) to dictate the width of the right box (.item2 - That contains alle the .itemitem$).
But as it is now the right box gets wider and presses the left one.
Hope one of you can help me get to a solution! :)
The Code:
<div class="parent">
<div class="item1">
<img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg" alt="" />
</div>
<div class="item2">
<div class="itemitem1 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem2 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem3 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem4 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem5 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem6 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem7 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem8 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
<div class="itemitem9 item">
<div class="innerimg"><img src="http://hartcornstudios.com/wp-content/uploads/2014/01/1280x900textPlaceholder-Image.gif" alt="" /></div>
<div class="innertext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque non lorem ante. Curabitur fermentum ligula vel quam fermentum, eget suscipit mauris dictum.</div>
</div>
</div>
</div>
CSS:
* {
border: 0;
padding: 0;
margin: 0;
cursor: pointer;
}
body {
background: black;
}
img {
max-wifth: 100%;
max-height: 100%;
}
.parent {
width: 930px;
height: 180px;
display: -webkit-flex;
align-items: stretch;
background-color: gray;
}
.item1 {
display: -webkit-flex;
align-items: center;
-webkit-justify-content: space-around;
-webkit-flex-grow: 0;
box-sizing: border-box;
overflow: hidden;
}
.item2 {
display: -webkit-flex;
-webkit-align-items: center;
-webkit-flex-grow: 0;
box-sizing: border-box;
overflow: hidden;
-webkit-flex-wrap: wrap;
-webkit-justify-content: space-around;
-webkit-align-content: space-between;
max-width: 100%;
background: red;
height: 100%;
}
.item {
width: 49%;
height: 100%;
background-color: #fff;
margin-bottom: 5px;
box-sizing: border-box;
display: -webkit-flex;
background: green;
}
.innerimg {
width: 50%;
height: 100%;
}
.innertext {
width: 50%;
height: 100%;
}
.innerimg img {
max-width: 100%;
max-height: 100%;
}
It seems like flex-shrink: 0; on the box i don't want to shrink does the trick!
Related
My application has an element that contains two range items (title and description). I would like the title width to be larger compared to the description width, but if the title is too large, limit it so that the description also appears.
My solution (full code here)
.info {
display: grid;
grid-template-columns: fit-content(50%) 1fr;
overflow: hidden;
margin-bottom: 20px
}
but this solution have a problem with the case where the title is long and the description is too short.
It works differently in my application. (code here)
If there is no description, the title width is still 50%.
How to implement this, maybe there is an easier way?
.container {
max-width: 500px;
margin: 0 auto;
}
.info {
display: flex;
align-items: left;
text-align: left;
}
.title {
overflow: hidden;
font-size: 16px;
font-weight: 700;
white-space: nowrap;
text-overflow: ellipsis;
max-width: 50%;
margin: 0 5px 0 0;
}
.description {
color: #67697c;
overflow: hidden;
font-size: 16px;
white-space: nowrap;
text-overflow: ellipsis;
}
<div class="container">
<section class="info">
<div class="title">
short title
</div>
<div class="description">
shor description
</div>
</section>
<section class="info">
<div class="title">
short title
</div>
<div class="description">
long description - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec quam nulla. In eu dui faucibus, euismod urna eu, consequat elit.
</div>
</section>
<section class="info">
<div class="title">
long title - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec quam nulla. In eu dui faucibus, euismod urna eu, consequat elit.
</div>
<div class="description">
short description
</div>
</section>
<section class="info">
<div class="title">
long title - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec quam nulla. In eu dui faucibus, euismod urna eu, consequat elit.
</div>
<div class="description">
long description - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec quam nulla. In eu dui faucibus, euismod urna eu, consequat elit.
</div>
</section>
<div class="title">
long title without description - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec quam nulla. In eu dui faucibus, euismod urna eu, consequat elit.
</div>
</section>
</div>
I have added flex in the parent div and I gave 50% max-width for title.
I am struggling with the setting three responsive columns in a row using Bootstrap 4.
.buttons {
border: 1px solid #e86225;
color: #e86225 !important;
padding: 10px 20px;
font-size: 14px;
}
.buttons:hover {
border: 1px solid #ffffff;
background-color: #e86225;
color: #ffffff !important;
transition: background-color 1s, border 1s, color 1s;
}
.container-custom {
padding-top: 80px;
padding-bottom: 80px;
}
.custom-link {
padding: 15px 0;
}
.container p {
margin: 25px 0;
max-width: 400px;
}
#media(max-width: 767px){
.container-custom {
padding-top: 50px;
padding-bottom: 50px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container">
<div class="row container-custom justify-content-center">
<h2>Blog</h2>
<div class='d-flex flex-wrap pt-5'>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHBMzMRL0g3ELio_DdCRvdajdF812AQPP2AbmM_jYr_66CnwgS" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQh3Afn3A1FKb24tpO6eAdqr8hCdnafjqwCNqgjdSAWK1igoWfk" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQFsb6TxovKdTNx-8gdo1R01nZXxRRauett2KQ_ci76VgjK2hR7" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
</div>
</div>
The problem is when media screen width is smaller than 767px, they are going to the left. I want to make image in the middle of column, the beginning of the text should be at the beginning of the imaeg and the end of text at the end of image, button should be at the beginning of the image too like here:
enter image description here
Just add width:100% to your images so they take all the space on the column
.buttons {
border: 1px solid #e86225;
color: #e86225 !important;
padding: 10px 20px;
font-size: 14px;
}
.buttons:hover {
border: 1px solid #ffffff;
background-color: #e86225;
color: #ffffff !important;
transition: background-color 1s, border 1s, color 1s;
}
.container-custom {
padding-top: 80px;
padding-bottom: 80px;
}
.custom-link {
padding: 15px 0;
}
.container p {
margin: 25px 0;
max-width: 400px;
}
img{
width:100%;
}
#media(max-width: 767px){
.container-custom {
padding-top: 50px;
padding-bottom: 50px;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container">
<div class="row container-custom justify-content-center">
<h2>Blog</h2>
<div class='d-flex flex-wrap pt-5'>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHBMzMRL0g3ELio_DdCRvdajdF812AQPP2AbmM_jYr_66CnwgS" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQh3Afn3A1FKb24tpO6eAdqr8hCdnafjqwCNqgjdSAWK1igoWfk" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQFsb6TxovKdTNx-8gdo1R01nZXxRRauett2KQ_ci76VgjK2hR7" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
</div>
</div>
The image width could be the issue. Add img { width: 100%; } and class="img-fluid" to your img tags.
Add to css:
img { width: 100%; }
HTML:
<img class="img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQh3Afn3A1FKb24tpO6eAdqr8hCdnafjqwCNqgjdSAWK1igoWfk" alt="">
or something similar so the images look like the layout in your attached image.
Example codepen using your code: https://codepen.io/brooksrelyt/pen/MZdLYj
I'm having trouble getting my images to resize fully.
Ideally I want the image to resize so that the option fits the screen, currently it only fits the width, why so? Is it possible to get the behavior I wish by just using css, maybe a media query is needed? Scorched over google trying to find a similar problem, but no luck!
Thanks in advance!
UPDATE:
To clarify, my problem is that the image is not resized, if the height of the window gets small enough - the header disappears and I get a scroll bar. I want all of the content to scale down, so that no scroll bar is needed.
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
background-color: aqua;
}
.content {
display: flex;
overflow-x: auto;
flex: 2 1 auto;
flex-wrap: nowrap;
}
.content::-webkit-scrollbar {
display: none;
}
.option {
display: flex;
flex-direction: column;
margin: auto 1em;
width: 90%;
height: 70%;
text-align: justify;
border: 3px solid black;
}
.img {
flex: 1;
}
.title {
flex: 0;
font-weight: bold;
text-align: center;
flex: 1;
margin: auto;
}
.desc {
flex: 0;
}
.header {
flex: 2;
text-align: center;
background-color: red;
}
.footer {
flex: 1;
background-color: brown;
}
<div class="container">
<div class="header">
Fusce pellentesque ante.
</div>
<div class="content">
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Proin hendrerit.</p>
<p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Nullam at.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Sed nec.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Morbi in.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Mauris ac.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
</div>
</div>
<div class="footer">
<p>Quisque venenatis volutpat dictum. Praesent.</p>
</div>
</div>
Fiddle
Hope this is what you want. If this is the right answer! Here is what i did.
give the container class of height and width a 100% and set display to flex and flex-direction: column so header, footer and content class get aligned vertically.
Now, if you set content class flex prorperty to 1 it will take rest of width and height.
give option class a property of display: flex and set flex-direction: column so image and the title and description get aligned vertically.
by assign min-height of 30px to the title and p in the options, rest of the space can be assigned to image, which I hope is what you want.
* {
margin: 0;
padding: 0;
}
.content {
min-height: 100%;
width: 100%;
min-width: 100%;
display: flex;
flex-direction: row;
box-sizing: border-box;
padding: 30px 0;
}
.content .option {
flex: 1;
max-height: 100vh;
min-width: 100%;
display: flex;
flex-direction: column;
}
.content .option img {
height: calc(100% - 160px);
width: 100%;
}
.content .option p {
height: 20px;
}
.content .option p:nth-child(2) {
height: 40px;
}
.header,
.footer {
position: fixed;
left: 0;
right: 0;
background-color: #ccc;
height: 30px;
}
.header {
top: 0;
}
.footer {
bottom: 0;
}
<div class="container">
<div class="header">
Fusce pellentesque ante.
</div>
<div class="content">
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Proin hendrerit.</p>
<p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Nullam at.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Sed nec.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Morbi in.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Mauris ac.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
</div>
</div>
<div class="footer">
<p>Quisque venenatis volutpat dictum. Praesent.</p>
</div>
</div>
html,
body {
padding: 0;
margin: 0;
}
.container {
height: 100vh;
display: flex;
flex-direction: column;
background-color: aqua;
justify-content: space-between;
}
.header,
.footer {
flex: 0 1 auto;
text-align: center;
}
.content {
display: flex;
overflow-x: auto;
flex-wrap: nowrap;
flex: 1 1 auto;
}
.footer {
background-color: brown;
}
img {
max-width: 600px;
}
<div class="container">
<div class="header">
Fusce pellentesque ante.
</div>
<div class="content">
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Proin hendrerit.</p>
<p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Nullam at.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Sed nec.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Morbi in.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
</div>
<div class="option">
<img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
<p class="title">Mauris ac.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
</div>
</div>
<div class="footer">
<p>Quisque venenatis volutpat dictum. Praesent.</p>
</div>
</div>
I am currently converting an old page away from using a <table> layout and have been trying to implement the behaviour using flexbox. The aim is to have 3 columns, the first two centred horizontally and vertically, with the last column containing a lot of text, which is displaying ok.
I am trying to use a media query to wrap the third column under the first two when the browser width is small as follows:
The code snippet below achieves the correct layout but will obviously fail on small widths and just produce a single column:
I am guessing this needs flex-wrap somehow?
Are multiple rows also possible with a single container row? Or is it best to have one container per row?
.row {
display: flex;
justify-content: center;
padding: 5px;
margin: 2px;
border: 1px solid #000;
font-size: 0.7em;
}
.col1, .col2, .col3 {
background-color: #999;
padding: 8px;
margin: 2px;
}
.col1, .col2 {
flex: 1;
/* Horizontal centring */
text-align: center;
/* Add vertical centring */
display: flex;
flex-direction: column;
justify-content:center;
}
.col3 {
flex: 3;
background-color:#fff;
}
#media(max-width: 400px){
.row {display: block;}
}
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
You can get rid of the row element with some tricks, specially if you know beforehand the width of the col1 and col2 elements. Notice the width of the col3 element is quite artificial:
.row {
display: flex;
flex-wrap: wrap;
width: 95%;
border: solid 1px red;
}
.col1,
.col2 {
background-color: lightblue;
padding: 10px;
background-clip: content-box;
}
.col3 {
flex-basis: calc(100% - 194px);
padding: 5px;
}
.col1,
.col2,
.col3 {
margin: 10px 0px;
}
.col1 {
border: solid 1px black;
border-right-width: 0px;
margin-left: 10px;
}
.col2 {
border: solid 1px black;
border-left-width: 0px;
border-right-width: 0px;
}
.col3 {
border: solid 1px black;
border-left-width: 0px;
}
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
Also, a solution for the normal layout, including 40% flex-basis for col1 and col2. They should be 50%, but since there are some margins and paddings, it's better to be on the low side and the the flex-grow compensate it
.row {
display: flex;
justify-content: center;
padding: 5px;
margin: 2px;
border: 1px solid #000;
font-size: 0.7em;
}
.col1,
.col2,
.col3 {
background-color: #999;
padding: 8px;
margin: 2px;
}
.col1,
.col2 {
flex: 1;
/* Horizontal centring */
text-align: center;
/* Add vertical centring */
display: flex;
flex-direction: column;
justify-content: center;
}
.col3 {
flex: 3;
background-color: #fff;
}
#media(max-width: 400px) {
.row {
flex-wrap: wrap;
}
.col1,
.col2 {
flex-basis: 40%;
}
.col3 {
flex-basis: 80%;
}
}
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
You would give your .row definition flex-wrap: wrap; and set a min-width value for column 3. When it gets below that width it will drop down as you have indicated above.
I was wondering if the layout I draw on the following image is possible using flexbox and this HTML structure:
<div class="box">
<img src="url..." alt="" />
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>
It would be great if this could be done with flexbox and without adding more boxes inside.
For fixed image width and height, it's possible. The main idea is in the following snippet.
.box {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: space-between;
height: 300px;
}
.img {
width: 300px; height: 300px;
}
h5, p {
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}
Since the height of the parent is the same as the height of the image, the content overflows and gets wrapped to the right. Then we have to manually set the width because it's otherwise going to 100% of the parent.
.box {
background-color: #ddd;
padding: 16px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
align-content: space-between;
}
.img {
background-color: #333;
color: #ddd;
width: 300px;
height: 300px;
}
h5, p {
padding: 0;
margin: 0;
background-color: #ccc;
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}
h5 {
font-size: 18px;
margin-bottom: 8px;
}
.box:nth-child(even) .img {
order: 3;
}
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus</p>
</div>
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a </p>
</div>
Full-height paragraph
You might want to extend your paragraph all the way to the bottom (maybe add some links there). It's easy to extend it so you can turn p into flexbox to snap something to the bottom, for example (this example not in the demo below, you can only notice this by the gray background).
.box {
/* ... */
justify-content: space-between;
}
p {
flex-grow: 1;
}
.box {
background-color: #ddd;
padding: 16px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
align-content: space-between;
justify-content: space-between;
}
.img {
background-color: #333;
color: #ddd;
width: 300px;
height: 300px;
}
h5, p {
padding: 0;
margin: 0;
background-color: #ccc;
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}
h5 {
font-size: 18px;
margin-bottom: 8px;
}
p {
flex-grow: 1;
}
.box:nth-child(even) .img {
order: 3;
}
<div class="box">
<div class="img"><img></div>
<h5>Version 2</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus</p>
</div>
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. </p>
</div>
Both versions above breaks when the text cannot fit (try changing browser window size).
Doesn't show all, but never breaks
You can set the max-height for the paragraph if you don't want it to break and clip the text which cannot fit with overflow: hidden.
p {
max-height: 250px;
overflow: hidden;
}
.box {
background-color: #ddd;
padding: 16px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
align-content: space-between;
justify-content: space-between;
}
.img {
background-color: #333;
color: #ddd;
width: 300px;
height: 300px;
}
h5, p {
padding: 0;
margin: 0;
background-color: #ccc;
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}
h5 {
font-size: 18px;
margin-bottom: 8px;
}
p {
flex-grow: 1;
}
.box:nth-child(even) .img {
order: 3;
}
p {
max-height: 250px;
overflow: hidden;
}
<div class="box">
<div class="img"><img></div>
<h5>Version 3</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus</p>
</div>
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>
<div class="box">
<div class="img"><img></div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. </p>
</div>