Keeping side menu 100% height - html

I have a side menu which i would like to keep at 100% page height.
The code is basically just like this right now:
body,
html {
width: 100%;
height: 100%;
}
.sideMenu {
width: 200px;
height: 100%;
float: left;
}
The problem with this is that the side menus height does not extend with the rest of the page. For example I have input fields that can be added to a form, and when a few inputs have been added the form extends below the original view port. While the menu does not.
Heres a jsfiddle to demonstrate https://jsfiddle.net/m5yfqdsu/, click the "add row" button to add inputs until theyre below the viewport.
So what is the best solution to keep the menu at 100% height?
Prefer a CSS solution, but JS works as well if needed.

Add position: fixed; to .sideMenu
// just a quick function to add more inputs
$(document).ready(function() {
$(".add").on("click", function() {
$("fieldset").append("<div class='rowContainer'><label>Label:</label><input type='text' /></div>");
});
});
* {
margin: 0;
padding: 0
}
body,
html {
width: 100%;
height: 100%;
}
fieldset {
padding: 10px;
}
.sideMenu {
width: 200px;
height: 100%;
background-color: #1c1c1c;
position: fixed;
top: 0;
left: 0;
}
.wrapper {
margin-left: 200px;
}
input {
width: 100%;
max-width: 400px;
height: 40px;
display: block;
margin-bottom: 20px;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sideMenu"></div>
<div class="wrapper">
<h1>Page Title</h1>
<form>
<fieldset>
<div class="rowContainer">
<label>Label:</label>
<input type="text" />
</div>
<div class="rowContainer">
<label>Label:</label>
<input type="text" />
</div>
<div class="rowContainer">
<label>Label:</label>
<input type="text" />
</div>
</fieldset>
</form>
<button class="add">Add row</button>
</div>

You can solve this in multiple ways.
One way is to make a container having 100% height, making its child elements scrollable. That way you don't need the actual absolute rule, but it does achieve the same result. I prefer not using absolute because that makes it easier if you want it to be responsive eventually.
That way, you can scroll the sidebar and content seperatly. Both won't be bigger then they need to be. If the sidebar grows, it will be scrollable too.
* {
margin:0;
padding:0;
}
html, body, .wrapper {
height:100%;
}
.wrapper {
position:relative;
overflow-y:hidden;
}
.sidebar {
width:100px;
float:left;
height:100%;
overflow-y:auto;
background-color:red;
}
.content {
width:300px;
float:left;
height:100%;
overflow-y:auto;
background-color:blue;
}
.spacer {
height:1000px;
}
<div class="wrapper">
<div class="sidebar">
sidebar
</div>
<div class="content">
content
<div class="spacer">
spacer
</div>
</div>
</div>

Related

Flex-box Footer Element Centered

I'm making a user-resizable GUI window with a header that gains height through new elements, a footer with static height, and a spacer in between that automatically takes up the rest of the height. I attempted using this answer, but the footer ended up vertically-centered. Picture:
If anyone knows why off the top of their head, it would be greatly appreciated. The element is being added to the page with javascript so the code is pretty messy. Thank you for your time.
What about the following:
<body>
<header class="header"></header>
<main class="spacer"></main>
<footer class="footer"></footer>
</body>
.
html {
height: 100%;
}
body {
display: flex;
flex-direction: column;
min-height: 100%;
}
.spacer {
flex: 1;
}
I still don't know what the issue was, but I made a solution using the css calc() function.
HTML:
<div id="myWindow">
<div id="header">
Header
</div>
<div id="footer">
<div id="subHeaderContainer">
<div id="subHeader">
Sub Header
</div>
</div>
<div id="subFooter">
Sub Footer
</div>
</div>
</div>
CSS:
#myWindow {
width: auto;
height: auto;
resize: both;
border: 2px solid black;
overflow:hidden;
}
#header {
height: 20px;
background-color: grey;
}
#footer {
width:100%;
height: calc(100% - 20px);
}
#subHeaderContainer {
width:100%;
height: calc(100% - 30px);
}
#subFooter {
width:100%;
height:30px;
}

Expand search div to the button

I've HTML structure like following
<div class="box-search-select">
<div class="search-left">
<input id="search" type="text">
</div>
<button type="submit" class="button">Search</button>
<div style="clear:both"></div>
</div>
and CSS as following
.box-search-select{
width:100%;
padding:20px 0;
}
.search-left{
float:left;
width: 90%;
}
.search-left input{
width:100%;
}
button{
float:right;
}
Output : (Normal screen size)
I want to expand "search-left" div width to the Search button.
Which should work properly for fluid responsive layouts too.
Here I've created fiddle if you wish to play : https://jsfiddle.net/j7g8143a/1
Now if I decrease the width of screen then the search button move to next line like following picture
but I want the "search-left" div to automatically adjust it's width according to screen size like following picture.
I need only CSS solution without using any media queries
EDIT: It should have to be compatible with IE9.
Here is your solution with demo and it will be work on IE9 also:
<div class="box-search-select">
<div class="search-left">
<input id="search" type="text">
</div>
<button type="submit" class="button">Search</button>
</div>
.box-search-select {
padding: 20px 68px 20px 0; /* give padding-right equal to button witdh */
position: relative;
}
.search-left input {
display: block;
width: 100%;
}
button {
background: #cccccc none repeat scroll 0 0;
border: 1px solid #dddddd;
padding: 1px;
position: absolute;
right: 0;
top: 20px;
width: 60px;
}
Demo:
https://jsfiddle.net/0u83dbm7/
You can use Flexbox
.box-search-select {
display: flex;
align-items: center;
}
.search-left {
flex: 1;
}
input {
width: 100%;
}
<div class="box-search-select">
<div class="search-left"><input id="search" type="text"></div>
<button type="submit" class="button">Search</button>
</div>
You can also use CSS tables
.box-search-select {
display: table;
}
.search-left,
button {
display: table-cell;
vertical-align: middle;
}
.search-left {
width: 100%;
}
input {
width: 100%;
}
<div class="box-search-select">
<div class="search-left"><input id="search" type="text"></div>
<button type="submit" class="button">Search</button>
</div>
Its gonna work I think
<div class="box-search-select">
<div class="search-left" style="width:80%">
<input id="search" type="text">
</div>
<button type="submit" class="button">Search</button>
<div style="clear:both"></div>
</div>
<style>
#search{
width:100%;
}
.box-search-select{
width:100%;
padding:20px 0;
}
.search-left{
float:left;
width: 90%;
}
.search-left input{
width:100%;
}
button{
float:right;
}
</style>
please let me know if this is not the expected output.
Put your input and button inside the div and use display:flex
.box-search-select{
padding:20px 0;
float:left;
width:100%
}
.search-left{
float:left;
width:100%;
display:flex;
}
.search-left input{
width:100%
}
button{
float:right;
}
<div class="box-search-select">
<div class="search-left">
<input id="search" type="text">
<button type="submit" class="button">Search</button>
</div>
<div style="clear:both"></div>
</div>

Table Element Not Taking 100% Of Parent Element

I created a sample of the situation in JSFiddle
I updated JSFiddle Here: http://jsfiddle.net/x11joex11/r5spu85z/8/ (this shows in more detail how the sticky footer works so well, just height issue).
I want the table to take up the remaining height, for some reason the height: 100% is not working?
From my tests it appears to be related to min-height: 100%. I need that to make the sticky footer work.
So a solution for me is another way to do the sticky footer, or a way to still give 100% height to the elements within.
HTML
<div class="wrapper">
<div class="wrapper_content">
<!--Header-->
<div class="header">Header</div>
<div class="content table">
<div class="row">
<div class="l_cell">left</div>
<div class="r_cell">right</div>
</div>
</div>
<div class="push"></div>
</div>
</div>
<!--Footer-->
<div class="footer">Footer</div>
CSS
body, html {
margin: 0;
padding: 0;
height: 100%;
}
.wrapper {
min-height: 100%;
margin: 0 auto -50px;
background-color: black;
}
.container {
}
.table {
display: table;
height: 100%;
width: 100%;
background-color: yellow;
}
.row {
display: table-row;
}
.l_cell {
display: table-cell;
width: 265px;
background-color: orange;
}
.r_cell {
display: table-cell;
background-color: purple;
}
.header {
background-color: red;
width: 100%;
}
.footer {
height: 50px;
background-color: green;
}
.push {
height: 50px;
}
Here is one solution, http://jsfiddle.net/7t4RT/
This question has been asked many times before. I recommend viewing some of the answers already provided here at StackOverflow.
The reason that we're unable to use height: 100% in your example is because no height has defined. The CSS is wondering... well how high is 100%? There are many ways to get our elements to fill their containers in either HTML or CSS. Simply choose one you feel works better for you.
The following is one of many ways to solve this problem.
HTML:
<div class="fill-height">
<p>Filled</p>
</div>
<div class="cant-fill-height">
<p>Not Filled</p>
</div>
CSS:
body {
background-color: #ccc;
}
.fill-height {
background-color: #0ff;
width: 200px;
position: absolute;
top: 0;
bottom: 0;
}
.cant-fill-height {
background-color: #ff0;
width: 200px;
height: 100%;
margin-left: 200px;
}
I found an answer to my problem for now, but it requires the use of display:table which I recall causes other errors down the road, but it does appear to work right now to create the layout I had in mind.
http://jsfiddle.net/x11joex11/r5spu85z/10/
CSS
body,html{margin:0;padding:0;height:100%;}
.wrapper{}
.table{
height:100%;
width:100%;
display:table;
background-color:yellow;
}
.row{display:table-row;}
.cell{display:table-cell;}
.footer{background-color:green;height:50px;}
.header{background-color:red;height:30px;}
.left{background-color:purple;}
.right{background-color:orange;}
HTML
<div class="wrapper table">
<div class="header row">
Header<br/>
Header2
</div>
<div class="content table">
<div class="row">
<div class="cell left">leftt<br/>left2</div>
<div class="cell right">right<br/>right2</div>
</div>
</div>
<div class="footer row">
Footer
<br/>
Footer2
</div>
</div>
An answer not requiring the use of display:table or table tags is preferred.
Notice the sticky footer effect remains.

css width of two items in a div to 100%

I have a page that looks like this jsfiddle, code below:
html:
<div class="parent">
<div class="child">
<input type="text">
<input type="submit">
</div>
</div>
css:
.parent { width: 500px; }
.child { width: 100%; }
How do I get it so that together they take up 100% of the parent div width (with the text input stretching accordingly)?
To clarify: I want the button(s) in a row to be fixed width and the input to take up the remaining width of the parent so that together the width = parent width. In the case that there are no button in the row, I'd like the textinput to take up the whole width.
.parent { width: 500px; margin:auto; }
.child { width: 100%; }
add this to make input stretches to full width
.child input { width: 100%; }
There are many ways to do this. One way to do this is to use the display:table-x attribute.
If you wrap the input elements in a div of their own like so:
<div class="parent">
<div class="text">
<input type="text" />
</div>
<div class="button">
<input type="submit" />
</div>
</div>
Then style the parent as display:table, the wrapper div's as display:table-cell, and give a width to div.button, like so:
.parent {
width: 500px;
background-color:blue;
display:table;
}
.text {
display:table-cell;
}
.text input {
width:100%;
-webkit-appearance:none;
}
.button {
display:table-cell;
background-color:red;
width:100px;
}
Then you can achieve the result you are looking for: http://jsfiddle.net/QpCCD/9/
This is similar to #panindra's post, but it keeps both inputs on the same line.
I've added some color to the sample to be able to see the position on the screen.
<html>
<head>
<style type="text/css">
body { background-color: black; }
.parent { width: 500px; background-color: white; text-align: center; }
.child { width: 100%; position: relative; margin: 0px 0px 0px 0px; border: 0px; }
.child input { width: 49%; margin: 0px 0px 0px 0px; border: 0px; }
</style>
</head>
<body>
<div class="parent">
<div class="child">
<input type="text">
<input type="submit">
</div>
</div>
</body>
</html>
Actually, this would be closer:
.child input { width: 248px; }

How to make div occupy remaining height?

I have this problem, I have two divs:
<div style="width:100%; height:50px;" id="div1"></div>
<div style="width:100%;" id="div2"></div>
How do I make div2 occupy remaining height of the page?
Use absolute positioning:
#div1{
width: 100%;
height: 50px;
background-color:red;/*Development Only*/
}
#div2{
width: 100%;
position: absolute;
top: 50px;
bottom: 0;
background-color:blue;/*Development Only*/
}
<div id="div1"></div>
<div id="div2"></div>
You can use this http://jsfiddle.net/Victornpb/S8g4E/783/
#container {
display: table;
width: 400px;
height: 400px;
}
#container > div{
display: table-row;
height: 0;
}
#container > div.fill{
height: auto;
}
Just apply the class .fill to any of the children to make then occupy the remaining height.
<div id="container">
<div>
Lorem ipsum
</div>
<div>
Lorem ipsum
</div>
<div class="fill"> <!-- this will fill the remaining height-->
Lorem ipsum
</div>
</div>
It works with how many children you want, no additional markup is required.
Demo
One way is to set the the div to position:absolute and give it a top of 50px and bottom of 0px;
#div2
{
position:absolute;
bottom:0px;
top:50px
}
Since you know how many pixels are occupied by the previous content, you can use the calc() function:
height: calc(100% - 50px);
I faced the same challenge myself and found these 2 answers using flex properties.
CSS
.container {
display: flex;
flex-direction: column;
}
.dynamic-element{
flex: 1;
}
https://stackoverflow.com/a/35348188/1084619
https://stackoverflow.com/a/35348188/1084619
You can use
display: flex;
CSS property, as mentioned before by #Ayan, but I've created a working example: https://jsfiddle.net/d2kjxd51/
With CSS tables, you could wrap a div around the two you have there and use this css/html structure:
<style type="text/css">
.container { display:table; width:100%; height:100%; }
#div1 { display:table-row; height:50px; background-color:red; }
#div2 { display:table-row; background-color:blue; }
</style>
<div class="container">
<div id="div1"></div>
<div id="div2"></div>
</div>
Depends on what browsers support these display types, however. I don't think IE8 and below do. EDIT: Scratch that-- IE8 does support CSS tables.
I tried with CSS, and or you need to use display: table or you need to use new css that is not yet supported on most browsers (2016).
So, I wrote a jquery plugin to do it for us, I am happy to share it:
//Credit Efy Teicher
$(document).ready(function () {
$(".fillHight").fillHeight();
$(".fillWidth").fillWidth();
});
window.onresize = function (event) {
$(".fillHight").fillHeight();
$(".fillWidth").fillWidth();
}
$.fn.fillHeight = function () {
var siblingsHeight = 0;
this.siblings("div").each(function () {
siblingsHeight = siblingsHeight + $(this).height();
});
var height = this.parent().height() - siblingsHeight;
this.height(height);
};
$.fn.fillWidth = function (){
var siblingsWidth = 0;
this.siblings("div").each(function () {
siblingsWidth += $(this).width();
});
var width =this.parent().width() - siblingsWidth;
this.width(width);
}
* {
box-sizing: border-box;
}
html {
}
html, body, .fillParent {
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="fillParent" style="background-color:antiquewhite">
<div>
no1
</div>
<div class="fillHight">
no2 fill
</div>
<div class="deb">
no3
</div>
</div>
You could use calc function to calculate remaining height for 2nd div.
*{
box-sizing: border-box;
}
#div1{
height: 50px;
background: skyblue;
}
#div2{
height: calc(100vh - 50px);
background: blue;
}
<div id="div1"></div>
<div id="div2"></div>
<div>
<div id="header">header</div>
<div id="content">content</div>
<div id="footer">footer</div>
</div>
#header {
height: 200px;
}
#content {
height: 100%;
margin-bottom: -200px;
padding-bottom: 200px;
margin-top: -200px;
padding-top: 200px;
}
#footer {
height: 200px;
}
Why not use padding with negative margins? Something like this:
<div class="parent">
<div class="child1">
</div>
<div class="child2">
</div>
</div>
And then
.parent {
padding-top: 1em;
}
.child1 {
margin-top: -1em;
height: 1em;
}
.child2 {
margin-top: 0;
height: 100%;
}