9

So I have a background image with the following css:

 body{
    background-image:url(cover.jpg);
    background-repeat:no-repeat;
    background-position:center;
    background-attachment:fixed;
}

And the background image is 1280 px in width. So I want my navigation bar fixed and centered with the background. However Im running into issues. Here is my code.

#navigation {
margin: 0 auto;
position:fixed;
top: 0;
width: 1280px;
height: 35px;
padding-top: 10px;
background-color: #000000;
}

But the navigation bar will be fixed but not centered. If I remove the fixed, it will center it but then its not fixed.

Any ways to accomplish this?

Jared
  • 471
  • 1
  • 3
  • 17

4 Answers4

23

you can make the following

#navigation {
    position:fixed;
    left:0;
    right:0;
    margin-left:auto;
    margin-right:auto;
    width: 1280px;
    height: 35px;
    padding-top: 10px;
    background-color: #000000;
}
Community
  • 1
  • 1
Moussawi7
  • 12,359
  • 5
  • 37
  • 50
  • width:100% works too! so nice. i added padding to the relatively positioned element beneath it that folded up after doing this. – WEBjuju Dec 09 '19 at 01:31
4
.fixed-centered {
    position: fixed; 
    left: 50%;
    transform: translate(-50%);
}
Catalin Enache
  • 758
  • 1
  • 10
  • 17
0

Create .container div with style:

//no float!
width:960px;
possition:relative;
margin:0; //centers

Then withing this container create fixed navigation bar :)

position:fixed;
float:left;
width:100%;
height:50px;

So now navigation bar sticks in the centered container.

Only thing you might need to edit on this code is top.

0

Fixed and auto are 2 different things, you can't call them both together. A better solution would be to make a container div, center it using margin:0 auto, add your background code from the body to it and take it off the body, then make another div and put your navigation in there. You may also want to set a height on that container div.

<div class="container">
   <div class="nav">
       <!-- nav here -->
   </div>
</div>
Severian
  • 427
  • 3
  • 18