height: 100% to use the remaining height? - html

The red is the header.
Then I have 5 rows, where I want to let use each 20% of the remaining space.
But instead it takes 20% of the window space. How can this be fixed?
html:
<div id="container">
<div id="header"></div>
<div id="items">
<div class="item" id="item1"></div>
<div class="item" id="item2"></div>
<div class="item" id="item3"></div>
<div class="item" id="item4"></div>
<div class="item" id="item5"></div>
</div>
</div>
css:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border: 0;
overflow: hidden;
}
#container {
position: relative;
width: 100%;
height: 100%;
}
#header {
position: relative;
width: 100%;
height: 10%;
background: red;
}
#items {
position: relative;
width: 100%;
height: 100%;
}
.item {
height: 20%;
width: 100%;
}
#item1 {
background: green;
}
#item2 {
background: blue;
}
#item3 {
background: orange;
}
#item4 {
background: purple;
}
#item5 {
background: brown;
}
http://jsfiddle.net/clankill3r/dabrm8js/

#items {
position: relative;
width: 100%;
height: 90%;
}
10% is taken by header, so you have 90% of height for items (and not all 100%)...

Set the height of your items div to 90%.
Next to the header (10%) they will fill the screen. Then the .item divs will each take up to 20% of their parent (#items).
So try
#items {
position: relative;
width: 100%;
height: 90%;
}

You can do this with CSS tables.
1) Set display:table on the container and give it a background color (this will be the color of the header)
2) Set display:table-row on the header and items
FIDDLE
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
border: 0;
overflow: hidden;
}
#container {
display: table;
width: 100%;
height: 100%;
background: red;
}
#header {
display: table-row;
width: 100%;
height: 40%;
}
#items {
display: table-row;
height: 100%;
}
.item {
height: 20%;
width: 100%;
}
#item1 {
background: green;
}
#item2 {
background: blue;
}
#item3 {
background: orange;
}
#item4 {
background: purple;
}
#item5 {
background: brown;
}
<div id="container">
<div id="header"></div>
<div id="items">
<div class="item" id="item1"></div>
<div class="item" id="item2"></div>
<div class="item" id="item3"></div>
<div class="item" id="item4"></div>
<div class="item" id="item5"></div>
</div>
</div>
NB: If CSS3 is an option this can also be done with flexbox.

I'd use flex boxes for this:
#container {
display: flex;
flex-direction: column;
height: 100%;
}
#header {
flex: 0 0 auto; /* fixed height */
min-height: 10%; /* you don't need this? */
}
#items {
flex: 1 0 auto; /* take the remaining height (grow) */
display: flex;
flex-direction: column;
}
.item {
flex: 1 1 auto; /* distribute height equally, 20% height for 5 rows, 25% for 4 etc. */
}
(test)
For older browsers support you need to add prefixed version of the properties and the older properties (like box-orient)
Tables may do the job too, if you can live with their limitations with padding, margins and positioning

Height of items need to be 90% as 10% is already used by header part.

Related

Content height is not automatically adjusted when scrolling

I am trying to create a modal that has a footer and an header. The content has two columns: LeftSection and RightSection. I want to have the second column fill the height of the content depending on what the first columns height is (which can differ based on content). From the snippet, this means to have the black div go down as much as the red one does.
.Container {
margin: auto auto;
width: 80vw;
height: 250px;
background-color: #8080801a;
flex: 1;
display: flex;
flex-direction: column;
}
.Header {
height: 50px;
width: 100%;
background-color: #61dafb;
}
.FlexContainer {
flex: 1;
display: flex;
overflow: auto;
}
.LeftSection {
width: 200px;
height: 400px;
background: red;
}
.RightSection {
width: 100%;
background-color: black;
}
.Footer {
height: 50px;
background-color: blue;
width: 100%;
}
<div class="Container">
<div class="Header"></div>
<div class="FlexContainer">
<div class="LeftSection" ></div>
<div class='RightSection' ></div>
</div>
<div class='Footer' />
</div>
Do you want this?
.Container {
margin: auto auto;
width: 80vw;
height: 250px;
background-color: #8080801a;
flex: 1;
display: flex;
flex-direction: column;
}
.Header {
height: 50px;
width: 100%;
background-color: #61dafb;
}
.FlexContainer {
flex: 1;
display: flex;
overflow: auto;
}
.LeftSection {
width: 200px;
height: 400px;
background: red;
position: sticky;
top: 0;
}
.RightSection {
width: 100%;
background-color: black;
position: sticky;
top: 0;
}
.Footer {
height: 50px;
background-color: blue;
width: 100%;
}
<div class="Container">
<div class="Header"></div>
<div class="FlexContainer">
<div class="LeftSection" ></div>
<div class='RightSection' ></div>
</div>
<div class='Footer' />
</div>

Auto resize child div to fill available space of parent div

I have two child divs inside a parent div. The first child div is 32% of the width, and the second child div is 68% of the width. If the first child div is set to display: none;, how do I make it so that the second child div goes from 68% of the width to 100% of the width? Thanks
.parent {
width: 400px;
height: 200px;
position: relative;
float: left;
display: inline-block;
}
.child1 {
width: 32%;
height: 100%;
float: left;
background-color: green;
}
.child2 {
width: 68%;
height: 100%;
float: left;
background-color: red;
}
<div class='parent'>
<div class='child1'></div>
<div class='child2'></div>
</div>
I would leverage the magic of flex!
flex: 0 0 32%; On child1 sets the width to 32%.
flex: 1; to the child2 means: Fill all the available space. So if the child1 disappears, child 2 will fill all the remaining space.
.parent {
width: 400px;
height: 200px;
display: flex;
}
.child1 {
flex: 0 0 32%;
background-color: green;
}
.child2 {
flex: 1;
background-color: red;
}
<div class='parent'>
<div class='child1'></div>
<div class='child2'></div>
</div>
If you use flex instead of float, setting display: none on one will adapt the other for you:
document.querySelector('button').addEventListener('click', () => {
document.querySelector('.child1').classList.toggle('hidden');
})
.parent {
width: 400px;
height: 200px;
position: relative;
display: flex;
}
.child1 {
flex: 0 0 32%;
background-color: green;
}
.child2 {
flex: 1 1 auto;
background-color: red;
}
.hidden {
display: none;
}
button {
margin-bottom: 1em;
}
<button>Toggle child1 visibility</button>
<div class='parent'>
<div class='child1'></div>
<div class='child2'></div>
</div>
Here instead of using float property you can use Flexbox. for more understanding follow this link.
so in flexbox you can achieve it by following the below code :-
.parent {
width: 400px;
height: 200px;
display: flex;
}
.child1 {
height: 100%;
background-color: green;
flex:1;
}
.child2 {
// display:none;
height: 100%;
flex:2;
background-color: red;
}
<div class='parent'>
<div class='child1'></div>
<div class='child2'></div>
</div>
here's how you can achieve that using your approach.
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
.parent {
width: 400px;
height: 200px;
font-size: 20px;
font-weight: bold;
color: white;
}
.child1 {
width: 32%;
height: 100%;
float: left;
background-color: green;
}
.child2 {
width: 100%;
height: 100%;
background-color: blue;
}

Grid layout with only one column that scrolls

I want to create a layout where the left section stays in the same place and only the right side can be scrolled. But when I use position: fixed; the left section becomes full width and height of the viewport.
.container {
position: relative;
margin: 0;
padding: 0;
width: 100%;
display: grid;
grid-template-columns: 40% 60%;
}
.left {
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: red;
}
.right {
height: 200vh;
background-color: blue;
}
<div class="container">
<div class="left">
</div>
<div class="right">
</div>
</div>
I created a right content, this make a overflow in right parent div.
*{
margin: 0;
padding: 0;
}
.container {
position: relative;
margin: 0;
padding: 0;
width: 100%;
height: 100vh;
display: grid;
grid-template-columns: 40% 60%;
}
.left {
/*position: fixed;
top: 0;
left: 0;*/
/* height: 100%;
width: 100%;*/
background-color: red;
}
.right {
overflow-y: scroll;
height: 100vh;
background-color: blue;
}
.right_content{
height: 200vh
}
<div class="container">
<div class="left">
LEFT
</div>
<div class="right">
<div class="right_content">RIGHT</div>
</div>
</div>
I played with it.
If you set your positions on the right and left classes to "inline" then the boxes will just be put in the container div following each other as you want them to. With fixed it will put it at 0,0 as you specifid but I think outside the workflow and your second div is inheriting from its parent div and using position relative so it also is at 0,0 (it has no position statement.
Also change your left height to "100vh". At 100% since the right one is at "200vh" it stretches out to be 200vh also.
so your code will look like this
#container {
display: grid;
position: relative;
margin: 0;
padding: 0;
width: 100%;
grid-template-columns: 40% 60%;
}
#left {
position: inline;
height: 100vh;
background-color: red;
}
#right {
position: inline;
height: 200vh;
background-color: blue;
}
There is an easy way to achieve what you want using flex-box. The only thing that you have to do is to wrap your content of the right side into an element with a defined height and the css style overflow-y: scroll;
body {
margin: 0;
padding: 0;
}
.container {
width: 100vw;
height: 100vh;
display: flex;
}
.left {
height: 100%;
width: 40%;
background-color: red;
}
.right {
height: 100%;
overflow-y: scroll;
width: 60%;
background-color: blue;
}
.right-content {
height: 200vh;
}
<div class="container">
<div class="left">
left content
</div>
<div class="right">
<div class="right-content">
right content
</div>
</div>
</div>

2nd Column fill remaining screen height not working

I have a navbar with a fixed height, underneath a control div with also a fixed height and below that I have another div calendar. calendar is scrollable. I want the calendar height to have the remaining screen height below control and the bottom of the screen. This way the window is not scrollable, only the calendar is scrollable. However setting height: 100% does not work and flex: 1 neither.
This is what I have when I set the height of calendar to a fixed height but as I explained I want the height to be the rest of the screen size.
Any Idea?
.navbar {
height: 50px;
background-color: indianred;
}
.window {
display: flex;
flex-direction: column;
flex: 1;
}
.control {
height: 100px;
background: khaki;
}
.calendar {
height: 200px;
width: 100%;
bottom: 0;
left: 0;
}
.wrapper {
position: relative;
background-color: lightgray;
width: 100%;
height: 100%;
overflow: scroll;
}
.main {
width: 1500px;
height: 1500px;
background-color: rosybrown;
display: flex;
flex-direction: column;
}
<nav class="navbar"></nav>
<div class="window">
<div class="control">
</div>
<div class="calendar">
<div class="wrapper">
<div class="main">
</div>
</div>
</div>
</div>
Run this Code below:
I used height: calc() method full height of the screen minus 150px for nav and controls.
.navbar {
height: 50px;
background-color: indianred;
}
.window {
display: flex;
flex-direction: column;
flex: 1;
}
.control {
height: 100px;
background: khaki;
}
.calendar {
height: calc(100vh - 150px);
width: 100%;
bottom: 0;
left: 0;
}
.wrapper {
position: relative;
background-color: lightgray;
width: 100%;
height: 100%;
overflow: scroll;
}
.main {
width: 1500px;
height: 1500px;
background-color: rosybrown;
display: flex;
flex-direction: column;
}
<nav class="navbar"></nav>
<div class="window">
<div class="control">
</div>
<div class="calendar">
<div class="wrapper">
<div class="main">
</div>
</div>
</div>
</div>

canvas having odd behaviour with width and height

So I'm having some issues using <canvas>. It's taking up too much room when at 100% height and width and when it's set at only 100% height, it still causes and overflow for some reason.
Examples
With width and height at 100%, canvas takes up way too much room
.flex {
display: flex;
flex-direction: column;
height: 100vh;
}
.upper {
background-color: blue;
height: 10%;
}
.lower {
flex: 1;
background-color: orange;
}
html, body {
padding: 0;
margin: 0;
}
canvas {
height: 100%;
width: 100%;
background-color: red;
padding: 0;
margin: 0;
}
.inner {
width: 100%;
height: 100%;
background-color: yellow;
position: relative;
}
<div class="flex">
<div class="upper">
</div>
<div class="lower">
<div class='inner'>
<canvas></canvas>
</div>
</div>
</div>
Here, canvas is set at 100% height, yet it causes an overflow for some reason.
.flex {
display: flex;
flex-direction: column;
height: 100vh;
}
.upper {
background-color: blue;
height: 10%;
}
.lower {
flex: 1;
background-color: orange;
}
html, body {
padding: 0;
margin: 0;
}
canvas {
height: 100%;
background-color: red;
padding: 0;
margin: 0;
}
.inner {
width: 100%;
height: 100%;
background-color: yellow;
}
<div class="flex">
<div class="upper">
</div>
<div class="lower">
<div class='inner'>
<canvas></canvas>
</div>
</div>
</div>