猿问

HTML - Flexbox 元素独立于其他元素居中对齐,同时保持其填充

我有一个弹性盒设计的页面,它将按钮放置在页面的边缘。


如果我到处都有按钮,则所有按钮都已就位:


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;

  flex-direction:row;

  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;

}

<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>

所以,上面的代码很好,并且按照我想要的方式工作。方框 2、5、8、11 都在中间。

但是,如果我不需要一些盒子(如 1、4、10),我仍然需要中间的 2、5、8、11。现在他们当然不再处于中间了:

现在我可以添加flex: 1;按钮来对此进行一些控制,但在这种情况下我会失去它们的填充(这很重要,因为它们是按钮)。

Flexbox 有没有办法让 2、5、8 和 11 始终位于中间,而与它们之前和之后的其他元素无关?


慕少森
浏览 92回答 2
2回答

泛舟湖上清波郎朗

margin:auto除了以下之外,还可以在中间元素上使用space-between:html,body {&nbsp; height: 100%;&nbsp; margin: 0;&nbsp; text-align: center;}#root {&nbsp; background-color: blue;&nbsp; height: 85%;&nbsp; width: 85%;}.tray {&nbsp; box-sizing: border-box;&nbsp; background-color: red;&nbsp; border: thin solid black;}.tray-top,.tray-bottom {&nbsp; height: 48px;&nbsp; line-height: 48px;&nbsp; clear:both;&nbsp; display:flex;}.tray-left,.tray-right {&nbsp; width: 48px;&nbsp; height: calc(100% - 96px);&nbsp; float: left;&nbsp; display:flex;&nbsp; flex-direction:column;}.tray-right {&nbsp; float: right;}.button {&nbsp; background-color: yellow;&nbsp; width: 46px;&nbsp; height: 46px;&nbsp; display:inline-block;}.middle {&nbsp;margin:auto;}<div id="root">&nbsp; <div class="tray tray-top">&nbsp; &nbsp; <!--<div class="button begin">1</div>-->&nbsp; &nbsp; <div class="button middle">2</div>&nbsp; &nbsp; <div class="button end">3</div>&nbsp; </div>&nbsp; <div class="tray tray-left">&nbsp; &nbsp; <!--<div class="button begin">4</div>-->&nbsp; &nbsp; <div class="button middle">5</div>&nbsp; &nbsp; <div class="button end">6</div>&nbsp; </div>&nbsp; <div class="tray tray-right">&nbsp; &nbsp; <div class="button begin">7</div>&nbsp; &nbsp; <div class="button middle">8</div>&nbsp; &nbsp; <div class="button end">9</div>&nbsp; </div>&nbsp; <div class="tray tray-bottom">&nbsp; &nbsp; <!--<div class="button begin">10</div>-->&nbsp; &nbsp; <div class="button middle">11</div>&nbsp; &nbsp; <div class="button end">12</div>&nbsp; </div></div>如果您想要完美居中,您可以使用如下所示的一些翻译进行纠正,因为不同的情况是已知的:html,body {&nbsp; height: 100%;&nbsp; margin: 0;&nbsp; text-align: center;}#root {&nbsp; background-color: blue;&nbsp; height: 85%;&nbsp; width: 85%;}.tray {&nbsp; box-sizing: border-box;&nbsp; background-color: red;&nbsp; border: thin solid black;}.tray-top,.tray-bottom {&nbsp; height: 48px;&nbsp; line-height: 48px;&nbsp; clear: both;&nbsp; display: flex;&nbsp; justify-content: space-between}.tray-left,.tray-right {&nbsp; width: 48px;&nbsp; height: calc(100% - 96px);&nbsp; float: left;&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; justify-content: space-between}.tray-right {&nbsp; float: right;}.button {&nbsp; background-color: yellow;&nbsp; width: 46px;&nbsp; height: 46px;&nbsp; display: inline-block;}.middle {&nbsp; margin: auto;}.tray-top .middle:first-child,.tray-bottom .middle:first-child {&nbsp; transform: translateX(50%)}.tray-top .middle:last-child,.tray-bottom .middle:last-child {&nbsp; transform: translateX(-50%)}.tray-left .middle:first-child,.tray-right .middle:first-child {&nbsp; transform: translateY(50%)}.tray-left .middle:last-child,.tray-right .middle:last-child {&nbsp; transform: translateY(-50%)}.tray .middle:first-child:last-child {&nbsp; transform: none;}<div id="root">&nbsp; <div class="tray tray-top">&nbsp; &nbsp; <!--<div class="button begin">1</div>-->&nbsp; &nbsp; <div class="button middle">2</div>&nbsp; &nbsp; <div class="button end">3</div>&nbsp; </div>&nbsp; <div class="tray tray-left">&nbsp; &nbsp; <!--<div class="button begin">4</div>-->&nbsp; &nbsp; <div class="button middle">5</div>&nbsp; &nbsp; <div class="button end">6</div>&nbsp; </div>&nbsp; <div class="tray tray-right">&nbsp; &nbsp; <div class="button begin">7</div>&nbsp; &nbsp; <div class="button middle">8</div>&nbsp; &nbsp; <!--<div class="button end">9</div>-->&nbsp; </div>&nbsp; <div class="tray tray-bottom">&nbsp; &nbsp; <div class="button begin">10</div>&nbsp; &nbsp; <div class="button middle">11</div>&nbsp; &nbsp; <!--<div class="button end">12</div>-->&nbsp; </div></div>

侃侃无极

您可以引入包装元素来定位并将按钮放置在: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溢出- 我添加了,但如果这不适合您的用例,您可能需要寻找其他选项。#rootmin-height注2:我个人对此布局的建议是使用grid而不是flexbox,
随时随地看视频慕课网APP

相关分类

Html5
我要回答