For the following HTML list I want to make only it only vertically scrollable and horizontally it should be visible by default.
Even after giving overflow-x as visible it is treating as scroll
<div class="container">
<div class="inner-container">
<div>Item 1</div>
<div class="hover-arrow"></div>
</div>
<div class="inner-container">
<div>Item 2</div>
<div class="hover-arrow"></div>
</div>
<div class="inner-container">
<div>Item 3</div>
<div class="hover-arrow"></div>
</div>
<div class="inner-container">
<div>Item 4</div>
<div class="hover-arrow"></div>
</div>
</div>
.container {
width: 100px;
overflow-y: scroll;
overflow-x: visible;
border: 1px solid black;
height: 200px;
}
.inner-container {
position: relative;
height: 100px;
background: grey;
}
.hover-arrow {
position: absolute;
right: -20px;
bottom: 0;
border-left: 20px solid grey;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
}
JS fiddle for the same
https://jsfiddle.net/m148ujcn/
According to W3Specs:
The computed values of ‘overflow-x’ and ‘overflow-y’ are the same as their specified values, except that some combinations with ‘visible’ are not possible: if one is specified as ‘visible’ and the other is ‘scroll’ or ‘auto’, then ‘visible’ is set to ‘auto’. The computed value of ‘overflow’ is equal to the computed value of ‘overflow-x’ if ‘overflow-y’ is the same; otherwise, it is the pair of computed values of ‘overflow-x’ and ‘overflow-y’.
In short:
If we set overflow-x or overflow-y to visible and something else for
the others, then visible value gets treated as auto
Made some changes in your fiddle check this: JSFiddle
<div class="wrapper">
<div class="container">
<div class="inner-container">
<div style="padding: 20px 20px 20px 30px;">Item 1</div>
<div class="hover-arrow"></div>
</div>
<div class="inner-container">
<div style="padding: 20px 20px 20px 30px;">Item 2</div>
<div class="hover-arrow"></div>
</div>
<div class="inner-container">
<div style="padding: 20px 20px 20px 30px;">Item 3</div>
<div class="hover-arrow"></div>
</div>
</div>
</div>
Related
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! :)
My question must be little bit confusing and will have many related questions. But I think my case is different.
Please run the code in full screen and reduce the size of the screen to around such that there will be
Two boxes per row
One box per row
In both cases there will be lot of empty space to right of the adjacent box, what I want is to align the box to the center and make look both right and left equal when such a case occurs. It should work for both the cases (Two boxes per row & One box per row)
body{
background-color : #E9EAED;
}
.box{
position: relative;
width: 400px;
float: left;
border: 1px solid rgba(35, 173, 278, 1);
height: 120px;
background-color: white;
cursor: pointer;
margin: 15px 15px 15px 15px;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="box">box 1 </div>
<div class="box">box 2 </div>
<div class="box"> box 3</div>
<div class="box"> box 4</div>
<div class="box">box 5 </div>
<div class="box">box 6 </div>
<div class="box"> box 7</div>
<div class="box"> box 8</div>
<div class="box"> box 9</div>
<div class="box"> box 10</div>
<div class="box">box 11 </div>
<div class="box">box 12 </div>
<div class="box"> box 13</div>
<div class="box"> box 14</div>
</body>
</html>
Add to body
body {
text-align: center;
}
And change the box class
.box {
float: left; //remove this
display: inline-block; //add this
text-align: left; // that your text would be aligned normally again
}
I have div content-3 which is inside container. I want to make this background color 100% for his height which may increase. I think this possible using css. Here is image of my requirement.
*{padding:0; margin:0; box-sizzing:border-box;}
.container{margin: 0px auto; width: 80%; border: 1px solid #333;}
.content{min-height:50px}
.content-3{background:green}
<div class="container">
<div class="content content-1">content 1</div>
<div class="content content-2">content 2</div>
<div class="content content-3">content 3</div>
<div class="content content-4">content 4</div>
</div>
As an alternative answer you could use the :before and :after pseudo-elements to achieve the same effect.
No changes to the HTML.
Add this to your CSS:
.content.content-3 {
position: relative;
}
.content.content-3:before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: -10vw;
right: 100%;
background: green;
}
.content.content-3:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 100%;
right: -10vw;
background: green;
}
Edit: Changed -100% to -10vw for left/right positions. vw means viewport width so given your container is 80% wide, you want each side to extend 10vw to make the full 100% with no horizontal scrolling.
As a visual trick, you can add extra padding to the div and counteract using negative margins. However you have to add overflow-x:hidden to body to prevent horizontal scroll:
*{padding:0; margin:0; box-sizzing:border-box;}
.container{margin: 0px auto; width: 80%; border: 1px solid #333;}
.content{min-height:50px}
.content-3{
background:green;
padding-left:100%;
padding-right:100%;
margin-left:-100%;
margin-right:-100%;
}
body{overflow-x:hidden}
<div class="container">
<div class="content content-1">content 1</div>
<div class="content content-2">content 2</div>
<div class="content content-3">content 3</div>
<div class="content content-4">content 4</div>
</div>
If you're able to create multiple container elements you could do something like:
<div class="container">
<div class="content content-1">content 1</div>
<div class="content content-2">content 2</div>
</div>
<div class="container-wrapper">
<div class="container">
<div class="content content-3">content 3</div>
</div>
</div>
<div class="container">
<div class="content content-4">content 4</div>
</div>
Then just make container-wrapper full-width with a green background.
<html>
<head>
<style type="text/css">
.floatleft {
border: 1px solid blue;
float: left;
}
.floatright {
border: 1px solid red;
float: right;
}
#media screen and (max-width: 800px) {
#div1 {
float: none;
}
}
</style>
</head>
<body>
<div id="div1" class="floatleft">Div 1</div>
<div id="div2" class="floatleft">Div 2</div>
<div id="div3" class="floatleft">Div 3</div>
<div id="div4" class="floatright">Div 4</div>
</body>
</html>
I'm doing my first RWD project and I'm struggling to understand how the repositioning of divs work upon resize.
In my code example, on resizing to 800px div1 will move above all the other divs, which is what I want, but what I'm finding difficult is how would I move div4 to the right of div1 as per my picture below.
You should wrap divs 1, 2 and 3 in a parent div that is floating left. Then your div 4 can float right on its own. Adjust the widths accordingly.
<div id="parent" class="floatleft">
<div id="div1">Div 1</div>
<div id="div2" class="floatleft">Div 2</div>
<div id="div3" class="floatleft">Div 3</div>
</div>
<div id="div4" class="floatright">Div 4</div>
See this fiddle
you are sort of on the right foot, you need to only call the pull-right class on div 2 and have it on the first one, the div one will have display default which is block and the other 2 will have display inline to be next to each other.
<div id="div4" class="floatright">Div 4</div>
<div id="div1">Div 1</div>
<div id="div2" style="display:inline;">Div 2</div>
<div id="div3" style="display:inline;">Div 3</div>
div{
border: 1px solid blue;
display:inline;
}
.floatright {
border: 1px solid red;
float: right;
}
see fiddler
Demo fiddler
You could set a percentage width for both, div1 having say 80% width and div4 having say 20% width? Although, div4 will have to be below div1 in the html for that to work.
Do you not have any widths set right now?
I have some HTML code that looks like this:
<div class="container">
<div class="element"> </div>
<div class="element"> </div>
<div class="element"> </div>
<div class="element"> </div>
</div>
And I want to display it in a two-column layout, where each element is displayed directly underneath the one above. I've made a JSFiddle to show my current progress, but I can't figure out how to remove the white gaps between the elements. Is it at all possible, or do i need to change the HTML (I'd rather not)?
An easy way would be to wrap each column items into separate divs. Your .box and .one, .two, .three css declarations are interfering.
[http://jsfiddle.net/grLyvomy/][1]
You could use a seperate div for each column (in your case two).
.container{
border: 1px black solid;
width: 320px;
}
.clear{
clear: both;
}
.leftColumn{
width: 50%;
float: left;
}
.rightColumn{
width: 50%;
float: right;
}
.box:nth-child(2n+1){
background: green;
border-bottom: 1px solid red;
}
.box:nth-child(2n){
background: red;
border-bottom: 1px solid green;
}
.one{ height: 50px; }
.two { height: 80px; }
<div class="container">
<div class="leftColumn">
<div class="box one">first</div>
<div class="box two">second</div>
<div class="box three">third</div>
</div>
<div class="rightColumn">
<div class="box else">first</div>
<div class="box two">second</div>
<div class="box three">third</div>
<div class="box four">fourth</div>
<div class="box one">last</div>
</div>
<div class="clear"></div>
</div>
http://jsfiddle.net/nvmcxjpL/8/
Try to change one and three divs height sum same as two by changing three div height 20px to 30px
.three {
float: left;
width: 50%;
height: 30px;
}