Flex layout overflow outer container - html

I have this fiddle showing my code.
It works as I want : basically it is a container with 1 row item that takes 100% widht, and two items with fixed width.
If the container is big enough they should stay in line, otherwise they should wrap.
I don't get why when my viewport gets smaller the two smaller blocks start to overflow on the side outside the container and they don't shrink as I expect.
I tried to remove the fixed width and using flex-basis, but It breaks the layout.
This is the code on the fiddle:
<div class="container">
<div class="search-form flex-row">
<span class="block big">hi</span>
<div class="flex-block">
<span class="block">hi</span>
<span class="block">hi</span>
</div>
<span class="block">hi</span>
</div>
</div>
CSS:
.container{
height:100vh;
width:100%;
padding:50px;
}
.search-form {
/* margin-bottom: 0px; */
margin: auto;
height: 100%;
font-size: 18px;
background-color:blue;
}
.flex-row {
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
flex-direction: column;
align-items: center;
}
.flex-block{
display:flex;
flex-wrap:wrap;
justify-content: center;
}
.block{
flex-shrink:1;
width:150px;
background-color:red;
margin:5px;
}
.block.big{
width:100%
}

Sry, I don't have 50 reputations, that's why I need to awnser.
First (maybe) problem:
did you put the meta tag for viewport in the head
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Second (maybe) compromise:
maybe you can work with min-width?

Simple math: 100% + 100px = too much
.container{
height:100vh;
width:100%;
padding:50px; /* Issue */
}
to the rescue you can use
fiddle demo using border-box
*{margin:0; box-sizing: border-box;}
or
fiddle demo using calc()
.container{
height:100vh;
width: calc(100% - 100px); /* cause 50padd + 50padd = 100 */
padding:50px;
}

Related

Overflow with flex-basis and min and max-width

My goal is to use flexbox to implement a slider that has a given width and can grow as items(images) are added into it, until a specific width is reached. After that width is reached i want to show a scrollbar on the x axis. I also want the slider not to shrink bellow the min-width.
This is what i tried:
HTML:
<div class="wrapper">
<div class="thisDivAllowsSliderToGrowToMaxWidth">
<div class="slider">
<div class="image"></div>
...
</div>
</div>
</div>
CSS:
.wrapper {
height: 400px;
display: flex;
justify-content: center;
align-items:center
}
.slider {
display:flex;
justify-content:flex-start;
height:100px;
flex-basis: 250px;
min-width:200px;
max-width:350px;
overflow-x:auto;
}
.image {
flex-shrink:0;
height:100px;
width:50px;
}
The issue that pops out is that as soon as overflow-x is added to the slider, it does not grow anymore but shows the scrollbar as soon as flex-basis width is reached.
Interestingly, adding a simple wrapping div (.thisDivAllowsSliderToGrowToMaxWidth) around the slider somehow fixes the issue. You can find the example here.
Can anyone answer why is this happening and am I using the flex properties as intended?
To make the flex container grow based on the width of the flex items, you can set the flex-basis to content
content will automatically size based on the flex item's content
$(".add-slide").on("click",function(){
$(".slider").append('<div class="image"></div>');
});
.wrapper {
height: 300px;
border :1px solid red;
display: flex;
justify-content: center;
align-items:center
}
.slider {
display:flex;
border: 1px solid black;
height:100px;
flex-basis: content;
min-width:200px;
max-width:350px;
overflow-x:auto;
overflow-y:hidden;
}
.image {
flex-shrink: 0;
border: 1px solid green;
height:100px;
width:50px;
}
button {
margin-bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="add-slide">add slide</button>
<div class="wrapper">
<div class="slider">
<div class="image"></div>
</div>
</div>

how to make a percentage height div responsive to content inside 100vh section

morning all,
thought this one would be straight forward but not (unless I'm missing something obvious)
[EDIT] The Layout of the front-page section has to match the front cover of a book that has a yellow stripe 28% of the height of the book, 60% down the page [/EDIT]
I have a section that I want 100vh within that section I have two divs set to percentage height, each with content. It all looks great until I reduce the screen size. How can I have a percentage height div and keep it responsive to the content (apart from using media queries). Note, I'm using flex to position the content within the divs.
[EDIT] In other words (if necessary) increase the height of the 60% .intro div and the 28% .stripe div to accommodate the text wrapping
I've tried playing with using min-height:28%; on the .stripe class but that then breaks the constraints of the image height being 80% of the .stripe height. Can anyone help please.
.fs {
height: 100vh;
}
.container {
margin:0;
background-color:#545454;
overflow: auto;
}
.intro {
height:60%;
}
.bees-intro {
font-size:60px;
color:#f4d00b;
text-align:center;
}
.stripe {
height:28%;
background-color:#f4d00b;
color:#000000;
}
.bee-quest {
font-size:80px;
margin-right:0px;
height:100%;
margin-right:40px;
}
.bee-quest img {
margin-right:20px;
height:80%;
}
.align-center-center {
display: flex;
justify-content: center;
align-items: center;
}
.align-right-center {
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
}
<section id="front-page">
<div class="fs container">
<div class="intro align-center-center">
<h1 class="bees-intro">The Bees of Greater Manchester</h1>
</div>
<div class="stripe">
<div class="bee-quest align-right-center">
<img src="bee-quest.png">
<span class="">QUEST</span>
</div>
</div>
</div>
</section>
A working pen can be seen HERE
I'm not 100% sure what you mean by it's not responsive but i'm guessing you want the yellow box to wrap around the bee logo and text when it's in mobile? To do this, remove the 28% height from the stripe and and use the content within it to determine the height.
Example here:
https://jsfiddle.net/m3fnp0nL/
Let me know if this isn't what you're after :)
Thanks to Shaun I've figured out a good solution
The .stripe is set to min-height: 28vh; and the image is set to height: calc(28vh - 20px);
I've set the .intro to min-height:60vh so as the wrapping text will increase the container size.
.fs {
height: 100vh;
}
.container {
margin:0;
background-color:#545454;
overflow: auto;
}
.intro {
min-height:60vh;
}
.bees-intro {
font-size:60px;
color:#f4d00b;
text-align:center;
}
.stripe{
background-color:#f4d00b;
color:#000000;
min-height:28vh;
}
.bee-quest {
font-size:80px;
margin:10px;
display:flex;
flex-wrap:wrap;
justify-content: center;
align-items: center;
}
.bee-quest img {
height: calc(28vh - 20px);
}
#media (min-width: 768px) {
.bee-quest img {
margin-right:20px;
}
.bee-quest span {
margin-right:20px;
}
}
.align-center-center {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.align-right-center {
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
}
<section id="front-page">
<div class="fs container">
<div class="intro align-center-center">
<h1 class="bees-intro">The Bees of Greater Manchester</h1>
</div>
<div class="stripe align-right-center">
<div class="bee-quest">
<img src="http://www.beequest.co.uk/images/bee-quest.png">
<span class="">QUEST</span>
</div>
</div>
</section>
The original Pen has been updated with the correct answer
The only problem I have now is that the content scrolls within the divs

Flexbox grid not working in Firefox [duplicate]

I'm having some trouble getting my image to take up no more than 100% of the available width of the parent container. I'm only noticing the issue in Firefox 36 (not IE or Chrome). So is it a firefox bug or am I missing something here?
Note: The image should never be larger than it's original size.
Chrome:
Firefox:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.container {
width:300px;
}
.flexbox {
display:flex;
}
.flexbox .column {
flex:1;
background-color: red;
}
.flexbox .middleColumn {
flex:3;
}
.flexbox .middleColumn img {
width:auto;
height:auto;
max-width:100%;
max-height:100%;
align-self: center;
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="flexbox">
<div class="column">This is the left column!</div>
<div class="middleColumn">
<img src="http://placehold.it/400/333333">
</div>
<div class="column">This is the right column!</div>
</div>
</div>
</body>
</html>
You need to add min-width:0 on .middleColumn, if you want to allow it to shrink below its min-content width (the intrinsic width of its <img>-child).
Otherwise, it gets the new default min-width:auto, which on a flex item will basically make it refuse to shrink below its shrinkwrapped size.
(Chrome hasn't implemented min-width:auto yet. I'm told IE has, in their next-gen rendering engine, so I'd expect that version should behave like Firefox here -- as will Chrome, once they implement this feature.)
Snippet with that fixed:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.container {
width:300px;
}
.flexbox {
display:flex;
}
.flexbox .column {
flex:1;
background-color: red;
}
.flexbox .middleColumn {
flex:3;
min-width:0;
}
.flexbox .middleColumn img {
width:auto;
height:auto;
max-width:100%;
max-height:100%;
align-self: center;
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="flexbox">
<div class="column">This is the left column!</div>
<div class="middleColumn">
<img src="http://placehold.it/400/333333">
</div>
<div class="column">This is the right column!</div>
</div>
</div>
</body>
</html>
I have to admit that I'm not sure why, but for some reason in Firefox it looks like you have to give the image a width/height (i.e. something other than "auto"). Our old friend 100% seems to do the trick:
.flexbox .middleColumn img {
width: 100%;
height: 100%;
display: block;
}
Here's a fiddle showing the working solution. Note that I changed the side columns to flex:2 to make the result a bit more apparent.
I seem to get this working with the following:
.flexbox {
display:flex;
}
.flexbox .column {
flex:1 1 0;
overflow:hidden;
background-color: red;
}
.flexbox .middleColumn {
flex-grow:3;
flex-shrink:3;
}
.flexbox .middleColumn img {
max-width:100%;
}
setting flex:1 1 0; on all columns sets them to equally grow and do so from the even and miniscule basis of 0px.
You then overide the grow and shrink on .middleColumn
max-width:100%; is needed as per usual
the magic seems to be overflow:hidden; on the item getting flexed.
the other stuff on the image is not needed.
In my experience, the approach is slightly different, maybe strange, but it works. Basically, I fix the max width to the real image width, so it won't pixelate, and use percentage width instead of max-width. If you have, say an <ul> (flex) container, the cells will be:
li{
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
width: 50%; // for example..
img{
display: block;
max-width: [your real img width in px] // instead of 100%;
width:100%; // instead of max-width
}
}

Flexbox creates blank space on the right of a page

Whenever i remove the flex display- it goes away. Can't understand what's happening there. Been going through source up and down, but no element seems to be stretching there.
html of it
<div class="newheadwrap">
<div class="newlogo newhead">with contents</div>
<div class="newtagline newhead" >with contents</div>
<div class="mainhead newhead">with contents</div>
</div>
and the css
.newheadwrap {
-o-display:flex;
-webkit-display:flex;
-ms-display:flex;
-moz-display:flex;
display:flex;
flex-direction:row;
width:966px;
margin:0 auto;
}
.newlogo {
width:17%;
padding-top:43px;
}
.newtagline {
width:33%;
color:white;
padding-top:93px;
font-family: berkeleyFont;
font-size:16px;
float:right !important;
}
.mainhead {
width:50%;
}
I know it's not advised to post live links here, but fiddle will not do in this case, so, here is a link
https://aps-direct.myshopify.com
One way to remove the extra padding to the right side of your current page:
Add overflow: hidden; to the .newheadwrap
.newheadwrap {
-o-display: flex;
-webkit-display: flex;
-ms-display: flex;
-moz-display: flex;
display: flex;
flex-direction: row;
width: 966px;
margin: 0 auto;
overflow: hidden;
}
You have extra space because of this:
.span9 {
width: 717px;
}
reduce the width and it will fit

Floating element breaks to new page while max-width present

I'm trying to understand how to make my layout responsive. I have the following code:
<style>
.wrapper{width:1000px;}
.left{float:left; width:100%;max-width:641px;display:inline;}
.right{float:left;width:359px;display:inline;}
</style>
<div class="wrapper">
<div class="left"></div>
<div class="right"></div>
</div>
Now this is OK while the window width is over 1000px. When I shrink the window, the div.right is pushed to the new line instead of giving me the resposive shrinking of div.left. Please point me to the right direction. Thanx!
You sound like you want something flex-box can solve easily. http://codepen.io/tkrugg/pen/pmhrE
If you support only recent browsers, you should give it a try.
.wrapper{
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
max-width:1000px;
}
.left{
flex-grow:1;
}
.right{
flex-basis:359px;
}
add this Style to your html. this will work fine.
Demo
.wrapper{
display:table;
max-width:1000px;
width:100%;
}
.left{
display:table-cell;
max-width:641px;
}
.right{
display:table-cell;
width:349px;
}
That's because you're doing it wrong. What you should do instead is to use display: table; CSS property. That's how fluid grids work. So your code becomes this:
<style>
.wrapper { max-width: 1000px; margin: 0 auto; }
.row { display: table; width: 100%; height: auto; }
.left { /* 641px / 1000px = 0.641 * 100 = 64.1% */
width: 64.1%; display: table-cell; vertical-align: top; }
.right { /* 359px / 1000px = 0.359 * 100 = 35.9% */
width: 35.9%; display: table-cell; vertical-align: top; }
</style>
<div class="wrapper">
<div class="row">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
You would have to create a breakpoint for mobile, and stack your divs on top of each other simply by setting display: block; and width: 100%;.
your code has some errors.only left div can see. if my answer doesn't help please put your whole code so people can see the error easily.
how ever your requirement can be achieved adding.
position: absolute;
to the right div like below.
.right{float:left;width:359px;display:inline;position: absolute;}
hope this helps.