gpt4 book ai didi

html - 如何使 Bootstrap 3 行水平居中?

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

我用 Bootstrap 3 创建了一个“大型菜单”。

我无法获取创建水平居中的大型菜单并保留功能的行。有问题的元素具有 .mega-dropdown-menu 类。

<ul class="dropdown-menu mega-dropdown-menu row">

这是与我的问题相关的代码片段:

body {
background: #f0f0f0;
}

.dropdown-menu {
background-color: #f5f5f5;
}
.navbar-header {
background-color: #3a7dbb;
}
.collapse.navbar-collapse.js-navbar-collapse {
background-color: #3a7dbb;
/* ?=border-bottom: 5px solid #214f82 */
}

.navbar-default .navbar-nav > li > a.dropdown-toggle {
font-size: 16px;
color: white;
}

.dropdown > .dropdown-menu > li > a {
font-size: 16px;
}

.glyphicon-shopping-cart:before {
color: white;
}

.navbar .navbar-brand {
padding-top: 0px;
}

.dropdown > .dropdown-menu > li > a:hover {
background-color: white;
}

.navbar-nav > li > .dropdown-menu {
background-color: #f5f5f5;
margin-top: 20px;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}

.navbar-default .navbar-nav > li > a {
/* width: 8em; */
font-weight: bold;
}

.mega-dropdown {
position: static !important;
width: 100%;
}

.mega-dropdown-menu {
padding: 20px 0px;
width: 100%;
box-shadow: none;
-webkit-box-shadow: none;
}

.center {
margin: auto;
}

.mega-dropdown-menu > li > ul {
padding: 0;
margin: 0;
}

.mega-dropdown-menu > li > ul > li {
list-style: none;
}

.mega-dropdown-menu > li > ul > li > a {
display: block;
padding: 3px 20px;
clear: both;
font-weight: normal;
line-height: 1.428571429;
color: #262626;
white-space: normal;
}

.i-am-centered {
margin: auto;
max-width: 300px;
}

.mega-dropdown-menu > li ul > li > a {
font-size: 16px;
}
.mega-dropdown-menu > li ul > li > a:hover,
.mega-dropdown-menu > li ul > li > a:focus {
text-decoration: none;
color: #444;
background-color: white;
}

li.dropdown:hover {
background-color: #c3c1c1;
}
ul.dropdown-menu.mega-dropdown-menu.row {
width: 80%;
}
.mega-dropdown-menu .dropdown-header {
color: #428bca;
font-size: 18px;
font-weight: bold;
}

.mega-dropdown-menu .dropdown-header > a {
color: #428bca;
font-size: 18px;
font-weight: bold;
padding-left: 0;
}

.mega-dropdown-menu form {
margin: 3px 20px;
}

.mega-dropdown-menu .form-group {
margin-bottom: 3px;
}

.navbar-default .navbar-nav .open .dropdown-menu > li > a {
color: black !important;
}

@media only screen and (min-width: 768px) {
.dropdown:hover > .dropdown-menu {
display: block;
}

.dropdown > .dropdown-toggle:active {
pointer-events: none;
}
}

/* .mega-dropdown-menu:before {
content: "";
border-bottom: 15px solid #fff;
border-right: 17px solid transparent;
border-left: 17px solid transparent;
position: absolute;
top: -15px;
left: 285px;
z-index: 10;
}

.mega-dropdown-menu:after {
content: "";
border-bottom: 17px solid #ccc;
border-right: 19px solid transparent;
border-left: 19px solid transparent;
position: absolute;
top: -17px;
left: 283px;
z-index: 8;
} */
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<div class="">
<nav class="navbar navbar-default">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".js-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img src="https://brm.institute/wp-content/themes/brmi/images/brm-institute-logo.png"></a>
</div>

<div class="collapse navbar-collapse js-navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown mega-dropdown">
<a href="https://google.com" target="_blank" class="dropdown-toggle" data-toggle="dropdown">Dummy MegaMenu<span class="caret"></span></a>

<ul class="dropdown-menu mega-dropdown-menu row">
<li class="col-sm-4">
<ul>
<li class="dropdown-header"><a href="google.com">Dresses</a></li>
<li><a href="#">Unique Features</a></li>
<li><a href="#">Image Responsive</a></li>
<li><a href="#">Auto Carousel</a></li>
<li><a href="#">Newsletter Form</a></li>
<li><a href="#">Four columns</a></li>
<li class="divider"></li>
<li class="dropdown-header">Tops</li>
<li><a href="#">Good Typography</a></li>
</ul>
</li>
<li class="col-sm-4">
<ul>
<li class="dropdown-header">Jackets</li>
<li><a href="#">Easy to customize</a></li>
<li><a href="#">Glyphicons</a></li>
<li><a href="#">Pull Right Elements</a></li>
<li class="divider"></li>
<li class="dropdown-header">Pants</li>
<li><a href="#">Coloured Headers</a></li>
<li><a href="#">Primary Buttons & Default</a></li>
<li><a href="#">Calls to action</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>

<!-- /.nav-collapse -->
</nav>
</div>

代码可用 codepen

如有任何帮助,我们将不胜感激

谢谢

最佳答案

对于您当前的问题,将 .mega-dropdown-menu 设置为:

01 使用翻译:

left: 50% /* instead of 0 */
transform: translateX(-50%);

02 使用边距(首选解决方案 IMO)

left: 0;
right: 0;
margin: 0 auto; /* Centers the element */

关于html - 如何使 Bootstrap 3 行水平居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55510192/

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