Is it possible to have one div element overflow from the bottom of the first column to the top of the second? The current behaviour is that if the div is too large, it is placed wholly at the top of the second column (see picture below).
I would instead like to have the class-alchemist div begin under the background-acolyte div and then begin at the top of the second column when it becomes too large for the first column, so maybe advanced alchemy would be the first section to display at the top of column two?
I'm only using HTML and CSS currently so non-JavaScript answers would be ideal. However, I can use JS if necessary.
Currently the CSS I'm using for the div that hold's the columns is as below:
display: flex;
flex-flow: column wrap;
height: 1080px;
Because boxes in flex-box are entirely contained in the row, or column, in which they're placed they cannot 'flow' across to a new row, or column. Given your requirements here, I'd suggest using CSS multi-col layout, as follows:
/* CSS custom property for the number of columns: */
:root {
--columnCount: 3;
}
/* simple CSS reset to remove margins, and padding and
setting common styles on fonts and box-sizing: */
*,
::before,
::after {
box-sizing: border-box;
font-size: 16px;
font-weight: 400;
line-height: 1.5;
margin: 0;
padding: 0;
}
/* the wrapping element containing the multiple-column element: */
main {
border: 1px solid #000;
margin-block: 1em;
margin-inline: auto;
padding: 0.5em;
/* specifying a preferred widht of 60viewport-width units, but
limiting maximum width to 1000px and minimum width to
20em: */
width: clamp(20em, 60vw, 1000px);
}
section {
/* columns property triggers multi-col layout, with
the number of specified columns, retrieved from
the --columnCount CSS custom property: */
columns: var(--columnCount);
/* as in grid and flex-box this defines the width
between adjacent columns; in multi-col there's
no 'row-gap' equivalent: */
gap: 0.5em;
}
h3 {
background-color: #030;
color: #fff;
/* using CSS logical properties to specify a margin
on the block-end edge of the element (the bottom
edge in left-to-right, top-to-bottom languages
such as English: */
margin-block-end: 1em;
text-indent: 0.5em;
}
article {
--borderColor: rebeccapurple;
/* the border is to show where each box is, in the
layout: */
border: 2px solid var(--borderColor);
/* forces the browser to render a complete border
around each 'block' of the element, even the
parts of it on different columns: */
box-decoration-break: clone;
}
article:nth-child(even) {
/* setting a vivid border-color for the even-numbered
<article> elements, in order to make the column-span
obvious: */
--borderColor: #f90;
}
article:not(:first-child) {
margin-block-start: 0.5em;
}
p {
margin-inline: auto;
width: 90%;
}
/* using #media to specify different column-counts at different
screen widths, by updating the --columnCount custom property: */
#media screen and (max-width: 1250px) {
:root {
--columnCount: 2;
}
}
#media screen and (max-width: 990px) {
:root {
--columnCount: 1;
}
}
<main>
<section>
<article>
<h3>Section 01</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Egestas maecenas pharetra convallis posuere. Sed enim ut sem viverra aliquet eget sit amet. Malesuada pellentesque elit eget gravida cum sociis. Ac orci phasellus egestas tellus rutrum tellus. Risus viverra adipiscing at in tellus integer feugiat scelerisque. Odio ut enim blandit volutpat maecenas volutpat blandit aliquam. Et malesuada fames ac turpis egestas maecenas pharetra convallis posuere. Orci dapibus ultrices in iaculis nunc. </p>
</article>
<article>
<h3>Section 02</h3>
<p>Orci phasellus egestas tellus rutrum tellus pellentesque. Venenatis a condimentum vitae sapien pellentesque habitant morbi tristique senectus. Erat nam at lectus urna duis convallis convallis tellus. Habitant morbi tristique senectus et netus et malesuada. Proin nibh nisl condimentum id. Luctus venenatis lectus magna fringilla urna porttitor rhoncus dolor. Vitae nunc sed velit dignissim sodales ut. Nunc vel risus commodo viverra maecenas accumsan. Purus viverra accumsan in nisl nisi. Sollicitudin tempor id eu nisl.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. In pellentesque massa placerat duis ultricies lacus sed. Interdum posuere lorem ipsum dolor sit. Proin sagittis nisl rhoncus mattis rhoncus. Tortor aliquam nulla facilisi cras. Malesuada fames ac turpis egestas integer eget aliquet. Massa enim nec dui nunc. Viverra nam libero justo laoreet. Aenean pharetra magna ac placerat vestibulum lectus mauris. Varius duis at consectetur lorem donec massa sapien faucibus et. Sed tempus urna et pharetra pharetra. Urna duis convallis convallis tellus id. Viverra tellus in hac habitasse.</p>
</article>
<article>
<h3>Section 03</h3>
<p>Facilisis volutpat est velit egestas dui id ornare. Phasellus faucibus scelerisque eleifend donec pretium vulputate sapien. Faucibus ornare suspendisse sed nisi lacus sed viverra. Urna cursus eget nunc scelerisque viverra mauris in. Malesuada fames ac turpis egestas integer eget. At urna condimentum mattis pellentesque. Phasellus faucibus scelerisque eleifend donec pretium. Nunc non blandit massa enim nec dui nunc mattis enim. Massa tincidunt dui ut ornare. Lorem sed risus ultricies tristique nulla aliquet enim. Et netus et malesuada fames ac turpis egestas maecenas pharetra. At lectus urna duis convallis convallis tellus. Ut tellus elementum sagittis vitae et leo duis ut. Odio ut enim blandit volutpat maecenas volutpat. Integer enim neque volutpat ac. Id eu nisl nunc mi ipsum faucibus. Sed vulputate mi sit amet mauris commodo quis imperdiet. Sit amet nulla facilisi morbi tempus.</p>
</article>
</section>
</main>
Reference:
border.
box-decoration-break.
box-sizing.
clamp().
columns.
CSS Custom Properties.
font-size.
font-weight.
gap.
line-height.
margin.
margin-block.
margin-inline.
#media.
:nth-child().
padding.
padding-block.
padding-inline.
:root.
text-indent.
var().
width.
Bibliography:
"Basic Concepts of Multicol," Mozilla Developer Network.
"When And How To Use CSS Multi-Column Layout," Rachel Andrew, Smashing Magazine.
Related
I've been trying to find a solution for quite a simple (on the first sight) task. There is a 2x2 css grid. And the first column is merged and there is an image inside. The other two rows are fluid and contain some text. The problem is that the image enlarges the height of the rows and takes up space for its original size, while I'm trying to make the text rows dictate the max-height for the image.
So now it looks like this:
But I'm trying to achieve something like this:
I've been trying max-height, height, object-fit, grid-auto-column etc properties on the image but nothing seems to work. Please advise.
Here is my code:
.container {
display: grid;
grid-template-columns: auto 1fr;
}
.aside {
grid-row: 1/3;
grid-column: 1/2;
background-color: pink;
}
.aside img {
display: block;
}
.row1 {
grid-row: 1/2;
grid-column: 2/3;
background-color: lightblue;
padding: 15px;
}
.row2 {
grid-row: 2/3;
grid-column: 2/3;
background-color: lightgreen;
padding: 15px;
}
<div class="container">
<div class="aside">
<img src="https://via.placeholder.com/350/" alt=""></div>
<div class="row1">Donec euismod nisi ligula, vitae imperdiet eros egestas quis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris ut sapien quis odio lobortis efficitur accumsan in massa. Pellentesque semper tempor consequat. Suspendisse potenti. Nullam condimentum, elit quis consectetur pulvinar, est augue fringilla libero, sed posuere dolor dolor vel ipsum. Pellentesque quis urna nunc. Maecenas quis posuere felis. Vestibulum id velit ante. Suspendisse ullamcorper tellus non velit facilisis, at molestie diam molestie. Nullam at efficitur sem. Vestibulum mollis massa id massa egestas hendrerit.</div>
<div class="row2">Mauris consequat tincidunt ullamcorper. Sed nec massa nec mi porttitor efficitur quis nec ex. Nullam et porta leo, vel tempor eros. Aenean turpis lacus, pharetra et tristique et, consequat commodo ligula. Sed in nisi in ipsum dignissim ullamcorper. Sed et ipsum vitae ante mollis interdum at ut velit. Suspendisse potenti.</div>
</div>
You can approximate it like below:
.container {
display: grid;
grid-template-columns: auto 1fr;
}
.aside {
grid-row: span 2;
background-color: pink;
/* added */
height:0;
min-height:100%;
/**/
}
.aside img {
display: block;
/* added */
width:100%;
height:100%;
object-fit:cover;
/* */
}
.row1 {
background-color: lightblue;
padding: 15px;
}
.row2 {
background-color: lightgreen;
padding: 15px;
}
<div class="container">
<div class="aside">
<img src="https://via.placeholder.com/350/" alt=""></div>
<div class="row1">Donec euismod nisi ligula, vitae imperdiet eros egestas quis. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris ut sapien quis odio lobortis efficitur accumsan in massa. Pellentesque semper tempor consequat. Suspendisse potenti. Nullam condimentum, elit quis consectetur pulvinar, est augue fringilla libero, sed posuere dolor dolor vel ipsum. Pellentesque quis urna nunc. Maecenas quis posuere felis. Vestibulum id velit ante. Suspendisse ullamcorper tellus non velit facilisis, at molestie diam molestie. Nullam at efficitur sem. Vestibulum mollis massa id massa egestas hendrerit.</div>
<div class="row2">Mauris consequat tincidunt ullamcorper. Sed nec massa nec mi porttitor efficitur quis nec ex. Nullam et porta leo, vel tempor eros. Aenean turpis lacus, pharetra et tristique et, consequat commodo ligula. Sed in nisi in ipsum dignissim ullamcorper. Sed et ipsum vitae ante mollis interdum at ut velit. Suspendisse potenti.</div>
</div>
Related question to understand the height:0;min-height:100%: How can you set the height of an outer div to always be equal to a particular inner div?
I don't think there's a way to do this with pure CSS.
Without the image, min-content would be the way to squeeze all extra space out of the row.
But since the image is content, min-content respects its size.
I think another way to ask your question would be:
"How do you get grid tracks to ignore the size of images?"
Not possible with pure CSS, as far as I can tell. Perhaps some absolute position sorcery.
Also, how is the browser supposed to know that you prefer the text height over the image height? The browser is setting heights based simply on the taller content.
.border {
margin-bottom: 20px;
border-radius: 3px;
border: 1px #d43f3a solid;
}
.header {
padding: 10px;
background: #f1f1f1;
border-bottom: 1px #ccc solid;
}
.content {
padding: 10px;
}
<div class="border">
<div class="header">
non erat euismod convallis vel id libero. Vivamus vel lectus hendrerit, sagittis diam eu, euismod dui. Curabitur quis leo et tellus pharetra tristique ac sit amet lacus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut non turpis risus. Duis vel scelerisque enim. Ut dapibus ut enim ac tempus. Sed sit amet tortor lorem. Vestibulum tincidunt est odio, eu volutpat lacus commodo at. Etiam dapibus nisl ut tempor ultricies. Donec rutrum facilisis purus at al
</div>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tincidunt ultrices venenatis. Nulla sed ex non erat euismod convallis vel id libero. Vivamus vel lectus hendrerit, sagittis diam eu, euismod dui. Curabitur quis leo et tellus pharetra tristique ac sit amet lacus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut non turpis risus. Duis vel scelerisque enim. Ut dapibus ut enim ac tempus. Sed sit amet tortor lorem. Vestibulum tincidunt est odio, eu volutpat lacus commodo at. Etiam dapibus nisl ut tempor ultricies. Donec rutrum facilisis purus at aliquet.
</div>
</div>
On first glance it looks perfectly normal, but when you keep zooming in (on my 4k screen at zoom level 150% in chrome) the right border next to the header div becomes different then the other ones.
I made a screenshot how it looks for me: https://imgur.com/mOvnFVQ.png
Now, this is fixable if I add overflow: hidden to the border class, but then any content that would overflow the div won't show (obviously). For example a custom made select that gets inserted via javascript
Is there a way to fix this? Without overflow: hidden
let try margin:0 1px for .header div
This code can fixed your problem but make sure that border width will be 1px fixed, if you want to change border-width you can remove border-width:thin to border:2px solid red; and so on.
.border {
margin-bottom: 20px;
border-radius: 3px;
border: 1px #d43f3a solid;
border-width: thin;
}
I want the background images to be horizontally on top of each other. So for instance:
PIC 1
PIC 2
PIC 3
Right now, the only solutions I'm seeing to this problem are to stack the photos because that's what most people are asking about, but I don't want to stack the photos. I just want them to be touching each other (or not, that's fine too) but not on top of each other (from a 3d point of view). The reason why I'm still using them as background images, in case you're wondering, is because I want there to be text on the images, as well as anything else I want to implement. Also, I like having the background-size as cover, but I need to change the formatting so that the position of the two images do not overlap.
Background image placement, sizing and cropping is a rather complex matter in CSS, for a very simple reason: to cover all possible cases.
Now, in theory, you could use a comma separated list of URL's, inside the background-image property and also specify the following properties (if needed) as lists of comma separated properties:
background-attachment
background-clip
background-image
background-origin
background-position
background-repeat
background-size
Needless to say, the first item in the list for each of the above applies to the first image, and so on...
For example:
body {
background-image: url("https://source.unsplash.com/random/800x600"), url("https://source.unsplash.com/random/600x800"), url("https://source.unsplash.com/random/700x700");
background-position: top center, center center, bottom center;
background-size: auto 33.33%, auto 33.33%, auto 33.33%;
background-repeat: repeat-x, repeat-y, repeat-x;
margin: 0;
min-height: 100vh;
}
If you find it more convenient, you can also use the list notation with the background shorthand property, as in:
body { background: url(https://source.unsplash.com/random/800x600) repeat-x top center /100% 33.33%, url(https://source.unsplash.com/random/600x800) repeat-y center center /100% 33.33%, url(https://source.unsplash.com/random/800x200) repeat-x bottom center /100% 33.33%;
margin: 0;
min-height: 100vh;
}
However, the real problem is you are requesting a more advanced logic, which requires background images to influence each other's position, sizing and cropping, pretty much like DOM element would.
Tough luck. That's not possible. Each element in the list of background-images is a separate layer which is rendered by itself, without regard to whether other background-image layers exist. The first one is rendered above the second one which is rendered above the third, etc...
If you think about it, considering the array of possible combinations, it's unrealistic to request the background property of a single element to handle all cases, considering responsiveness.
That's why the proper way to go here, instead of using the background property, is to create a child item in your element with the purpose of controlling the background display according to the design specs.
You want to give .parent (feel free to change the class name) position:relative and give <background-sizer> element position:absolute; width:100%; height: 100%; top:0; left:0;, making sure it covers the entire area of its closest positioned parent. Inside <background-sizer> you will probably want to add some dummy <span>s to help you control the background, eventually using #media queries, if you need them displayed differently on different devices.
Here's an example:
body {
margin: 0;
}
.parent {
position: relative;
padding: 2rem;
}
background-sizer {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
background-sizer > span {
flex: 1;
background-size: cover;
border: solid rgba(255,255,255,.35);
border-width: 1px 0;
}
background-sizer::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
background: rgba(0,0,0,.15);
top: 0;
height: 100%;
z-index: 1;
}
background-sizer > span:nth-child(1) {
background-image: url(https://source.unsplash.com/random/800x500);
}
background-sizer > span:nth-child(2) {
background-image: url(https://source.unsplash.com/random/500x800);
}
background-sizer > span:nth-child(3) {
background-image: url(https://source.unsplash.com/random/500x500);
}
.contents {
position: relative;
background-color: rgba(0,0,0,.42);
border: 1px solid white;
padding: 1rem;
box-sizing: border-box;
z-index: 1;
color: white;
}
.some-more-content {
padding: 0 2rem;
}
body > * {
margin: 0 auto;
max-width: 800px;
}
.contents {
opacity: 1;
transition: opacity .4s ease-in-out;
}
.contents:hover {
opacity: .21;
}
<div class="parent">
<background-sizer>
<span></span>
<span></span>
<span></span>
</background-sizer>
<div class="contents">
<h2>This is your element</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ultrices in iaculis nunc sed. Morbi blandit cursus risus at ultrices mi tempus imperdiet. Accumsan lacus vel facilisis volutpat est velit. Platea dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim. Sem nulla pharetra diam sit amet. Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Elementum nisi quis eleifend quam adipiscing. Id neque aliquam vestibulum morbi blandit cursus risus. At consectetur lorem donec massa sapien faucibus et molestie ac. Faucibus interdum posuere lorem ipsum dolor. Congue nisi vitae suscipit tellus mauris a. At quis risus sed vulputate odio. Massa tincidunt dui ut ornare lectus sit. Tincidunt nunc pulvinar sapien et ligula. Mauris augue neque gravida in fermentum et sollicitudin ac. Quisque id diam vel quam elementum. Parturient montes nascetur ridiculus mus mauris vitae ultricies leo. Lacus vestibulum sed arcu non odio euismod lacinia. Et ultrices neque ornare aenean euismod elementum nisi quis.
<p>Rutrum tellus pellentesque eu tincidunt tortor. Pellentesque massa placerat duis ultricies lacus sed turpis. Ornare aenean euismod elementum nisi quis eleifend quam adipiscing. Lectus quam id leo in vitae. Et malesuada fames ac turpis. Aliquam vestibulum morbi blandit cursus. Pellentesque habitant morbi tristique senectus et netus. Nunc lobortis mattis aliquam faucibus purus in massa tempor. Vitae semper quis lectus nulla at volutpat. Ac ut consequat semper viverra nam libero justo. Fusce ut placerat orci nulla pellentesque dignissim.
<p>Lacus sed turpis tincidunt id aliquet risus feugiat. Mauris rhoncus aenean vel elit scelerisque mauris pellentesque. Facilisis mauris sit amet massa vitae tortor condimentum lacinia. Sodales ut etiam sit amet nisl purus in mollis nunc. Habitant morbi tristique senectus et netus et. Tortor dignissim convallis aenean et tortor at. Arcu vitae elementum curabitur vitae. Eget nunc scelerisque viverra mauris in aliquam sem. Posuere morbi leo urna molestie at. Magna eget est lorem ipsum dolor. Integer vitae justo eget magna. A pellentesque sit amet porttitor eget dolor. Egestas diam in arcu cursus euismod. Libero enim sed faucibus turpis.
</div>
</div>
<div class="some-more-content">
<h2>And here's some more content</h2>
<p>Turpis cursus in hac habitasse platea dictumst quisque sagittis. In massa tempor nec feugiat nisl pretium fusce id velit. Neque volutpat ac tincidunt vitae semper quis. Sit amet porttitor eget dolor morbi non arcu risus. Ut venenatis tellus in metus vulputate eu scelerisque felis. Id eu nisl nunc mi ipsum faucibus vitae. Ut diam quam nulla porttitor massa. Nec feugiat in fermentum posuere urna nec tincidunt praesent semper. Commodo ullamcorper a lacus vestibulum. Et sollicitudin ac orci phasellus egestas tellus. Vulputate dignissim suspendisse in est ante in nibh mauris cursus. Pellentesque sit amet porttitor eget dolor. Sed cras ornare arcu dui.
<p>Id semper risus in hendrerit gravida rutrum quisque non. Amet luctus venenatis lectus magna fringilla. Adipiscing bibendum est ultricies integer quis auctor elit sed vulputate. Purus semper eget duis at tellus. Integer quis auctor elit sed vulputate mi sit amet mauris. Pulvinar mattis nunc sed blandit libero volutpat sed cras ornare. Faucibus turpis in eu mi bibendum neque. Ultrices neque ornare aenean euismod elementum nisi quis. Risus viverra adipiscing at in tellus integer feugiat. Tempor orci dapibus ultrices in iaculis nunc sed augue. Pharetra massa massa ultricies mi quis hendrerit. Tellus orci ac auctor augue mauris. Neque volutpat ac tincidunt vitae semper. Sit amet facilisis magna etiam tempor orci. Elementum facilisis leo vel fringilla est ullamcorper eget nulla. Nibh tortor id aliquet lectus. Blandit cursus risus at ultrices mi.
</div>
A few notes:
intended as proof of concept (copy/paste won't help you much); you should take the time to read what the properties do, in order to use it. If you don't, chances are you won't be able to adapt it to your needs.
most likely, you don't need any of the cosmetics (padding, margin, color, etc...)
i placed a darkening layer on the <background-sizer> to make sure the text remains readable, given the images are randomly generated. You probably want to that.
Here's a simple flex based blog layout:
<div class='Page'>
<div class='Container'>
<div class='Content'>
<h1>Hello</h1>
<p>Cras ac mauris purus. Phasellus at ligula condimentum, pretium nisi eget, aliquet enim. Sed at massa velit. Cras ac mi dolor. Nullam id felis sit amet neque tempus sodales. In ultricies et turpis in faucibus. Morbi fringilla metus pellentesque, varius enim a, dapibus ex. Sed aliquet urna nisi, eu fermentum diam pretium quis. Curabitur vel cursus turpis. Sed a varius leo, in viverra arcu. Donec porttitor, dolor vel laoreet iaculis, magna arcu tempus ex, at porttitor tellus nunc ultricies felis. Quisque congue sapien in quam tempor, non dapibus felis dignissim. Pellentesque ex eros, dignissim eget tortor non, aliquet ullamcorper nisi. Sed interdum non eros quis fringilla. Morbi condimentum tellus at blandit dignissim. Aenean metus elit, interdum et suscipit quis, ullamcorper sit amet risus.</p>
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sapien magna, lacinia sit amet quam sed, dignissim tincidunt neque. Duis sed sapien hendrerit, consectetur neque quis, tempor nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent fringilla enim odio, sit amet venenatis ex commodo in. Pellentesque in enim in libero vulputate fermentum. Suspendisse elementum felis neque, in rhoncus diam hendrerit eget. Cras tempor porta bibendum. Fusce eget tellus a enim euismod lobortis in vitae nibh. Duis ornare turpis non ex consectetur, sit amet malesuada elit feugiat.</pre>
</div>
</div>
</div>
With this CSS
.Page {
border: 1px solid blue;
}
.Container {
display: flex;
flex-direction: column;
align-items: center;
}
.Content {
border: 1px solid red;
padding: 10px;
max-width: 700px;
min-width: 0;
}
pre {
overflow: auto;
background: #f2f2f2;
border: 1px solid #ccc;
border-radius: 4px;
padding: 20px;
}
Working example: https://codepen.io/anon/pen/xdeyrY
When the browser width is >700px, the red Container is centered and the pre code block has an overflow scrollbar. But as soon as you resize the browser < 700px, the pre code block stretches the container to the full 700px and the content gets cut off.
Why is the width of the container not limited by the browser/screen width in this case?
If you remove align-items: center; everything works as expected. If you set white-space: normal on pre, it also works as expected. But neither of those is an option.
The only workaround I came up with is to add this media query:
#media only screen and (max-width: 700px) {
.Container {
align-items: initial;
}
}
This does the trick, but seems a bit like a hack. Is this some flexbox bug/edge case, or am I missing some min-width: 0 trick here? It seems like using flex + align-items:center + max-width + pre just doesn't work well together..
In addition to Michael_B's answer, if you need the flex column direction for i.e. multiple .Content elements, you can also simply set width: 100% on the .Content.
To adjust the width to your padding/border you can either use box-sizing: border-box;, which I did, or CSS Calc (width: calc(100% - 22px);)
Also, for the reason Michael gave, I removed the min-width: 0 as it has no effect
Updated codepen
Stack snippet
.Page {
border: 1px solid blue;
}
.Container {
display: flex;
flex-direction: column;
align-items: center;
}
.Content {
border: 1px solid red;
padding: 10px;
max-width: 700px;
width: 100%;
box-sizing: border-box;
}
pre {
overflow: auto;
background: #f2f2f2;
border: 1px solid #ccc;
border-radius: 4px;
padding: 20px;
}
<div class='Page'>
<div class='Container'>
<div class='Content'>
<h1>Hello</h1>
<p>Cras ac mauris purus. Phasellus at ligula condimentum, pretium nisi eget, aliquet enim. Sed at massa velit. Cras ac mi dolor. Nullam id felis sit amet neque tempus sodales. In ultricies et turpis in faucibus. Morbi fringilla metus pellentesque, varius enim a, dapibus ex. Sed aliquet urna nisi, eu fermentum diam pretium quis. Curabitur vel cursus turpis. Sed a varius leo, in viverra arcu. Donec porttitor, dolor vel laoreet iaculis, magna arcu tempus ex, at porttitor tellus nunc ultricies felis. Quisque congue sapien in quam tempor, non dapibus felis dignissim. Pellentesque ex eros, dignissim eget tortor non, aliquet ullamcorper nisi. Sed interdum non eros quis fringilla. Morbi condimentum tellus at blandit dignissim. Aenean metus elit, interdum et suscipit quis, ullamcorper sit amet risus.</p>
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sapien magna, lacinia sit amet quam sed, dignissim tincidunt neque. Duis sed sapien hendrerit, consectetur neque quis, tempor nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent fringilla enim odio, sit amet venenatis ex commodo in. Pellentesque in enim in libero vulputate fermentum. Suspendisse elementum felis neque, in rhoncus diam hendrerit eget. Cras tempor porta bibendum. Fusce eget tellus a enim euismod lobortis in vitae nibh. Duis ornare turpis non ex consectetur, sit amet malesuada elit feugiat.</pre>
</div>
</div>
</div>
It is indeed a min-width: 0 problem.
It's applied in your code, but the set-up is not quite right.
The min-width and min-height overrides work only in the direction of the main axis.
This means that the min-width: 0 override works only in flex-direction: row.
Similarly, the min-height: 0 fix applies only in flex-direction: column.
Your flex container is flex-direction: column. Your flex item has min-width: 0. Therefore, the override is having no effect.
Switch your container to row-direction. Since you're not applying flex properties to the content of the flex item, the switch won't change anything, except allow your <pre> tag to shrink.
You will also need to switch align-items: center to justify-content: center.
revised demo
More details here: Why doesn't flex item shrink past content size?
I am trying to create a layout with a header on top, underneath it a sidebar and the main content.
I would like to have the sidebar and the content view take over the vertical space left by the header. The problem is that the header can dynamically re-size so I cannot perform a calc(). My solution was to use the flexbox scheme.
I divided the viewport horizontally into two parts. One is the header and one is a wrapper for the sidebar and main content.
The sidebar is floated left and given a percentage of the width and the content is floated right and given the rest.
The problem is that I am trying to make the sidebar always be 100% height of the wrapper.
I tried height: 100% and min-height: 100% but these do not work.
I do not wish to absolutely position it since if the wrapper were to overflow with the main content, the sidebar would not expand accordingly.
Here is my pen: http://codepen.io/markt5000/pen/JXNXpW
As you can see the orange is the header and the red space is the wrapper with the sidebar and the content.
here is the layout
<div class="header">
</div>
<div class="row">
<div id="sidebar">
</div>
<div id="main-content">
</div>
</div>
There's no need for:
height, min-height or calc on your flex items
absolute positioning
floats (in fact, they're useless because floats are ignored in a flex formatting context)
Flex properties have all the power you need to make the layout work. The key is to use flex: 1 to make items expand the full available length of the container.
So your header's height can be dynamic and the sidebar and main content can be made to consume whatever height remains. No scrollbars.
Here's your code with some revisions:
html { height: 100%; }
body {
height: 100%;
margin: 0;
padding: 0;
}
.outer-flex-container {
height: 100%;
display: flex;
flex-direction: column; /* main flex container stacks items vertically */
}
.header {
height: 80px; /* demo purposes; from your original code */
background-color: orange;
}
.nested-flex-container {
display: flex; /* inner flex container stacks items horizontally */
flex: 1; /* KEY RULE: tells this second flex item of main container
to consume all available height */
align-items: stretch; /* KEY RULE: default setting. No need to include. Tells
children to stretch the full length of the container
along the cross-axis (vertically, in this case). */
}
.sidebar {
flex-basis: 20%; /* set width to 20% */
background-color: aqua;
}
.content {
flex: 1; /* set width to whatever space remains */
background: magenta;
}
<div class="outer-flex-container">
<div class="header">HEADER</div><!-- main flex item #1 -->
<div class="nested-flex-container"><!-- main flex item #2 -->
<div class="sidebar">SIDEBAR</div><!-- inner flex item #1 -->
<div class="content">MAIN CONTENT</div><!-- inner flex item #2 -->
</div><!-- close inner flex container -->
</div><!-- close outer flex container -->
Revised Codepn
Is this what you're going for? http://codepen.io/Shambolaz/pen/xVdVyd
.view .row
{
flex: 1 1 auto;
background:red;
height: 90%;
}
When content is larger than it's element, to get true 'fixed position' behavior add overflow: auto to the content element.
Also make sure height is concretely specified for all parents of the content element.
By that I mean there must be an ancestor element that has a specified height (either fixed px or 100vh).
Stack Overflow puts the snippet in an iFrame, which has a fixed-height parent, so the elements using height: 100% have something to calculate from.
We can therefore just add height: 100% to the content div, and the scrollbar will show up there.
This is the modified pen (also has a fixed height ancestor). You can experiment by removing the height CSS rules at various points, and see the scrollbar move up the element tree.
html { height: 100%; }
body {
height: 100%;
margin: 0;
padding: 0;
}
.outer-flex-container {
display: flex;
flex-direction: column;
height: 100%;
}
.header {
height: 80px;
background-color: orange;
}
.nested-flex-container {
display: flex;
flex: 1;
}
.sidebar {
flex-basis: 20%;
background-color: aqua;
}
.content {
flex: 1;
background: magenta;
overflow-y: auto;
height: 100%;
}
<div class="outer-flex-container">
<div class="header">HEADER</div><!-- main flex item #1 -->
<div class="nested-flex-container"><!-- main flex item #2 -->
<div class="sidebar">SIDEBAR</div><!-- inner flex item #1 -->
<div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Porttitor eget dolor morbi non arcu risus quis varius quam. Commodo viverra maecenas accumsan lacus. Vitae elementum curabitur vitae nunc sed velit dignissim. Auctor urna nunc id cursus metus aliquam eleifend. Bibendum ut tristique et egestas. Orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt. Diam maecenas ultricies mi eget mauris pharetra et. Ultricies mi eget mauris pharetra. Nibh mauris cursus mattis molestie.
<br/>
Urna neque viverra justo nec ultrices dui sapien. Diam vulputate ut pharetra sit amet aliquam id diam maecenas. Nunc non blandit massa enim nec dui nunc. Amet mattis vulputate enim nulla. Nisl condimentum id venenatis a condimentum vitae sapien. Magna sit amet purus gravida quis blandit turpis. Sed turpis tincidunt id aliquet risus. Aliquet lectus proin nibh nisl condimentum id venenatis a. Vitae purus faucibus ornare suspendisse sed. Sit amet commodo nulla facilisi nullam. Egestas fringilla phasellus faucibus scelerisque eleifend. Facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum. Viverra adipiscing at in tellus integer feugiat. Scelerisque eu ultrices vitae auctor eu augue. At urna condimentum mattis pellentesque.
<br/>
Quisque id diam vel quam elementum pulvinar etiam non quam. Morbi tempus iaculis urna id volutpat lacus laoreet non curabitur. Diam quis enim lobortis scelerisque fermentum dui. Tortor id aliquet lectus proin nibh. Pellentesque pulvinar pellentesque habitant morbi tristique senectus et netus. Egestas fringilla phasellus faucibus scelerisque. Venenatis a condimentum vitae sapien pellentesque habitant. Quis vel eros donec ac odio tempor orci. Ultrices sagittis orci a scelerisque. Ligula ullamcorper malesuada proin libero nunc consequat interdum. Dis parturient montes nascetur ridiculus mus mauris. Nisl vel pretium lectus quam id leo in vitae. Euismod elementum nisi quis eleifend quam adipiscing vitae. Mattis vulputate enim nulla aliquet porttitor lacus luctus accumsan. Urna porttitor rhoncus dolor purus non. Lobortis mattis aliquam faucibus purus in massa tempor nec. Risus pretium quam vulputate dignissim suspendisse in. Malesuada bibendum arcu vitae elementum curabitur. Quisque id diam vel quam elementum pulvinar. A erat nam at lectus urna duis convallis.
<br/>
Nulla facilisi nullam vehicula ipsum. Nibh ipsum consequat nisl vel pretium lectus quam id leo. Morbi tristique senectus et netus et malesuada fames ac turpis. Quis imperdiet massa tincidunt nunc pulvinar sapien. Amet volutpat consequat mauris nunc congue nisi vitae. Feugiat sed lectus vestibulum mattis ullamcorper. Pharetra magna ac placerat vestibulum lectus. Sed faucibus turpis in eu mi bibendum neque egestas. A condimentum vitae sapien pellentesque habitant. Mi bibendum neque egestas congue quisque egestas diam. Vulputate odio ut enim blandit volutpat maecenas volutpat blandit. Lacus luctus accumsan tortor posuere ac ut.
<br/>
Sit amet cursus sit amet dictum. Sem integer vitae justo eget magna fermentum iaculis eu non. Etiam dignissim diam quis enim lobortis scelerisque fermentum dui faucibus. Congue mauris rhoncus aenean vel elit scelerisque mauris pellentesque pulvinar. Facilisis gravida neque convallis a cras semper. Nullam vehicula ipsum a arcu. Sed egestas egestas fringilla phasellus faucibus scelerisque eleifend donec pretium. Et odio pellentesque diam volutpat commodo sed egestas egestas. Velit aliquet sagittis id consectetur. Faucibus vitae aliquet nec ullamcorper. In iaculis nunc sed augue lacus viverra. Vitae semper quis lectus nulla at volutpat diam ut venenatis. Et malesuada fames ac turpis. Felis imperdiet proin fermentum leo vel. Fringilla est ullamcorper eget nulla facilisi etiam dignissim. Quis varius quam quisque id diam vel quam. Ornare arcu dui vivamus arcu felis bibendum ut. Turpis massa sed elementum tempus egestas sed. Morbi tincidunt augue interdum velit euismod in pellentesque massa placerat. Arcu ac tortor dignissim convallis aenean et tortor.
</div><!-- inner flex item #2 -->
</div><!-- close inner flex container -->
</div><!-- close outer flex container -->