我目前正在尝试在导航栏中选择列表元素并将其向右浮动,但是由于某些原因,列表元素并未向右浮动。知道为什么吗?
的HTML:
<body>
<header>
<!--Navigation bar-->
<nav class="navbar navbar-expand-sm bg-light">
<!--Navbar items-->
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">Register</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Login</a>
</li>
<li class="nav-item">
</li>
</ul>
</nav>
</header>
<!--Javascript files should be linked at the bottom of the page-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"; integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"; integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"; integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
CSS:
body{
background:#262733;
}
.navbar-nav .nav-item {
float: right;
}
最佳答案
顺序错误。如果要在.navbar-nav .nav-item
元素内选择.nav-item
,则应为.navbar-nav
。所以正确的代码是
body{
background:#262733;
}
.navbar-nav .nav-item {
float: right;
}
关于html - CSS不选择子元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48054379/