gpt4 book ai didi

html - Flex 对齐内容结束引导导航项

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:49 25 4
gpt4 key购买 nike

Codepen -> https://codepen.io/SubSul/pen/LgWzVw .

我修改了 bootstrap 4 导航栏组件,在右上角包含一个用于登录的固定按钮下拉列表。我现在正尝试将导航项与登录按钮上的操纵杆对齐,但 justify-content: flex-end 似乎没有将其向右推。

我尝试将 margin-left 添加到导航项,但无济于事。

在桌面上我正试图实现这一点 -

enter image description here

在移动设备上,我试图让 Logo 居中对齐,并将登录按钮固定在右侧。我可以使用 margin-right 将 Logo 移动到中心,但是单击汉堡包时的过渡会将 Logo 稍微向左移动,我如何将其固定在中心 -

enter image description here

<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>

<div class="main">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar Logo</a>
<div class="collapse navbar-collapse justify-content-end" id="navbarText">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
<div class="login-widget">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Log In
</button>
<div class="dropdown-menu dropdown-menu-right">
<form class="px-4 py-3">
<div class="form-group">
<label for="exampleDropdownFormEmail1">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com">
</div>
<div class="form-group">
<label for="exampleDropdownFormPassword1">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck">
<label class="form-check-label" for="dropdownCheck">
Remember me
</label>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">New around here? Sign up</a>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
</div>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin="anonymous"></script>
</body>
</html>

样式

.main {
display: flex;
justify-content: space-between;
}
.dropdown-menu {
margin-top: 15px;
}
.navbar-collapse {
/* margin-left: 100%; */
}
.login-widget {
/* margin-left: 320px; */
}

最佳答案

我想出了一个解决方案,它不是我做过的最干净的,但也许它对你有用,我没有使用 flexbox 来做,所以你可以删除 .main css,首先你需要将 .login-widget div 移动到 nav 标签内,这样它就会看起来像你想要的桌面。

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar Logo</a>
<div class="collapse navbar-collapse justify-content-end" id="navbarText">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>

<div class="login-widget">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Log In
</button>
<div class="dropdown-menu dropdown-menu-right">
<form class="px-4 py-3">
<div class="form-group">
<label for="exampleDropdownFormEmail1">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com">
</div>
<div class="form-group">
<label for="exampleDropdownFormPassword1">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck">
<label class="form-check-label" for="dropdownCheck">
Remember me
</label>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">New around here? Sign up</a>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
</div>

</nav>

但我们在移动设备上会遇到问题,当您单击汉堡菜单时,.login-widget div 将与菜单的其他导航项对齐,破坏你的布局,所以我要解决这个问题,它给 .login-widget 一个绝对位置

.login-widget{
position: absolute;
right: 16px;
top: 4px;
}

这将解决 .login-widget 的问题,但现在我们有另一个问题 .navbar-brand 没有居中,所以我要修复它的方法是再次使用绝对位置

.navbar-brand{
position: absolute;
top: 8px;
right: calc(109px + 16px);
left: calc(56px + 16px);
text-align: center;
}

最后,您必须将此类包装在媒体查询中以重置桌面的值。

@media only screen and (min-width: 992px){
.login-widget{
position: static;
right: auto;
top: auto;
}

.navbar-brand{
position: static;
top: auto;
right: auto;
left: auto;
text-align: left;
}
}

Here如果您想测试它,您有一个代码笔,请告诉我这是否是您正在寻找的解决方案!

关于html - Flex 对齐内容结束引导导航项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53220767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com