html - 如何对齐在 div 内居中的无序列表?
问题描述
我试过这样。如何在 div 中居中列出项目?
.navsection{
background-color: cadetblue;
text-align: center;
}
.navsection ul{
margin: 0;
padding: 0;
list-style: none;
/* display: table;
margin: 0 auto; */
}
.navsection ul li{
display: block;
float: left;
}
.navsection ul li a{
text-decoration: none;
color: rgb(116, 4, 26);
display: inline-block;
padding: 10px 20px;
background: rgb(127, 210, 212);
margin: 5px 2px;
}
<div class="navsection tem clear">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Gallery</a></li>
</ul>
</div>
解决方案
使用flex
选择器的规则.navsection
。它应该如下所示:
.navsection {
display: flex;
justify-content: center;
align-items: center;
background-color: cadetblue;
}
.navsection{
display: flex;
justify-content: center;
align-items: center;
background-color: cadetblue;
}
.navsection ul{
margin: 0;
padding: 0;
list-style: none;
/* display: table;
margin: 0 auto; */
}
.navsection ul li{
display: block;
float: left;
}
.navsection ul li a{
text-decoration: none;
color: rgb(116, 4, 26);
display: inline-block;
padding: 10px 20px;
background: rgb(127, 210, 212);
margin: 5px 2px;
}
<div class="navsection tem clear">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Gallery</a></li>
</ul>
</div>
第二种解决方案(无弹性):
删除选择器中的float: left
and makedisplay: inline-block
而不是。display: block
.navsection ul li
它应该是这样的:
.navsection ul li{
display: inline-block;
/*float: left; */
}
.navsection{
background-color: cadetblue;
text-align: center;
}
.navsection ul{
margin: 0;
padding: 0;
list-style: none;
/* display: table;
margin: 0 auto; */
}
.navsection ul li{
display: inline-block;
/*float: left; */
}
.navsection ul li a{
text-decoration: none;
color: rgb(116, 4, 26);
display: inline-block;
padding: 10px 20px;
background: rgb(127, 210, 212);
margin: 5px 2px;
}
<div class="navsection tem clear">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Gallery</a></li>
</ul>
</div>
推荐阅读
- r - 根据 data.table 中的列列表过滤条件
- java - 在 Android Studio 中访问 aar 文件的类时出现 classNotFoundException
- c - 如何使用 microsoft azure client sdk c 从 iot hub 消息中获取 EventEnqueuedUtcTime
- reactjs - Material Ui table 减少列间距
- typo3 - 如何交付在模板外创建的 TYPO3 Fluid Form 元素?
- java - Android中随机输出中的EditText所有可能的数字
- java - 为什么这个二进制搜索代码在 Eclipse IDE 上给出错误的输出?
- anylogic - 插入从数据库加载的参数后,代理群体产生错误
- amazon-web-services - com.amazonaws.services.gluejobexecutor.model.InternalServiceException:要更新的项目大小已超过允许的最大大小
- sql - SQL:在有区间的交集中求和