This question already has answers here:
Bootstrap 4 row fill remaining height
(3 answers)
How to make the row stretch remaining height
(1 answer)
Fill remaining vertical space with CSS using display:flex
(6 answers)
Flexbox - Fill remaining space [duplicate]
(3 answers)
Flexbox fill available space vertically
(2 answers)
Closed 3 years ago.
So I've been looking around for a solution to this and I cannot find one anywhere.
I've tried flex-grow: 1;, align-self: stretch; and a few other flexbox properties but none seem to do the trick.
I would like to get the last column to stretch and take up the remaining height from the row.
Following what everyone has said I've made some adjustments, still not quite what I need it to be however.
(The blue box is what I want to stretch)
View the below snippet:
.product {
border: 1px solid rgba(0,0,0,.2);
padding: 15px;
height: 100%;
}
.product .image {
padding-bottom: 100%;
background: rgba(255,0,0,.2);
margin-bottom: 15px;
}
.product .content {
border: 1px solid blue;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-4">
<div class="product">
<div class="image"></div>
<div class="content">
The height of this is dynamic.
</div>
</div>
</div>
<div class="col-4">
<div class="product">
<div class="image"></div>
<div class="content">
It can vary from product to product, but I would like this class to stretch.
</div>
</div>
</div>
<div class="col-4">
<div class="product">
<div class="image"></div>
<div class="content">
To fill the rest of the columns height.
</div>
</div>
</div>
</div>
</div>
I did this on jsfiddle https://jsfiddle.net/e4sufy7k/1/
I remove your inner row and col and add height: 100% to your .card .body. Is it what you expected ?
.card {
border: 1px dashed red;
border-radius: 0;
background: #000 !important;
color: #fff;
}
.card .head {
border: 1px solid blue;
padding: 15px;
}
.card .body {
padding: 15px;
border: 1px dashed #000;
background: #fff;
color: #000;
height: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-4 card">
<div class="head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="body">
This should match the height of the talest card.
</div>
</div>
<div class="col-4 card">
<div class="head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class=" body">
Stretch me please!
</div>
</div>
<div class="col-4 card">
<div class="head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="body">
I should be dynamic and stretch to fill the rest of the column!
</div>
</div>
</div>
</div>
</div>
You have to put your card div inside col-4, and stretch it to 100%, to ensure it will be the same size as the parent. After that, just set the height of body also to 100%.
Like that:
<div class="container">
<div class="row">
<div class="col-4">
<div class="card">
<div class="head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="body">
This should match the height of the tallest card.
</div>
</div>
</div>
<div class="col-4">
<div class="card">
<div class="head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="body">
Stretch me please!
</div>
</div>
</div>
<div class="col-4">
<div class="card">
<div class="head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="body">
I should be dynamic and stretch to fill the rest of the column!
</div>
</div>
</div>
</div>
</div>
And style like that:
.card {
border: 1px dashed red;
border-radius: 0;
background: #000 !important;
color: #fff;
height: 100%;
}
.card .head {
border: 1px solid blue;
padding: 15px;
}
.card .body {
padding: 15px;
border: 1px dashed #000;
background: #fff;
color: #000;
width: 100%;
height: 100%;
}
In Bootstrap, .row is the flex-box container, so only the immediate children (.cols) can interact with each other. In your example, the col items you want stretched are not siblings of each other so they cannot get the stretch applied compared to each other.
The .card elements are siblings of a .row so they do get the applied effect.
To get the .cols you want to streach, they need to be siblings.
<style>
.card-head {
border: 1px solid blue;
background: #000;
color: #fff;
padding: 15px;
}
.card-body {
padding: 15px;
background: #fff;
color: #000;
border: 1px solid red;
}
</style>
<div class="container">
<div class="row no-gutter">
<div class="col-4 card-head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="col-4 card-head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
<div class="col-4 card-head">
Hello there, I should be an automatically set height! This could be through an 'img' or something similar.
</div>
</div>
<div class="row no-gutter">
<div class="col-4 card-body">
This should match the height of the talest card.
</div>
<div class="col-4 card-body">
Stretch me please!
</div>
<div class="col-4 card-body">
I should be dynamic and stretch to fill the rest of the column!
</div>
</div>
</div>
Alternatively, you can leave your html markup the way you have it and use styles to make it appear as you want. If you move the background and color styles of .card to .head it will appear as you want.
.card {
border: 1px dashed red;
border-radius: 0;
.head {
border: 1px solid blue;
background: #000;
color: #fff;
padding: 15px;
}
.body {
padding: 15px;
background: #fff;
color: #000;
}
}
Ok so I was able to fix this by adding:
display: flex;
flex-direction: column;
To my .product class and putting:
flex-grow: 1;
On my .content class.
Thanks everyone for your help! :)
Related
I am scaling several divs and have one that is larger than the others in width and height, the other divs that are after this one are too low, not aligned on the same line.
Note: execute the code below on full page, Follows the code:
body {
background-color: #2E5173;
}
div {
border-radius: 10px;
background-color: white;
margin: 10px;
width: 240px;
height: 250px;
display: inline-block;
box-shadow: 0 2px 4px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12)!important;
}
.big {
width: 508px;
height: 508px;
}
<div class=""> </div>
<div class=""> </div>
<div class=""> </div>
<div class=""> </div>
<div class=""> </div>
<div class=""> </div>
<div class=""> </div>
<div class=""> </div>
<div class="big"> </div>
<div class="">this div is very low</div>
<div class="">this div is very low</div>
The code above looks like this:
I need it to look like this:
Can anyone help?
You can easily do this using CSS grid:
.container {
display:grid;
grid-template-columns:repeat(auto-fit,240px); /* The width */
grid-auto-rows:250px; /* The height */
grid-auto-flow:dense; /*This is the important property*/
/* The margin */
grid-gap:20px;
padding:10px;
}
.container div {
border-radius: 10px;
background-color: #2E5173;
}
.big {
grid-column:span 2; /* Take twice the width*/
grid-row:span 2; /* Take twice the height*/
}
<div class="container">
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div class="big"> </div>
<div>this div is very low</div>
<div>this div is very low</div>
</div>
CSS Grid can provide you with great control of your layouts and is not super complicated. A few of the resources I've used in the last are listed below:
www.w3schools.com/css/css_grid.asp
learncssgrid.com
css-tricks.com/snippets/css/complete-guide-grid
CSS Grid also works well with media queries if you need the page to be responsive.
My issue is that I wanted side-by-side elements with borders, but I noticed without doing some margin-hack it was difficult to use the border property and it still didn't look right. However when I use outline or box-shadow, I get this alignment issue at the end.
.inner {
outline: 1px solid black;
width: 50%;
height: 50px;
float: left;
margin: 0;
display: inline-block;
box-sizing: border-box;
position: relative;
background: #fff;
}
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
It looks alright when there's an even number of elements but when I have this last element it looks odd. Some might suggest I just make it fit to the end which would be okay but the size can be configurable sometimes so this could be a common occurrence.
What is the proper way to achieve this where the last element lines up the border(or outline) correctly?
Because you're using outline to create your border, the outlines at the center are actually overlapping one another. When you get to the bottom where there is only one div the outline is not being overlapped and therefore looks misaligned. You could solve this issues by building it as a table:
.table {
width: 100%;
display: table;
border-collapse: collapse;
}
.column {
display: table-row;
}
.inner {
display: table-cell;
border: 1px solid black;
width: 50%;
height: 50px;
background: #fff;
}
<div class="table">
<div class="column">
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="column">
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="column">
<div class="inner"></div>
</div>
</div>
I have the following layout on my webpage:
The left side, represented here by D, is perfect. There's a lot of content in there and it scrolls as it should. Even when the content grows, the height remains 100% of the webpage's height while the scroll increases.
The right side has some content on B. Everything fine as well. Using Bootstrap 4, B and A are row inside a col and, as such, have 50% share of the parent container.
The problem is that I would A to fill all of the remaining space while being scrollable independent of the number of Cs inside it. This means that with 1 C, it should have its height as half of the page's height (as depicted in the picture) and by having 20 Cs, it should occupy the same height (while being scrollable such that the user can see the 20th C by scrolling to the bottom of A).
So, I am being able to achieve vertical scrollability on A if I set a hardcoded height as height=350px but this is far from ideal since desktops have varying heights. On the other hand, if I don't set a height, A's height become the height necessary to wrap all elements inside it, so with 4 Cs the right-bg layout already surpass the left part of the webpage, causing a break on the overall layout.
<div class="right-bg">
<div class="container col" style="height:100%">
<div class="row"> <!-- THIS IS B -->
<div class="col">
<div class="row" id="chart-div">
<canvas id="myChart"></canvas>
</div>
<div class="row horizontal-menu-wrapper">
</div>
</div>
</div>
<div class="container row ranking-container"> <!-- THIS IS A -->
<div class="container rounded-card"> <!-- THIS IS C -->
<div class="row">
<div class="col-lg-1 col-md-1 col-sm-1">
<div id="ranking-trophy-header" class="row card-header">
<i title="General" class="fas fa-trophy header-icon"></i>
</div>
</div>
<div class="col-lg-11 col-md-11 col-sm-11">
<div class="row row-card">
<div class="col-lg-5 col-md-5 col-sm-5">
<p class="ranking-percentage-value"> Top 1%</p>
</div>
<div class="col ranking-field">
<p class="form-field">Position</p>
<p class="form-ranking-value"><span class="absolute-ranking">#<span class="absolute-ranking-value">83</span></span></p>
</div>
<div class="col ranking-field">
<p class="form-field">Pool</p>
<p class="form-pool-value">9470</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
CSS:
html {
height: 100%;
}
body {
height: 100%;
}
.left-bg {
flex-flow: column;
display: flex;
width: 54%;
height: 100%;
overflow: scroll;
padding-bottom: 10px;
}
.right-bg {
flex-flow: column;
display: flex;
width: 46%;
height: 100%;
padding-top: 12px;
}
.ranking-container {
vertical-align: center;
margin: 5px;
margin-top: 15px;
margin-bottom: 5px;
padding: 15px;
overflow: scroll;
border-radius: 10px;
margin-bottom: -9999px;
padding-bottom: 9999px;
}
Use d-flex, flex-column, and h-100 for its parent. And flex-grow-1 for the row that you want that it occupies all of the available space.
html,
body {
height: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<div class="container d-flex flex-column h-100 bg-light">
<div class="row">
<div class="col p-5 bg-danger"></div>
</div>
<div class="row bg-info flex-grow-1">
<div class="col"></div>
</div>
</div>
It works as expected irrespective of how many row you have.
https://codepen.io/anon/pen/RJVaKw
You need to use bootstrap-4.1 because flex-grow-1 does not exist in the earlier version of it.
Most of your style is unnecessary. You can achieve this with pure Bootstrap classes.
Update
It does not work in Chrome. To fix the issue, use flex-grow-1 for the column inside flex-grow-1 row. And set its overflow-y to scroll. You may hide the scrollbars too.
html,
body {
height: 100%;
}
.overflow-y-scroll {
overflow-y: scroll;
}
/*hide scrollbar in webkit-browsers */
::-webkit-scrollbar {
width: 0px;
background: transparent;
}
::-webkit-scrollbar-thumb {
background: #FF0000;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<div class="container d-flex flex-column h-100 bg-light">
<div class="row">
<div class="col p-5 bg-danger"></div>
</div>
<div class="row bg-info flex-grow-1">
<div class="col flex-grow-1 bg-primary overflow-y-scroll">
<!-- content -->
</div>
</div>
</div>
https://codepen.io/anon/pen/jKmYdq
Try it:
<div class="container col" style="position: absolute; top: 0px; bottom: 0px; ">
</div>
You can give any block level element an explicit height and then add:
overflow: auto;
which will add a scrollbar to that element if the content of the element is taller than the element's own explicitly declared height.
Here is a layout similar to the one you have described above, using CSS Grid.
Note that vertical scrollbars automatically display in the areas D and A because the content within those elements is taller than the height of the parent elements themselves.
Working Example:
main {
display: grid;
grid-template-rows: 50% 50%;
grid-template-columns: 50% 50%;
width: 80vw;
height: 80vh;
border: 6px solid rgb(191, 191, 191);
border-radius: 15px;
}
.a-area {
background-color: rgb(255, 63, 63);
}
.b-area {
background-color: rgb(255, 127, 0);
}
.c-area {
border: none;
background-color: rgb(63, 63, 63);
}
.d-area {
grid-area: 1 / 1 / 3 / 2;
color: rgb(127, 127, 127);
}
main div {
color: rgb(255, 255, 255);
border: 4px solid rgb(191, 191, 191);
overflow-x: hidden;
overflow-y: auto;
}
main div h2 {
margin: 0;
padding-right: 6px;
font-size: 24px;
line-height: 36px;
text-align: right;
}
main div p,
.c-area {
margin: 6px 12px;
}
<main>
<div class="d-area">
<h2>D</h2>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
</div>
<div class="b-area">
<h2>B</h2>
</div>
<div class="a-area">
<h2>A</h2>
<div class="c-area">
<h2>C</h2>
</div>
<div class="c-area">
<h2>C</h2>
</div>
<div class="c-area">
<h2>C</h2>
</div>
<div class="c-area">
<h2>C</h2>
</div>
<div class="c-area">
<h2>C</h2>
</div>
<div class="c-area">
<h2>C</h2>
</div>
</div>
</main>
CODE
<body align="left" style="background-color: white;">
<div style="background-color:#1e836c;height:100vh;width:1vw;"></div>
<div align="center" style="background-color: pink;height:100vh;width:1vw;"></div>
<div align="right" style="background-color: red;height:100vh;width:1vw;"></div>
</body>
How can I implement bootstrap's grid system in the code above by keeping the same width and size of the divs?
I want to make the left and right divs very smalll and the center one bit bigger
You can achieve that using flexbox
.parent {
display: flex;
height: 200px
}
.child {
flex: 1;
margin: 0 5px;
border: red solid
}
.parent > div:nth-of-type(2) {
flex: 3 /* this will be 3x times bigger than the other child */
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
I can achieve this by bootstrap grid pattern by giving col-md-1 for
left and right and col-md-8 for the center, but i wanna make the left
and right too small as possible
Using bootstrap, you can use col-xs-2 (or col-xs-1) for smaller childs and col-xs-8(col-xs-10) for middle one child
I used col-xs-2 +col-xs-10 for the demo (having margins)
[class^="col"] div {
height: 200px;
border: red solid
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-2">
<div></div>
</div>
<div class="col-xs-8">
<div></div>
</div>
<div class="col-xs-2">
<div></div>
</div>
</div>
</div>
I just want the width of that red line only.. Thats the tricky part
So that's not divs, that's a border property
using bootstrap grid and pseudo elements::before/::after here you go:
.col-xs-10 {
height: 200px;
border: red solid
}
.col-xs-10::before,
.col-xs-10::after {
border-left: red solid;
position: absolute;
content: '';
top: -3px;
height: 200px
}
.col-xs-10::before {
left: -20px
}
.col-xs-10::after {
right: -20px
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-10 col-xs-offset-1">
</div>
</div>
</div>
I don't understand very well, but I think that's it:
#div-01{background-color: pink;height:100vh;width:1vw;float: left;}
#div-02{background-color: red;height:100vh;width:1vw;float: right;}
<div id="div-01"></div>
<div id="div-02"></div>
if you want other thing please explain in detail
UPDATE
doing so you have the main div in the center and the side floating.
#div-center{border: solid blue;height:2000px;width:65%;display: block;margin: 0 auto;}
#div-right{border: solid pink;height:200px;width:15%;position: fixed;right: 0px;}
#div-left{border: solid red;height:200px;width:15%;position: fixed;left: 0px;}
<div id="div-right"></div>
<div id="div-left"></div>
<div id="div-center"></div>
I am using bootstrap and I have two container inside a bootstrap container. Like this:
<div class="container">
<div id="container-map">
aasdasd
</div>
<div id="container-buttons">
asdasda
</div>
</div>
What I am trying to do is center the two divs, #container-map and #container-buttons side by side, inside the main container.
This is my custom CSS for the two divs:
#container-map,
#container-buttons {
display: inline-block;
margin: 0 auto 0 auto;
width: 500px;
height: 500px;
}
Is there a reason you don't want to use the bootstraps built in gridsystem? Something like this?
<div class="container">
<div class="row">
<div class="col-md-3 col-md-offset-3">
<div class="container-map">
asdf
</div>
</div>
<div class="col-md-3">
<div class="container-buttons">
asdf
</div>
</div>
</div>
</div>
Just change your CSS to this
#container-map,
#container-buttons {
float: left;
margin-left: auto;
}
Both containers will be centered and side by side
You can try the code from this example (using text-align: center; on .container display:inline-block; for divs).
<style>
.container {
position:relative;
text-align:center;
}
#dv1, #dv2 {
display:inline-block;
width:100px;
margin:0 3px;
background:#33f;
}
</style>
<div class="container">
<div id="dv1">Div 1</div>
<div id="dv2">Div 2</div>
</div>
you make both your divs to take equal height using flex. You can refer the link to find out the browsers which support it. Have a look at this:
.container {
display: flex;
width: 400px;
background: #eee;
}
.column {
flex: 1;
background: #fff;
border: 1px solid #ccc;
margin: 1px;
}
<div class="container">
<div class="column">
<p>aasdasd</p>
</div>
<div class="column">
<p>asdasda</p>
<p>asdasda</p>
</div>
</div>