Prevent content getting covered changing from relative header to fixed header - html

I have a relative positioned header and when the user scrolls down the page, I want to shift to a fixed header. This is working fine except that the header ends up covering the element the user had scrolled to. Is it possible to prevent this so that the gap between the header and the element remains the same for a seamless transition?
$(function() {
var hh = $('header').outerHeight();
$(window).scroll(function() {
var scroll = $(window).scrollTop();
var last = 0;
if (scroll >= hh) {
$('body').addClass('sticky');
} else {
$('body').removeClass('sticky');
}
last = scroll;
});
});
body,html {
margin:0;
}
.site-container {
position:relative;
width:90%;
height:1500px;
margin:0 auto;
background:gray;
}
img {
width:100%;
}
header {
display:flex;
flex-direction:column;
width:100%;
background:white;
}
body.sticky header {
position:fixed;
width:inherit;
}
.top-bar {
background:purple;
color:white;
padding:3px 0;
text-align:right;
}
ul {
margin:0;
padding-left:10px;
list-style-type:none;
}
li {
display:inline-block;
margin-left:10px;
line-height:30px;
}
li:first-child {
margin-left:0;
}
.site-content {
padding:50px 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="site-container">
<header>
<div class="top-bar">(555) 555-5555</div>
<nav>
<ul>
<li>Home</li>
<li>Lorem</li>
<li>Ipsum</li>
</ul>
</nav>
</header>
<div class="site-content">
<img src="https://via.placeholder.com/800x400/0000FF/808080" />
</div>
</div>

There is a simpler solution using css only, without jQuery needed.
Try css property "position: sticky" - https://developer.mozilla.org/en-US/docs/Web/CSS/position
body,html {
margin:0;
}
.site-container {
position:relative;
width:90%;
height:1500px;
margin:0 auto;
background:gray;
}
img {
width:100%;
}
header {
display:flex;
flex-direction:column;
width:100%;
background:white;
position: sticky;
top: 0;
}
.top-bar {
background:purple;
color:white;
padding:3px 0;
text-align:right;
}
ul {
margin:0;
padding-left:10px;
list-style-type:none;
}
li {
display:inline-block;
margin-left:10px;
line-height:30px;
}
li:first-child {
margin-left:0;
}
.site-content {
padding:50px 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="site-container">
<header>
<div class="top-bar">(555) 555-5555</div>
<nav>
<ul>
<li>Home</li>
<li>Lorem</li>
<li>Ipsum</li>
</ul>
</nav>
</header>
<div class="site-content">
<img src="https://via.placeholder.com/800x400/0000FF/808080" />
</div>
</div>

Related

Why doesn't my div do margin under navbar when i try to expand it?

I have a small problem with my navbar. When I try to expand it by using toggle button (on media max-width), my green div stays in the same place without doing margin under my navbar. I hope somebody will help me with that. I guess the problem is with my static height of nav and top:60px;
nav
{
height:60px;
background-color:powderblue;
}
#media (max-width: 554px) {
.icon
{
display:block;
}
ul
{
flex-direction:column;
position:fixed;
top:60px;
width:100%;
display:none;
}
}
jsfiddle link: https://jsfiddle.net/h9s8a4yu/2/
The problem is with your positions and you have to change some CSS for small screens
*{
box-sizing:border-box;
padding:0;
margin:0;
text-decoration:none;
font-size:20px;
}
a
{
color:black;
}
nav
{
height:60px;
background-color:powderblue;
}
.logo
{
color:white;
font-size:40px;
float:left;
padding-left:30px;
}
ul
{
display:flex;
flex-direction:row;
float:right;
align-items:center;
justify-content:center;
list-style:none;
margin-right:30px;
display:block;
}
li
{
float:left;
margin-left:15px;
padding-top:20px;
}
.fa
{
float:right;
margin-right:15px;
padding-top:20px;
}
.icon
{
float:right;
display:none;
}
ul a:hover
{
background-color:red;
}
#media (max-width: 554px) {
nav {
height: auto;
}
.icon
{
display:block;
}
ul
{
flex-direction:column;
position:relative;
top:60px;
width:100%;
display:none;
float: none;
top: 0;
margin: 0;
}
ul.active
{
display:block;
}
li
{
float: none;
width:100%;
margin-left:0px;
border-bottom:2px solid black;
background-color:blue;
text-align:center;
padding:10px;
}
li:hover
{
background-color:red;
}
.clear {
clear: both;
}
}
}
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav>
<div class="logo">
Logo
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
<div class="clear"></div>
<ul id="expand_list">
<li>home</li>
<li>about</li>
<li>services</li>
<li>contact</li>
<li>feedback</li>
</ul>
</nav>
<div style="margin-top:30px;width:100%;height:300px;background-color:green;">
</div>
<script>
function myFunction(){
const list =document.getElementById("expand_list");
list.classList.toggle("active");
}
</script>
</body>
</html>
Yes, fixed height of 60px can be problematic. So it's better to make it flexible. And set 30% and 70% width to logo and navigation respectively.
nav {
/* height:60px; */
background-color:powderblue;
display: flex;
flex-direction: row;
width: 100%;
flex-wrap: wrap;
}
.logo
{
color:white;
font-size:40px;
float:left;
padding-left:30px;
width: 30%;
}
ul
{
display:flex;
flex-direction:row;
float:right;
align-items:center;
justify-content:center;
list-style:none;
/* margin-right:30px; */
width: 70%;
}
.icon
{
float:right;
display:none;
width: 70%;
}
If you position an element as fixed it will be taken out of natural flow and the green div won't be aware of it. And your active class should toggle display flex instead of block as you have set flex for ul above.
#media (max-width: 554px) {
...
ul
{
flex-direction:column;
/* position:fixed; */
/* top:60px; */
width:100%;
display:none;
}
ul.active
{
/* display:block; */
display: flex;
}
...
}

How do I center my logo in the navigation bar?

How do I center my logo in my Navigation bar? I kind of already have it centered, but the issue that I'm running into is when I place an image in the content area, the image covers up part of the logo. I do want the logo kind of dropped a little from the navigation links. I don't want a giant space between the logo and the content image. Is there a way I can achieve this? The image attached is the design I'm trying to accomplish. Thanks in advance!
body {
font-family:Georgia;
font-size:12pt;
}
* {
margin:0;
padding:0;
}
#header {
background-color:#000000;
height:100px;
margin:auto;
}
#header ul {
margin:0 auto;
width:35%;
padding:12px;
list-style: none;
}
#header li {
float: left;
}
#header a {
padding:0 14px;
text-align:center;
display:block;
text-decoration:none;
color:#FFFFFF;
font-size:18pt;
}
#header ul li a.logo {
background: url("Logo.png");
background-repeat:no-repeat;
height:140px;
display:block;
width:140px;
padding: 0;
.clearfix {
*zoom: 1;
}
.clearfix:after {
content: "";
clear: both;
display: table;
}
#MainContent {
}
#MainContent img {
margin-top:-10px;
position:absolute;
left:0;
width:100%;
}
}
<body>
<div id="header" class="clearfix">
<ul class="Nav">
<li>Home</li>
<li>Menu</li>
<li></li>
<li><a href="gallery.html" >Gallery</a></li>
<li>About</li>
</ul>
</div>
<div id="MainContent">
<img src="Photos/drinks.jpeg">
</div>
</body>
You can add a z-index to your .logo class. This will render it above the image.
e.g.
.logo {
z-index: 100;
}
https://developer.mozilla.org/de/docs/Web/CSS/z-index

How to hide scrollbar when Navigation drawer is open

I'm leaving the first post. I'd be happy if you help my code.
I'm making my homepage layout, But I was in trouble.
I want disable scrollbar of Main Contents Section when Navigation Drawer button is clicked.
I tried to #drawer-toggle:checked~html{overflow:hidden} but it's not working.
If you have any other way, please teach me.
body,html{
margin:0 auto;
height:100%
}
footer{
height:40px;
line-height:40px;
text-align:center
}
footer,header{
background:#ccc;
display:table-row
}
header{
background:#000;
color:#fff;
height:50px;
line-height:50px;
padding-left:50px;
position:fixed;
top:0;
width:100%;
z-index:7
}
main{
background:#eee
}
#container{
margin-top:50px
}
#drawer{
background:#fff;
height:100%;
left:-300px;
overflow-x:hidden;
padding:10px;
top:0;
width:85%;
max-width:250px;
z-index:9
}
#drawer,#drawer-toggle-label{
position:fixed
}
#drawer-toggle{
display:none
}
#drawer-toggle:checked~#drawer{
left:0;
top:0
}
#drawer-toggle:checked~#drawer-toggle-label{
background:rgba(0,0,0,.54);
height:100%;
width:100%
}
#drawer-toggle-label{
background:rgba(0,0,0,0);
height:50px;
left:0;
top:0;
width:50px;
z-index:8
}
#drawer-toggle-label:active{
background:#5c6bc0
}
#drawer-toggle-label:before{
background:#fff;
box-shadow:0 5px 0 #fff,0 10px 0 #fff;
content:'';
height:2px;
left:16px;
position:absolute;
top:19px;
width:18px
}
#wrapper{
display:table;
width:100%;
height:100%
}
<body>
<div id=wrapper>
<header>Header</header>
<div id=container>
<input type=checkbox id=drawer-toggle>
<label for=drawer-toggle id=drawer-toggle-label></label>
<nav id=drawer>
Drawer
<ul><li>Menu
<li>Menu
<li>Menu
</ul>
</nav>
<main>
<center>
<table style=height:1000px;width:640px;background:#fff>
<tr><td style=vertical-align:top>Main Contents
</table>
</center>
</main>
</div>
<footer>Footer</footer>
</div>
</body>
You're hoping to achieve something with CSS that is called the parent selector which isn't available, you cannot select a parent element of the clicked element in pure CSS. Your ~ selector only selects siblings (elements with the same parent) in the code. Besides this, your code has some lacking apostrophes and unclosed tags. I've updated your code, and added a JavaScript solution on the go.
var element = document.getElementById('drawer-toggle');
element.addEventListener('click', function(e) {
document.getElementsByTagName('body')[0].classList.toggle('hide-scroll');
})
body, html {
margin:0 auto;
height:100%;
}
footer {
height:40px;
line-height:40px;
text-align:center
}
footer, header {
background:#ccc;
display:table-row
}
header {
background:#000;
color:#fff;
height:50px;
line-height:50px;
padding-left:50px;
position:fixed;
top:0;
width:100%;
z-index:7
}
main {
background:#eee
}
#container {
margin-top:50px
}
#drawer {
background:#fff;
height:100%;
left:-300px;
overflow-x:hidden;
padding:10px;
top:0;
width:85%;
max-width:250px;
z-index:9
}
#drawer, #drawer-toggle-label {
position:fixed
}
#drawer-toggle {
display:none
}
#drawer-toggle:checked~#drawer {
left:0;
top:0
}
#drawer-toggle:checked~#drawer-toggle-label {
background:rgba(0,0,0,.54);
height:100%;
width:100%
}
#drawer-toggle-label {
background:rgba(0,0,0,0);
height:50px;
left:0;
top:0;
width:50px;
z-index:8
}
#drawer-toggle-label:active {
background:#5c6bc0
}
#drawer-toggle-label:before {
background:#fff;
box-shadow:0 5px 0 #fff,0 10px 0 #fff;
content:'';
height:2px;
left:16px;
position: absolute;
top:19px;
width:18px
}
#wrapper {
display:table;
width:100%;
height:100%
}
.hide-scroll {
overflow: hidden;
}
<div id="wrapper">
<header>Header</header>
<div id="container">
<input type="checkbox" id="drawer-toggle">
<label for="drawer-toggle" id="drawer-toggle-label"></label>
<nav id="drawer">
Drawer
<ul>
<li>Menu</li>
<li>Menu</li>
<li>Menu</li>
</ul>
</nav>
<main>
<center>
<table style="height:1000px;width:640px;background:#fff">
<tr><td style="vertical-align:top">Main Contents
</table>
</center>
</main>
</div>
<footer>Footer</footer>
</div>
When clicking the label, we toggle a class on the body which disables the overflow, thus disabling scrolling.

Float and margin CSS

I've created five different sections of images, displayed just as you can see in jsfiddle, the problem is that I can't control the margin of these imagesections. The thing is that I'm trying to add some space between section 1, 2, 3, 4 and 5, but whenever I add the margin the section just refuse to move.
I've tried to reset the float with the codes:
clear:both;
and
float: none;
but without success. The result of those codes is just that the section jumps to a new line.
The fiddle: https://jsfiddle.net/p0eaxkpz/
I found your problem, there is an error in your code: margin-left:0,5%; should be margin-left:0.5%;
Here is your updated jsfiddle
I have simply added margin: 0; padding: 0; to all elements, you can see it's working well. Basically, there is no need for you to wrap the images with <section> tags.
I also got a more modular aproach
#charset "UTF-8";
* {
margin: 0;
padding: 0;
}
body
{
background-color:black;
width:100%;
height:100%;
padding-bottom:20%;
}
#wrapper
{
width:80%;
height:1000px;
background-color:white;
margin:auto;
}
#headerloga
{
float:left;
width:20%;
height:20%;
font-family: "Gotham Medium";
font-weight:800;
font-size:40px;
padding-top:10%;
padding-left:10%;
}
#menu
{
float:left;
width:60%;
height:auto;
font-family:"Gotham Medium";
font-size:15px;
padding-top:17.5%;
padding-left:10%;
}
ul {
float: left;
width: auto;
padding: 0;
margin: 0;
list-style-type: none;
}
a {
float: left;
width: auto;
text-decoration: none;
color: black;
padding: 0.2em 0.6em;
}
a:hover {
color:#ED1C91;
}
li {
display: inline;
}
#imageshosting {
background-color:#CCC;
width:60%;
height:80%;
position:absolute;
clear:both;
top:40%;
left:18%;
}
.img-hosted {
display:block;
position:relative;
float:left;
height:49%;
margin:0.5%
}
.w33 {
width:32.33%;
}
.w66 {
width:65.66%;
}
.img-nextline {
clear: both;
}
#foot {
background-color:#666;
clear:both;
position:absolute;
top:120%;
left:18%;
width:60%;
height:10%;
}
<div id="wrapper"><!-- Webpage wrapper-->
<div id="headerloga">
Test
Test2
<section style="color:#ED178C; ">Test3 </section>
</div>
<div id="menu"><!--Menu-->
<ul>
<li>KUNDER</li>
<li>GAMLA FAVORITER</li>
<li>OM OSS</li>
<li>KONST</li>
<li>KONTAKT</li>
</ul>
</div><!-- End of menu-->
<!-- images starts-->
<div id="imageshosting">
<img class="img-hosted w33" src="images/testimage1.jpg"/>
<img class="img-hosted w33" src="images/testimage2.jpg"/>
<img class="img-hosted w33" src="images/testimage3.jpg"/>
<!-- New image line -->
<img class="img-hosted w66 img-nextline" src="images/testimage4.jpg"/>
<img class="img-hosted w33" src="images/testimage5.jpg"/>
</div><!--Imageshosting ends-->
<!--Footer here-->
<div id="foot">
<p>Hello</p>
</div>
</div><!-- End of webpage wrapper-->

CSS aligning to center

I was trying to align a div to the center inside of another div. I searched, and all answers say to put "margin:auto;" or "margin: 0px auto;". I tried both, and neither will work.
My CSS is:
#slider div
{
margin:0px auto;
width:620px;
}
EDIT: I've tried all that has been suggested so for, no change for anything.
Could it be that #slider is Jquery???
As requested, here is the entire pages code:
<html>
<head>
<script type="text/javascript" src="C:\Users\Pam\Desktop\jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="C:\Users\Pam\Desktop\easyslider1.7\js\easyslider1.7.js"></script>
<style type="text/css">
body
{
margin-top:0px;
margin-bottom:0px;
margin-left:0px;
margin-right:0px;
background-image:url('./bg.jpg');
}
#headerCon
{
display:block;
background-color:#151515;
width:100%;
height:40px;
padding-left:auto;
padding-right:auto;
position:fixed;
}
#header
{
width:950px;
height:100%;
margin-right:auto;
margin-left:auto;
}
#menu
{
margin-left:10%;
}
#title
{
background-image:url('./title.png');
background-repeat:no-repeat;
display:block;
height:123px;
margin:0px auto;
text-align:center;
}
#container
{
text-align:center;
display:block;
width:850px;
margin-right:auto;
margin-left:auto;
height:100%;
/* background-color:#dfdfdf; */
max-padding:100%;
position:relative;
}
a.link
{
text-decoration:none;
font-weight:bold;
font-family:Arial,Helvetica,sans-serif;
display:block;
width:100px;
height:30px;
border-right:1px solid #000000;
float:left;
color:#00af64;
padding-left:0px;
padding-top:10px;
text-align:center;
}
a.link:hover
{
text-decoration:none;
font-weight:bold;
font-family:Arial,Helvetica,sans-serif;
display:block;
width:100px;
height:30px;
border-right:1px solid #000000;
float:left;
color:#00af64;
padding-left:0px;
padding-top:10px;
text-align:center;
background-color:#252525;
}
p
{
color:#fff;
font-family:Arial,Helvetica,sans-serif;
}
#button1
{
margin-left:34%;
}
/* EASY SLIDER */
#slider div
{
position: absolute;
top: 50%;
left: 50%;
margin-top:2px;
margin-left:2px;
width:620px;
}
#sliderContain
{
position:absolute;
margin-left:3px;
margin-top:3px;
}
#slider ul, #slider li{
margin:0;
padding:0;
list-style:none;
}
#slider, #slider li{
width:620px;
height:230px;
overflow:hidden;
}
#transBorder
{
background-color:#111111;
opacity:0.60;
filter:alpha(opacity=60); /* For IE8 and earlier */
display:block;
height:236px;
width:626;
position:absolute;
z-index:-1;
float:up;
}
#slider1prev
{
display:none;
}
#slider1next
{
display:none;
}
</style>
</head>
<body>
<div id="headerCon">
<div id="header">
<div id="menu">
Home
Monsters
Areas
Trades
Classes
</div>
</div>
</div>
<div>
<br />
<br />
</div>
<div id="container">
<div id="title"> </div>
<div id="sliderContain">
<div id="slider">
<ul>
<li><img src="./reddragon.png"/></li>
<li><img src="./swamp.png"/></li>
<li><img src="./town.png"/></li>
</ul>
</div>
</div>
<div id="transBorder">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#slider").easySlider({
auto: true,
continuous: true,
nextId: "slider1next",
prevId: "slider1prev"
});
});
</script>
</body>
</html>
It would be easier to see what's up if you posted the whole code.
Anyway use this css for child div:
position: relative;
margin: 0 auto;
And this css for the parent one:
text-align:center;
margin: 0 auto;
And tell me if it works or not.
You can use absolute positioning​
#one {
background: red;
position: relative;
}
#two {
width: 300px;
height: 150px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px; /* negative half of height */
margin-left: -150px; /* negative half of width */
background: blue;
} ​
http://jsfiddle.net/3kj6A/
You can try this:
#slider div
{
position: absolute;
left: 50%;
margin-left:-310px;
width:620px;
}
If that does not work, you can do the same as above, just change the margin-left to:
margin-left:-25%;
Hope that helps.
jQuery stuff can be aligned with CSS, if "#slider div" is still not aligned, you should check the other elements in the CSS, they could be affecting #slider.
Or your problem might be that the code should be:
#slider {
code in here
}
You do not need to state that the element is a div. "#slider div" refers to the div in the slider(child div), not the slider it self(parent div). Correct me if I had misunderstood.