I have navigation bar that is fixed to the top of the page when user scroll down.
I also need it to be fixed to the top left corner of the right block when user scroll horizontally. How to do this? Thanks for any advice.
CSS:
#box {
margin: 0 auto;
}
#content {
height: 500px;
width: 1000px;
}
#left {
max-width: 20%;
display: inline-block;
background: #aaa;
}
#right {
max-width: 80%;
display: inline-block;
background: red;
height: 100%;
width: 100%;
}
nav {
position: fixed;
top: 0;
height: 50px;
background: #666;
}
HTML:
<div id="box">
<div id="content">
<div id="left">LEFT</div>
<div id="right">
<nav>
Some text
</nav>
RIGHT
</div>
</div>
</div>
jsfiddle here: https://jsfiddle.net/deguac8y/
You can use jQuery to detect scrolling horizontally and by using class you can switch between having a fixed position nav to absolute position nav when horizontal scrolling is detected without losing the current scroll position.
JSFiddle
$(document).ready(function (){
var detectScroll = 0;
$(window).scroll(function () {
var documentScrollLeft = $(document).scrollLeft();
if (detectScroll != documentScrollLeft) {
detectScroll = documentScrollLeft;
$('nav').addClass('notFixed');
var scrollTop = $(window).scrollTop();
$('nav').css('top',scrollTop);
} else {
$('nav').removeClass('notFixed');
$('nav').css('top','auto');
}
});
});
body {
margin:0
}
#box {
margin: 0 auto;
}
#content {
height: 500px;
width: 1000px;
}
#left {
max-width: 20%;
display: inline-block;
background: #aaa;
}
#right {
max-width: 80%;
display: inline-block;
background: red;
height: 100%;
width: 100%;
}
nav {
position: fixed;
height: 50px;
background: #666;
}
nav.notFixed {
position:absolute;
top:auto;
left:auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="box">
<div id="content">
<div id="left">LEFT</div>
<div id="right">
<nav>Some text</nav>RIGHT</div>
</div>
</div>
Related
I want the fixed progress bar have same width as the yellow container and always positioned at the bottom of screen which overlapping on top of yellow container with CSS.
I have try to using width: inherit in the fixed child but the yellow parent do not have width so it doesn't work.
.container {
display: flex;
width: 500px;
height: 1200px;
}
.left {
flex: 1;
background: blue;
}
.right {
flex: 1;
background: yellow;
}
.progress {
position: fixed;
background: gray;
bottom: 0;
}
<div class="container">
<div class="left">
</div>
<div class="right">
<div class="progress">I'm progress bar</div>
</div>
</div>
Position fixed will be fixed to the page's scroll position and should not be used as a child of a static element.
Instead, you should set position to absolute on the progress bar, and position relative on the container.
.container {
position: relative;
}
.child {
position: absolute;
width: 100%;
left: 0;
}
.container {
position: relative;
display: flex;
width: 500px;
height: 100px;
}
.left {
flex: 1;
background: blue;
}
.right {
flex: 1;
background: yellow;
}
.progress {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background: gray;
}
<div class="container">
<div class="left">
</div>
<div class="right">
<div class="progress">I'm progress bar</div>
</div>
</div>
Alternatively, you can use JavaScript to match two elements width's while keeping position fixed using offsetWidth.
function resizeProgress() {
const container = document.querySelector('.container');
const progress = document.querySelector('.progress');
progress.style.width = container.offsetWidth + 'px';
progress.style.left = container.offsetLeft + 'px';
}
function resizeProgress() {
const container = document.querySelector('.container');
const progress = document.querySelector('.progress');
progress.style.width = container.offsetWidth + 'px';
progress.style.left = container.offsetLeft + 'px';
}
resizeProgress();
.container {
position: relative;
display: flex;
width: 500px;
height: 1200px;
}
.left {
flex: 1;
background: blue;
}
.right {
flex: 1;
background: yellow;
}
.progress {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background: gray;
}
<div class="container">
<div class="left">
</div>
<div class="right">
</div>
</div>
<div class="progress">I'm progress bar</div>
Is there a way for stickies to take into account other stickes on the page?
For example:
body {
display: flex;
min-height: 2000px;
flex-direction: column;
}
#header {
height: 40px;
flex: 0 0 auto;
position: sticky;
top: 0;
background: yellow;
}
#footer {
flex: 0 0 auto;
height: 20px;
}
#main {
display: flex;
flex: auto;
background: blue;
}
#side {
width: 200px;
background: red;
}
#side > div {
position: sticky;
top: 0px;
}
<div id="header">header</div>
<div id="main">
<div id="side">
<div>side</div>
</div>
<div id="content">
content
</div>
</div>
<div id="footer">footer</div>
Notice that if I scroll down the header will overlap the sidebar because they have the same top position.
To fix I have to make the top position of the sidebar take the value of the header height
body {
display: flex;
min-height: 2000px;
flex-direction: column;
}
#header {
height: 40px;
flex: 0 0 auto;
position: sticky;
top: 0;
background: yellow;
}
#footer {
flex: 0 0 auto;
height: 20px;
}
#main {
display: flex;
flex: auto;
background: blue;
}
#side {
width: 200px;
background: red;
}
#side > div {
position: sticky;
top: 40px;
}
<div id="header">header</div>
<div id="main">
<div id="side">
<div>side</div>
</div>
<div id="content">
content
</div>
</div>
<div id="footer">footer</div>
But what if the header has variable height? Can I tell the browser somehow to position the stickies so they dont overlap others?
I think you would need to use javascript for this. First to get the height of the header and then set the top position of your side div using that value. I am not aware of any pure css way of doing it I am afraid.
If you are using jQuery it is simply using the .height() method if not you can use this:
var clientHeight = document.getElementById('myDiv').clientHeight;
var offsetHeight = document.getElementById('myDiv').offsetHeight;
The offset method gets the height with any padding and borders.
I need to have a fixed height header, a fixed height footer, and a 100% height div sandwiched in between (it's holding a full page background image). I'm using a sticky footer, as this is a template that will also be used for pages with regular content that might overflow (without the background image). This works on the regular pages, but on the page that requires a 100% height container, it fails. I can't get the 100% height div to expand to 100%. Any help would be much appreciated.
Here's the test page showing the problem I'm having with the 100% height div: http://www.dunnandtigheinteriors.com/new/wp-content/themes/dunntighe/testhome.html
html, body {
margin: 0;
padding: 0;
height: 100%;
}
body {
font-family: Georgia, Times, "Times New Roman", serif;
background: #EFEFEF;
font-size: 14px;
}
#wrapper {
min-height: 100%;
/* height: 100%;
position: relative; Required to absolutely position the footer
text-align: center;*/
}
#headerHolder {
width: 100%;
background: #FFFFFF;
height: 50px;
}
#bkgHolder {
width: 100%;
height: calc(100% - 100px);
background: #DEDFE1;
padding-bottom: 25px;
background-image: url('images/DunnTigheWhiteOverlay.png');
background-repeat: no-repeat;
background-position: center;
background-size: auto 100%;
overflow:auto;
padding-bottom: 25px; /* must be same height as the footer */
}
#footerHolder {
text-align: center;
font-size: 10px;
height: 25px;
color: #888888;
background-color: #0074a2;
/* position: absolute;*/
/* bottom: 0; Sit it on the bottom
left: 0;*/
width: 100%; /* As wide as it's allowed */
position: relative;
margin-top: -25px;
clear:both;
}
#footerHolder p {
padding: 5px 0 0 0;
margin: 0;
}
#pageText {
overflow:auto;
padding-bottom: 25px; /* must be same height as the footer */
}
.pageContent {
text-align: left;
width: 680px;
margin: 0 auto;
padding-bottom: 15px;
}
and html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="test.css" media="screen" />
</head>
<body>
<div id='wrapper'>
<div id="headerHolder">
</div>
<div id="bkgHolder">
<div id='content'>
some content here
</div>
</div>
</div>
<div id="footerHolder">
<p>All Content and Images, Copyright © Dunn & Tighe Interiors</p>
</div>
</body> </html>
Something like this work?
http://jsfiddle.net/SinisterSystems/8aLg3/1/
HTML:
<div id="bkgHolder"></div>
<div class="wrapper">
<div id="header">
<h1>I'm the Sticky Header</h1>
</div>
<div id='content'>
<p>Some content here</p>
</div>
</div>
<footer class="footer">
I'm the Sticky Footer.
</footer>
CSS:
* {
margin: 0;
}
html, body {
height: 100%;
}
#header {
background:#CCC;
height:50px;
}
#bkgHolder {
background-image: url('http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg');
background-repeat: no-repeat;
background-position: center top;
background-size: cover;
width:100%;
height:100%;
position:absolute;
z-index:-10;
}
.wrapper {
min-height: 100%;
margin-bottom: -25px;
position:relative;
}
.wrapper:after {
content: "";
display: block;
}
.footer, .wrapper:after {
height: 25px;
}
.footer {
background:#6AF;
}
p {
color:#FFF;
}
EDIT:
You could always do something like this?
JS Fiddle: http://jsfiddle.net/SinisterSystems/8aLg3/6/
The last answer got convoluted. Let me identify what this does:
View Here: http://jsfiddle.net/SinisterSystems/8aLg3/9/
Static Header that will be at the top of your page
Static Footer that will remain on the bottom of your page
Implemented a jQuery function that will size and resize on window resizing your image to the current restrains without breaking aspect ratio.
BG Image will be inherently centered.
A javscript solution to this is not ideal, but is required for what you're asking to accomplish.
HTML:
<div id="bkgHolder"></div>
<div class="wrapper">
<div id="header">
<h1>I'm the Sticky Header</h1>
</div>
<div id='content'>
<p>Some content here</p>
</div>
</div>
<footer class="footer">
I'm the Sticky Footer.
</footer>
CSS:
* {
margin: 0;
}
html, body {
height: 100%;
}
#header {
background:#CCC;
height:50px;
}
#bkgHolder {
background-image: url('http://www.hdwallpapers.in/walls/abstract_color_background_picture_8016-wide.jpg');
background-repeat: no-repeat;
background-position: center;
background-size:contain;
width:100%;
height:100%;
position:absolute;
margin-top:50px;
z-index:-10;
}
.wrapper {
min-height: 100%;
margin-bottom: -25px;
position:relative;
}
.wrapper:after {
content: "";
display: block;
}
.footer, .wrapper:after {
height: 25px;
}
.footer {
background:#6AF;
}
p {
color:#FFF;
}
jQuery Script Addition:
$(function() {
var h = $(window).height();
var w = $(window).width();
$('#bkgHolder').css({
'width': w,
'max-height': h-75,
});
window.onresize = function(event) {
h = $(window).height();
w = $(window).width();
$('#bkgHolder').css({
'width': w,
'max-height': h-75,
});
}
});
View Here: http://jsfiddle.net/SinisterSystems/8aLg3/9/
I have my HTML structure like this:
<div id="pagewrap">
<div id="header">
</div>
<div id="content">
<div id="left"></div>
<div id="right"></div>
</div>
<div id="footer"></div>
</div>
I want to increase size of content div when either divs in content div increases as same size as other div.
How can I achieve this?
This is how my css is:
#pagewrap
{
width: 100%;
height: 100%;
}
#header{width: 100%;height:97px;position:relative;}
#left{position:absolute;left:0px;width:20%;background-color:#1C2326;}
#right{position:absolute;right:0px;width:80%;background-color:#2D3538;color:#fff;}
#footer{clear:both;height: 80px;background-color:#72D27C;}
If you want the wrapper to be affected by the contents' dimensions, you can't use position: absolute in the inner divs. Try floating them instead (and add overflow: hidden to the container to clear the inner floats):
#pagewrap { width: 100%; height: 100%; }
#content { overflow: hidden; }
#header { width: 100%; height: 97px; position:relative; }
#left { float: left; width: 20%; background-color: #1C2326; }
#right { float: left; width: 80%; background-color: #2D3538; color: #fff; }
#footer { height: 80px; background-color: #72D27C; }
http://jsfiddle.net/h4hbx/
I think maybe this fiddle is closer to what you had in mind. You can let the left div (static position, no float) set the height of content, and then pin the top and bottom of the right div to the content div. As left grows, content grows, and right is tied to content, giving you the effect you want. However, this is asymmetrical -- if you want either div to cause the other to follow it, that's another problem.
CSS:
html, body {
height: 100%;
}
#pagewrap {
width: 100%;
height: 100%;
}
#content {
position: relative;
}
#header {
width: 100%;
height:97px;
}
#left {
left:0px;
width:20%;
background-color:#1C2326;
color: #fff;
top: 0;
}
#right {
position:absolute;
right:0px;
width:80%;
background-color:#2D3538;
color:#fff;
bottom: 0;
top: 0;
}
#footer {
clear:both;
height: 80px;
background-color:#72D27C;
}
I seem to be having a problem with my footer growing to 100% width of the page. Currently when it expands there is a gap on each side of the footer. I tried putting the footer outside the the wrapper and inside and pretty much get the same results. I've attached my code to see if anyone can spot what im doing wrong.
<div id="wrapper"> <!--Begin Wrapper -->
<div id="riaandoviwrap">
<div id="riaandovi">Ria And Ovi</div>
</div>
<div id="slideshowwrap">
<div id="slideshow"><img src="images/DSC00495.JPG" /></div>
</div>
<div id="slideswrap">
<div id="slide1">SLIDE 1</div>
<div id="slide2">SLIDE 2</div>
<div id="slide3">SLIDE 3</div>
</div>
<hr />
<div id="contentwrap">
<div id="content"></div>
</div>
<div id="footerwrap">
<div id="footerleft">© 2012 Ria and Ovi</div>
<div id="footerright">Share this on:</div>
</div>
</div> <!--End Wrapper -->
body {
background: #f7f6f6;
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
}
#wrapper {
width: 100%
}
#riaandoviwrap {
width: 300px;
min-height: 150px;
}
#riaandovi {
font-family: Script;
font-size: 75px;
}
#slideshowwrap {
width: 950px;
background: url(../images/slider-bg2.png);
clear: both;
}
#slideshow {
min-height: 350px
}
#slideswrap {
width: 950px;
min-height: 100px;
background: #09F;
margin-top: 6px;
clear: both;
}
#slide1 {
width: 300px;
float: left;
}
#slide2 {
width: 300px;
float: left;
}
#slide3 {
width: 300px;
float: left;
}
#contentwrap {
}
#content {
}
#footerwrap {
min-height: 105px;
background: url(../images/footer-bg.png);
margin: 0px;
}
#footerleft {
width: 350px;
float: left;
}
#footerright {
width: 350px;
float: left;
}
hr {
max-width: 950px
}
img {
border: 5px solid #FFF
}
Set padding and margin to zero for the body tag. Althought you're not setting one manually, browsers do have a default padding/margin.
Include a reset sheet in your document to reset all of those default styles. Recommend Eric Meyer's since its more complete:
http://meyerweb.com/eric/tools/css/reset/