Bootstrap mt-auto 不对齐底部
Posted
技术标签:
【中文标题】Bootstrap mt-auto 不对齐底部【英文标题】:Bootstrap mt-auto does not align bottom 【发布时间】:2019-08-03 12:04:50 【问题描述】:正如您在 html 中看到的那样,我有一个侧边栏。 我想在底部对齐 User01 和 Logout 项目。
我已将 mt-auto 类分配给 ul 项目,但它不起作用。 然后我用 mt-auto 类创建了另一个部门,但是也没有帮助,知道如何在 Bootstrap 中将它们对齐底部吗?
在 align item end 中使用 d-flex 会更好吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css"
integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
<title>Control Panel</title>
</head>
<body>
<div class="container m-0 p-0">
<div class="row p-0">
<div class="sidebar col-lg-2 col-md-3 col-sm-4 d-block bg-secondary px-4 pt-3 bg-dark text-light">
<div class="d-flex align-items-start flex-column">
<a href="index.html" class="navbar-brand text-white mb-3"> <div class="d-inline">Admin Panel</div></a>
<ul class="navbar-nav text-white">
<li class="nav-item">
<a href="index.html" class="nav-link text-white active">Dashboard</a>
</li>
<li class="nav-item">
<a href="tasks.html" class="nav-link text-white">Tasks</a>
</li>
<li class="nav-item">
<a href="categories.html" class="nav-link text-white">Categories</a>
</li>
<li class="nav-item">
<a href="users.html" class="nav-link text-white">Users</a>
</li>
</ul>
<div class="mt-auto">
<ul class="navbar-nav text-white">
<li class="nav-item dropdown">
<a href="" class="nav-link dropdown-toggle text-white" data-toggle="dropdown">
<i class="fas fa-user"></i> User01
</a>
<div class="dropdown-menu">
<a href="profile.html" class="dropdown-item"><i class="fas fa-user-circle"></i> Profile</a>
<a href="settings.html" class="dropdown-item"><i class="fas fa-cog"></i> Settings</a>
</div>
</li>
<li class="nav-item">
<a href="login.html" class="nav-link text-white">
<i class="fas fa-sign-out-alt"></i> Logout
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
【参考方案1】:将position-absolute
和h-100
类添加到侧边栏中,然后将mt-auto
更改为position-absolute
并给出对齐方式@987654325@。
您也可以在布局应用程序中使用 flex 是一种智能对齐单向方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css"
integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
<title>Control Panel</title>
</head>
<body>
<div class="container m-0 p-0">
<div class="row p-0">
<div class="sidebar col-lg-2 col-md-3 col-sm-4 d-block bg-secondary px-4 pt-3 bg-dark text-light position-absolute h-100">
<div class="d-flex align-items-start flex-column">
<a href="index.html" class="navbar-brand text-white mb-3"> <div class="d-inline">Admin Panel</div></a>
<ul class="navbar-nav text-white">
<li class="nav-item">
<a href="index.html" class="nav-link text-white active">Dashboard</a>
</li>
<li class="nav-item">
<a href="tasks.html" class="nav-link text-white">Tasks</a>
</li>
<li class="nav-item">
<a href="categories.html" class="nav-link text-white">Categories</a>
</li>
<li class="nav-item">
<a href="users.html" class="nav-link text-white">Users</a>
</li>
</ul>
<div class="position-absolute" style="bottom: 0">
<ul class="navbar-nav text-white">
<li class="nav-item dropdown">
<a href="" class="nav-link dropdown-toggle text-white" data-toggle="dropdown">
<i class="fas fa-user"></i> User01
</a>
<div class="dropdown-menu">
<a href="profile.html" class="dropdown-item"><i class="fas fa-user-circle"></i> Profile</a>
<a href="settings.html" class="dropdown-item"><i class="fas fa-cog"></i> Settings</a>
</div>
</li>
<li class="nav-item">
<a href="login.html" class="nav-link text-white">
<i class="fas fa-sign-out-alt"></i> Logout
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
#使用 Flex 对齐布局
// CSS
.sidebar
height: 100vh;
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container m-0 p-0" >
<div class="row p-0">
<div class="sidebar col-lg-2 col-md-3 col-sm-4 bg-secondary px-4 pt-3 bg-dark text-light d-flex flex-column justify-content-between">
<div class="d-block">
<a href="index.html" class="navbar-brand text-white mb-3"> <div class="d-inline">Admin Panel</div></a>
<ul class="navbar-nav text-white">
<li class="nav-item">
<a href="index.html" class="nav-link text-white active">Dashboard</a>
</li>
<li class="nav-item">
<a href="tasks.html" class="nav-link text-white">Tasks</a>
</li>
<li class="nav-item">
<a href="categories.html" class="nav-link text-white">Categories</a>
</li>
<li class="nav-item">
<a href="users.html" class="nav-link text-white">Users</a>
</li>
</ul>
</div>
<div class="d-block">
<ul class="navbar-nav text-white">
<li class="nav-item dropdown">
<a href="" class="nav-link dropdown-toggle text-white" data-toggle="dropdown">
<i class="fas fa-user"></i> User01
</a>
<div class="dropdown-menu">
<a href="profile.html" class="dropdown-item"><i class="fas fa-user-circle"></i> Profile</a>
<a href="settings.html" class="dropdown-item"><i class="fas fa-cog"></i> Settings</a>
</div>
</li>
<li class="nav-item">
<a href="login.html" class="nav-link text-white">
<i class="fas fa-sign-out-alt"></i> Logout
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
【讨论】:
以上是关于Bootstrap mt-auto 不对齐底部的主要内容,如果未能解决你的问题,请参考以下文章