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%;
}
Related
I'm creating a home page for my web application. I'm thinking of 3 vertical split layout for it. I have some piece of code to create 3 horizontal splits but however, my goal is to create 3 vertical splits. How can I do that?
The image below is to create 3 horizontal splits but again my objective is to create 3 vertical splits.
body,
html {
height: 100%;
padding: 0;
margin: 0;
}
<body>
<div style="width:100%; height :100%; background-color:Lime;">
<div style="width:100%; height:34%; background-color:Blue;">
a
</div>
<div style="width:100%; height:33%; background-color:Gray;">
b
</div>
<div style="width:100%; height:33%; background-color:Aqua;">
c
</div>
</div>
</body>
The expected output is 3 vertical split layout.
There are several ways to accomplish this. One is to use inline-block level display to line your divs up. The height here is arbitrary. Consider the following:
body,
html {
height: 100%;
padding: 0;
margin: 0;
}
.column {
display:inline-block;
width:33.33%;
margin-right:-4px;
height:500px;
}
.column-a {background-color:Blue;}
.column-b {background-color:Gray;}
.column-c {background-color:Aqua;}
<body>
<div class="container">
<div class="column column-a">
a
</div>
<div class="column column-b">
b
</div>
<div class="column column-c">
c
</div>
</div>
</body>
Another way to do this would be to use flex display. Here is a fiddle with a simple example. You can use the same markup, but change your css to this:
body,
html {
height: 100%;
padding: 0;
margin: 0;
}
.container {
display:flex;
}
.column {
flex:1;
height:100vh;
}
.column-a {background-color:Blue;}
.column-b {background-color:Gray;}
.column-c {background-color:Aqua;}
Welcome to Stack Overflow _
To neaten the code put the flex & height elements into CSS classes
// parent div
.flexDisplay {
flex: 1;
height: 100%;
}
// child divs
.flexHeight {
display: flex;
height: 100%;
}
.flexHeightResize {
display: flex;
height: 50%; // adjust percentage as required
}
then add to HTML
<div class="flexDisplay" style="background-color:Lime;">
<div class="flexHeight" style="background-color:Blue;">
A
</div>
<div class="flexHeightResize" style="background-color:Gray;">
B
</div>
<div class="flexHeight" style="background-color:Aqua;">
C
</div>
</div>
body,
html {
height: 100%;
padding: 0;
margin: 0;
}
.flexDisplay {
display: flex;
height: 100%;
}
.flexHeight {
flex: 1;
height: 100%;
}
.flexHeightResize {
flex: 1;
height: 50%; // adjust percentage as required
}
<body>
<div class="flexDisplay" style="background-color:Lime;">
<div class="flexHeight" style="background-color:Blue;">
A
</div>
<div class="flexHeightResize" style="background-color:Gray;">
B
</div>
<div class="flexHeight" style="background-color:Aqua;">
C
</div>
</div>
</body>
Try display: flex on the parent and flex: 1; height: 100% on the children
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>
Is there a way to achieve the following behavior in css/html :
Please note the green side bar has not to be responsive but I cannot give it a fixed width with
width: XX px;
because it can contain more or less elements, so no idea of XX in advance.
The brown bar has to be responsive and takes all the remaining width.
Thanks in advance for any trick! I have tried tables but with no success as we can't specify a div to restrict its with to what is necessary.
You can achieve that easily with flexbox. Here's the example:
http://codepen.io/anon/pen/JKXXNE
#container {
display:flex;
}
#sidebar, #content {
height: 100px;
}
#sidebar {
background-color: green;
}
#content {
background-color: brown;
flex: 1;
}
You can use Flexbox, and if you set flex: 1 on right div it will take rest of free space and width of left div will still be dynamic.
.parent {
display: flex;
border: 2px solid black;
}
.left {
background: #22B14C;
padding: 10px;
}
.right {
background: #EFE4B0;
padding: 10px;
flex: 1;
}
span {
margin: 0 20px;
}
<div class="parent">
<div class="left"><span>Span</span><span>Span</span></div>
<div class="right">Right</div>
</div>
This can also be done with CSS Table layout you just need to set width: 100% on .right div and it will take rest of free space
.parent {
display: table;
border: 2px solid black;
}
.left {
background: #22B14C;
display: table-cell;
padding: 10px;
}
.right {
background: #EFE4B0;
display: table-cell;
width: 100%;
padding: 10px;
}
span {
margin: 0 20px;
}
<div class="parent">
<div class="left"><span>Span</span><span>Span</span></div>
<div class="right">Right</div>
</div>
For older browsers, use display: table
html, body{
height: 100%;
margin: 0;
}
.tbl{
display:table;
}
.row{
display:table-row;
}
.cell{
display:table-cell;
}
.content{
width: 100%;
}
#left_col {
background: orange none repeat scroll 0 0;
width: 1%;
}
#right_col {
background: green none repeat scroll 0 0;
width: 100%;
}
<div class="tbl content">
<div class="row">
<div id="left_col" class="cell">
wide content <br>
content <br>
wider contentcontent <br>
</div>
<div id="right_col" class="cell"></div>
</div>
</div>
Another way to achieve this without using flexbox can be:
Working Fiddle:
https://jsfiddle.net/y00e5w6m/
(Note i have used sample css and input just to showcase how this can be done. This should be tuned a bit according to requirements)
Sample Output:
Html:
<div style="float:left;width:100%;border:1px solid #000;">
<div id="dynamic-content" style="float:left;background-color:#090;border:1px solid #900">
<div style="float;left;">
Mango
</div>
<div style="float;left;margin-left:5px;">
Banana
</div>
<div style="float;left;margin-left:5px">
Orange
</div>
</div>
<div id="other-content" style="float:left;background-color:#630;border:1px solid #009;">
</div>
</div>
JS:
var items=["mango","grapes","banana"];
var windowWidth = $(window).width();
console.log(windowWidth);
var dynamicContentWidth = $("#dynamic-content").width();
console.log(dynamicContentWidth);
var otherContentWidth = dynamicContentWidth >= windowWidth ? windowWidth : windowWidth-dynamicContentWidth-20;
console.log(otherContentWidth);
$("#other-content").width(otherContentWidth);
$("#other-content").height($("#dynamic-content").height());
I want to make for my marketing site a 3 column layout that has images in the top banner.
I want to have a liquid left/right side with a fixed center. The html would ideally look like this:
<div id="pixelLeft"> </div>
<div id="bannerCenter">
<img src="images/mybanner.png" />
</div>
<div id="pixelRight"> </div>
<style>
#pixelLeft { background: url(../../images/pixel_left_fixed.png) 0 0 repeat-x; }
#pixelRight { background: url(../../images/pixel_right_fixed.png) 0 0 repeat-x; }
#bannerCenter { /* something here to make fixed width of 1550px */ }
</style>
The images in the left/right pixel sides are 1px x 460px.
The image mybanner.png is 1550px x 460px.
Thanks in advance! (Especially if it will work in ALL browsers!)
Is this helpful?
CSS Only Demo
jQuery Demo(Cross Browser Compatible)
<div class="wrap">
<div id="pixelLeft"> </div>
<div id="bannerCenter">
<img src="images/mybanner.png" />
</div>
<div id="pixelRight"> </div>
</div>
<div style="clear:both;"></div>
*{
margin:0;
padding:0;
}
#bannerCenter{
background:#ddd;
width: 500px;
float:left;
}
#pixelLeft{
background:#999;
width: calc(50% - 250px);
float:left;
}
#pixelRight{
background:#999;
width: calc(50% - 250px);
float:right;
}
#bannerCenter,#pixelLeft,#pixelRight{
height: 400px;
}
You can use jQuery instead of using calc(50% - 250px); to make it compatible for older browsers.
$(document).ready(function() {
$(window).on('resize', function() {
$('#pixelLeft, #pixelRight').css('width',($('body').width()-$('#bannerCenter').width())/2);
}).trigger('resize');
});
Update: June 2018
Added flexbox solution for same problem.
*{
margin:0;
padding:0;
}
.wrap {
display: flex;
}
#pixelLeft, #pixelRight{
display: flex;
flex:1;
}
#bannerCenter{
background:#ddd;
min-width: 500px;
display: flex;
flex: 1;
}
#pixelLeft{
background:#999;
}
#pixelRight{
background:#999;
}
#bannerCenter,#pixelLeft,#pixelRight{
height: 400px;
}
<div class="wrap">
<div id="pixelLeft"> </div>
<div id="bannerCenter">
<img src="images/mybanner.png" />
</div>
<div id="pixelRight"> </div>
</div>
Here's a good solution, in my opinion the easiest one. It looks clean and it doesn't need wrapper div.
Demo
HTML
<body>
<div id="left_container">
<div id="left">
left content
</div>
</div>
<div id="center">
center content
</div>
<div id="right_container">
<div id="right">
right content
</div>
</div>
</body>
CSS
#left_container {
width:50%;
float:left;
margin-right:-480px; /* minus half of the center container width */
/* not important */
height: 200px;
}
#left {
margin-right:480px; /* half of the center container width */
/* not important */
background: #888;
height: 600px;
}
#center {
width:960px; /* size of the fixed width */
float:left;
/* not important */
color: #FFF;
background: #333;
height: 500px;
}
#right_container {
width:50%;
float:right;
margin-left:-480px; /* minus half of the center container width */
/* not important */
height: 300px;
}
#right {
margin-left:480px; /* half of the center container width */
/* not important */
height: 300px;
background-color: #888;
}
enjoy!
There are several solutions to this, probably the post popular of which is the Holy Grail method. It's a little above my head, but these links explain it pretty well.
http://alistapart.com/article/holygrail
http://matthewjamestaylor.com/blog/perfect-3-column.htm
I would start with A List Apart's article. Good luck.
After re-reading it, I think this is what I would do:
HTML
<div id="header">
</div><div id="container">
<div id="center" class="column"></div>
<div id="left" class="column"></div>
<div id="right" class="column"></div>
</div><div id="footer"></div>
CSS
body {
min-width: 550px; /* 2x LC width + RC width */
}
#container {
padding-left: 200px; /* LC width */
padding-right: 150px; /* RC width */
}
#container .column {
position: relative;
float: left;
}
#center {
width: 100%;
}
#left {
width: 200px; /* LC width */
right: 200px; /* LC width */
margin-left: -100%;
}
#right {
width: 150px; /* RC width */
margin-right: -150px; /* RC width */
}
#footer {
clear: both;
}
/*** IE6 Fix ***/
* html #left {
left: 150px; /* RC width */
}
You'll need to adjust some of the dimensions, but the inline comments should help with that. So there you have it. The Holy Grail Layout.
<body>
<div style=" width: 200px; float: left; background: red; height: 100px;">Left</div>
<div style=" float: right; width: 200px; background: red; height: 100px;">Right</div>
<div style=" background: blue; margin:0 auto; height:100px;">Center content goes here</div>
</body>
Here is simple trick through html and css only to do such a layered structure and it will keep middle layer in center even if you will resize browser.
I am new to css and want to know how is it better to create frames in css than html frames? For learning I want to create a screen with 2 equal sized frames, with the first frame divided into 2 columns. Can someone please guide me how do I start with it?
You can simulate frames by using div tags and the overflow CSS attribute.
Demo: http://jsfiddle.net/wdm954/wPywB/
HTML...
<div id="a">
<div class="col1">testing col1</div>
<div class="col2">testing col2</div>
</div>
<div id="b">
Test test test.
</div>
CSS...
#a, #b {
width: 50%;
height: 400px;
float: left;
overflow-y: scroll;
}
#a .col1, #a .col2 {
width: 50%;
float: left;
}
Optionally you can using some jQuery to dynamically set the height of the frame-like columns...
$(document).ready(function() {
$('#a, #b').height( $(window).height() );
$(window).resize(function() {
$('#a, #b').height( $(window).height() );
});
});
Got an example done for you. Just add the 'frame' class to a div and it will look like a frame. Works fine in IE5.5+ and Safari 5 (browsers that I tested).
Also added a 'border' class to simulate a frame 'drag' bar. But you probably want something more stylish. :P
Click here for an example.
CSS:
* {
margin:0;
padding:0
}
html, body {
overflow:hidden;
width:100%;
height:100%
}
body {
background:lightgrey
}
.frame {
float:left;
width:49.5%;
height:100%;
overflow:auto
}
.frame .frame {
background:cyan
}
.frame.border {
border-right:3px ridge buttonface
}
HTML:
<div class="frame">
<div class="frame border">
<p>Left column</p>
</div>
<div class="frame">
<p>Right column</p>
</div>
</div>
<div class="frame">
<p>Right frame</p>
</div>
Many Thanks Guys!!! This is how I did:
CSS
body{
margin: 0;
padding: 0;
border: 0;
overflow: hidden;
height: 100%;
max-height: 100%;
}
#framecontent{
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 320px;
overflow: auto;
}
#maincontent{
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 130px;
overflow: hidden;
background: #fff;
}
.innermargin{
margin: 15px;
}
HTML
<div id="framecontent">
<div class="innermargin">
<h4>Search Result</h4>
</div>
</div>
<div id="maincontent">
<div class="innermargin">
<h4>Search Criteria</h4>
</div>
</div>
you can use the Javascript frame library.
don't forget to declare charset as Utf-8 in your head section:
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<div id="thewin"></div>
<script src="http://pastebin.com/raw/kR6B0XCY"></script>
<script>
//you can create multiple frames to.
createnewwindow("my frame","thewin","windowid","<h5>my contents</h5><button onClick=\"wht()\">change to white</button>");
function wht(){
setdefcol("white"); //optional semi transparent gray is default
hidefrm("windowid")
showfr("windowid");
}
</script>
Simple and easy solution.