I am having trouble turning my normal navbar into a sticky one. When I don't set the position as fixed, the navbar looks and works fine. But when I make the position fixed, it glitches out and looks wonky, and I have examples of both down below. My HTML and CSS are here too:
body {
margin: 0px;
padding: 0px;
font-family: 'Arial', serif;
}
header {
position: fixed;
}
p {
line-height: 200px;
}
.navbaratta {
background-color: #E9B63C;
list-style: none;
text-align: center;
padding: 20px 0 20px 0;
margin-top: 0;
}
.navbaratta>li {
display: inline-block;
padding-right: 70px;
border-right: 1px solid #000;
margin-right: 70px;
}
.navbaratta>li>a {
text-decoration: none;
color: #1A1A1A;
font-weight: bold;
}
.navbaratta>li>a:hover {
color: #474747;
}
.Logo {
display: block;
margin-left: auto;
margin-right: auto;
}
.LogoDiv {
background-color: #1A1A1A;
width: 100%;
}
li.HOME {
border-left: 1px solid #000;
padding-left: 70px;
}
<header>
<div class="LogoDiv">
<img src="NavBarFinal.png" class="Logo">
</div>
<ul class="navbaratta">
<li class="HOME">HOME</li>
<li>BIOGRAPHY</li>
<li>CONTACT US</li>
<li>PHOTO GALLERY</li>
</ul>
</header>
with position fixed:
without position fixed:
The header is not taking the complete width. So, just update your header CSS.
header {
position: fixed;
width: 100%; // Added
}
You need to add positional properties when you fix a div or alternatively you give it a width. I usually prefer first approach. So use it as below will solve your problem.
Your problem caused by using element with relative positioning which is displayed as block so it takes width of parent element when it is relative. So you dont care its width since its parent is body. However when you change it to fixed positioning there is nothing in terms of reference so it wraps content thats all. By entering left:0 and right:0 you stretch it to whole screen. You also need to add a padding top to your body element. Otherwise some parts of your text will be displayed behind navbar. I hope it helps
header {
position: fixed;
left: 0;
right: 0;
}
Related
We are writing a custom website, but we want it to look similar to Wordpress, so we have written the code with the 'sticky' left position bar, and the scrolling right one.
But when you bring the page inward, the right columns wraps under the left one. Any ideas why and how to resolve?
Here is the CSS code:
html, body, section, article, aside {
min-height: 100%;
}
.sidemenu
{
position: sticky;
top: 0;
height: 100vh;
background-color: #333333;
color: #ffffff;
width: 160px;
float: left;
}
.menu-link a
{
padding: 8px 2px 2px 8px;
display: block;
color: #ffffff;
text-transform: capitalize;
}
.pagebody
{
float: left;
max-width: 95%;
text-align: left;
padding: 20px;
}
So you have two DIVs, left is 'sidemenu' right is 'pagebody'.
Hope you can help.
To fix the position of the sidebar, you need to used position: fixed;. After that, wrap the sidebar div and body div into one container and set its width to 100% (I also gave the body a margin of 0 at this point to remove gaps).
Give the body div a left-margin equal to the width of the sidebar, then set the width of the body using a calculation (as shown below). I also gave it a really long height to demonstrate scrolling.
You can omit your floats.
Here is the adjusted code:
html,
body,
section,
article,
aside {
min-height: 100%;
margin: 0;
}
.main {
width: 100%;
}
.sidemenu {
position: fixed;
top: 0;
height: 100vh;
background-color: #333333;
color: #ffffff;
width: 160px;
}
.menu-link a {
padding: 8px 2px 2px 8px;
display: block;
color: #ffffff;
text-transform: capitalize;
}
.pagebody {
width: calc(100% - 199.75px);
text-align: left;
padding: 20px;
height: 300vh; /**** used to demonstrate scrolling ****/
margin-left: 160px;
background-color: #BBB;
}
<div class="main">
<div class="sidemenu">
Side Menu
</div>
<div class="pagebody">
body
</div>
</div>
I have a bigger HTML header containing a menu and a large picture.
I would like to place text on the image somewhere as a "title" to the page.
Whenever I try to add my <h1> tag somewhere, it positions the text above the menu and it's not what I want.
I would like to be able to position any form of tags somewhere in the picture and I am struggling to find a solution as my code is not efficient to do this.
I am starting to understand what my problem is but I cannot find a solution.
Here is a template of what's going on. I want to place the text somewhere next to my face (as weird as it sounds lol), anyone?
body {
font: 15px/1.5 Gravity, Arial;
padding: 0;
margin: 0;
}
header {
width: 100%;
height: 800px;
background: url('../img/web_bg.jpg');
background-size: cover;
}
.logo {
line-height: 60px;
position: fixed;
float: left;
margin: 16px 46px;
color: #000;
font-weight: bold;
font-size: 20px;
letter-spacing: 2px;
}
nav {
z-index: 100;
position: fixed;
width: 100%;
line-height: 60px;
}
nav ul {
line-height: 60px;
list-style: none;
background: rgba(0, 0, 0, 0);
overflow: hidden;
color: #fff;
padding: 0;
text-align: right;
margin: 0;
padding-right: 40px;
transition: 1s;
}
nav.black ul {
background: #fff;
z-index: 100;
}
nav ul li {
display: inline-block;
padding: 16px 40px;
;
}
nav ul li a {
text-decoration: none;
color: #000;
font-size: 16px;
}
nav ul li a:hover {
background-color: #white;
border: none;
color: #000;
text-align: center;
opacity: 0.6;
}
.menu-icon {
line-height: 60px;
width: 100%;
background: #000;
text-align: right;
box-sizing: border-box;
padding: 15px 24px;
cursor: pointer;
color: #fff;
display: none;
}
<header id="home">
<h1>MOHANAD ARAFE</h1>
<nav>
<div class="menu-icon">
<i class="fa fa-bars fa-2x"></i>
</div>
<div class="logo">MOHANAD ARAFE</div>
<div class="menu">
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
</nav>
</header>
You are going good, cheers for that. For the problem you are facing I would suggest you to play with z-index. It is a CSS property, and defines the elements as layers. Element with greater z-index will be the top most layers, followed by the elements with lesser z-index. I would suggest you to set z-indec of image to lowest, and make the content above in another container, and set the z-index of this container to a higher range, this should solve your problem.
Here's more reference on z-index
Happy Coding.
I would suggest using grid in these kind of situations where you have to deal with position of elements. A crash course on grid will be the best option. I personally use it a lot and don't have to care about anything other than z index.
You can use position: absolute; for the h1 tag and use top value in %, and left value in %
h1{
position:absolute;
top: value in %;
left: value in %;
}
header{
position:relative;
}
Note: apply a class name for h1 and apply css for that class or else it might affect h1 tag in sub pages.
I'm required to make sure my nav bar remains on the right of my header such that the user utilises the horizontal scroll bar to access the navigation options which aren't visible.
Currently I've got 5 elements in my nested and once the user resizes the browser the nav menu jumps/moves down.
See fiddle here
HTML:
<header> <!-- Navigation menu bar -->
<nav>
<ul> <!-- Navigation menu bar options. These are fixed in terms of content. -->
<li>My Profile</li>
<li>Log Out</li>
<li>FAQs</li>
<li>Extras</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
CSS:
header {
background-color: #3366FF;
width: 100%;
height: 86px;
position: fixed;
top: 0;
left: 0;
z-index: 100;
opacity: 0.90%;
}
#logo {
margin: 0px;
float: left;
width: 200px;
height: 86px;
background: url("../images/logo.png") no-repeat center;
}
nav {
float: right;
padding: 20px;
}
#menu-icon {
display: hidden;
width: 100px;
height: 86px;
background: url(http://www.w3newbie.com/wp-content/uploads/icon.png);
padding: 0;
margin: 0;
}
a:hover#menu-icon {
border-radius: 2px 2px 0 0;
}
ul {
list-style: none;
}
nav ul li {
text-align: center;
display: inline-block;
padding: 10px;
}
nav ul li a:hover {
color: #363636;
text-decoration: none;
}
I would just put a min-width on your header element, stopping it from ever becoming too narrow for the list elements to fit, yet retaining the flexibility.
I tried min-width:660px; and it seemed to work fine to me.
header {
background-color: #3366FF;
width: 100%;
min-width: 660px;
height: 86px;
position: fixed;
top: 0;
left: 0;
z-index: 100;
opacity: 0.90%;
}
ALTERNATELY you can change your nav css to
nav {
text-align:right;
padding: 20px;
}
..And then change your header's "height" to "min-height" instead.
You have to remove the natural flexibility of the menu by adding a specific width. That way, regardless of the size of the browser window, the menu will remain the width you want it to be and the user will have to scroll to see it.
You can use JS to dynamically do this by setting the width of the header to be a certain amount of pixels, for e.g., greater than whatever is the width of the window upon loading of the document and resizing of the window.
You could use CSS Table display to sort the layout in the header and stop the menu from wrapping underneath. (added bonus: this avoids floats altogether)
header {
display: table;
}
header #logo, header nav {
display: table-cell;
border:1px solid red;
}
https://jsfiddle.net/S5bKq/1277/
As for the responsive menu itself - a quick internet search for 'responsive menus' will net you several examples/tutorials.
Here I have a nav bar set up with a centered logo. The only problem is that I can't get it to look quite right with spacing. The logo is set to 'position: absolute' 'left: 50%' and 'margin-left: -125px' so that is always in the center. Now I just need to get the text balanced around it in a more symmetrical way, but I can't seem to figure out how to do so.
Here's the HTML
<link rel="stylesheet" href="stylesheet.css"/>
</head>
<body>
<div class="header">
<div class="nav">
<ul>
<li class="navright">HOME</li>
<li class="navright">MENU</li>
<li id="logo"><img src="Images/pantry logo.png" width="536" height="348"/></li>
<li class="navleft">CONTACT</li>
<li class="navleft">ABOUT</li>
</ul>
</div>
</div><!--end header-->
And the CSS.
.header {
width: 960px;
height: 200px;
margin: 0 auto;
text-align: center;
position: relative;
}
div ul li {
display: inline-block;
padding: 105px 70px 40px 0;
font-family: "Montserrat", "Helvetica", sans-serif;
font-size: 18px;
color: #4f4d4b;
text-decoration: none;
list-style: none;
}
div ul li a {
list-style: none;
text-decoration: none;
color: #4f4d4b;
}
.nav ul li a:visited {
text-decoration: none;
color: #4f4d4b;
}
#logo a img {
width: 250px;
height: auto;
position: absolute;
left: 50%;
margin-left: -125px;
display: inline-block;
}
#logo {
height: 60px;
padding-top: 40px;
width: 250px;
}
You can go to the site here.
Here's what I would do, and this is just the way I would normally go about things.
I would take the padding of the li, then add the 105px padding to the top of the header (or nav). Next, add some arbitrary margin-right to each li element (say 48px), while of course setting li:last-child to margin: 0; Next take the padding-top and the height off the li#logo and change it to this:
#logo { width: 250px; position: relative; }
Finally, just use a transform to center the logo if you are going to use absolute positioning. This essentially does the same thing as the margin-left, but it is a little more flexible. So the image css should look like this:
#logo a img {
width: 250px;
height: auto;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
I used this code and it worked perfectly for me. I can make you a fiddle or something also if you are having trouble still.
I am trying to make a "bar" that stretches over the top of my web page (like Facebook). Then I have some navigational links on the right. However if you resize the page and then use the horizontal scrollbar the red background is missing.
http://jsfiddle.net/ejJnL/embedded/result/
http://jsfiddle.net/ejJnL/
<div class="header-container">
<div class="header">
<ul class="main-navigation">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
<div class="clear"></div>
</div>
</div>
CSS:
.header-container {
background-color: red;
height: 40px;
width: 100%;
}
.header {
height: 100%;
line-height: 40px;
margin: 0 auto;
width: 960px;
}
.header img {
vertical-align: middle;
}
.main-navigation {
float: right;
list-style: none;
margin: 0;
padding: 0;
}
.main-navigation li {
float: left;
}
.main-navigation a {
color: #ffffff;
font-weight: bold;
text-decoration: none;
}
.header-container {
background-color: red;
height: 40px;
position: fixed:
left: 0;
right: 0;
}
Fix the position and declare it attached to the left and right edges of the viewport as shown above.
You don't need declarations of width: 100% or height: 100% as div are already block elements and will fill their containers.
You should declare body { margin:0} if you want the bar to extend to the page edges.
You should probably use max-width not width for your .header so the menu still shows in a small window.
You may also want to add an overflow: none to the .header-container
http://jsfiddle.net/SbpZG/
Fixed width on the .header-container and position: relative on the .main-navigation