I am trying to get my main content and menu divs to stretch to the top of the footer div. My HTML is:
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
#wrapper {
min-height: 100%;
position: relative;
}
#header {
padding: 10px;
background: #3095C7;
}
#main {
padding-bottom: 80px;
bull
}
#content {
padding-left: 310px;
background: #FFEFC4;
}
#menu {
background: #67b5d1;
width: 300px;
position: absolute;
float: left;
}
#footer {
text-align: center;
padding: 30px 0;
width: 100%;
height: 50px;
position: absolute;
bottom: 0;
left: 0;
background: #3095C7;
}
<div id="wrapper">
<div id="header"></div>
<div id="main">
<div id="menu">
menu here
</div>
<div id="content">
content here
</div>
</div>
<div id="footer">footer content</div>
</div>
I have set the main, content, and menu height to 100%, but that of course just is to the very bottom of the wrapper div, which is beyond footer. What I am trying for is for menu and content to stretch from the header all the way down to the top of footer, so it fills the whole page. I have played around with vh but isn't spot on constantly when re-sizing the window.
Are there any tricks that will make the divs fill all the blank space and not overlap the footer?
Very simple with flexbox (browser support is IE10+ and everything else that's remotely modern)
Set up #wrapper to be display: flex; flex-flow: column nowrap
Set up #main to be flex: 1 1 auto and display: flex so 1) that it grows to the necessary height and 2) so that it's children will also grow to the height of #main.
Set flex: 0 1 300px on #menu (and remove floats/position: absolute) and set #content to be flex: 1 1 auto.
EDIT
Forgot to remove position: absolute (and associated bottom/left rules) from the footer. I also added a ton of content (and modified the left menu to not shrink flex: 0 0 300px instead of flex: 0 1 300px so that a bunch of content doesn't collapse it into nothing-ness
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
#wrapper {
min-height: 100%;
display:flex;
flex-flow: column nowrap
}
#header {
padding: 10px;
background: #3095C7;
}
#main {
display: flex;
flex: 1 1 auto;
}
#content {
flex: 1 1 auto;
background: #FFEFC4;
}
#menu {
background: #67b5d1;
flex: 0 0 300px;
}
#footer {
text-align: center;
padding: 30px 0;
width: 100%;
height: 50px;
background: #3095C7;
}
<div id="wrapper">
<div id="header"></div>
<div id="main">
<div id="menu">
menu here
</div>
<div id="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu tristique ex, at rhoncus sem. Interdum et malesuada fames ac ante ipsum primis in faucibus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec non ipsum ut nulla volutpat gravida. Donec euismod nibh venenatis fermentum dignissim. Nulla sagittis tristique felis vitae fermentum. Phasellus at viverra sem. In scelerisque mi ac dolor convallis, vitae convallis felis condimentum. Donec porta leo nec semper luctus. In dignissim bibendum viverra. Maecenas molestie, dui eget finibus hendrerit, arcu turpis dapibus arcu, laoreet aliquam lectus justo a lorem. In et enim ac elit tincidunt imperdiet. Phasellus eget erat sed nulla placerat venenatis et in ligula. Mauris volutpat feugiat diam sit amet bibendum. Donec vulputate tristique augue vel pharetra. Mauris orci quam, pharetra lacinia commodo eu, tristique at est.
Morbi metus sapien, venenatis a pulvinar eget, accumsan et nisi. Phasellus vitae blandit augue. Proin quis mollis orci. Ut consequat tempor nulla id dignissim. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce in euismod nibh, pharetra laoreet eros. Donec feugiat neque est, ac pharetra quam sollicitudin a.
Donec hendrerit ac magna at tincidunt. Pellentesque eget eros vel mauris porttitor aliquam ac vel tortor. Vestibulum vitae porttitor enim, eu scelerisque quam. Suspendisse tincidunt nisi non eros condimentum, quis faucibus arcu pellentesque. Morbi aliquet, est at pretium molestie, est arcu volutpat lectus, et condimentum leo risus sed velit. Maecenas at fermentum magna. Duis sit amet pretium ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum diam est, facilisis eu tellus vel, sagittis pretium metus. Aenean fermentum sem eu finibus dignissim. Fusce lacinia purus at libero ornare ultricies. Etiam pellentesque erat nisi, a ullamcorper arcu varius eu. Quisque lacinia bibendum ipsum vitae bibendum.
Maecenas non consequat augue, id euismod magna. Vestibulum ut maximus eros, ut efficitur neque. Aenean feugiat nunc et viverra pulvinar. Praesent vitae lobortis mi. Duis eu lorem a velit consequat fringilla at eu lacus. Ut mi mauris, cursus et lectus non, auctor iaculis eros. Sed sit amet efficitur arcu. Maecenas in enim quis massa vestibulum imperdiet ac quis dui. Curabitur malesuada, neque eu scelerisque sagittis, erat nisl condimentum sapien, vitae volutpat felis nisi ac lectus. Fusce iaculis mollis enim, vitae consectetur metus egestas sed. Proin lacus lorem, finibus ut tincidunt quis, tincidunt a urna. Donec fringilla risus augue, et bibendum diam cursus vitae. Aenean mattis sapien eget volutpat ornare.
</div>
</div>
<div id="footer">footer content</div>
</div>
I think your code is a bit messed.
If I understood correctly your question you have a header and footer with fixed height and you want the content of the web to fill the height of the (vertical) window.
When using height:100% remember all parents need to have same height 100% but then you may have a problem as height 100% plus the fixed height of footer and header will create a scroll bar.
you can use css calc to substract px's to 100% height. I calculate the pixels you need to subtract in your example and the height for your content shoudl be:
height:calc(100% - 110px);
AND the height of your main:
height:calc(100% - 20px);
The rest is your css's with many changes to achieve what I think you were looking for. corrent me if I am wrong: FIDDLE
(you can see I also used calc to set the width to your content since your menu also had fixed width)
Note: carefull with the use of this technique if you NOT using border-boxfor your containers as all the paddings you are using add width (or height) to your elements. I recomend you to get use to add
* {
box-sizing: border-box;
}
in your css sheets. Once you get use to your live will be easier (just my humble opinion, some may disagree)
Assuming your footer has a fixed height, you can automatically set the height of #menu and #content using position: absolute:
position: absolute;
top: 0;
bottom: 110px; /* #footer height */
Most of the time, absolutely positioned elements have auto values of height and width computed to fit the contents of the element. However, non-replaced absolutely positioned elements can be made to fill the available space by specifying (as other than auto) both top and bottom and leaving height unspecified (that is, auto). Likewise for left, right, and width.
See: https://developer.mozilla.org/en-US/docs/Web/CSS/position#Notes
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
#wrapper {
min-height: 100%;
position: relative;
}
#header {
padding: 10px;
background: #3095C7;
}
#main {
position: absolute;
top: 20px;
bottom: 0;
width: 100%;
}
#content {
position: absolute;
left: 310px;
right: 0;
top: 0;
bottom: 50px;
background: #FFEFC4;
}
#menu {
background: #67b5d1;
top: 0;
bottom: 50px;
width: 300px;
position: absolute;
}
#footer {
text-align: center;
padding: 30px 0;
width: 100%;
height: 50px;
position: absolute;
bottom: 0;
left: 0;
background: #3095C7;
}
<div id="wrapper">
<div id="header"></div>
<div id="main">
<div id="menu">
menu here
</div>
<div id="content">
content here
</div>
</div>
<div id="footer">footer content</div>
</div>
Add a specified height into your CSS, e.g. height: 100px and then set overflow to hidden.
So basically, just add:
height: 100px;
overflow: hidden;
to your menu and content.
Related
When hovering a div, I want to display a full-page div with a background image and text on top. The hovered div should then stay on top of everything. This works so far, with the only exception that I can’t figure out how to make the overflow text scrollable.
Any ideas how to make this work?
JSFiddle
html:
<div class="container">
<div class="title">Show content</div>
<div class="content">
<div class="background"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg/800px-Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg"></div>
<div class="infocontainer">
<div class="info">
Lorem Ipsum...
</div>
</div>
</div>
</div>
css:
.container {
position: fixed;
left: 50%;
top: 10%;
}
.content {
display: none;
}
.title:hover + .content {
display: block;
width: 100%;
height: 100%;
}
.title:hover {
position: relative;
z-index: 3;
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.infocontainer {
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
z-index: 2;
overflow: scroll;
}
.info {
height: 100%;
padding-right: 70%;
}
UPDATE: When I put .info on top with z-index, it becomes scrollable but I get a flickering effect because .title:hover gets over overridden. See: jsfiddle.net/dm41eo5z
You can't hover to make something appear over the whole page, because as soon as it is over the whole page you are no longer hovering the original thing (that's why your example above flickers) and it will disappear. If you use a JavaScript Event listener (such as onMouseOver) you could make it appear by adding a class with a "display: absolute" or "block" or whatever you want it to do. Then it would stay open until you use some other event listener (such as "Click" on an X) to close the element.
The .content div disappears as soon as you stop hovering the .title. To prevent that, continue showing .content while you hover it:
.title:hover + .content, .content:hover {
display: block;
width: 100%;
height: 100%;
}
Note: to hide .content, the user will have to move the cursor out of the screen.
Demo:
.container {
position: fixed;
left: 50%;
top: 10%;
}
.content {
display: none;
}
.title:hover + .content, .content:hover {
display: block;
width: 100%;
height: 100%;
}
.title:hover {
position: relative;
z-index: 3;
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.infocontainer {
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
z-index: 2;
overflow: scroll;
}
.info {
height: 100%;
padding-right: 70%;
}
<div class="container">
<div class="title">Show content</div>
<div class="content">
<div class="background"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg/800px-Mount_Everest_as_seen_from_Drukair2_PLW_edit.jpg"></div>
<div class="infocontainer">
<div class="info">
Pellentesque venenatis tempor ultrices. Nunc maximus erat vel tellus vestibulum, id auctor justo tristique. Ut volutpat eu tellus ut vulputate. Cras id finibus massa. Quisque neque lacus, pretium sed luctus in, semper ut quam. Donec elementum volutpat elementum. Morbi nibh nunc, scelerisque congue turpis nec, lacinia venenatis tortor.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi quis magna urna. Etiam tincidunt velit lacus, ac blandit ex pretium nec. Nulla semper erat ut tortor luctus, sit amet suscipit felis ullamcorper. Etiam laoreet, mauris ut volutpat pellentesque, velit neque euismod sem, in condimentum mauris orci nec nibh. Vivamus ac sem et turpis pellentesque volutpat ac ac ligula. Phasellus feugiat dapibus maximus. Donec eros felis, suscipit eu neque quis, sagittis faucibus ipsum. Nam auctor molestie quam nec tristique. Aliquam dolor velit, condimentum in vehicula ut, pretium sed leo. Proin sit amet quam nunc.
Integer eu orci quam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sed felis vitae justo faucibus blandit at a dolor. Curabitur id nisi nec elit aliquam convallis nec eu est. Aenean viverra id diam ac accumsan. Praesent tempor, magna eget molestie sodales, neque libero pretium magna, id euismod justo erat at est. Phasellus ultrices metus et massa varius, at rutrum augue pretium. Mauris ultrices felis et magna luctus sodales. Curabitur sodales pellentesque ante auctor molestie. Donec sed massa scelerisque elit auctor lacinia ac vel risus. In eleifend vulputate sapien a tristique. Nam eleifend rutrum metus.
</div>
</div>
</div>
</div>
Thank you for your replies so far but they don't exactly achieve what I'm looking for. Is there maybe a way to keep .title on top but specifically target .info for scrolling?
The screenshot above shows the problem. Unlike the other similar question I don't need this to work for international text. I need this to work for varying screen sizes and varying amounts of text. The text will come from a databse in production, so I can't simply figure out the breakpoint at which the div needs to move down in advance.
The solution I am looking for would first move the text block below the image, and then resize the text div vertically as necessary to accomidate all the text. JS or CSS solutions are fine.
HTML below:
<div class="product">
<img class="productImg" src="http://placehold.it/300x240">
<div class="productTxt">
<h1>Title</h1>
<h3>Price</h3>
<p>Suspendisse sed fermentum neque, vel rutrum velit. Curabitur eget dolor luctus, sodales felis sed, dapibus justo.
Suspendisse in condimentum ante. Sed nec dui tristique, sollicitudin velit eget, ultricies dui.
Ut rhoncus ornare urna, quis venenatis velit ornare eu. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Etiam ornare sem finibus lectus volutpat, in feugiat elit ultrices. In sed vulputate eros, quis volutpat elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse nisi libero, posuere nec laoreet quis, viverra et lorem. Duis odio ante, efficitur et felis at, tincidunt interdum ante.
Quisque vestibulum eu est a egestas. Nunc hendrerit rutrum dui at cursus. Aliquam mollis nec magna ac mattis.
Nam nec est tincidunt leo facilisis finibus non nec mauris. Vestibulum rutrum tristique tincidunt. </p>
</div>
</div>
CSS:
.product {
box-sizing: border-box;
margin: 0px;
padding: 10px;
width: 100%;
height: 330px;
border-bottom: 2px solid #16A085;
}
.productImg {
width: 240px;
height: 300px;
display: inline-block;
position: relative;
}
.productTxt {
box-sizing: border-box;
vertical-align: top;
display: inline-block;
height: 100%;
width: calc(100% - 270px);
padding: 10px;
text-align: center;
position: relative;
border: 2px solid blue;
}
I can't really remove the relative positioning from the productTxt div without breaking the positioning of another element that is within the same div (which I have removed for simplicity in this example)
Remove the fixed height in of parent div .product and add a min-width to .productTxt. If you use 100% for width or height, it's relative to the parent div, and you must specifically set the parent's width or height. That's why your height: 100% on .productTxt was not changing based on your content and was fixed to parent div .product's height: 330px.
.product {
box-sizing: border-box;
margin: 0px;
padding: 10px;
width: 100%;
border-bottom: 2px solid #16A085;
}
.productTxt {
box-sizing: padding-box;
vertical-align: top;
display: inline-block;
height: 100%;
width: calc(100% - 270px);
min-width: 220px; /* change this to what you want */
padding: 10px;
text-align: center;
position: relative;
border: 2px solid blue;
}
To move the text block below the image remove display: inline-block; from .productTxt class.
If you want to keep the height of .product class fix then adjust height of .productTxt according to .product class and give css overflow: auto; to .productTxt all you data will displayed with scroll panel,so your HTML is
<div class="product">
<img class="productImg" src="http://placehold.it/300x240">
<div class="productTxt">
<h1>Title</h1>
<h3>Price</h3>
<p>Suspendisse sed fermentum neque, vel rutrum velit.
Curabitur eget dolor luctus, sodales felis sed, dapibus
justo.Suspendisse in condimentum ante. Sed nec dui
tristique,sollicitudin velit eget, ultricies dui.
Ut rhoncus ornare urna, quis venenatis velit ornare eu.
Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas.
Etiam ornare sem finibus lectus volutpat, in feugiat elit
ultrices. In sed vulputate eros, quis volutpat elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse nisi libero, posuere nec laoreet quis, viverra
et lorem. Duis odio ante, efficitur et felis at, tincidunt
interdum ante.
Quisque vestibulum eu est a egestas. Nunc hendrerit rutrum
dui at cursus. Aliquam mollis nec magna ac mattis.
Nam nec est tincidunt leo facilisis finibus non nec
mauris.Vestibulum rutrum tristique tincidunt.
</p>
</div>
</div>
And your CSS will be
.product {
box-sizing: border-box;
margin: 0px;
padding: 10px;
width: 100%;
height: 550px;
border-bottom: 2px solid #16A085;
}
.productImg {
width: 240px;
height: 300px;
display: inline-block;
position: relative;
}
.productTxt {
box-sizing: border-box;
vertical-align: top;
height: 230px;
width: calc(100% - 270px);
padding: 10px;
text-align: center;
position: relative;
border: 2px solid blue;
overflow: auto;
}
This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 5 years ago.
I have some content in a nested column whose parent is a nested flex container:
body,
html,
.container {
height: 100%;
}
body {
color: #fff;
}
.container {
display: flex;
overflow: hidden;
}
.sidebar {
width: 200px;
flex-shrink: 0;
background: red;
}
.main {
flex: 1;
display: flex;
}
.content {
flex: 1;
background: blue;
}
.scroll {
overflow-x: auto;
}
.overflowing {
width: 1024px;
}
.panel {
width: 100px;
flex-shrink: 0;
background: green;
}
<div class="container">
<div class="sidebar"></div>
<div class="main">
<div class="content">
<div class="scroll">
<div class="overflowing">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non lorem blandit, aliquet augue in, egestas risus. Curabitur sit amet justo eget metus faucibus sodales. Vestibulum rhoncus vel libero id imperdiet. Quisque ante quam, tempus in metus a, aliquam sollicitudin tortor. Nam in sagittis nunc, et feugiat augue. Phasellus augue lacus, maximus et ipsum ac, placerat tincidunt risus. Curabitur velit diam, fermentum ac quam eget, ultricies elementum ipsum. Nullam justo dolor, consequat porttitor semper a, eleifend vitae ante. Phasellus egestas dolor sed erat dapibus, a scelerisque dui sagittis. Pellentesque eget venenatis nisi. Vestibulum neque nisl, cursus ut sagittis a, ultrices ac nunc. Etiam auctor nunc porta leo fermentum, a iaculis leo vestibulum. Donec lobortis, tellus a aliquet malesuada, ipsum elit sagittis lectus, sed mollis magna diam eu mauris. Vivamus semper nunc eget nunc lacinia pharetra.</div>
</div>
</div>
<div class="panel"></div>
</div>
</div>
The content has to be fixed-width (for whatever reason). On narrower viewports (like the one in the preview above) it's wider than its container, so I want it to scroll horizontally. I thought it would be as easy as wrapping it in a <div> with overflow-x: auto, but for some reason the content is pushing the right panel off-screen (you can see the panel if you view the result in full-screen).
This seems to happen only if the flex container is nested and the content's width is fixed. How can I prevent the panel from being pushed away?
Updated Answer
The original answer to this question is unnecessarily complex.
The simple truth is that flex items, by default, are min-width: auto. This means they cannot shrink below the size of their content.
That's why a horizontal scroll bar doesn't render in the blue text element. The content is unable to overflow because the item is always expanding to accommodate its content.
The solution is to override min-width: auto with min-width: 0.
.main {
min-width: 0; /* NEW */
}
.content {
min-width: 0; /* NEW */
}
jsFiddle demo
More details here: Why doesn't flex item shrink past content size?
Original Answer
The problem you're describing in your question doesn't exist in Chrome 47 or IE11. The code preview, whether small or full-screen, shows all three panels and horizontal scroll. It does what you want.
In Firefox and Chrome 48, however, there's clearly a problem. There's no horizontal scroll, and the right panel (green), is pushed off screen in the small preview.
These are bugs in Firefox and Chrome 48.
Here's the fix:
To enable a horizontal scrollbar in flexbox in FF/Chrome 48 add min-width: 0; to the parent(s) of the scrolling items.
(optional) To enable a vertical scrollbar add min-height: 0.
The above is a cross-browser solution; it doesn't appear to have any effect on non-buggy browsers. Hence, adding both min-width: 0 and min-height: 0 to your production code should be okay.
body,
html,
.container {
height: 100%;
}
body {
color: #fff;
}
.container {
display: flex;
overflow: hidden;
}
.sidebar {
width: 200px;
flex-shrink: 0;
background: red;
}
.main {
flex: 1;
display: flex;
min-width: 0; /* NEW */
}
.content {
flex: 1;
background: blue;
min-width: 0; /* NEW */
}
.scroll {
overflow-x: auto;
}
.overflowing {
width: 1024px;
}
.panel {
width: 100px;
flex-shrink: 0;
background: green;
}
<div class="container">
<div class="sidebar"></div>
<div class="main">
<div class="content">
<div class="scroll">
<div class="overflowing">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non lorem blandit, aliquet augue in, egestas risus. Curabitur sit amet justo eget metus faucibus sodales. Vestibulum rhoncus vel libero id imperdiet. Quisque ante quam, tempus in metus a, aliquam sollicitudin tortor. Nam in sagittis nunc, et feugiat augue. Phasellus augue lacus, maximus et ipsum ac, placerat tincidunt risus. Curabitur velit diam, fermentum ac quam eget, ultricies elementum ipsum. Nullam justo dolor, consequat porttitor semper a, eleifend vitae ante. Phasellus egestas dolor sed erat dapibus, a scelerisque dui sagittis. Pellentesque eget venenatis nisi. Vestibulum neque nisl, cursus ut sagittis a, ultrices ac nunc. Etiam auctor nunc porta leo fermentum, a iaculis leo vestibulum. Donec lobortis, tellus a aliquet malesuada, ipsum elit sagittis lectus, sed mollis magna diam eu mauris. Vivamus semper nunc eget nunc lacinia pharetra.</div>
</div>
</div>
<div class="panel"></div>
</div>
</div>
Bug reports:
https://bugzilla.mozilla.org/show_bug.cgi?id=1043520
https://github.com/angular/material/issues/6841
https://code.google.com/p/chromium/issues/detail?id=580196
I have 3 divs inside a div wrapper.
Div 1 is set to float:left, Div 3 to float:right.
How can I force Div 2 to take up all the space left, be centered and that all 3 are still in one row?
If i try margin: 0 auto; or overflow: hidden; Div 3 doesn't stay in the same row.
.div_wrapper {
width: 600px;
}
.div_1{
float: left;
}
.div_2 {
margin: 0 auto; NOT WORKING
overflow: hidden; NOT WORKING
}
.div_3 {
width: 60px;
float: right;
}
If you float all three to the left and set their widths to 33.33333%.
The trick to your problem is with the order of your <div> elements, and setting overflow:auto on the parent. The <div> is a block-level element by default, so you don't need to do anything special for it to try to use all available width.
Notice the order of the children: 1, 3, 2
<style>
.div_wrapper {width: 600px; overflow:auto;}
.div_1 {width:60px; float:left;}
.div_2 {}
.div_3 {width:60px; float:right;}
</style>
<div class="div_wrapper">
<div class="div_1">Morbi mollis iaculis egestas. Nullam quis nisi at lorem laoreet congue non sollicitudin ante.</div>
<div class="div_3">Pellentesque sodales, justo eget iaculis egestas, nulla lectus consectetur est, pharetra rhoncus purus tortor vel mauris. </div>
<div class="div_2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent id justo quis purus suscipit finibus nec eu augue. Suspendisse sollicitudin rhoncus risus, sit amet consequat velit suscipit non. Vivamus faucibus faucibus nisl eu gravida. Etiam id ipsum non lorem semper tincidunt. Suspendisse potenti. Quisque massa orci, venenatis at purus sed, maximus ullamcorper sem. Cras non sollicitudin quam, ut iaculis felis. Donec rhoncus nisi quis sapien imperdiet tincidunt nec et massa. Mauris felis leo, viverra in consectetur quis, malesuada non neque. Cras at magna massa. Cras non tellus vel ex euismod blandit et non dolor. Nullam at ex nisi. Integer feugiat congue euismod. Quisque consequat urna eget ligula finibus pellentesque.</div>
</div>
You can simply use another wrapper inside the first wrapper with the max width of 540px, if 600px is the max width which is also floated to the left. And inside the inner_wrapper it you can simply assign them again a % width.
HTML:
<div class="div_wrapper">
<div class="div_inner_wrapper">
<div class="div_1_2 div_1"></div>
<div class="div_1_2 div_2"></div>
</div>
<div class="div_1_2_3 div_3"></div>
</div>
CSS:
.div_wrapper {
width: 600px;
}
.div_inner_wrapper {
float: left;
width: 540px;
}
.div_1_2 {
float: left;
width:50%;
}
.div_1 {
height: 30px; // only here for displaying the div
background-color: green; // only here for displaying the div
}
.div_2 {
height: 30px; // only here for displaying the div
background-color: yellow; // only here for displaying the div
}
.div_3 {
float:left;
width:60px;
height: 30px; // only here for displaying the div
background-color: red; // only here for displaying the div
}
Example
This question already has answers here:
How do you get the footer to stay at the bottom of a Web page?
(32 answers)
Closed 8 years ago.
I am trying to have my footer (just a div with a line of text in it) be at the bottom of the screen if the content doesn't go all the way to the bottom, or be at the bottom of the content if the content requires scroll bars. If the content doesn't require scroll bars, it works perfectly, but when the content is too long, the footer is still in the same spot, sitting right on top of the content.
My basic div structure is:
<div id="container">
<div id="body"></div>
<div id="footer"></div>
</div>
My corresponding CSS (stripped down somewhat):
body {
margin: 0;
padding: 0;
height: 100%;
}
html {
margin: 0;
padding: 0;
height: 100%;
}
#container {
width: 674px;
min-height: 100%;
height: 100%;
position: relative;
margin: 0 auto;
}
#body {
width: 616px;
padding: 5px 14px 5px 14px;
margin: 0 auto;
padding-bottom: 20px;
}
#footer {
position: absolute;
bottom: 0;
width: 644px;
height: 20px;
margin: 0 auto;
}
The simplest solution is to use min-height on the <html> tag and position the <footer> with position:absolute;
Demo: jsfiddle and SO snippet:
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px;
/* bottom = footer height */
padding: 25px;
}
footer {
background-color: orange;
position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
overflow: hidden;
}
<article>
<!-- or <div class="container">, etc. -->
<h1>James Dean CSS Sticky Footer</h1>
<p>Blah blah blah blah</p>
<p>More blah blah blah</p>
</article>
<footer>
<h1>Footer Content</h1>
</footer>
Why not using: { position: fixed; bottom: 0 } ?
A simple solution that i use, works from IE8+
Give min-height:100% on html so that if content is less then still page takes full view-port height and footer sticks at bottom of page. When content increases the footer shifts down with content and keep sticking to bottom.
JS fiddle working Demo: http://jsfiddle.net/3L3h64qo/2/
Css
html{
position:relative;
min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
margin:0;
padding:0;
}
.pageContentWrapper{
margin-bottom:100px;/* Height of footer*/
}
.footer{
position: absolute;
bottom: 0;
left: 0;
right: 0;
height:100px;
background:#ccc;
}
Html
<html>
<body>
<div class="pageContentWrapper">
<!-- All the page content goes here-->
</div>
<div class="footer">
</div>
</body>
</html>
Use this one. It will fix it.
#ibox_footer {
padding-top: 3px;
position: absolute;
height: 20px;
margin-bottom: 0;
bottom: 0;
width: 100%;
}
Use min-height as some pixel value, instead of %.
Like:
min-height:620px;
height:auto;
and footer as:
.footer {
height:70px;
clear:both;
position:relative;
bottom:0;
width: 100%;
}
do it using jQuery put inside code on the <head></head> tag
<script type="text/javascript">
$(document).ready(function() {
var docHeight = $(window).height();
var footerHeight = $('#footer').height();
var footerTop = $('#footer').position().top + footerHeight;
if (footerTop < docHeight) {
$('#footer').css('margin-top', 10 + (docHeight - footerTop) + 'px');
}
});
</script>
This should help you.
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer {
height: 155px;
}
the easiest hack is to set a min-height to your page container at 400px assuming your footer come at the end. you dont even have to put css for the footer or just a width:100% assuming your footer is direct child of your <body>
The model being shared here is very similar to Ryan Fait's StickyFooter
http://ryanfait.com/sticky-footer
Just one div is missing so far in this discussion (the model proposed here by Kenneth Palanganas worked fine for local Win81 design for about 48 hours and then in ie/chrome collapsed for unknown reason). Ryan's "push" div will satisfy some reluctant browsers. Note that px is usual, however, for liquid layout consistency, em may be preferred.
* { border: 0; margin: 0; padding: 0; }
html, body { height: 100%; }
.wrapper { height: auto !important; height: 100%; margin: 0 auto -1em; min-height: 100%; }
.footer, .push { height: 1em; }
<div class="wrapper"><p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer"><p>This is a footer</p>
</div>
I would like to share how I solved mine using Javascript function that is called on page load. This solution positions the footer at the bottom of the screen when the height of the page content is less than the height of the screen.
function fix_layout(){
//increase content div length by uncommenting below line
//expandContent();
var wraph = document.getElementById('wrapper').offsetHeight;
if(wraph<window.innerHeight){ //if content is less than screenheight
var headh = document.getElementById('header').offsetHeight;
var conth = document.getElementById('content').offsetHeight;
var footh = document.getElementById('footer').offsetHeight;
//var foottop = window.innerHeight - (headh + conth + footh);
var foottop = window.innerHeight - (footh);
$("#footer").css({top:foottop+'px'});
}
}
function expandContent(){
$('#content').append('<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at ante. Mauris eleifend, quam a vulputate dictum, massa quam dapibus leo, eget vulputate orci purus ut lorem. In fringilla mi in ligula. Pellentesque aliquam quam vel dolor. Nunc adipiscing. Sed quam odio, tempus ac, aliquam molestie, varius ac, tellus. Vestibulum ut nulla aliquam risus rutrum interdum. Pellentesque lorem. Curabitur sit amet erat quis risus feugiat viverra. Pellentesque augue justo, sagittis et, lacinia at, venenatis non, arcu. Nunc nec libero. In cursus dictum risus. Etiam tristique nisl a nulla. Ut a orci. Curabitur dolor nunc, egestas at, accumsan at, malesuada nec, magna.</p>'+
'<p>Nulla facilisi. Nunc volutpat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut sit amet orci vel mauris blandit vehicula. Nullam quis enim. Integer dignissim viverra velit. Curabitur in odio. In hac habitasse platea dictumst. Ut consequat, tellus eu volutpat varius, justo orci elementum dolor, sed imperdiet nulla tellus ut diam. Vestibulum ipsum ante, malesuada quis, tempus ac, placerat sit amet, elit.</p>'+
'<p>Sed eget turpis a pede tempor malesuada. Vivamus quis mi at leo pulvinar hendrerit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque aliquet lacus vitae pede. Nullam mollis dolor ac nisi. Phasellus sit amet urna. Praesent pellentesque sapien sed lacus. Donec lacinia odio in odio. In sit amet elit. Maecenas gravida interdum urna. Integer pretium, arcu vitae imperdiet facilisis, elit tellus tempor nisi, vel feugiat ante velit sit amet mauris. Vivamus arcu. Integer pharetra magna ac lacus. Aliquam vitae sapien in nibh vehicula auctor. Suspendisse leo mauris, pulvinar sed, tempor et, consequat ac, lacus. Proin velit. Nulla semper lobortis mauris. Duis urna erat, ornare et, imperdiet eu, suscipit sit amet, massa. Nulla nulla nisi, pellentesque at, egestas quis, fringilla eu, diam.</p>'+
'<p>Donec semper, sem nec tristique tempus, justo neque commodo nisl, ut gravida sem tellus suscipit nunc. Aliquam erat volutpat. Ut tincidunt pretium elit. Aliquam pulvinar. Nulla cursus. Suspendisse potenti. Etiam condimentum hendrerit felis. Duis iaculis aliquam enim. Donec dignissim augue vitae orci. Curabitur luctus felis a metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In varius neque at enim. Suspendisse massa nulla, viverra in, bibendum vitae, tempor quis, lorem.</p>'+
'<p>Donec dapibus orci sit amet elit. Maecenas rutrum ultrices lectus. Aliquam suscipit, lacus a iaculis adipiscing, eros orci pellentesque nisl, non pharetra dolor urna nec dolor. Integer cursus dolor vel magna. Integer ultrices feugiat sem. Proin nec nibh. Duis eu dui quis nunc sagittis lobortis. Fusce pharetra, enim ut sodales luctus, lectus arcu rhoncus purus, in fringilla augue elit vel lacus. In hac habitasse platea dictumst. Aliquam erat volutpat. Fusce iaculis elit id tellus. Ut accumsan malesuada turpis. Suspendisse potenti. Vestibulum lacus augue, lobortis mattis, laoreet in, varius at, nisi. Nunc gravida. Phasellus faucibus. In hac habitasse platea dictumst. Integer tempor lacus eget lectus. Praesent fringilla augue fringilla dui.</p>');
}
/*sample CSS*/
body{ background: black; margin: 0; }
#header{ background: grey; }
#content{background: yellow; }
#footer{ background: red; position: absolute; }
#header, #content, #footer{ display: inline-block; width: 100vw; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onload="fix_layout()">
<div id="wrapper">
<div id="header" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
[some header elements here]
</div>
<div id="content" class="container">
[some content elements here]
</div>
<div id="footer" class="footer">
[some footer elements here]
</div>
</div>
</body>
Hope that helps.