How to make content div full height when using padding on wrapper? - html

I know similar questions have been asked numerous times, but I've tried a number of answers and none seem to work for my situation. I've found a lot of solutions for sticky footers, but that's not exactly what I'm looking for, or maybe I haven't figured out how to use correctly for my situation:
I have fully fixed position navigation (header/sidebars/footer). The content flows on top of the header and footer.
My content wrapper layer is currently a 100% width/ 100% min-height
layer above the navigation layer, and using simple padding on top/bottom and margins on left/right, I'm able to show the navigation elements.
Inside the wrapper layer I have my content div, which has my page
background and a box-shadow. But I can't get the content div to
expand to the full height of the wrapper, because the wrapper height
is based on percentage. So everything works perfectly when I have
content that fills or overflows the window, but when I don't, the content div is too small.
Sticky footer doesn't work in this situation because the sticky footer just covers the content (the content itself is still 100% height). I basically want the content to be min-height 100% - minus the 50px header and 50px footer.
Is there any css solution to this without using a javascript hack or calc()?
I would be willing to have the header + footer not be fix positioned - but I want to keep the box-shadow on the content div (i.e. 50px from top and bottom of the page).
JSFiddle:
https://jsfiddle.net/tyhenry/n9rpbj3m/5/
HTML:
<div id="nav">
<div id="header">
header
</div>
<div id="left-side">
left sidebar
</div>
<div id="right-side">
right sidebar
</div>
<div id="footer">
footer
</div>
</div>
<div id="page-layer">
<div class="page">
content<br>
content<br>
content<br>
</div>
</div>
CSS:
* {
box-sizing: border-box;
}
html, body{
background-color: #eeffff;
text-align: center;
margin: 0;
padding: 0;
height: 100%;
}
#header {
position: fixed;
top:0;
left:0;
width: 100%;
height: 50px;
background-color: #ddd;
}
#left-side {
background-color: #bbb;
position: fixed;
left: 0;
top: 50px;
width: 50px;
height: 100%;
}
#right-side {
background-color: #bbb;
position: fixed;
right: 0;
top: 50px;
width: 50px;
height: 100%;
overflow: hidden;
}
#footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 50px;
background-color: #aaa;
}
#page-layer{
position:relative;
padding: 50px 0 50px 0;
margin: 0 50px 0 50px;
min-height: 100%;
}
.page {
background-color: #fff;
box-shadow: 0px 1px 20px 0px rgba(0, 0, 0, 0.8);
width:100%;
}

If you change the display property to flex it fills the whole (height between header and footer) and adjusts when you resize.
#page-layout {
display: flex;
}

give this you the desirable outcome ? can i use viewport units
add:
.page {
min-height: 100vh; /*min-height not height :)*/
}

Try this updated fiddle https://jsfiddle.net/n9rpbj3m/6/
#page-layer{
position:absolute;
top:50px;
left:50px;
right:50px;
bottom:50px;
}
.page {
position:relative;
background-color: #fff;
box-shadow: 0px 1px 20px 0px rgba(0, 0, 0, 0.8);
height:auto;
min-height: 100%;
width:100%;
}

Related

Border-box not working, overlay div is including the padding

I'm creating a gallery with images having an overlay dark background and caption text. The placement is alright but the overlay div is falling out of the bounds of the image because a padding is used on the container element.
I read about it at several places and learned that border-box could solve this problem but it isn't. Am I doing something wrong here? Check out the code:
HTML:
<div class="dest-item">
<img src="http://lorempixel.com/500/400">
<div class="dest-caption">
<div class="dest-text">
<h3>This is a caption</h3>
</div>
</div>
</div>
CSS:
.dest-item{
position:relative;
overflow:hidden;
z-index:1;
padding:10px;
width: 500px;
}
.dest-item img{
width: 100%;
}
.dest-caption{
position: absolute;
top: 0px;
background: rgba(0,0,0,0.2);
z-index: 2;
width: 100%;
height: 100%;
box-sizing: border-box;
}
.dest-text{
position: absolute;
bottom: 0;
background: rgba(255,255,255,0.9);
width: 100%;
padding: 0px 10px;
}
Playground link: Code Pen
Try this (fork here:http://codepen.io/anon/pen/RNqbjB)
CSS:
/*remove the padding*/
.dest-item{
position:relative;
overflow:hidden;
z-index:1;
padding:0px;
width: 500px;
}
HTML:
<!--Add a wrapper and add the padding to that-->
<div style="padding:10px;">
<div class="dest-item">
<img src="http://lorempixel.com/500/400">
<div class="dest-caption">
<div class="dest-text">
<h3>This is a caption</h3>
</div>
</div>
</div>
</div>
Remove the padding from whole dest-item div. you don't need that padding over there as I think:
.dest-item {
position: relative;
overflow: hidden;
z-index: 1;
/* padding: 10px; */
width: 500px;
}
Not sure if this is along the right lines?
.dest-item{
position:relative;
overflow:hidden;
z-index:0;
padding:10px;
width: 500px;
}
.dest-item img{
width: 100%;
}
.dest-caption{
position: absolute;
top: 0;
left: 0;
background: rgba(0,0,0,0.2);
width: 100%;
height: 100%;
}
.dest-text{
color: black;
position: absolute;
text-shadow: 2px 2px 40px rgba(255,255,255, 1);
bottom: 0;
left: 0;
background: rgba(255,255,255,0.38);
width: 100%;
padding: 2px 0px 10px 20px;
}
<div class="dest-item">
<img src="http://lorempixel.com/500/400">
<div class="dest-caption">
<div class="dest-text">
<h3>This is a caption</h3>
</div>
</div>
</div>
You don't need border-box to do what you want.
There are 3 types of box-sizing:
content-box is the default behavior and only includes the width and height. It does not account for padding or border width. If you have an element with 500px width + 10px padding + 1px border then the display size of the whole element is 522px wide and the size of the available space for actual content is 500px.
padding-box includes the padding but not the border. Same example as above, if you are using padding-box then the display size is 502px but the available content space is 480px.
border-box covers everything. So in our example, the display size is 500px but available space for content is 478px.
Margins are never counted in the size, in any case.
Depending on how you want the end result to look, then you will achieve this differently but based on your Code Pen sample, it looks like you want to fill the entire item container so the overlay cover the 10px padding as well. You can do this without changing the box-sizing for anything.
First, you need to offset your .dest-caption element to the left by 10px to account for the padding. Then you need to add 10px padding and remove the border-box attribute.
Like this:
.dest-caption {
position: absolute;
top: 0px;
left: -10px; /* offset */
background: rgba(0,0,0,0.5);
z-index: 2;
width: 100%;
height: 100%;
padding: 10px; /* add padding */
}
With that fixed, your text and its box is misaligned and the size of the box is not well-controlled. It is affected by the margin of the H3 tag. Fix this by removing the margins from the H3 tag inside of any .dest-text elements:
.dest-text H3 {
margin: 0px;
}
Without the margins on the H3 tag, the text overlay actually disappears out of the drawable area because it's misaligned. You can fix this by offsetting .dest-text from the bottom by the .dest-caption padding width (x2). You will probably also want top and bottom padding for .dest-text.
.dest-text {
position: absolute;
bottom: 20px; /* account for padding on .dest-caption */
background: rgba(255,255,255,0.9);
width: 100%;
padding: 10px 10px; /* add top/bottom padding */
}
Code Pen Link

Fixed sidebar, content under it, how to fix?

I'm trying to make a fixed sidebar, that will stay on that position when you scroll the page(as in the 2nd image), but the page content is going under that sidebar, not on its right (first image, you can see the blue part on the sidebar, that's the div going under the bar.)
Img1:
img2:
(I'm using images links because i can't post them)
html:
<div class='barralado'>
~sidebar content~
</div>
<div class='conteudo'>
<div class='inicio'>
<div class='topo'>
<p class='titulo'>Liga Juizforana</p>
</div>
</div>
</div>
css:
.barralado {
position: fixed;
top: 0;
left: 0;
}
.conteudo {
}
.conteudo .topo {
background-color: #ffffff;
}
.topo .titulo {
font-size: 4em;
color: white;
text-shadow: 2px 2px 1px rgba(150, 150, 150, 1);
margin-top: 3em;
margin-left: 3.5em;
}
I tried to put both to float left, i tried to clear, i tried all the positions on both, and it didn't worked
2nd mini question: how to make the "conteudo" div 100vh after put it on the right of the sidebar? When i try it, it doesn't change to 100vh.
A fixed positioned div will be fixed on the page, even if you scroll it. And all the stuff goes under it (it has z-index priority).
To fix it, give your content div left padding equal to the width of your sidebar.
.barralado {
position: fixed;
top: 0;
left: 0;
min-height: 300px; // I suggest you give a value to your sidebar.
width:300px; //This is your width;
}
.contneudo{
padding-left:300px; //This is the padding that will go around your sidebar
}
If your sidebar has a fixed width you could using padding to the left of your content container of the sidebar width. Or you could float it to the right and set the width with the CSS calc function.
.conteudo {
width: -webkit-calc(100% - 200px);
width: calc(100% - 200px);
float:right;
}
Using your code and padding option:
.barralado {
position: fixed;
top: 0;
left: 0;
width: 300px;
}
.conteudo {
padding-left: 300px;
}
.conteudo .topo {
background-color: #ffffff;
}
.topo .titulo {
font-size: 4em;
color: white;
text-shadow: 2px 2px 1px rgba(150, 150, 150, 1);
margin-top: 3em;
margin-left: 3.5em;
}
There are a few other ways but hopefully one of the 2 options will help you achieve what you want.
I find it easier to set a grid for your body content, with an empty spacer div, and then your main content within the div.
HTML File:
<div class="gridWrapper">
<div class="spacer"> </div>
<div class="yourMainContent"> Your content here</div>
</div>
CSS File:
.gridWrapper {
display: grid;
grid-template-columns: 250px 1fr;
}
In this scenario of fixed widths, you can make your content div absolute positioned, to avoid going under your sidebar:
.conteudo {
position: absolute;
top: 0;
left: 192px; // this is your sidebar width, according to your screenshot
}
or if for some reason you need to avoid absolute positioning, you can offset it with margin:
.conteudo {
margin-left: 192px; // this is your sidebar width, according to your screenshot
}

Placing footer at bottom AND expand it to full screen WITHOUT re-aligning HTML

I have a footer which expands to full width of screen and my content area is 960px wide. I want the footer to always be at bottom of the page. Now the problem is that I cannot place footer's html at the end since I am following the NopCommerce framework and I must place all of my content inside the two main wrapper divs which are 960px & 930px wide.
I know that I can make my footer expand beyond the container div as mentioned at Extend child div beyond container div. But I want to know whether this can be achieved without using JS? Setting footer to position:absolute; bottom:0; right:0; left:0 does not work as the footer gets stuck if I scroll down on page. I also cannot use position:fixed and bottom:0 as it will make other content scroll below it, which is not what I want.
HTML:
<div class="master-wrapper-page">
<div class="master-wrapper-content">
<footer></footer>
</div>
</div>
CSS:
.master-wrapper-page {
margin: 2px auto 0;
background: none repeat scroll 0 0 #fff;
box-shadow: 0 0.5em 2em rgba(0, 0, 0, 0.1);
margin: 30px auto 0;
width: 960px;
}
.master-wrapper-content {
background: none repeat scroll 0 0 #ffffff;
float: left;
margin: 15px 15px 0;
padding: 15px 0 0;
text-align: left;
width: 930px;
margin: 0;
}
footer {
background-color: #1a1a1a;
clear: both;
height: 248px;
z-index: 90000;
}
Here is the jsfiddle in case it helps: http://jsfiddle.net/6rLco5kq/
Thanks.
Look here. Do you mean that ?
footer {
position: absolute;
bottom: 0px;
width: 100%;
background-color: #1a1a1a;
clear: both;
height: 248px;
z-index: 90000;
}
http://jsfiddle.net/6rLco5kq/1/
I was messing around with your jsfiddle and you could add
position: fixed;
bottom: 0;
width: xx;
To your footer, and then add this to your master-wrapper-content to keep your content from overflowing onto your footer.
margin-bottom: 248px;

Changing div height according to window height

I've a html structure like:-
<body>
<div class="header">header</div>
<div class="content">
hello
</div>
<div class="footer">footer</div>
</body>
And the applied style on it are:-
<style>
body {
padding: 0px !important;
margin: 0px !important;
}
.header {
height: 30px;
background: gray;
}
.footer {
height: 30px;
background: green;
position: fixed;
bottom: 0px;
width: 100%;
}
.content{
background: yellow;
}
</style>
What I want is, the content div's height will be equal to the full height of the window except the header & footer part. Currently I'm just seeing a small yellow strip for the content part, as the text within it very minimal, the rest of the page is white. I want, the content div will occupy that place. I tried to use height : 100%; in the content div, but it didn't work. please help.
Try to modify your content class like:-
.content{
background: yellow;
position: fixed;
width: 100%;
top: 30px;
bottom: 30px;
}
The top and bottom is 30px as the height of header and footer is 30px. it'll work for you.
Try making a div class="wrapper" that surrounds your div class="content"... In the css give the .wrapper 100% width and height. I hope that helps.

CSS - header to stay in top of container

I have this container which can scroll the content. I would like the header in this container to always stay in the top.
http://jsfiddle.net/z9ze5/
Container:
.lists {
width: 300px;
height: 250px;
margin: 30px auto;
background: #39C;
overflow: scroll;
position: relative;
}
Header:
.box_header {
width: 100%;
height:30px;
overflow:hidden;
position: absolute;
margin: 0;
background: #DDD;
z-index: 999;
}
If you are willing to alter your mark-up, here is one way of doing it:
<div class="lists">
<header class="box_header">
<h1>HEADER 2</h1>
<div class="setting" id="btn2"></div>
</header>
<section class="content">
<p>Lorem Ipsum ....</p>
</section>
</div>
Wrap your scroll area in a <section> (or other block level element).
For your CSS:
.lists {
width: 300px;
height: 250px;
margin: 30px auto;
background: #39C;
position: relative;
}
section.content {
width: 300px;
height: 220px;
margin: 0 auto;
background: #39C;
position: relative;
top: 30px;
overflow: scroll;
}
Please see fiddle: http://jsfiddle.net/audetwebdesign/nGGXx/
More Advanced Example
If you study the following example:
http://jsfiddle.net/audetwebdesign/fBNTP/
uou can see how your scrolling boxes could be applied in a semi-flexible layout.
I lined up two scrolling boxes side by side and made their width proportionate to the width of the page.
The height is trickier to adjust. I fixed the height of the parent container, see the following rule:
.contentWrapper {
border: 1px solid red;
margin-top: 1.00em;
padding: 30px 0;
overflow: auto;
height: 400px;
}
If you change the height from 400px to some other value, the scrolling boxes will adjust themselves.
Hopefully, these examples will give you and others some more insights into how to build these more advanced layout designs.
If you want a non-css fix, add this listener...
$('.lists').scroll(function() {
$('.box_header', this).css('top', $(this).scrollTop()+'px');
});
and then change .lists css to give relative positioning
.box_header {
width: 100%;
height:30px;
overflow:hidden;
position: relative;
margin: 0;
background: #DDD;
z-index: 999;
}
Any position absolute within a position relative is absolute to the relative container. In order to have a header that stays in position, you'd need to position it above, not within, the scrolling container.
look at adding position: fixed to your header div .box_header. You may have to add padding of the height of the box header div to section.content but as you have that set to 30px that should be fine. IE6 and lower has issues with fixed positioning but hopefully we can live with that now - less people are using that than are still listening to Moby.