Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a navbar defined thus in my layout file

<div class="container">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
Markup removed for brevity
</nav>
@RenderBody()
@RenderSection("OptionalStuff", false)
</div>


this renders all content centred on the page.
I would like to lock the navbar to the top of the page so it never scrolls out of sight
I've tried adding fixed-top to the navbar class list and it does work but it expands the navbar to the entire page width whilst the rest of the page remains centred. Any ideas as to how I can lock it to the top and keep the same layout

I hope this makes sense

What I have tried:

<div class="container">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark fixed-top">
</nav>
@RenderBody()
@RenderSection("OptionalStuff", false)
</div>
Posted
Updated 14-Nov-21 22:03pm
v2
Comments
Peter_in_2780 14-Nov-21 6:55am    
Posting as a comment not solution because I'm away from anywhere I could test it..
<div class="container">
<div class="fixed-top">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
</nav>
</div>
@Render....

Basically, wrap you navbar in a fixed-top div.
[edit] lots of &xxx; [/edit]
pkfox 14-Nov-21 8:17am    
Hi Peter, same result the navbar goes the full screen width and the content is centred.
Peter_in_2780 15-Nov-21 1:41am    
Try margin:auto on your navbar. That SHOULD stop it stretching width-wise to fill its container.
I'm shooting in the dark here because I have no real idea what your assorted CSS classes are doing.
pkfox 15-Nov-21 4:01am    
Thanks for all your help Peter I do appreciate it, I don't have any custom CSS in the mix just the usual bootstrap.css. It's definitely fixed-top that stretches the navbar though.

1 solution

How about:
Razor
<div class="fixed-top">
    <div class="container">
        <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
            ...
        </nav>
    </div>
</div>
<div class="container">
    @RenderBody()
    @RenderSection("OptionalStuff", false)
</div>
 
Share this answer
 
Comments
pkfox 15-Nov-21 4:24am    
That did it thanks Richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900