When I add more inbox1 class divs into the div with class box1, the height of box1 increases. You can observe this in jsfiddle with red background never disappearing, however many inbox1's are added. What is the reason of this?
.outer {
position: relative;
width: 400px;
height: 600px;
background-color: black;
display: flex;
flex-flow: row wrap;
}
.container {
position: relative;
flex: 1;
display: flex;
flex-direction: column;
}
d1 {
flex: 1;
background-color: yellow;
}
.d2 {
flex: 1;
background-color: green;
}
.box1 {
display: flex;
flex-flow: row wrap;
align-content: flex-start;
color: white;
flex: 1;
background-color: red;
overflow: scroll;
}
.box2 {
color: white;
flex: 12;
background-color: blue;
}
.inbox1 {
flex: 1 100%;
background-color: purple;
}
<div class="outer">
<div class="container">
<div class="box1">
<div class="inbox1">inbox1</div>
<div class="inbox1">inbox1</div>
<div class="inbox1">inbox1</div>
<div class="inbox1">inbox1</div>
<div class="inbox1">inbox1</div>
</div>
<div class="box2"></div>
</div>
<div class="d1"></div>
<div class="d2"></div>
</div>
Edit :
The purpose of this structure is that, container div is the content of a website, and d1 and d2 divs represent sidebars. However their order is not set in the example because they are needed for the sake of the result.
Container div has two parts, upper(red background) and lower. In my structure this upper and lower are set as flex boxes in container which is display flex with column direction. Upper is also display flex with row direction. However in this set up, as new items are added into the upper, the height of the upper increases. I dont want it enlarge with added childrens.
Note that, if I remove d1 and d2 divs and remove display:flex and related css attributes from outer div, and add container in it as a classic relative element, and change NOTHING else, this problem magically disappears.
Edit 2:
Thanks to oriol, i confirmed this only occurs with chrome.
If I understand you right, here is 3 ways to accomplish that
By giving flex: 0 to your box1, you tell it to only be as big as its content, and by adding max-height: 80px you say "start scroll" when content gets higher than 80px
.outer {
position:relative;
width:400px;
height:600px;
background-color:black;
display: flex;
flex-flow: row wrap;
}
.container {
position:relative;
flex: 1;
display: flex;
flex-direction: column;
}
.d1 {
flex: 1;
background-color: yellow;
}
.d2 {
flex: 1;
background-color: green;
}
.box1 {
display: flex;
flex-flow: row wrap;
align-content: flex-start;
color: white;
flex: 0; /* changed from 1 to 0 */
background-color: red;
overflow: scroll;
max-height: 80px; /* added */
}
.box2 {
color: white;
flex: 12;
background-color: blue;
}
.inbox1 {
flex: 1 100%;
background-color: purple;
}
<div class="outer">
<div class="container">
<div class="box1">
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
</div>
<div class="box2">
</div>
</div>
<div class="d1">
</div>
<div class="d2">
</div>
</div>
Set the flex-basis to 8% (box1 has flex 1 and box2 has flex 12, 100 / 12+1 = 7.69) and when content grow beyond that, it starts to scroll.
Note that the container needs height:100%, so flex-basis: 8% have where to get its value.
.outer {
position:relative;
width:400px;
height:600px;
background-color:black;
display: flex;
flex-flow: row wrap;
}
.container {
position:relative;
height: 100%; /* added */
flex: 1;
display: flex;
flex-direction: column;
}
.d1 {
flex: 1;
background-color: yellow;
}
.d2 {
flex: 1;
background-color: green;
}
.box1 {
display: flex;
flex-flow: row wrap;
align-content: flex-start;
color: white;
flex: 1;
flex-basis: 8%; /* added */
background-color: red;
overflow: scroll;
}
.box2 {
color: white;
flex: 12;
background-color: blue;
}
.inbox1 {
flex: 1 100%;
background-color: purple;
}
<div class="outer">
<div class="container">
<div class="box1">
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
</div>
<div class="box2">
</div>
</div>
<div class="d1">
</div>
<div class="d2">
</div>
</div>
Use an inner div with position: absolute (since flex has some issues to force scroll, this one make it happen)
.outer {
position:relative;
width:400px;
height:600px;
background-color:black;
display: flex;
flex-flow: row wrap;
}
.container {
position:relative;
flex: 1;
display: flex;
flex-direction: column;
}
.d1 {
flex: 1;
background-color: yellow;
}
.d2 {
flex: 1;
background-color: green;
}
.box1 {
display: flex;
flex-flow: row wrap;
align-content: flex-start;
flex: 2; /* temp. adjusted to make it slightly bigger */
position: relative; /* added to make position: absolute relate to this */
}
.box1 .inner { /* added rule to make box1 content scroll */
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: scroll;
color: white;
background-color: red;
}
.box2 {
color: white;
flex: 12;
background-color: blue;
}
.inbox1 {
flex: 1 100%;
background-color: purple;
}
<div class="outer">
<div class="container">
<div class="box1">
<div class="inner">
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
<div class="inbox1">
inbox1
</div>
</div>
</div>
<div class="box2">
</div>
</div>
<div class="d1">
</div>
<div class="d2">
</div>
</div>
Related
I have a 2-cell flexbox with a fixed-height (mobile screen), and one cell stacked on top of the other. I need all of the content of the top-cell to be displayed.. the bottom-cell should yield as much space as possible to allow this. This part was easy.
.main-container {
height: 400px;
width: 200px;
display: flex;
flex-direction: column;
}
.content-container {
flex-grow: 1;
flex-shrink: 0;
background-color: green;
}
.content {
height: 100px;
}
.remaining-space {
flex-grow: 0;
flex-shrink: 1;
height: 100%;
background-color: lightgrey;
}
<div class="main-container">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space"></div>
</div>
... with that the content-container will grow as needed (in this example it's 100px) and the rest is taken up by the remaining-space cell.
What I need to do is fill the remaining-space cell with a circle that is as big as it can be, but doesn't force the remaining-space cell to grow in height/width... and I need to do it with just html/css.
The only way I've seen to enforce a 1:1 ratio (square/circle) is by using the padding-bottom trick where the padding is calculated using the element's width. This doesn't work for me because remaining-space cell might have a larger width than it's height.. which will cause the cell to grow in height. This is especially difficult in that the container is responsive so the actual width in pixels is never known.
A simple gradient can do it:
.main-container {
height: 400px;
width: 200px;
display: inline-flex;
vertical-align: top;
flex-direction: column;
}
.content-container {
flex-grow: 1;
flex-shrink: 0;
background-color: green;
}
.content {
height: 100px;
}
.remaining-space {
flex-grow: 0;
flex-shrink: 1;
height: 100%;
background:
radial-gradient(circle closest-side, red 98%, transparent)
lightgrey;
}
<div class="main-container">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space"></div>
</div>
<div class="main-container" style="height:200px;">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space"></div>
</div>
Clip-path can also do it:
.main-container {
height: 400px;
width: 200px;
display: inline-flex;
vertical-align: top;
flex-direction: column;
}
.content-container {
flex-grow: 1;
flex-shrink: 0;
background-color: green;
}
.content {
height: 100px;
}
.remaining-space {
flex-grow: 0;
flex-shrink: 1;
height: 100%;
background: lightgrey;
}
.remaining-space>div {
height: 100%;
background: red;
clip-path: circle(closest-side)
}
<div class="main-container">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space">
<div></div>
</div>
</div>
<div class="main-container" style="height:200px;">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space">
<div></div>
</div>
</div>
Where you can also have content inside
.main-container {
height: 400px;
width: 200px;
display: inline-flex;
vertical-align: top;
flex-direction: column;
}
.content-container {
flex-grow: 1;
flex-shrink: 0;
background-color: green;
}
.content {
height: 100px;
}
.remaining-space {
flex-grow: 0;
flex-shrink: 1;
height: 100%;
background: lightgrey;
}
.remaining-space>div {
height: 100%;
background: red;
clip-path: circle(closest-side);
display:flex;
align-items:center;
justify-content:center;
font-size:30px;
}
<div class="main-container">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space">
<div> text </div>
</div>
</div>
<div class="main-container" style="height:200px;">
<div class="content-container">
<div class="content"></div>
</div>
<div class="remaining-space">
<div> text </div>
</div>
</div>
This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 2 years ago.
I want to make a website with a list using bootstrap + css's flexbox, where the item list should have a height up to the screen's bottom not overflowing it.
I was able to get a working solution like this:
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
body {
background-color: black;
}
.wrapper .content-wrapper .content {
flex: 1 1 1px;
}
.wrapper .content-wrapper .content {
display: flex;
flex-direction: column;
}
.wrapper .content-wrapper {
width: 100%;
}
.wrapper {
display: flex;
height: 100%;
}
.side {
background-color: blue;
display: flex;
flex-direction: column;
width: 224px;
}
.content-wrapper {
display: flex;
flex-direction: column;
}
.topbar {
height: 100px;
background-color: aqua;
}
.main {
flex: 1 1 1px;
background-color: pink;
overflow-y: scroll;
}
.item {
background-color: white;
border-style: solid;
height: 200px;
}
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<div class="side"></div>
<div class="content-wrapper">
<div class="content">
<div class="topbar"></div>
<div class="main">
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
</div>
</div>
</div>
</div>
</body>
</html>
With the help from this link:
Prevent flex item from exceeding parent height and make scroll bar work
As you can see the scroll bar's bottom arrow is at the end of the screen. (Expected behaviour)
However, when I try to expand my main div into 2 more columns (using bootstrap + flex):
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
body {
background-color: black;
}
.wrapper .content-wrapper .content {
flex: 1 1 1px;
}
.wrapper .content-wrapper .content {
display: flex;
flex-direction: column;
}
.wrapper .content-wrapper {
width: 100%;
}
.wrapper {
display: flex;
height: 100%;
}
.side {
background-color: blue;
display: flex;
flex-direction: column;
width: 224px;
}
.content-wrapper {
display: flex;
flex-direction: column;
}
.topbar {
height: 100px;
background-color: aqua;
}
.main {
flex: 1 1 1px;
background-color: pink;
display: flex;
}
.header {
background-color: yellow;
overflow-y: scroll;
height: 100%;
}
.details {
background-color: crimson;
}
.item {
background-color: white;
border-style: solid;
height: 200px;
}
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<div class="side"></div>
<div class="content-wrapper">
<div class="content">
<div class="topbar"></div>
<div class="main">
<div class="header col-lg-4">
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
</div>
<div class="details col-lg-8"></div>
</div>
</div>
</div>
</div>
</body>
</html>
Now the item list overflow below the bottom of the screen. (see the bottom arrow of the scrollbar is missing)
Any help would be appreciated.
You have a couple of fixed lengths in your code, including:
.side {
width: 224px;
}
.topbar {
height: 100px;
}
These hard limits make the solution to overflow problems relatively simple, since the easiest way to trigger a scrollbar is to create an overflow condition, which is best accomplished with a fixed length.
In this case, the .topbar { height: 100px } is the key to the scrollbar on the sibling element.
(Note that you need to disable flex-shrink on these lengths for the values to always be respected.)
Here's a revised version of your code, with various adjustments for greater performance and efficiency.
body {
margin: 0;
background-color: black;
}
.wrapper {
display: flex;
height: 100vh;
}
.side {
/* width: 224px; */ /* will not be respected without adding `flex-shrink: 0` */
flex: 0 0 224px; /* new; flex-grow, flex-shrink, flex-basis */
background-color: blue;
display: flex;
flex-direction: column;
}
.content-wrapper {
flex: 1; /* consume remaining space */
display: flex;
flex-direction: column;
}
.wrapper .content-wrapper .content {
display: flex;
flex-direction: column;
}
.topbar {
flex: 0 0 100px;
/* height: 100px; */
background-color: aqua;
}
.main {
height: calc(100vh - 100px); /* new; sets overflow condition */
background-color: pink;
display: flex;
}
.header {
background-color: yellow;
overflow-y: scroll;
/* height: 100%; */
}
.details {
background-color: crimson;
}
.item {
background-color: white;
border-style: solid;
height: 200px; /* not a flex item, so no need to disable flex-shrink */
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="side"></div>
<div class="content-wrapper">
<div class="content">
<div class="topbar"></div>
<div class="main">
<div class="header col-lg-4">
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
<div class="item">item</div>
</div>
<div class="details col-lg-8"></div>
</div>
</div>
</div>
</div>
jsFiddle demo
I need css styling for 2 columns. The first column should use the complete width and the second column next to it should use only the width it needs. How can i do that? Is there any way to do this with display: flex?
Example:
"-" = whitespace
if second column is display: none, the first column should use width 100%
[First-Column-------------------------------------------------------]
and if not
[First-Column------------------------------------------------][HELLO]
[First-Column-------------------------------------------][HELLOHELLO]
You can do this with flexbox. Just use flex: 1 for the first child and flex: 0 for the second.
.wrapper {
display: flex;
flex-flow: row wrap;
color: white;
}
.child1 {
flex: 1;
background: red;
}
.child2 {
flex: 0;
background: blue;
}
<div class="wrapper">
<div class="child child1">
test
</div>
<div class="child child2">
test
</div>
</div>
If you hide the second child, the result looks like this:
.wrapper {
display: flex;
flex-flow: row wrap;
color: white;
}
.child1 {
flex: 1;
background: red;
}
.child2 {
flex: 0;
background: blue;
display: none;
}
<div class="wrapper">
<div class="child child1">
test
</div>
<div class="child child2">
test
</div>
</div>
If you want items with only width of the content you could use this:
.wrapper {
display: flex;
flex-flow: row wrap;
color: white;
justify-content: space-between;
}
.child1 {
background: red;
}
.child2 {
background: blue;
}
<div class="wrapper">
<div class="child child1">
test
</div>
<div class="child child2">
test
</div>
</div>
You can do this with below code:
.mainDiv, .mainDiv div{
display: block;
}
.mainDiv .firstDiv{
float: left;
width: 200px;
}
.mainDiv .secondDiv{
float: left;
width: calc(100% - 200px);
}
<div class="mainDiv">
<div class="firstDiv"> First Div </div>
<div class="secondDiv"> Second Div </div>
</div>
.main{
display:flex;
}
<div class="main">
<div>[First-Column------------------------------------------------]</div>
<div> [HELLO]</div>
</div>
I have a flex box layout. I want the width of .outer-2 to be the width of its children, with .outer-1 and outer-3 taking up the rest of the space.
How can I achieve this?
JSFiddle
.container {
display: flex;
}
.outer-1 {
background: red;
height: 100px;
flex: 1;
}
.outer-2 {
display: flex;
flex: 1;
}
.outer-3 {
background: blue;
height: 100px;
flex: 1;
}
.inner {
flex-basis: 30px;
background: green;
height: 100px;
margin: 0 3px;
}
<div class="container">
<div class="outer-1">
</div>
<div class="outer-2">
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
</div>
<div class="outer-3">
</div>
</div>
You need to change the flex properties for the second child of container preventing it from growing to fit it's parent. That, and adding a width or min-width to each .inner element will prevent their parent from collapsing them down.
.container{
display: flex;
}
.outer-1{
background: red;
height: 100px;
flex: 1;
}
.outer-2{
display: flex;
flex: 1;
}
.outer-3{
background: blue;
height: 100px;
flex: 1;
}
.inner{
width: 30px;
flex: 1 0 30px;
background: green;
height: 100px;
margin: 0 3px;
}
<div class="container">
<div class="outer-1">
</div>
<div class="outer-2">
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
</div>
<div class="outer-3">
</div>
</div>
I would like an intro section on the left side of a .container and a side bar on the right.
On the left side underneath the .intro section I want there to be four divs equally spaced like a grid.
I'm having problems with getting the "grid set up". I think part of the problem is that the parent has some flexbox attribute effecting the children.
Requirement : The intro section should be centered in the .left-side and the "grid" should not be centered the boxes should take up as much space as necessary to fit 2 on a row with margins in between. The .intro should be 80 percent of the width of the leftside.
I don't want to do any major changes to the structure this is just a small sample of how my project is set up.
.container{
width: 80%;
margin: 0 auto;
display: flex;
}
.left-side{
flex:8;
display: flex;
justify-content:center;
flex-wrap: wrap;
}
.side-bar{
flex: 2;
height: 100vh;
background: powderblue;
}
.intro{
flex:3;
width:80%;
height: 300px;
background: skyblue;
}
.box{
background: red;
width: 45%;
height: 100px;
flex:4;
border:1px solid orange;
}
<div class="container">
<div class="left-side">
<div class="intro">
intro
</div>
<div class="recent">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
</div>
<div class="side-bar">
sidebar
</div>
Flex items can also be flex containers. This enables you to nest multiple containers, with flex-direction: row or column, in a larger container.
For your layout, you can build a column consisting of two flex items. The first item (.intro) has 80% width and can be centered horizontally. The second item (.recent) can be a flex container with four items arranged in a 2x2 grid.
.container {
width: 80%;
margin: 0 auto;
display: flex;
justify-content: center;
height: 100vh;
}
.left-side {
flex: 4;
display: flex;
flex-direction: column;
}
.side-bar {
flex: 1;
background: powderblue;
}
.intro {
flex: 3;
height: 300px;
width: 80%;
margin: 0 auto;
background: skyblue;
}
.recent {
display: flex;
flex-wrap: wrap;
background-image: url("http://i.imgur.com/60PVLis.png");
background-size: contain;
}
.box {
margin: 5px;
flex-basis: calc(50% - 10px);
height: 100px;
box-sizing: border-box;
background: red;
}
body { margin: 0; }
<div class="container">
<div class="left-side">
<div class="intro">intro</div>
<div class="recent">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
</div>
<div class="side-bar">
sidebar
</div>
</div>