2 回答
TA贡献1818条经验 获得超3个赞
margin:auto
除了以下之外,还可以在中间元素上使用space-between
:
html,
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
height: 85%;
width: 85%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top,
.tray-bottom {
height: 48px;
line-height: 48px;
clear:both;
display:flex;
}
.tray-left,
.tray-right {
width: 48px;
height: calc(100% - 96px);
float: left;
display:flex;
flex-direction:column;
}
.tray-right {
float: right;
}
.button {
background-color: yellow;
width: 46px;
height: 46px;
display:inline-block;
}
.middle {
margin:auto;
}
<div id="root">
<div class="tray tray-top">
<!--<div class="button begin">1</div>-->
<div class="button middle">2</div>
<div class="button end">3</div>
</div>
<div class="tray tray-left">
<!--<div class="button begin">4</div>-->
<div class="button middle">5</div>
<div class="button end">6</div>
</div>
<div class="tray tray-right">
<div class="button begin">7</div>
<div class="button middle">8</div>
<div class="button end">9</div>
</div>
<div class="tray tray-bottom">
<!--<div class="button begin">10</div>-->
<div class="button middle">11</div>
<div class="button end">12</div>
</div>
</div>
如果您想要完美居中,您可以使用如下所示的一些翻译进行纠正,因为不同的情况是已知的:
html,
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
height: 85%;
width: 85%;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top,
.tray-bottom {
height: 48px;
line-height: 48px;
clear: both;
display: flex;
justify-content: space-between
}
.tray-left,
.tray-right {
width: 48px;
height: calc(100% - 96px);
float: left;
display: flex;
flex-direction: column;
justify-content: space-between
}
.tray-right {
float: right;
}
.button {
background-color: yellow;
width: 46px;
height: 46px;
display: inline-block;
}
.middle {
margin: auto;
}
.tray-top .middle:first-child,
.tray-bottom .middle:first-child {
transform: translateX(50%)
}
.tray-top .middle:last-child,
.tray-bottom .middle:last-child {
transform: translateX(-50%)
}
.tray-left .middle:first-child,
.tray-right .middle:first-child {
transform: translateY(50%)
}
.tray-left .middle:last-child,
.tray-right .middle:last-child {
transform: translateY(-50%)
}
.tray .middle:first-child:last-child {
transform: none;
}
<div id="root">
<div class="tray tray-top">
<!--<div class="button begin">1</div>-->
<div class="button middle">2</div>
<div class="button end">3</div>
</div>
<div class="tray tray-left">
<!--<div class="button begin">4</div>-->
<div class="button middle">5</div>
<div class="button end">6</div>
</div>
<div class="tray tray-right">
<div class="button begin">7</div>
<div class="button middle">8</div>
<!--<div class="button end">9</div>-->
</div>
<div class="tray tray-bottom">
<div class="button begin">10</div>
<div class="button middle">11</div>
<!--<div class="button end">12</div>-->
</div>
</div>
TA贡献2051条经验 获得超10个赞
您可以引入包装元素来定位并将按钮放置在:
html,
body {
height: 100%;
margin: 0;
text-align: center;
}
#root {
background-color: blue;
height: 85%;
width: 85%;
min-height: 236px;
}
.tray {
box-sizing: border-box;
background-color: red;
border: thin solid black;
}
.tray-top,
.tray-bottom {
height: 48px;
line-height: 48px;
clear:both;
display:flex;
flex-direction:row;
}
.tray-left,
.tray-right {
width: 48px;
height: calc(100% - 96px);
float: left;
display:flex;
flex-direction:column;
}
.tray-right {
float: right;
}
.button {
background-color: yellow;
width: 46px;
height: 46px;
display:inline-block;
}
.flex-positioning-wrapper { display: inline-block; flex-basis: calc(100% / 3); display: flex; }
.flex-positioning-wrapper .begin { margin-bottom: auto; margin-right: auto; }
.flex-positioning-wrapper .middle { margin: auto; }
.flex-positioning-wrapper .end { margin-top: auto; margin-left: auto; }
<div id="root">
<div class="tray tray-top">
<div class="flex-positioning-wrapper"><!--<div class="button begin">1</div>--></div>
<div class="flex-positioning-wrapper"><div class="button middle">2</div></div>
<div class="flex-positioning-wrapper"><div class="button end">3</div></div>
</div>
<div class="tray tray-left">
<div class="flex-positioning-wrapper"><!--<div class="button begin">4</div>--></div>
<div class="flex-positioning-wrapper"><div class="button middle">5</div></div>
<div class="flex-positioning-wrapper"><div class="button end">6</div></div>
</div>
<div class="tray tray-right">
<div class="flex-positioning-wrapper"><div class="button begin">7</div></div>
<div class="flex-positioning-wrapper"><div class="button middle">8</div></div>
<div class="flex-positioning-wrapper"><div class="button end">9</div></div>
</div>
<div class="tray tray-bottom">
<div class="flex-positioning-wrapper"><!--<div class="button begin">10</div>--></div>
<div class="flex-positioning-wrapper"><div class="button middle">11</div></div>
<div class="flex-positioning-wrapper"><div class="button end">12</div></div>
</div>
</div>
注意:当前设置使得if太小.tray-left
而.tray-right
溢出- 我添加了,但如果这不适合您的用例,您可能需要寻找其他选项。#root
min-height
注2:我个人对此布局的建议是使用grid而不是flexbox,
- 2 回答
- 0 关注
- 91 浏览
添加回答
举报