简体   繁体   English

如何将Flexbox导航栏固定在屏幕顶部?

[英]How can I make my flexbox navigation bar fixed to the top of the screen?

what are some non-hacky (or not very hacky) ways to make my flexbox navigation bar fixed to the top of the screen, so that it is still responsive but also no matter how far down I scroll I can still see the navigation bar. 有什么非hacky(或不是很hacky)的方法可以将我的flexbox导航栏固定在屏幕顶部,这样它仍然可以响应,但是无论我向下滚动多远,我仍然可以看到导航栏。

Here is the code I have for it: 这是我的代码:

 .navbar { display: flex; background: #e74c3c; height: 60px; font-family: 'Nova Flat', cursive; } .navitem { flex: 1; position: relative; top: 2vh; font-size: 1.7em; margin-left: 5vw; } .navitem a { text-decoration: none; color: white; } .dropdown-content { display: none; z-index: 1; color: black; font-size: 0.7em; } .dropdown-content a { color: black; } .dropdown:hover .dropdown-content { display: inline-block; } 
 <div class="navitem"><a href="#">LOGO</a></div> <div class="navitem"><a href="/search">Find Books</a></div> <div class="dropdown navitem"> <a class="profile">My Account</a> <div class="dropdown-content"> <p><a href="/:user_id/profile">My Profile</a></p> <p><a href="/<%= current_user.id %>">My Bookshelf</a></p> <p><a href="/<%= current_user.id %>/add_a_book">Add a new book to your list</a></p> <p><a href="/<%= current_user.id %>/book_list">Detailed Book List</a></p> <p> <%= link_to "Sign Out", destroy_user_session_path, :method => :delete %> </p> </div> </div> 

just give the navbar a fixed position will work: 只需将navbar固定位置即可:

.navbar {
  position: fixed;
  top: 0;
  width: 100%;
}

Give the navbar (which you didn't even include in your HTML code, BTW...) position: fixed, and width: 100% , and give the content a padding-top as least as high as the navbars height, so the navbar won't hide the first lines of the content. navbar (您甚至没有包含在HTML代码中的BTW ...) position: fixed, width: 100% ,并将内容的padding-top为与导航栏高度一样低。导航栏不会隐藏内容的第一行。

Also, add margin: 0 to html and body to prevent the default margin which will otherwise show. 另外,在htmlbody上添加margin: 0可以防止默认的margin出现。

 html, body { margin:0; } .navbar { position: fixed; width: 100%; display: flex; background: #e74c3c; height: 60px; font-family: 'Nova Flat', cursive; } .navitem { flex: 1; position: relative; top: 2vh; font-size: 1.7em; margin-left: 5vw; } .navitem a { text-decoration: none; color: white; } .dropdown-content { display: none; z-index: 1; color: black; font-size: 0.7em; } .dropdown-content a { color: black; } .dropdown:hover .dropdown-content { display: inline-block; } .content { padding-top: 60px; height: 1200px; background: yellow; } 
 <div class="navbar"> <div class="navitem"><a href="#">LOGO</a></div> <div class="navitem"><a href="/search">Find Books</a></div> <div class="dropdown navitem"> <a class="profile">My Account</a> <div class="dropdown-content"> <p><a href="/:user_id/profile">My Profile</a></p> <p><a href="/<%= current_user.id %>">My Bookshelf</a></p> <p><a href="/<%= current_user.id %>/add_a_book">Add a new book to your list</a></p> <p><a href="/<%= current_user.id %>/book_list">Detailed Book List</a></p> <p> <%= link_to "Sign Out", destroy_user_session_path, :method => :delete %> </p> </div> </div> </div> <div class="content"> lots of content here... </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "如何使用 css 使我的导航栏粘在屏幕顶部" - How can i make my navigation bar stick to the top of my screen with css 如何使导航栏固定在滚动顶部 - How to make the navigation bar fixed on top on scroll 如何让我的顶部导航栏不随网站滚动? position:已修复; 不工作,代码如下: - How can I keep my Top Navigation bar from scrolling with the website? position: fixed; is not working, Code below: 我无法用 CSS 固定我的导航栏 - I can't make my navigation bar fixed with CSS 如何使导航栏始终停留在屏幕中间的固定位置面板的顶部? - How to make a navigation bar always stay at the top of a fixed-position panel that is in the middle of the screen? 如何进行固定的响应式顶部导航? - How can I make a fixed responsive top navigation? 如何制作固定在屏幕顶部的自适应导航栏 - How can I make a responsive navbar that is fixed to the top of the screen 在屏幕顶部固定创建导航栏 - Creating a Navigation Bar FIXED on the top of screen 如何使导航栏居中,但在固定位置的右上角有更多导航 - How can I center the navigation bar, but have more navigation on the top right in fixed positions 如何创建固定在屏幕顶部并调整大小以适合所有屏幕尺寸的水平居中导航栏? - How do I create a horizontally centered navigation bar that's fixed to the top of the screen and resizes to work on all screen sizes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM