Max-width not working on Flexbox item (item is shrinking to width of content) - css

I have a full viewport width, full viewport height "hero image" on a homepage. I've made this a Flex container. Inside the flex container is one child div (containing a heading). This flex item should be aligned to the bottom of the flex container. It has a max-width and should be horizontally centered inside the parent.
However, the max-width is being ignored. The div is taking the width of its content - the heading. Please see JS Fiddle: https://jsfiddle.net/4h7q6d5x/
As you can see the .main div inside the hero image is taking the width of its content - unlike the .main div below the hero image.
I have looked at various questions, including this one max-width not working on flex item
But adding width:100% to the flex item doesn't work. At smaller viewport sizes the the width and padding don't play nicely and the heading is cropped off the right hand edge.
How do I get max-width to work with Flexbox?
.hero_image {
min-height: 100vh;
background-color:yellow;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.main {
max-width:500px;
margin:0 auto;
padding:0 50px;
background-color:pink;
}
<div class="hero_image">
<div class="main">
<h1>Heading <br>couple of words</h1>
</div>
</div>
<div class="main">
<p>Lots of content</p>
</div>

The default "width" (actually flex-basis) inside flex-containers is auto which makes it only as wide as it's content.
But adding width:100% to the flex item doesn't work. At smaller viewport sizes the the width and padding don't play nicely and the heading is cropped off the right hand edge.
Then I'd suggest a couple of changes. Don't use a flex-column but position your .main div using align-items:flex-end.
Then set the default "width" (actually flex-grow) with flex: 1.
.hero_image {
min-height: 50vh;
/* for example */
background-color: yellow;
display: flex;
align-items: flex-end;
justify-content: center;
}
.main {
flex: 1;
max-width: 50%;
/* for example */
margin: 0 auto;
padding: 0 50px;
background-color: pink;
}
<div class="hero_image">
<div class="main">
<h1>Heading <br>couple of words</h1>
</div>
</div>
<div class="main">
<p>Lots of text</p>
</div>

Related

Vertical center div with flexbox

I tried vertically centering a div with flexbox:
Oddly align-items: center didn't change anything. After playing around for some time I realized that I need to set body and html to height:100%;.
HTML:
<div id="login_container">
<div class="login"></div>
<div class="login"></div>
<div class="login"></div>
</div>
CSS:
#login_container {
display: flex;
align-items: center;
justify-content: center;
height:100%;
}
.login {
background-color: #008000;
width: 100px;
height: 100px;
margin: 2px;
}
For comparison: This fiddle is working while this one isn't.
Why do I have to add
body,html {
height:100%;
}
so that the div is vertically aligned? Am I missing something?
Default value of height is auto, this means your html/body will not be full screen.
To get full screen content you have specify height to 100% or 100vh.
so that the div is vertically aligned? Am I missing something?
Yes, your content is vertically centred of parent (in this case vertically center to content of html/body). You have just missed to set height of html/body to 100% (full screen).

CSS puzzle: making a select2 box 100% width in a fluid-width div?

I am using the awesome Select2 jQuery plugin.
Currently I have a fixed-width div floating to the left of a fluid-width div, this works well:
<div class="row">
<div class="col left">Label</div>
<div class="col right"></div>
</div>
.row {
display: table;
}
.col {
display: table-cell;
}
.col.left {
width: 150px
}
But it doesn't work so well when I add a select2 box inside the fluid right div. Now it becomes clear that the fluid right div is not actually 100% width, it adapts to the width of its content, and as a result the select2box also changes size constantly:
<div class="row">
<div class="col left">Label</div>
<div class="col right"><select style="width: 100%" class="select2">
Here is a JSFiddle demonstrating the problem: http://jsfiddle.net/vfa4831b/4/
How can I make the .right fluid-width div adapt to the width available, and stay at that size?
Adding width: 100%; to .col.right makes the div 100% width, but also overflows the boundaries of .row.
UPDATE: I need IE8 support, unfortunately, so can't just use calc.
Try this:
.row {
display: table;
width: 100%;
background: #ccc;
}
Your row that uses display: table wasn't actually being set to be 100%.

layouting 2 divs with dynamic content

I need to layout 2 divs (http://jsfiddle.net/tWE8W/) positioned in a container with a fixed width and height:
<div class="container">
<div class="left">
<div class="element"></div>
</div>
<div class="right">
<div class="element"></div>
</div>
</div>
both divs contain divs of type element with fixed dimensions. the elements can be added and removed dynamically.
div1 is positioned left. The elements should be stacked 2 high and grow to the right.
div2 is positioned right.The elements should be positioned horizontally (float: left). When the elements reach the right corner of div2 (also the rght corner of the container). The should start a new line.
div1 should have a dynamic width based on th enumber of elements it contains.
it only needs to work on the latest version of Google Chrome.
Use the Flexible Box Layout for the boxes on the left like
A C E
B D
FIDDLE
(Relevant) CSS
.left{
float:left;
display:flex;
flex-direction: column;
flex-wrap: wrap;
align-content: flex-start;
}
Browser support is also quite good nowadays
EDIT:
You can fiddle with the align-content property to align the boxes on the left.
Setting align-content: space-between; spaces the boxes out like this:
FIDDLE
I hope i understood right http://jsfiddle.net/nindos/8DTp2/9/
<style>
.container
{
height: 50px;
width: 300px;
overflow: hidden;
border: 1px solid black;
}
.left,.right{float:left;width:50%;height:100%;overflow:auto}
.left{background-color:red}
.right{background-color:blue}
.element{display:inline-block;background-color:pink}</style>

Prevent CSS3 flex-shrink from crushing content

I'm trying to create a flexible layout in CSS3 with 3 boxes stacked on each other. The boxes are siblings and thus have the same parent element. The height of the first box must fit its content. The height of the following two boxes shall grow to fit their respective content until they're about to overflow their parent. In that case, they shall shrink so that they don't overflow.
The problem is that I can't figure out how to prevent one of the shrinking boxes from becoming crushed if its content is small in relation to the other shrinking box. I want those boxes to shrink down to a certain point where they won't shrink anymore – let's say the equivalent of two rows of text for example. Setting min-width isn't an option because I don't want the boxes to be taller than their content in case the content is only one row for example. If any of the boxes has come to the point where it shall not shrink anymore and the parent can't hold them without overflowing, the parent shall get a scrollbar.
I don't know the content in advance so the layout has to be dynamic. I want to solve this only with CSS, if possible.
Here's an example of the problem where box3 is too small:
p {
margin: 0;
}
.container, .box {
border: 1px solid black;
}
.box {
background-color: white;
margin: 1em;
overflow: auto;
}
#container {
background-color: yellow;
display: flex;
flex-direction: column;
height: 15em;
overflow: auto;
}
#box1 {
flex: 0 0 auto;
}
#box2 {
}
#box3 {
}
<div id="container" class="container">
<div id="box1" class="box">
<p>◼</p>
</div>
<div id="box2" class="box">
<p>◼◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◻◻◻◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◻◻◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◻◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◻◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◻◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◻◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◻◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◻◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◻◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◼◻◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◼◼◻◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◻◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◻◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◻</p>
<p>◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼</p>
</div>
<div id="box3" class="box">
<p>◼◻◻</p>
<p>◼◼◻</p>
<p>◼◼◼</p>
</div>
</div>
IF I understand your question correctly, the flex-shrink property should be what you are looking for.
Set #box1 to flex-shrink: 0
Set #box2 to flex-shrink: 1
Set #box3 to flex-shrink: 1

Adjust top while maintaining bottom position with CSS

A website I'm working on now is meant to replicate a document that you could print. There is a header, a body, and a footer. All three elements use CSS to adjust the margins and height of each one.
The footer is 0.5in high and must end at least 0.5in from the bottom of the page, but cannot exceed that. Based on other word processors, if the footer is larger than 0.5in, the footer moves up on the page so that it maintains that 0.5in border.
This means that the body will shrink, so I've set it up (I think) so that a larger footer will shrink the body which is the expected behavior.
However, what I want is a way for the footer to adjust its position on the page so that it is always at least 0.5in from the bottom of the page.
Note I'd like the solution to work for any number of pages for one document, so I can't use fixed positions.
I'm including my demo code which works as long as the height is small enough.
<div style="height: 9in;
padding-left: 1in;
padding-right: 1in;
padding-top: 0.5in;
padding-bottom: 0.5in;
background-color: #eee;
margin-top: -0.08in;
margin-left: -0.08in;">
<div style="height: 0.5in"> Nick 1 </div>
<div style="max-height: 9in; height: 9in;">I love stuff.</div>
<div style="min-height: 0.5in; height: 0.5in; margin-top: 0.5in;">Footer</div>
</div>
Look into the CSS sticky footer technique.
The priciple is this:
the content has a bottom padding equal to the height of the footer
the footer uses relative positioning
the footer has a top margin equal to it's own (negated) height
The result is an overlap of the footer over the padding of the content. Since the sizes match the overlap makes the footer "stick" to where the content ends.
This is essentially a sticky footer.
The CSS
.wrap {
min-height:100%;
margin-bottom: -.4in; /* same as footer */
}
.push, #footer {
height:.4in;
}
The HTML
<div class="wrap">
<div></div>
<div></div>
<div class="push"></div>
</div>
<div id="footer">
i would suggest you use native javascript or jquery CSS is not a programming language but javascript will give you more functionality for testing for the conditions needed 
I don't think what you're looking for can be done with CSS only, at least not until flexbox model is more widely supported. If you're using Chrome 24 or higher you can see the code below in action at http://jsfiddle.net/2late2die/bNJZG/1/
.page {
width:8in;
height:10.5in;
background:#fff;
position:relative;
margin:.5in auto;
box-shadow:rgba(0,0,0,.2) 0 0 .1in;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
-webkit-flex-flow: column;
-moz-flex-flow: column;
-ms-flex-flow: column;
-ms-flex-direction: column;
flex-flow: column;
}
.page .body {
-webkit-flex: 1 0 auto;
-moz-flex: 1 0 auto;
-ms-flex: 1 0 auto;
flex: 1 0 auto;
}
It basically sets the body of the page to be a flexbox item that stretches to take up the entire vertical space between header and footer. You still however would need to manage the height of the body yourself, because if it gets more than the space left between header and footer it will overflow.

Resources