我在以flex为中心的div中创建了以下列表。
如您在代码段中所见,列表没有居中(正如我期望在list-div2
translate
的{{1}}和middle-text
规则上使用flex一样。
有人能帮我吗?为什么它无法正确居中?
div
body {
margin: 0;
}
.container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
height: 100vh;
background-color: white;
}
.container>div {
min-height: 100vh;
border: 1px solid black;
box-sizing: border-box;
background-color: inherit;
}
.container>div .content {
height: 100vh;
width: 100vw;
background-color: inherit;
}
.full-width {
width: 100%;
}
.half-width {
width: 50%;
}
.full-width>.content>.third-parent {
height: 100%;
display: flex;
flex-direction: row;
}
.full-width>.content>.third-parent>.third {
position: relative;
flex: 1 1 0px;
border: 1px solid black;
width: 100%;
}
.full-width>.content>.third-parent>.third>img {
position: absolute;
width: 100%;
height: auto;
left: 50%;
top: 50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.middle-text {
position: absolute;
width: 100%;
left: 50%;
top: 50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.full-width>.content>.third-parent>.third>.middle-text>.list-div2 {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.full-width>.content>.third-parent>.third>.middle-text>.list-div2 li {
position: relative;
display: block;
border: 1px solid red;
margin-bottom: 5px;
padding: 10px;
text-align: center;
text-transform: uppercase;
visibility: visible;
list-style-type: bullet;
}
答案 0 :(得分:2)
这是由于浏览器默认将填充/边距添加到ul
标记中。
示例:
body {
margin: 0;
}
.container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
height: 100vh;
background-color: white;
}
.container>div {
min-height: 100vh;
border: 1px solid black;
box-sizing: border-box;
background-color: inherit;
}
.container>div .content {
height: 100vh;
width: 100vw;
background-color: inherit;
}
.full-width {
width: 100%;
}
.half-width {
width: 50%;
}
.full-width>.content>.third-parent {
height: 100%;
display: flex;
flex-direction: row;
}
.full-width>.content>.third-parent>.third {
position: relative;
flex: 1 1 0px;
border: 1px solid black;
width: 100%;
}
.full-width>.content>.third-parent>.third>img {
position: absolute;
width: 100%;
height: auto;
left: 50%;
top: 50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.middle-text {
position: absolute;
width: 100%;
left: 50%;
top: 50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.full-width>.content>.third-parent>.third>.middle-text>.list-div2 {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.full-width>.content>.third-parent>.third>.middle-text>.list-div2 li {
position: relative;
display: block;
border: 1px solid red;
margin-bottom: 5px;
padding: 10px;
text-align: center;
text-transform: uppercase;
visibility: visible;
list-style-type: bullet;
}
.items-list2 {
margin: 0;
padding: 0;
}
<div class="container">
<div class="full-width">
<div class="content">
<div class="third-parent">
<div class="third" id="one">
<img src="https://fakeimg.pl/350x200/?text=left">
</div>
<div class="third" id="two">
<div class="middle-text">
<h1>Headline</h1>
<div class="list-div2">
<ul class="items-list2" id="list">
<li>Entry A</li>
<li>Entry B</li>
<li>Entry C</li>
<li>Entry D</li>
</ul>
</div>
</div>
</div>
<div class="third" id="three">
<img src="https://fakeimg.pl/350x200/?text=right">
</div>
</div>
</div>
</div>
</div>
答案 1 :(得分:2)
实际上,用户代理样式表还剩下空白。因此,您需要使用ul {padding-left:0}
覆盖该样式ul, menu, dir {
display: block;
list-style-type: disc;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
-webkit-padding-start: 40px;
}
答案 2 :(得分:1)
您应该将padding: 0;
交给ul
ul.items-list2 {
padding: 0;
}
W3C spec说ul的默认边距为40px,但是不同的浏览器都可以使用。因此,对边距和填充进行规范化总是很有益的。
body {
margin: 0;
}
ul.items-list2 {
padding: 0;
}
.container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
height: 100vh;
background-color: white;
}
.container>div {
min-height: 100vh;
border: 1px solid black;
box-sizing: border-box;
background-color: inherit;
}
.container>div .content {
height: 100vh;
width: 100vw;
background-color: inherit;
}
.full-width {
width: 100%;
}
.half-width {
width: 50%;
}
.full-width>.content>.third-parent {
height: 100%;
display: flex;
flex-direction: row;
}
.full-width>.content>.third-parent>.third {
position: relative;
flex: 1 1 0px;
border: 1px solid black;
width: 100%;
}
.full-width>.content>.third-parent>.third>img {
position: absolute;
width: 100%;
height: auto;
left: 50%;
top: 50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.middle-text {
position: absolute;
width: 100%;
left: 50%;
top: 50%;
visibility: visible;
text-align: center;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.full-width>.content>.third-parent>.third>.middle-text>.list-div2 {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.full-width>.content>.third-parent>.third>.middle-text>.list-div2 li {
position: relative;
display: block;
border: 1px solid red;
margin-bottom: 5px;
padding: 10px;
text-align: center;
text-transform: uppercase;
visibility: visible;
list-style-type: bullet;
}
<div class="container">
<div class="full-width">
<div class="content">
<div class="third-parent">
<div class="third" id="one">
<img src="https://fakeimg.pl/350x200/?text=left">
</div>
<div class="third" id="two">
<div class="middle-text">
<h1>Headline</h1>
<div class="list-div2">
<ul class="items-list2" id="list">
<li>Entry A</li>
<li>Entry B</li>
<li>Entry C</li>
<li>Entry D</li>
</ul>
</div>
</div>
</div>
<div class="third" id="three">
<img src="https://fakeimg.pl/350x200/?text=right">
</div>
</div>
</div>
</div>
</div>