Trouble centering fixed navigation bar - html

Basically I'm looking to have a fixed navbar on my site, which moves with you when you scroll down. So far everything is good, but I'm really having trouble centering the bar.
HTML
<header id="header">
<nav id="nav">
<ul>
<li>Front</li>
<li>Mastering</li>
<li>Samples</li>
<li>Contact</li>
</ul>
</nav>
</header>
CSS
#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}
#nav {
z-index: 1;
width:100%;
margin-top: 20px;
display: block;
position: fixed;
text-align:center;
}
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
}
#nav li{
margin-top: 9px;
float: left;
padding-left: 21px;
display:block;
}
JSFiddle
https://jsfiddle.net/kjr1591d/1/
Would love to have some help. Thank you in advance :)

I think you looking for something like this-
#header {
z-index: 1;
width: 100%;
height: 80px;
position: fixed;
background-color:rgba(0,0,0,0.8);
display: block;
}
#nav {
z-index: 1;
width:100%;
margin: 15px 0;
display: block;
position: fixed;
text-align:center;
}
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 50%;
/* To help to identify the location */
height:50px;
background-color:green;
text-align:center;
padding:0
}
#nav li{
margin:15px 0 0;
/*float: left;*/
padding:0 10px;
display:inline-block;
}
<header id="header">
<nav id="nav">
<ul>
<li>Front</li>
<li>Mastering</li>
<li>Samples</li>
<li>Contact</li>
</ul>
</nav>
</header>

jiddle
#nav ul{
list-style: none;
display: block;
margin: 0 auto;
width: 80%;
/* To help to identify the location */
height:50px;
background-color:green;
}

Change the ul and li's to display as inline-block and assign the li's a left and right margin of equal sizing.

Related

How to make image to fit inside the container with padding and margin set?

I have the css code below which defines the body-content wrapper class, and I need images to fit inside the wrapper, but they are extending out of it.
I tried setting overflow on the wrapper to be auto but that doesn't work either.
body {
background-color:#231f20;
}
.body-content {
padding-left:50px;
padding-right:50px;
margin-left:50px;
margin-right:50px;
}
div.logoimg{
background-image: url( https://i.stack.imgur.com/z3XY6.png);
width:291px;
height:65px;
max-width: 100%;
max-height: 100%;
float: left;
overflow: auto;
}
.largeimg{
background-image: url(https://i.stack.imgur.com/3B2Un.jpg);
width:1460px;
height:369px;
max-width: 100%;
max-height: 100%;
background-size: 100%;
background-repeat: no-repeat;
z-index: -1;
overflow: auto;/*hidden;*/
position: relative;
display: block;
}
.menu{
padding-top:20px;
padding-bottom: 9px;
padding-left:20px;
background-color: #231f20;
float:left;
width: auto;
overflow: hidden;
}
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.menu ul li {
float: left;
}
.menu ul li a {
display: block;
padding: 8px;
background-color:#231f20;
color: #ffd41b;
}
.menu ul li a:hover{
display: block;
background-color: #ffd41b;
color: #231f20;
}
.menu ul li.icon {
display: none;
}
<div class="body-content">
<div class="logoimg"></div>
<div class="menu" id="myMenu">
<ul>
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("About Us", "Index", "About")</li>
<li>#Html.ActionLink("Contact", "Contact", "Home")</li>
<li class="icon">☰</li>
</ul>
</div>
<div class="largeimg"></div>
This resulting page looks like this:
I need images to be responsive and to fit inside the container with paddings and margins like this:
I tried was searching the internet for a solution but I found only the overflow suggestion which didn't work.
How do I get the images to stay inside the container?

Vertical alignment of navigation bar not working

So, I have a navigation bar and then an <ul> which has some <li>inside. I want it to be vertically aligned with the navigation bar .navbar but it seems it's not working. Do anyone have andy idea what am I doing wrong?
Here is the fiddle and code: http://jsfiddle.net/x7EAg/2/
<style>
.navbar {
width: 100%;
height: 90px;
background: black;
border-radius: 0px !important;
}
.navbar .logo-holder {
background-image: url(../img/logo.png);
width: 75px;
height: 57px;
margin-top: 15px;
}
.navbar .sections {
list-style: none;
margin-left: 70px;
margin-bottom: 50px;
}
.navbar .sections li {
display: inline-block;
padding: 0 25px 0 0;
vertical-align: middle;
}
</style>
<nav class="navbar" role="navigation">
<div class="container">
<div class="logo-holder"></div>
<ul class="sections">
<li>Shop</li>
<li>Team</li>
<li>Events</li>
<li>Experience</li>
<li>Company</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
Thank you!
If I understand what you are trying to achieve. Then you should make the logo absolutely positioned and then aligning the ul can be done with line-height. Full css:
.navbar {
width: 100%;
height: 90px;
line-height:90px;
background: black;
border-radius: 0px !important;
}
.navbar .logo-holder {
position: absolute;
top:0;
left:0;
background-repeat: no-repeat;
background-image: url(../img/logo.png);
width: 75px;
height: 57px;
margin-top: 15px;
}
.navbar .sections {
list-style: none;
margin-left: 70px;
margin-bottom: 50px;
}
.navbar .sections li {
display: inline-block;
padding: 0 25px 0 0;
}
And updated fiddle
i changed the display of your logo-holder to inline-block and then set vertical-align:middle
now it appears next to the logo, and vertically centered.
see here for a fiddle http://jsfiddle.net/gaurav5430/x7EAg/3/
this is the complete css:
.navbar {
width: 100%;
height: 90px;
background: black;
border-radius: 0px !important;
}
.navbar .logo-holder {
background-image: url(../img/logo.png);
width: 75px;
height: 57px;
margin-top: 15px;
display:inline-block;
vertical-align:middle;
}
.navbar .sections {
display:inline-block;
vertical-align:middle;
list-style: none;
margin:0px;
padding:0px;
background:#aaa;
}
.navbar .sections li {
display: inline-block;
padding: 0 25px 0 0;
vertical-align: middle;
}
What I believe is going on is your logo is pushing your ul down. like was mentioned above. You may want to float your logo-holder class left. That would allow you to position your li as you needed. Line-height is a way to do this, you could also use margin, padding, or absolute position for your li as needed. Good luck.

Internet explorer not playing nice - Site left aligned and not 100% width

My site works perfectly in chrome but now that I have tested it with IE7 and not only is the site left aligned, but the full width (100%) header background no longer extends all the way across and the header and content is left aligned.
Seemingly it is only the footer that works correctly. I dont have a clue why this is, but rather than "hack" it to work, I am sure that there is something I have put wrong in my code.
I cant figure this out though! Here is my code:
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head>
<title>Title</title>
<link href="main2.css" rel="stylesheet" type="text/css">
<!--[if !IE 7]>
<style type="text/css">
#wrap {display:table;height:100%}
</style>
<![endif]-->
</head>
<body>
<div id="wrap">
<div id="header">
<div id="header_content">
<img src="/static/img/header.jpg" id="logo" alt="coming soon" title="coming soon">
<ul>
<li>Link1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
</div>
<div id="main">
There are many sticky footer methods to be found in Google. I've tried many of them and they usually fail in some regards. The problem it seems is that some of these methods are old and may have worked in older browsers but they don't in newer browser releases. Because those pages are old, and were heavily linked too in the past, they still rank high in Google. Many webmasters looking for a sticky footer solution end up scratching their heads as they try these same old methods because they are the first ones they end up finding when they search.
</div>
</div>
<div id="footer">
<div id="footer_content">
© 2012 Company, Inc.
<ul>
<li>Contact</li>
<li>Terms</li>
<li>Privacy</li>
</ul>
</div>
</div>
</body>
</html>
CSS
* { margin: 0; padding: 0; }
html, body {height: 100%; }
body { min-width:900px; }
/* LAYOUT */
#wrap {min-height: 100%;}
#main { background-color: purple; margin: 0 auto; overflow: auto; padding-bottom: 60px; width: 900px; }
body:before { content:""; height:100%; float:left; width:0; margin-top:-32767px;/ } /*Opera Fix*/
/* HEADER */
#header { background-color: orange; width: 100%; line-height: 60px; }
#header_content { position: relative; width: 900px; margin: 0 auto; }
#header ul { list-style: none; position: absolute; top: 0; right: 0; }
#header ul li { float: left; }
#header ul li a { font-weight: bold; text-decoration: none; }
#header ul li a:hover { }
/* FOOTER */
#footer { margin-top: -60px; clear:both; background-color: blue; line-height: 60px; }
#footer_content { width: 900px; margin: 0 auto; position: relative;}
#footer ul { list-style: none; position: absolute; top: 0px; right: 0; }
#footer ul li { float: left; margin-right: 5px; }
#footer ul li a { font-weight: bold; margin-right: 5px; text-decoration: none; }
#footer ul li a:hover { }
Small CSS Change:
* { margin: 0; padding: 0; }
html, body {height: 100%; }
body { min-width:900px; }
/* LAYOUT */
#wrap {min-height: 100%;}
#main { background-color: purple; margin: 0 auto; overflow: auto; padding-bottom: 60px;}
#main .wrap {width: 900px; margin: auto;}
body:before { content:""; height:100%; float:left; width:0; margin-top:-32767px;/ } /*Opera Fix*/
/* HEADER */
#header { background-color: orange; width: 100%; line-height: 60px; margin: auto;}
#header_content { position: relative; margin: 0 auto; width: 900px; }
#header ul { list-style: none; position: absolute; top: 0; right: 0; }
#header ul li { float: left; }
#header ul li a { font-weight: bold; text-decoration: none; }
#header ul li a:hover { }
/* FOOTER */
#footer { margin-top: -60px; clear:both; background-color: blue; line-height: 60px; }
#footer_content { width: 900px; margin: 0 auto; position: relative;}
#footer ul { list-style: none; position: absolute; top: 0px; right: 0; }
#footer ul li { float: left; margin-right: 5px; }
#footer ul li a { font-weight: bold; margin-right: 5px; text-decoration: none; }
#footer ul li a:hover { }
Fiddle: http://jsbin.com/iyedik/1

What is the correct way to vertically align in this circumstance?

Aligning things vertically seems like a dark art. This is a section of my currect sites code (specifically, the header). The site is coded like this to do with the footer being docked to the bottom of the page.
HTML:
<div id="header-wrap" class="full_width">
<div id="header-container" class="dc1">
<div id="header" class="thin_width rel">
<img src="/static/img/header.jpg" id="logo" alt="coming soon" title="coming soon">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
</div>
</div>
CSS:
#header-wrap { top: 0; left: 0; }
#header-container { height: 60px; border-bottom: 1px solid #E5E5E5; }
#header { margin: 0 auto; }
#header h1 { color: #beffbf; text-align: left; width: 290px; margin: 0; position: absolute; left: 0; top: 20px; }
#header h1 em { color: #90b874; font-size: small; display: block; }
#header ul { margin: 0; padding: 0; list-style: none; position: absolute; top: 35px; right: 0; }
#header ul li { float: left; margin-right: 5px; }
#header ul li a{ color: #90b874; font-weight: bold; font-size: 1.4em; margin-right: 5px; text-decoration: none; }
#header ul li a:hover { color: #beffbf; }
.dc1 { background-color: #FFFFFF; }
.rel { position: relative; }
.full_width { width: 100%; }
.thin_width { width: 450px; }​
Here's a JSFiddle Exmple
How should I go about trying to vertically align the links on the right and the logo?
I would like to try and do this without using fixed padding since it makes it a pain if I ever update the logo or link height.
So, what is the correct way to vertically align in this circumstance?
display:table-cell works well for this
#header-wrap { top: 0; left: 0; }
#header-container { height: 60px; border-bottom: 1px solid #E5E5E5; display:table-cell; vertical-align:middle; }
#header { margin:0 auto; overflow:auto;}
Check the demo http://jsfiddle.net/AHsBN/2/
All you need to do is set element.style
#header ul { top:0;} and #header { position: relative;}
This is my solution, no positioning, heights and such are needed, just adjust the vertical-align as you wish to either, top, middle, bottom or baseline etc (I used height: 300px for #header-container to more easily illustrate vertical-align: middle;
#header > a { background: red; }
ul { background: green; }
#header-container { background: blue; height: 300px; }
#header > a, ul { display: block; }
li { display: inline-block; }
#header > a { float: left; }
ul { float: right; font-size: 200%; }
#header-container { display: table; width: 100%; }
#header { display: table-cell; vertical-align: middle; }
Also no need to clear the floats as its within a CSS table.
HTML
<div id="header">
<img src="http://www.google.co.in/images/srpr/logo3w.png" alt="">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
CSS
#header { position: relative; background: #ddd; }
#header a.logo {display: inline-block; vertical-align: middle; *display: inline; zoom: 1;}
#header ul { display: inline-block; vertical-align: middle; *display: inline; zoom: 1; position: absolute; right: 0px; top: 50%; height: 20px;}
#header ul li { display: inline; position: relative; top: -50%; }
Example: http://jsfiddle.net/gZceQ/4/
Code above should work in all browsers :)

Implementing this with CSS and HTMl

This is my css -
#nav {
position: relative;
background-color: #292929;
float: left;
width:960px;
margin-left:auto;
margin-right:auto;
}
#nav li {
float: left;
list-style: none;
width:auto;
}
#nav li a {
color: #e3e3e3;
position: relative;
z-index: 2;
float: left;
}
ul, li {
margin:0;
padding: 0;
}
#blob {
position: absolute;
top: 0;
z-index : 1;
background: #0b2b61;
background-repeat:repeat;
}
I want to place this object in the center. How do i do that. I also want the background to fit the page. I tried a lot, but it isn't working.
<ul id="nav">
<li>Home</li>
</ul>
Place an object in the center:
CSS:
.Center1
{
position:relative;
text-align:center;
}
.Center2
{
position:relative;
margin-left:auto;
margin-right:auto;
width:1024px;
height:800px;
text-align:left;
}
Markup:
<body class="body">
<div class="Center1">
<div class="Center2">
<!-- Your content -->
You want place <li> int the center, right?
The easiest way according to your current markup is to apply fixed width and add margin: 0 auto; to your li.
Semantics of that solution is really bad, but i guess you don't really care.