Prevent flex item from collapsing - html

How can I fix the title and make sure it will display?
.cont {
display: flex;
flex-direction: column;
width: 250px;
height: 150px;
border: 1px solid red;
overflow: hidden;
}
.title {
display: flex;
background: #ccc;
justify-content: space-between;
}
<div class="cont">
<div class="title">
<div>title that collapse</div>
<div>28/3/2018</div>
</div>
<div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ab sapiente provident iure ipsa ipsum nulla distinctio vero nisi officiis, id accusantium perferendis enim quisquam. Optio est deserunt atque, explicabo enim excepturi aliquid expedita rerum
voluptates similique ipsam vitae sapiente sequi at earum.
</div>
</div>
https://codepen.io/anon/pen/xWYZEK

Here are two options for solving this problem: min-height or flex-shrink.
Use min-height
Instead of height: 150px use min-height: 150px.
Since you've forced the box to have a fixed height, the content inside will overflow when necessary. With min-height, the box can expand to accommodate content.
.cont {
display: flex;
flex-direction: column;
width: 250px;
min-height: 150px; /* adjustment */
border: 1px solid red;
overflow: hidden;
}
.title {
display: flex;
background: #ccc;
justify-content: space-between;
}
<div class="cont">
<div class="title">
<div>title that collapse</div>
<div>28/3/2018</div>
</div>
<div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ab sapiente provident iure ipsa ipsum nulla distinctio vero nisi officiis, id accusantium perferendis enim quisquam. Optio est deserunt atque, explicabo enim excepturi aliquid expedita rerum
voluptates similique ipsam vitae sapiente sequi at earum.
</div>
</div>
Disable flex-shrink
An initial setting of a flex container is flex-shrink: 1. This means that flex items are permitted to shrink in order to avoid overflowing the container.
Since you have a fixed height on the container, the title is shrinking so that all items can fit inside.
Therefore, an alternative to the min-height option is to disable flex-shrink.
.cont {
display: flex;
flex-direction: column;
width: 250px;
height: 150px; /* back to original */
border: 1px solid red;
overflow: hidden;
}
.title {
flex-shrink: 0; /* new */
display: flex;
background: #ccc;
justify-content: space-between;
}
<div class="cont">
<div class="title">
<div>title that collapse</div>
<div>28/3/2018</div>
</div>
<div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ab sapiente provident iure ipsa ipsum nulla distinctio vero nisi officiis, id accusantium perferendis enim quisquam. Optio est deserunt atque, explicabo enim excepturi aliquid expedita rerum
voluptates similique ipsam vitae sapiente sequi at earum.
</div>
</div>

Related

Can I make the height of an element grow dynamically once a column flexbox container starts to overflow?

I am trying to make a 3 column layout for a part of an interface. I am using flexbox to lay it out since I can use flex-direction: column and let it keep moving to the right. Each content box is a width of 30%, which will ideally give me the 3 columns I am looking for. The problem I am having is when the content grows too big for the container vertically, it begins to overflow in the x-direction.
See this codepen for an example of what I am trying to avoid: https://codepen.io/jtris1/pen/RwBZPNp
I tried setting the min-height of the content container to 100%, but this results in it creating a single column down the container instead of the 3 I need. I also tried setting max-width on the container to see if it would force the content down, but the content still overflows in the x-direction.
HTML
<div className='guideline-detail-overlay-container'>
<div className="overlay-info">
<h2>Title</h2>
</div>
<div className='overlay-flex-content'>
<div className="overlay-demographics overlay-content-box">
<h3>Demographics</h3>
...
</div>
<div className="overlay-med-history overlay-content-box">
<h3>Medical History</h3>
...
</div>
<div className="overlay-family-history overlay-content-box">
<h3>Family History</h3>
...
</div>
<div className="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
...
</div>
</div>
</div>
CSS
.guideline-detail-overlay-container {
/* display: none; */
position: absolute;
background-color: #ffffff;
border: 1px solid black;
width: 100%;
height: 100%;
padding: 2rem 4rem;
box-sizing: border-box;
}
.overlay-flex-content {
min-height: auto;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
}
.overlay-content-box {
width: 30%;
height: auto;
margin-bottom: 1.5rem;
}
.overlay-info {
width: 100%;
height: auto;
text-align: center;
margin-bottom: 2rem;
}
Notice that when you use a 100% height you're assign the same height that the div (guideline-detail-overlay-container) parent. Change the height to auto should fix your problem. You also should delete height property.
guideline-detail-overlay-container {
height : auto;
}
While Flexbox Layout is an easy way to define grids, sometimes it is even easier to use the 'older' CSS columns mechanism to flow a text.
Define a parent columns container (.guideline-detail-overlay-container)
Define the preferred maximum the number of columns it should hold.
Define the preferred width of a single column. This value will resize with the width of the viewport and serves as a trigger to wrap content elements and to calculate whether there is enough room given the number of columns required.
I have disabled all Flexbox Layout related properties in your CSS code, inserted columns layout alternatives and commented the CSS and left the original code in-place for comparison.
snippet
/* * { outline: 1px dashed } /* for debugging */
/* Set for all elements */
*, ::before, ::after { box-sizing: border-box }
.guideline-container {
/* position: relative; /* obsolete, has no use */
/* min-height: 70vh; /* obsolete, content will stretch the parent */
/* justify-content: center; /* obsolete, not a flexbox container */
}
.guideline-detail-overlay-container {
/* display: none; */
/* position: absolute; /* obsolete, has no use */
padding: 2rem 4rem;
background-color: #ffffff;
border: 1px solid black;
/* obsolete, stretching is automatic
width : 100%;
height: 100%;
*/
}
.overlay-flex-content {
/* Set default required columns and column width (wrap trigger) */
columns: 3 250px;
/* 'columns' mechanism will differentiate on viewport resize */
/* height: 100%; /* obsolete, stretched by content */
/*
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
*/
}
.overlay-content-box {
/*
width: 30%;
height: auto;
margin-bottom: 1.5rem;
*/
}
.overlay-content-box:first-child>h3 {
/* To remove jagged top of first item in first column */
/* A well know issue */
margin-top: 0;
}
.overlay-info {
column-span: all;
/* width: 100%; /* obsolete, handled by 'column-span' */
/* height: auto; /* obsolete, HTML default */
text-align: center;
margin-bottom: 2rem;
}
<div class='guideline-container'>
<div class='guideline-detail-overlay-container'>
<div class="overlay-info">
<h2>Title</h2>
</div>
<div class='overlay-flex-content'>
<div class="overlay-demographics overlay-content-box">
<h3>Demographics</h3>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis possimus iure deserunt explicabo quasi quis aliquam vitae reiciendis nisi dignissimos obcaecati animi iusto cumque eum a, voluptate nesciunt assumenda corrupti?
</div>
<div class="overlay-med-history overlay-content-box">
<h3>Medical History</h3>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Odio, dolorem reprehenderit. Saepe sunt, aut ratione quasi quaerat voluptatum, iste, fugiat autem dolor velit exercitationem blanditiis nisi. Et in odio ducimus.
</div>
<div class="overlay-family-history overlay-content-box">
<h3>Family History</h3>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Aut, neque cumque voluptatem adipisci nemo harum, hic dolorem accusamus, pariatur debitis velit est rem doloremque. Laboriosam ad corrupti sit excepturi suscipit.
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
</div>
</div>
</div>

HTML Flex. Center elements

I need to center the elements of the column (like this:)
My actual code:
.education_flexColumn {
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
flex: 1;
flex-grow: 1;
}
.education_flexRow {
display: flex;
flex: 1;
justify-content: center;
align-items: stretch;
flex-grow: 1;
}
.divfh {
display: flex;
align-self: center;
flex: 1;
justify-content: center;
height: 100%;
flex-grow: 1;
}
<main>
<div class="education_flexRow">
<div class="education_flexColumn">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat voluptatum accusamus ipsum nulla, facere perspiciatis, quos quod, sint atque illum maiores iure. Excepturi harum quisquam iste ut esse ea non.
</div>
<div class="education_flexColumn">
<div class="divfh">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt id at dolore veniam illo ut et hic incidunt sit nostrum, laboriosam ipsa porro recusandae facere dolores. Laudantium amet placeat molestiae!</div>
<div class="divfh">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus, voluptatem reprehenderit explicabo magnam totam animi neque aliquam? Iure, minima aut ducimus placeat aliquam mollitia ad tempora rerum ex commodi sunt!</div>
</div>
</div>
</main>
On my actual result, the 2nd column content is not centered align.
You need an align-items: center; on the .divfh. The align-self does nothing since you want to align the children:
html, body, main, .education_flexRow {
height: 100%;
}
.education_flexColumn {
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
border-right: 1px solid black;
}
.education_flexColumn > .divfh:first-child {
border-bottom: 1px solid red;
}
.education_flexRow {
display: flex;
justify-content: center;
align-items: stretch;
border: 1px solid black;
}
.divfh {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
<main>
<div class="education_flexRow">
<div class="education_flexColumn">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat voluptatum accusamus ipsum nulla, facere perspiciatis, quos quod, sint atque illum maiores iure. Excepturi harum quisquam iste ut esse ea non.
</div>
<div class="education_flexColumn">
<div class="divfh">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt id at dolore veniam illo ut et hic incidunt sit nostrum, laboriosam ipsa porro recusandae facere dolores. Laudantium amet placeat molestiae!</div>
<div class="divfh">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus, voluptatem reprehenderit explicabo magnam totam animi neque aliquam? Iure, minima aut ducimus placeat aliquam mollitia ad tempora rerum ex commodi sunt!</div>
</div>
</div>
</main>
If you don't mind the opportunity to slightly change your approach, I would suggest you to use the grid template layout.
Your current layout has problems when reducing the box contents and they don't align correctly anymore if their width didn't grow because of their content too small to fit the whole remaining horizontal space.
In this demo I just flattened the html structure so that the second column doesn't need anymore to be its own parent element and gave to the first .area item the class wide so that it will span 2 rows.
The content of the boxes will be centered both on the horizontal and vertical axes because of display: flex.
.areas {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 4rem; /*row min height*/
grid-gap: 5px; /*distance between boxes*/
}
.wide {
grid-row: span 2;
}
.area {
outline: solid 1px lightgray;
display: flex;
align-items: center; /*content centered vertically*/
justify-content: center; /*content centered horizontally*/
}
<main>
<div class="areas">
<div class="area wide">Lorem, ipsum dolor sit ame...</div>
<div class="area">Lorem ipsum dolotiae!...</div>
<div class="area">Lorem ipsum ex commodi sunt!...</div>
</div>
</main>

CSS Layout with a container and two sidebars

I've tried to achieve this layout with CSS, however, the sidebars (in red) expand whenever there is content in them. I would like if the sidebars maintained a constant width regardless of the content inside of them.
.wrapper {
display: flex;
}
.container {
flex-grow: 1;
max-width: 1024px;
width: 100%;
}
.sidebar {
flex-grow: 1;
min-width: fit-content;
}
You can also use flex-basis CSS property to set the width of each section as the code below:
.wrapper {
display: flex;
}
.container {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 70%;
border: 1px solid #000;
}
.sidebar {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 15%;
border: 1px solid red;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>css flex</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<section class="sidebar">side-left Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad aliquid dicta facere fuga harum incidunt maiores molestiae necessitatibus nihil, nisi obcaecati odio odit quis quisquam rem reprehenderit totam vel voluptatibus?</section>
<section class="container">container Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi debitis deserunt dolore eius enim eum exercitationem explicabo, fuga iusto nam nesciunt nostrum obcaecati odio odit quas rem sapiente suscipit tempora tenetur velit. Accusantium aliquam dignissimos ea eos laboriosam molestiae nemo nobis optio pariatur, rerum tenetur.</section>
<section class="sidebar">side-right Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab assumenda consectetur dignissimos eos et hic in ipsa ipsum nam officiis perferendis qui, quisquam recusandae, repellendus, repudiandae rerum tempore. Asperiores at atque consectetur cum, deleniti deserunt dolorum error exercitationem fuga harum illum ipsam nisi nostrum omnis placeat provident, quaerat, qui quos sed sequi temporibus velit voluptatum?</section>
</div>
</body>
</html>
Here is an example of a basic CSS layout with a container element and two sidebars:
HTML:
<div class="container">
<div class="sidebar-left">
<!-- sidebar content goes here -->
</div>
<div class="main-content">
<!-- main content goes here -->
</div>
<div class="sidebar-right">
<!-- sidebar content goes here -->
</div>
</div>
CSS:
.container {
display: flex;
}
.sidebar-left {
width: 20%;
background-color: #ccc;
}
.main-content {
width: 60%;
background-color: #fff;
}
.sidebar-right {
width: 20%;
background-color: #ccc;
}```
flex-basis is the way.
.wrapper {
display: flex;
}
.container {
flex-shrink: 1;
flex-grow: 1;
flex-basis: 1024px;
}
.sidebar {
flex-shrink: 1;
flex-grow: 1;
flex-basis: calc((100vw - 1024px)/2);
}
Replacing 1024px with whatever you desire the container width to be.

wrong Flexbox size

I want to make 3 blocks with flex.
the first must occupy an entire column and the other 2 must be arranged in the second column with 50% of the height each. the first of the second column being an image, I would like the third, which contains only text, to be the same height.
unfortunately, even if this text block seems to have the same size as my image, the size of the 1st column is limited to the end of the text in this block.
.superposition {
display: flex;
width: 70%;
}
.block-orange {
background-color: #F26522;
color: white;
padding: 10px;
flex: 0 0 30%;
}
.superposition .flex-col {
display: flex;
flex-direction: column;
}
.superposition div div {
flex: 0 0 50%;
}
.bg-white {
background-color: yellow;
color: #627188;
}
.bg-grey{
background-color: grey;
}
<section class="superposition">
<div class="block-orange">
<h2>bright ass orange</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque sunt possimus tenetur porro aliquam, tempora itaque aperiam perspiciatis reiciendis dignissimos assumenda odit incidunt sit voluptatem quae laudantium. Accusamus, cum at?</p>
</div>
<div class="flex-col">
<div class="bg-grey">
<img src="img/header-soleil.png" alt="couché de soleil">
</div>
<div class="bg-white">
<h2>finaly a layout that blows your head off</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.Totam quod excepturi laboriosam vero numquam tenetur corporis iusto magni quaerat eaque dolore, assumenda unde est nostrum saepe fugiat nam doloremque esse.
</p>
</div>
</div>
</section>
why the first column (block-orange) does not adapt in height to the second column?
It seems that you want a grid in which:
Content can expand column height.
Column heights always match.
Two rows are each 50% column height.
This sounds like a "two-dimensional" layout, controlled by both row and column.
Building such layouts with flexbox will likely be a struggle and/or produce fragile layouts.
For reference, see Relationship of grid layout to other layout methods:
do I only need to control the layout by row or column – use a flexbox
do I need to control the layout by row and column – use a grid
Also see Equal height rows in CSS Grid Layout.
I recommend a grid layout instead.
Here's a demonstration:
body {
margin: 0;
}
.superposition {
display: grid;
grid-template-columns: 30% 40%;
grid-auto-rows: 1fr;
}
.block-orange {
background-color: orange;
grid-row: 1/3;
}
.bg-grey {
background-color: grey;
}
.bg-white {
background-color: yellow;
}
.bg-grey img {
width: 100%;
height: 100%;
object-fit: cover;
}
.cellpad {
box-sizing: border-box;
padding: 1em;
}
<section class="superposition">
<div class="block-orange cellpad">
<h2>bright orange</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque sunt possimus tenetur porro aliquam, tempora itaque aperiam perspiciatis reiciendis dignissimos assumenda odit incidunt sit voluptatem quae laudantium. Accusamus, cum at?</p>
</div>
<div class="bg-grey">
<img src="https://fakeimg.pl/440x320/282828/eae0d0/" alt="">
</div>
<div class="bg-white cellpad">
<h2>finally a layout that blows your mind</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.Totam quod excepturi laboriosam vero numquam tenetur corporis iusto magni quaerat eaque dolore, assumenda unde est nostrum saepe fugiat nam doloremque esse.
</p>
</div>
</section>
To achieve the result you are looking for, one approach would be to apply
flex-direction: column;
to the entire .superposition parent <div>, in combination with:
flex-wrap: wrap;
which will ensure that if .block-orange occupies 100% of the height of .superposition, then .bg-grey will follow it by starting at the top of .superposition, to the right of .block-orange.
i.e. The divs are still wrapping but they are wrapping horizontally, rather than wrapping vertically.
Working Example:
.superposition {
display: flex;
flex-direction: column;
flex-wrap: wrap;
width: 70%;
height: 500px;
}
.block-orange {
flex: 1 0 100%;
width: 30%;
padding: 0 10px;
color: white;
background-color: #F26522;
}
.bg-grey,
.bg-white {
flex: 0 0 50%;
width: 70%;
padding: 0 10px;
}
.bg-white {
color: #627188;
background-color: yellow;
}
.bg-grey {
background-color: grey;
}
<section class="superposition">
<div class="block-orange">
<h2>bright ass orange</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque sunt possimus tenetur porro aliquam, tempora itaque aperiam perspiciatis reiciendis dignissimos assumenda odit incidunt sit voluptatem quae laudantium. Accusamus, cum at?</p>
</div>
<div class="bg-grey">
<img src="img/header-soleil.png" alt="couché de soleil">
</div>
<div class="bg-white">
<h2>Finally, a layout that blows your head off</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.Totam quod excepturi laboriosam vero numquam tenetur corporis iusto magni quaerat eaque dolore, assumenda unde est nostrum saepe fugiat nam doloremque esse.</p>
</div>
</section>

How to place elements like this

I tried to set to parent block this:
display: flex;
flex-direction: column;
flex-wrap: wrap;
max-height: 500px;
and it works, but it will break if, as example, the second block will be larger. So I am looking for another decision. (I cannot use css-grid)
Codepen Link
Image how it might looks
Try this
.bigger {
height: 300px !important;
}
.smaller {
height: 99px !important;
margin-bottom: 2px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<div class="container-fluid">
<div class="row main">
<div class="col-sm-6">
<div class="col-sm-12"><p>
1 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error nihil consequuntur itaque placeat est voluptates, temporibus iste nesciunt dignissimos excepturi.
</p> </div>
<div class="col-sm-12">
<p>
2 Lorem ipsum dolor sit amet consectetur adipisicing elit. Sint optio laudantium dolorem doloremque non praesentium sit. Corporis quisquam soluta, ullam unde deleniti at expedita facilis animi dicta facere quam iste!
</p>
</div>
</div>
<div class="col-sm-6">
<p>
3 Lorem ipsum dolor sit amet consectetur adipisicing elit. Ullam voluptas maxime eos labore, sed, temporibus tenetur a sunt ratione voluptates excepturi est eaque? Porro, nostrum impedit laborum minima quos at nulla sit officiis. Dignissimos, at. Ipsam delectus dicta ipsum cupiditate. Ullam voluptas maxime eos labore, sed, temporibus tenetur a sunt ratione voluptates excepturi est eaque? Porro, nostrum impedit laborum minima quos at nulla sit officiis. Dignissimos, at. Ipsam delectus dicta ipsum cupiditate.
</p>
</div>
</div>
</div>
Please have a look at the below working example below, hope it helps :)
body .child {
font-family: sans-serif;
}
.parent {
display: flex;
flex-direction: row;
flex-wrap: wrap;
max-width: 90%;
margin: 0 auto;
}
.child {
margin: 10px;
padding: 50px 20px;
min-width: 39%;
box-shadow: inset 0 0 0 2px #000;
}
.parent {
max-height: none;
}
.child--1 {
order: 1;
}
.child--2 {
order: 3;
}
.child--3 {
order: 2;
padding: 100px 20px;
}
#media screen and (max-width: 500px) {
.parent {
flex-direction: column;
}
.child {
min-width: auto;
}
}
<div class="parent">
<div class="child child--1">Child 1</div>
<div class="child child--2">Child 2</div>
<div class="child child--3">Child 3</div>
</div>