英文:
HTML Navbar flipping when inserting the float:right property
问题
我有一个HTML导航栏,并且我想把导航栏中的所有元素移到右边。下面,我尝试使用float: right;
来实现这个目标。虽然这确实将所有内容移到了右边,但它翻转了整个导航栏(原本在左边的元素现在在右边,反之亦然)。我已经附上了代码。为什么会这样?
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: linear-gradient(rgb(83, 152, 42), rgb(51, 110, 14));
}
li {
float: right;
border-right: 1px solid #000;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgb(95, 161, 6);
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
li a:hover:not(.active) {
background-color: rgb(0, 90, 0);
}
.active {
background-color: #04AA6D;
}
@media screen and (max-width: 600px) {
ul li {
border-right: none;
}
ul li {
float: none;
}
}
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Member</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Manual</a></li>
<li><a href="#">Forums</a></li>
<li class="dropdown">
<a href="#" class="dropbtn">Hello there</a>
<div class="dropdown-content">
<a href="#">Settings</a>
<a href="#">Notifications</a>
<a href="#">Log out</a>
</div>
</li>
</ul>
</body>
</html>
英文:
I have an HTML Navbar, and I want to shift all the elements in the navbar to the right. Below, I have attempted to use float: right;
to do so. Although this does move everything to the right, it flips the entire navbar (so that elements that were originally in the left are now on the right, and vice versa). I have attached the code below. Why is it doing this?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: linear-gradient(rgb(83, 152, 42), rgb(51, 110, 14));
}
li {
float: right;
border-right: 1px solid #000;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgb(95, 161, 6);
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
li a:hover:not(.active) {
background-color: rgb(0, 90, 0);
}
.active {
background-color: #04AA6D;
}
@media screen and (max-width: 600px) {
ul li {
border-right: none;
}
ul li {
float: none;
}
}
</style>
</head>
<body>
<ul>
<li><a href="#"><i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Member <i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Manual</a></li>
<li><a href="#">Forums</a></li>
<li class="dropdown">
<a href="#" class="dropbtn">Hello there</a>
<div class="dropdown-content">
<a href="#">Settings</a>
<a href="#">Notifications</a>
<a href="#">Log out</a>
</div>
</li>
</ul>
</body>
</html>
<!-- end snippet -->
答案1
得分: 0
我已经添加了一些样式:display: block, width: 100% 和 text-align: right,它起作用了(重要的是使链接充满整个宽度,以改善用户体验)。
但我建议使用 flex 布局来实现这种布局,并采用移动优先的方法,这意味着您的媒体查询应该是以 min-width 定义的,从小屏幕到大屏幕进行设计。
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
祝你好运!
英文:
I have added some display: block, width: 100% and text-align: right and it works (important to make the link full width for improve the user experience)
But, I would recommend to use flex layout to achieve this kind of layouts, and use a mobile first apprach, that meands that your media queryes are defined with min-width to design from small to large screens
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Good luck!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: linear-gradient(rgb(83,152,42), rgb(51,110,14));
}
li {
float: right;
border-right:1px solid #000;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgb(95,161,6);
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
li a:hover:not(.active) {
background-color: rgb(0, 90, 0);
}
.active {
background-color: #04AA6D;
}
/** here are the modifications */
@media screen and (max-width: 600px) {
ul li { border-right: none; width: 100% }
ul li { float: none; }
ul li a { display: block; text-align: right; }
li a, .dropbtn { text-align: right; display: block; }
}
<!-- language: lang-html -->
<ul>
<li><a href="#"><i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Member <i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Manual</a></li>
<li><a href="#">Forums</a></li>
<li class="dropdown">
<a href="#" class="dropbtn">Hello there</a>
<div class="dropdown-content">
<a href="#">Settings</a>
<a href="#">Notifications</a>
<a href="#">Log out</a>
</div>
</li>
</ul>
<!-- end snippet -->
答案2
得分: 0
你可以在父级 ul
上使用 display: flex
和 justify-content: flex-end
来使当前顺序对齐到末尾。
ul {
display: flex;
justify-content: flex-end;
}
示例:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
display: flex;
justify-content: flex-end;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: linear-gradient(rgb(83, 152, 42), rgb(51, 110, 14));
}
li {
/* float: right; */
border-right: 1px solid #000;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgb(95, 161, 6);
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
li a:hover:not(.active) {
background-color: rgb(0, 90, 0);
}
.active {
background-color: #04AA6D;
}
@media screen and (max-width: 600px) {
ul li {
border-right: none;
}
ul li {
float: none;
}
}
</style>
</head>
<body>
<ul>
<li><a href="#">Home<i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Member <i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Manual</a></li>
<li><a href="#">Forums</a></li>
<li class="dropdown">
<a href="#" class="dropbtn">Hello there</a>
<div class="dropdown-content">
<a href="#">Settings</a>
<a href="#">Notifications</a>
<a href="#">Log out</a>
</div>
</li>
</ul>
</body>
</html>
英文:
You can use display display: flex
and justify-content: flex-end
on parent ul
to align to the end on current order.
ul {
display: flex;
justify-content: flex-end;
}
Example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<style>
ul {
display: flex;
justify-content: flex-end;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-image: linear-gradient(rgb(83, 152, 42), rgb(51, 110, 14));
}
li {
/* float: right; */
border-right: 1px solid #000;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgb(95, 161, 6);
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
li a:hover:not(.active) {
background-color: rgb(0, 90, 0);
}
.active {
background-color: #04AA6D;
}
@media screen and (max-width: 600px) {
ul li {
border-right: none;
}
ul li {
float: none;
}
}
</style>
</head>
<body>
<ul>
<li><a href="#">Home<i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Member <i class="fa fa-fw fa-home"></i></a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Manual</a></li>
<li><a href="#">Forums</a></li>
<li class="dropdown">
<a href="#" class="dropbtn">Hello there</a>
<div class="dropdown-content">
<a href="#">Settings</a>
<a href="#">Notifications</a>
<a href="#">Log out</a>
</div>
</li>
</ul>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论