我正在尝试将class =“ nav-item login”项目右对齐。我知道我可以使用“ navbar-nav ml-auto”来执行此操作,但是我想知道样式表中是否可以执行此操作?
下面列出了我的问题的HTML和CSS。谢谢
.navbar {
font-family: 'Roboto', sans-serif;
}
.navbar-light .navbar-brand {
text-decoration: none;
color: #000000;
}
.navbar-brand:hover {
text-decoration: underline;
}
.login {
margin-left: auto;
}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'great_songs:great_songs' %}">Great Songs</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:great_songs' %}">Songs</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:artists' %}">Artists</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:genres' %}">Genres</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:producers' %}">Producers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:labels' %}">Labels</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item login">
<a class="nav-link" href="{% url 'users:logout' %}">log out</a>
</li>
{% else %}
<li class="nav-item login">
<a class="nav-link" href="{% url 'users:login' %}">log in</a>
</li>
<li class="nav-item login">
<a class="nav-link" href="{% url 'users:register' %}">register</a>
</li>
{% endif %}
</li>
</ul>
</div>
</nav>
答案 0 :(得分:1)
您使用了哪个引导程序版本?如果您使用了引导程序3,则添加此CSS float:right; #navbar类。
答案 1 :(得分:1)
这很简单。
您甚至不需要CSS。
只需使用<ul>
。
将包含<div class="d-flex justify-content-end">
<ul>
<li>
</li>
</ul>
</div>
的那行代码放入div。
示例:
Find(name)
希望这会有所帮助。
答案 2 :(得分:1)
这是您想要的吗?
.navbar {
font-family: 'Roboto', sans-serif;
display: flex;
justify-content: space-between;
}
.navbar-light .navbar-brand {
text-decoration: none;
color: #000000;
}
.navbar-brand:hover {
text-decoration: underline;
}
.login {
margin-left: auto;
}
@media only screen and (max-width: 1000px) {
#login-list {
display: inherit;
}
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Great Songs</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#">Songs</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Artists</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Genres</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Producers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Labels</a>
</li>
</ul>
<ul class="navbar-nav" id="login-list">
<li class="nav-item login">
<a class="nav-link" href="#">Log out</a>
</li>
<li class="nav-item login">
<a class="nav-link" href="#">Log in</a>
</li>
<li class="nav-item login">
<a class="nav-link" href="#">Register</a>
</li>
</ul>
</div>
</nav>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
答案 3 :(得分:0)
首先,您需要在CSS代码中进行一些更正,以将.navbar选择器更改为#navbar,因为您已在HTML中将其定义为 id 休息,您可以简单地使用float:right;。 #navbar中的[属性]即可将其显示在右侧。我删除了一些DTL标签
#navbar {
font-family: "Roboto", sans-serif;
float: right;
}
.navbar-light .navbar-brand {
text-decoration: none;
color: #000000;
}
.navbar-brand:hover {
text-decoration: underline;
}
.login {
margin-left: auto;
}
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:great_songs' %}">Songs</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:artists' %}">Artists</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:genres' %}">Genres</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:producers' %}">Producers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'great_songs:labels' %}">Labels</a>
</li>
<li class="nav-item login">
<a class="nav-link" href="{% url 'users:logout' %}">log out</a>
</li>
<li class="nav-item login">
<a class="nav-link" href="{% url 'users:login' %}">log in</a>
</li>
<li class="nav-item login">
<a class="nav-link" href="{% url 'users:register' %}">register</a>
</li>
</li>
</ul>
</div>
</nav>