I am trying to have a fixed and centered navbar div which is within another div that's using a parallax effect. If I set the position to relative, the navbar will be centered within the other div however setting it to fixed will have a fixed navbar but not centered. I would like to avoid using margin left x amount of px because it doesn't seem consistent for cross resolutions. Is there anyway to keep the navbar fixed position but have it centered within the other div without mar?
This is my code:
#page-wrap {
position: relative;
min-width: 1366px;
max-width: 2048px;
margin: 0px auto;
width: 95%;
}
header {
background: url('../images/cover1.jpg') no-repeat;
background-size: 100% 90%;
width: 100%;
height: 1000px;
}
#nav {
background: #f0f;
margin: 0 auto;
height: 500px;
min-width: 980px;
max-width: 2048px;
position: fixed;
opacity: 1;
}
#main {
background: #fff;
position: relative;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="page-wrap">
<header>
<nav id="nav">Nav</nav>
</header>
<div id="main">Lorem ipsum flash.</div>
</div>
<script>
$(document).on('scroll', function(e) {
$('#nav').css('opacity', ($(document).scrollTop() / 500));
var st = $(window).scrollTop();
$('header').css({
'background-position-y': 0 + (st * .77) + "px"
});
});
</script>
You could keep your nav in center of screen, but you need to wrap it inside fixed container, working Fiddle. HTML is slightly different:
<div id="page-wrap">
<header>
<div class="nav_container">
<nav id="nav">Nav</nav>
</div>
</header>
<div id="main">Lorem ipsum flash.</div>
</div>
In CSS fixed element becomes .nav_container and nav must become inline-block - that way we can use text align:
#page-wrap {
position: relative;
min-width: 1366px;
max-width: 2048px;
margin: 0px auto;
width: 95%;
}
header {
background: url('http://placehold.it/500x500') no-repeat;
background-size:100% 90%;
width: 100%;
height: 500px;
}
.nav_container {
margin: 0 auto;
width: 100%;
position: fixed;
opacity: 1;
text-align: center;
}
.nav_container nav {
display: inline-block;
background: #f0f;
min-width: 100px;
max-width: 2048px;
height: 100px;
}
#main {
background: #fff;
position: relative;
height: 500px;
}
How about centering your nav using javascript?
$('#nav').css({'left': (($(document).width() - $('#nav').width())/2 )+"px"});
This should suffice for what you're trying to do. Let me know if this doesn't work.
#nav {
background: #f0f;
margin: 0 auto;
height: 500px;
min-width: 980px;
max-width: 2048px;
position: fixed;
opacity: 1;
left: 50%;
}
Related
I'm a beginner and I was playing around with css (code given below)
and I set the yellow div to a 1000px and I thought the blue div would automatically
wrap around it given height:100%;
but to my surprise the yellow div seemed to overflow, I tried using the overflow:auto; but it added a scroll bar to prevent the overflow (which is not what I needed)
so is there anyway that the parent blue div always completely wraps around the yellow div no matter if i set it to a 1000px or 100% height using only CSS?
html,
body {
margin: 0;
height: 100%;
width: 100%;
max-height: 100%;
max-width: 100%;
}
#header {
height: 100px;
background: black;
width: 100%;
position: relative;
}
#rest {
height: 100%;
width: 100%;
background: blue;
position: absolute;
}
#content {
width: 50%;
left: 50%;
transform: translate(-50%, 0%);
background: yellow;
height: 1000px;
position: absolute;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="header"></div>
<div id="rest">
<div id="content">
</div>
</div>
</body>
</html>
Try like below:
html,
body {
margin: 0;
height: 100%;
width: 100%;
max-height: 100%;
max-width: 100%;
}
#header {
height: 100px;
background: black;
width: 100%;
position: relative;
}
#rest {
min-height: 100%; /* update here */
width: 100%;
background: blue;
position: absolute;
}
#content {
width: 50%;
margin: auto; /* remove absolute and center with margin */
background: yellow;
height: 1000px;
}
<div id="header"></div>
<div id="rest">
<div id="content">
</div>
</div>
The below code is the closet I was able to achieve and exactly how I need it to function, the only problem with this approach is that because the fixed div is layered on top of the main div, it renders the main container not clickable.
This is how I need the layout to function:
There should be three columns, the outer two columns (sidepanels, left and right) are fixed.
The header and footer are also fixed and take up the max width of the
center "main" column (and are positioned in the center like the center "main" column).
On window resize, only the width of the center column (along with the
header and footer) is auto adjusted while the side columns stay the same
width (squeezing the center column).
I am trying to avoid the use of flexbox for browser compatibility.
body {
background: #333;
color: #FFF;
margin: 0;
height: 100%;
max-width: 1240px;
margin: 0 auto;
}
.fixed {
position: fixed;
z-index: 1;
height: 100%;
width: 100%;
max-width: inherit;
}
.main {
background: #444;
position: relative;
padding: 70px 10px;
height: 1000px;
width: auto;
min-width: 280px;
max-width: 800px;
margin: 0 220px;
}
.header,
.footer {
position: absolute;
background: #555;
height: 60px;
left: 220px;
right: 220px;
}
.header {
top: 0;
}
.footer {
bottom: 0;
}
.left,
.right {
position: absolute;
top: 0;
width: 220px;
background: #666;
height: 100%;
}
.left {
left: 0;
}
.right {
right: 0;
}
<div class="fixed">
<div class="left">left</div>
<div class="header">header</div>
<div class="footer">footer</div>
<div class="right">right</div>
</div>
<div class="main">
main
</div>
This is probably really easy question, but I have no idea how to do this.
The width is set as 100%, and height is auto. I want to hide 200px from bottom, but margin-bottom: -200px is not working.
.banner {
width: 100%;
height: auto;
}
.banner-photo {
width: 100%;
height: auto;
}
.banner-photo img {
width: 100%;
height: auto;
}
<div class="banner">
<div class="banner-photo">
<img src="https://media-exp1.licdn.com/media/AAEAAQAAAAAAAANbAAAAJDE5NjBkNDk1LTY3ZGQtNDA0NS04YTJiLTdkNmU3NjZiNjI3Mg.png"/>
</div>
</div>
you can create a div like this, and it will effectively hide whatever is 200px from the bottom:
div {
height: 200px;
width: 100%;
position: fixed;
bottom:0;
background-color: white;
z-index: 100;
}
You need the negative margin-bottom on your content (your img), and overflow: hidden on your container.
I've hidden 200px from your fiddle example - note that it doesn't leave much showing!
.banner {
width: 100%;
height: auto;
}
.banner-photo {
width: 100%;
height: auto;
overflow: hidden;
}
.banner-photo img{
width: 100%;
height: auto;
margin-bottom: -200px;
}
<div class="banner">
<div class="banner-photo">
<img src="https://media-exp1.licdn.com/media/AAEAAQAAAAAAAANbAAAAJDE5NjBkNDk1LTY3ZGQtNDA0NS04YTJiLTdkNmU3NjZiNjI3Mg.png"/>
</div>
</div>
This seemed simple in my head.. But it's not. I have 2 div's. floating next to eachother
The right div should have an image that is always sticking to the footer
It's not even applying the 100% Height of my .block div, the parent div ( body ) is also set at 100%
For some reason this is not working with my following code.
JSFIDDLE
HTML
<div class="header">
Header
</div>
<div class="block">
<img src="http://www.zwaldtransport.com/images/placeholders/placeholder1.jpg" />
</div>
CSS
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: url('background.jpg') no-repeat center center fixed;
background-size: cover;
}
.header {
float: left;
height: 100%;
width: 40%;
background: red;
}
.block {
float: left;
width: 60%;
height: 100%;
background: green;
}
.block img {
max-width: 100%;
}
You can try this. check and let me know, have you asked for this:
.header {
height: 100%;
width: 40%;
background: red;
}
.block {
position:absolute;
bottom:0;
width: 60%;
height: auto;
background: green;
}
Okay so I have been working on implementing the 'holy grail'-style layout for my website, so far it's pretty close but I noticed two things I want to fix.
The goal is a 'sticky' footer with the page length expands with the browser window height, a header, and 3 columns. 2 fixed columns on the left and right side, and a fluid column in the middle.
The issues I am having are that right now, my center 'fluid' column doesn't seem to be acting like I expected. Basically I want the fixed columns to always be fully shown, with the center column filling the remaining horizontal space. But the center column is taking up a lot of room and making it so that I have to scroll to view the right column (see image below). Also, the 'text-align: center' code doesn't appear to be centering text within the viewable area of the center column. Any help appreciated!
image: http://i.imgur.com/FPuSiIu.png
html:
<html>
<head>
<link type="text/css" rel="stylesheet" href="test.css" />
</head>
<body>
<div id="header">
<p>Header</p>
</div>
<div id="container">
<div id="center">
<p>Content</p>
</div>
<div id="left">
<p>Content</p>
</div>
<div id="right">
<p>Content</p>
</div>
</div>
<div id="footer">
<p>Footer</p>
</div>
</body>
</html>
css:
* {
margin: 0;
}
#container {
width:100%;
}
#header {
text-align: center;
background: #5D7B93;
height: 95px;
padding: 5px;
position: fixed;
top: 0;
width: 100%;
z-index: 15;
}
#center{
text-align: center;
margin-top: 105px;
background: red;
position: relative;
float: left;
width: 100%;
height: 100%;
}
#left {
height: 100%;
width: 150px;
text-align:center;
background:#EAEAEA;
margin-top: 105px;
margin-left: -100%;
overflow: scroll;
position: relative;
float: left;
}
#right {
position: relative;
height: 100%;
width: 150px;
margin-right: -100%;
margin-top: 105px;
background: blue;
text-align: center;
float: left;
}
#footer {
text-align:center;
background: #5D7B93;
height:25px;
padding:5px;
position: fixed;
bottom: 0;
width: 100%;
}
No need to float. Just position: absolute the sidebars and give the center div fixed margin on both sides.
JSFiddle
CSS
#container{
position: relative;
}
#left, #right {
width: 200px;
height: 100%;
position: absolute;
top: 0;
}
#left {
left: 0;
}
#right {
right: 0;
}
#center {
margin: 0 200px;
}
i've done this on my layout and it works fine for me
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container{
display: inline-flex;
width: 100%;
height: 100%;
background: lightblue;
}
#left {
width: 240px!important;
min-width: 240px!important;
background: red;
height: 100%;
}
#right {
width: 400px!important;
min-width: 400px!important;
background: red;
height: 100%;
}
#center {
background: blue;
width: 100%;
min-width: 600px;
height: 100%;
}