I was wondering if there was a way to have an element stretch itself with the remaining window space it has when a fixed width has taken up a certain amount of the window.
For example
<div id ="first">
This div will ALWAYS be fixed at 20px height
</div>
<div id="second">
This div will take up 100% of the remaining space between the top div (first div that is 20px high ) and the bottom of the window.
</div>
Thank You
Here's an example using fixed positioning:
html, body {
margin:0;
padding:0;
height:100%;
}
#first {
height:20px;
background:yellow;
position:fixed;
top:0;
left:0;
right:0;
z-index:1;
}
#second {
padding-top:20px;
height:100%;
background:pink;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-ms-box-sizing:border-box;
-o-box-sizing:border-box;
box-sizing:border-box;
overflow:auto;
}
And one using relative positioning:
html, body {
margin:0;
padding:0;
height:100%;
}
#first {
height:20px;
background:yellow;
position:relative;
z-index:1;
}
#second {
margin-top:-20px;
padding-top:20px;
height:100%;
background:pink;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
-ms-box-sizing:border-box;
-o-box-sizing:border-box;
box-sizing:border-box;
overflow:auto;
}
Both are assuming the following HTML:
<div id ="first">
This div will ALWAYS be fixed at 20px height
</div>
<div id="second">
This div will take up 100% of the remaining space between the top div (first div that is 20px high ) and the bottom of the window.
</div>
If you borrow the sticky footer idea from here, and you adapt it to get a fixed header, it would look like this (fiddle).
the html:
<div id ="first">
This div will ALWAYS be fixed at 20px height
</div>
<div id="second">
<div id="push"></div>
This div will take up 100% of the remaining space between the top div (first div that is 20px high ) and the bottom of the window.
</div>
the css:
#first {
height: 20px;
}
#push {
height: 20px;
background: #fcc;
}
html, body {
height: 100%;
}
#second {
min-height: 100%;
height: auto !important;
height: 100%;
margin: -20px 0;
background: #cfc;
}
Is this what you want?
http://jsfiddle.net/xvnCd/
In your first sentence you say "fixed width" but in the code you say height, so I'm not sure if this is what you're looking for.
Note that this version will lose you 20px at the bottom of the viewport, so it may or may not work for you depending on how you need this to be used.
Related
I'm trying to make a box width 100% when its position absolute ?
This below image is what I am trying to make
https://i.imgur.com/qMaT361.gif
<div class="box1">
<div class="box2">
float
</div>
</div>
.box1 { position:relative; width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:absolute; top:10px; right:0; left:0; width:100%; height:50px; background:red; }
Demo : https://jsfiddle.net/otkg9nfh/8/
Please help~
When a div is set to absolute position the div takes the boundry set by the parent div (relative position). If you wan to make the div 100% of the browser screen you will have to make the div position fixed insted of absolute
.box1 { position:relative; width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:fixed; top:10px; left:0; right:0; width:100%; height:50px; background:red; }
<div class="box1">
<div class="box2">
float
</div>
</div>
.
you can use viewport width width:100vw
.box1 { position:relative; width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:relative; width: 100vw; height:50px; background:red; }
<div class="box1">
<div class="box2">
float
</div>
</div>
.box{position: relative;}
.box1 { width:500px; height:100px; margin:0 auto; overflow:visible; background:#f1f1f1;}
.box2 { position:absolute; top:10px; left:35.55%; right:0; width:100%; height:50px; background:red;}
<div class="box">
<div class="box1">
<div class="box2">
float
</div>
</div>
</div>
If you want div to start from left with some space using absolute then do like this.
You can do it 2 way
One is by using position: fixed. But you will lost the relative positioning based it's parent. Because when you apply fixed it goes out regular document flow and it consider the body it's parent.
Another way by using calc() if you want to keep the position; absolute.
.box2 { position:absolute; top:10px; right:0; left:calc(250px - 50vw); width:100vw; height:50px; background:red;}
Here left:calc(250px - 50vw); used to make center the element. 250px half size of parent container and 50vw is ensure half size of window vertical width.
It's still not clear from your question what you want the element to be "100%" of, but assuming it's the parent of the parent (the document body in this case), you can simply remove position:relative; from .box1.
https://developer.mozilla.org/en-US/docs/Web/CSS/position#Types_of_positioning
An absolutely positioned element is an element whose computed position value is absolute or fixed. The top, right, bottom, and left properties specify offsets from the edges of the element's containing block. (The containing block is the ancestor to which the element is relatively positioned.) If the element has margins, they are added to the offset.
.box1 {
width: 500px;
height: 100px;
margin: 0 auto;
overflow: visible;
background: #f1f1f1;
}
.box2 {
position: absolute;
top: 10px;
left: 0;
right: 0;
width: 100%;
height: 50px;
background: red;
}
<div class="box1">
<div class="box2">
float
</div>
</div>
As Shahil M suggested, you can also remove box2 from inside of box1. Ultimately you need to either change your markup or you css. As is often the case when things don't behave, you might want to re-examine why you've structured things like this to begin. Typically when you apply absolute positioning you are expecting the parent to act as the containing block.
I'm working on this layout:
<div id="outer">
<div id="leftBar"></div>
<div id="middleContainer">
<div id="middle"></div>
</div>
</div>
with this stylesheet:
#outer{
background-color:yellow;
white-space:nowrap;
}
#leftBar{
background-color:purple;
display:inline-block;
height:300px;
width:100px;
}
#middle{
background-color:blue;
height:300px;
width:100px;
margin:0 auto;
}
#middleContainer{
background-color:green;
width:100%;
height:100%;
display:inline-block;
}
http://jsfiddle.net/thirtydot/8ac2e/2/
There's a (purple) side bar that needs to stay fixed and a middle (blue) div that needs to be centered in the remaining space. I've wrapped the blue div in a (green) spacing div, but if I set its width to 100% then it overflows off the page so it doesn't flow correctly.
I've tried using absolute positioning of the side bar but that knocks the centering off too.
How to get the blue div to be centered in the remaining space? I'd rather not use float-left or float-right if possible.
This is a classical situation for floats. It's just most flexible. Use float: left for the sidebar and overflow: hidden for your green middleContainer. This one takes the rest then. And change all display: inline-block to display: block;
http://jsfiddle.net/8ac2e/4/
This even works if your sidebar has flexible dimensions, the divs have different height and so on.
You could absolutely position the left bar, and add a margin-left to the middleContainer equal to the width of the left bar and make the middleContainer display block. See http://jsfiddle.net/8ac2e/5/
#leftBar{
background-color:purple;
display:inline-block;
height:300px;
width:100px;
position: absolute;
}
#middleContainer{
background-color:green;
margin-left:100px;
height:100%;
display:block;
}
Depending on the browser support that you need to consider, you can use calc() to calculate the width of the #middleContainer by subtracting the width of the #leftBar from 100% like this:
#middleContainer{
background-color:green;
width: calc(100% - 100px);
height:100%;
display:inline-block;
}
Fiddle: http://jsfiddle.net/8ac2e/1/
More about calc() including browser support: https://developer.mozilla.org/en-US/docs/Web/CSS/calc
You can solve it using CSS table.
#container{
display: table;
background-color:yellow;
width:100%;
height:100%;
white-space:nowrap;
}
#leftBar{
background-color:purple;
display:table-cell;
height:300px;
width:20%;
}
#middle{
background-color:blue;
height:300px;
width:100px;
margin:0 auto;
}
#middleContainer{
background-color:green;
width:80%;
height:100%;
display:table-cell;
}
I have layout comprising of a 100% width header, 2 column content divs (30-70% width) and a 70% width footer (visible only in the bottom of right div).
My HTML mark up is like:
<section id="mySection" >
<header id="headerTop">
</header>
<div id="wrapperLeft">
</div>
<div id="wrapperRight">
</div>
<footer id="footerRight">
</footer>
</section>
My CSS is
#mySection
{
margin:0 auto;
padding:0;
text-align:center;
vertical-align:middle;
overflow:hidden;
}
#headerTop
{
position:absolute;
top:0;
left:0;
height:40px;
width:100%;
overflow:hidden;
}
#wrapperLeft
{
position:absolute;
top:40px;
left:0;
width:30%;
bottom:0;
overflow:auto;
}
#wrapperRight
{
position:absolute;
top:40px;
left:30%;
width:70%
bottom:30px;
overflow:auto;
}
#footerRight
{
position:absolute;
left:30%;
bottom:0;
width:70%;
overflow:hidden;
}
I would like to know if I can design this better such that if i hide the left or right div, the other div is displayed at 100%. I think i can change the CSS dynamically via javascript and adjust the left and width values for the other div, but it is getting messy and would like to avoid it if possible.
Ideally would love to call show or hide on the div and the other div automatically adjusts itself to 100% width.
I have no control over the height of the content in either div and would want the browser to display scrollbar when the content height exceeds the window.
Thanks in advance for your help.
I would add a wrapper to the divs so you can float then instead of positioning then absolutely. This way you can make at least one div 100% wide. For instance the right div. If you want both divs to be dynamic in size you will have to use jquery. For instance adding classes if you want to keep the jquery to a minimal.
example HTML:
<div id="header"></div>
<div id="main">
<div id="left"></div>
<div id="right"></div>
</div>
<div id="footer"></div>
example CSS :
#main{
position:relative;
overflo:hidden // This will make the container grow with the children
width:960px;
}
#left{
width:200px;
float:left;
height:100%;
}
#right{
float:left;
width:100%;
height:100%;
}
Example of CSS with additional classto toggle divs
#main.only-left #left{
width:100%;
}
#main.only-left #right{display:none;}
I think I know what you're talking about. I've created a little example here. Basically set 30% on the sidecolumn, and display: block; on the main column. Click on the body anywhere to toggle the side column to show how the main column adapts... is this going in the right direction?
Codepen sketch
HTML
<div class='wrapper'>
<header>Header</header>
<section>
<aside>Sidebar</aside>
<article>Main article</article>
</section>
<footer>Footer</footer>
</div>
CSS
body {
margin: 0;
padding: 0;
}
section {
overflow: hidden;
position: relative;
}
header {
background: crimson;
height: 100px;
width: 100%;
}
aside {
background: #efefef;
float: left;
height: 300px;
width: 30%;
}
aside.hide { display: none; } /** For demo purposes **/
article {
background: #ccc;
display: block;
height: 300px;
}
footer {
background: crimson;
float: right;
height: 100px;
width: 70%;
}
jQuery (just for hideToggle example)
$('html').on('click', function(){
$('aside').toggleClass('hide');
});
UPDATE: Here's an example with a little assitance from jQuery for class toggling. Could probably be generalized more... http://codepen.io/kunalbhat/pen/kuAcg
HTML:
<div id="wrap">
<div id="header">
<div class="logo"></div>
<div class="category"></div>
</div>
</div>
CSS:
#wrap {
width:960px;
margin:0 auto;
position:relative; }
#header_wrap {
position:fixed;
top:0;
left:0;
width:100%;
min-width:960px;
height:105px;
background:rgba(256, 256, 256, 0.6); z-index:999; }
#header {
width:860px;
position:relative;
height:90px;
padding:15px 40px 0 40px;
margin:0 auto; }
.logo {
float:left;
margin-top:-35px; }
.category { float:right; margin-top:-9px; }
I made a fixed header menu on top of page.
But if browser window's width is smaller than wrap width (especially, when zooming on safari in iphone/ipad.), I can't see the right side(category) of header(fixed).
Help!
Do not specify the width in 'px' give it in the form of %, as you maximize or minimize your browser then either the scrollbar will appear and cut off your div
I'm trying to design a 2 column layout using divs. My left column size is fixed to 150 px. But right column is not fixed size, so I want it to extend to right boundary of the browser. I used width:auto and width:100% values in right column but they didn't work.
CSS :
html {
height:100%; width:100%
}
body {
color: #000040;
text-align: left;
padding:0;
margin:0;
height:100%;
width:100%
}
#header {
position:relative;
float:left;
background-color: #000053;
width: 100%;
height: 76px
}
#wrapper {
position:relative;
overflow:hidden;
width:100%;
height: 100%;
margin:0px auto;
padding:0;
background-color:Aqua
}
#container {
clear:left;
float:left;
overflow:hidden;
width:100%;
height:100%
}
#left_column {
position: relative;
float: left;
background-color:Fuchsia;
width: 150px;
overflow:hidden;
height:100%
}
#right_column {
position: relative;
float:left;
overflow:hidden;
background-color:Blue;
height: 100%;
width:auto }
HTML
<html>
<body>
<div id="wrapper">
<div id="header">
HEADER
</div>
<div id="container">
<div id="left_column">
LEFT COLUMN
</div>
<div id="right_column">
RIGHT COLUMN
</div>
</div>
</div>
</body>
</html>
I would remove all position statements and only put a float:left on the left column, not the right nor the container. Give the right column a margin-left:150px and it should work fine.
Except for the left-floated column, you can also remove the width:100% statements from the rest; when they're not floated, they'll be 100% wide automatically.
The overflow:hidden is only needed on the wrapper; at least if you are using it to have the div grow in height to accommodate the floats inside it.
change for the div right_column the position from relative to fixed, and width from auto to 100%. Also add left:150px;
With these changes you css for right_column will look like the following:
#right_column {
position: fixed;
left:150px;
float:left;
overflow:hidden;
background-color:Blue;
height: 100%;
width:100%; }
you can check it here http://jsbin.com/ejetu3