[英]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. 另外,在
html
和body
上添加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.