亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

左,中和右對齊3個不相等的塊

左,中和右對齊3個不相等的塊

一只萌萌小番薯 2020-02-02 15:10:40
我正在嘗試對齊包含3個內容塊的頂部菜單。我想要實現的是:塊1:左對齊塊2:水平居中塊3:右對齊如果所有3個塊的大小都相同,則可以使用flexbox(如代碼段中所示),但它們不是,所以不會產生我需要的輸出。相反,flexbox在3個塊之間放置了相等的空間-導致中間塊偏離中心對齊。我想知道這是否可以使用flexbox來實現,如果不能,則可以采用另一種解決方案。這需要在生產中穩健地工作,因此由于支持不足,因此不適用“網格”解決方案。.container {  margin: 20px 0;}.row {  background-color: lime;  display: flex;  justify-content: space-between;}.item {  background-color: blue;  color: #fff;  padding: 16px;}<div class="container">  <div class="row">    <div class="item">left, slightly longer</div>    <div class="item">center, this item is much longer</div>    <div class="item">right</div>  </div></div>
查看完整描述

2 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

您可以考慮flex-grow:1;flex-basis:0%使用left和right元素,然后text-align在其中對齊內容。我添加了一個額外的包裝程序,以僅在文本周圍保留背景。


訣竅是通過僅刪除中間內容并將其均分為左元素和右元素來計算可用空間。


.container {

  margin: 20px 0;

  padding-top:10px;

  background:linear-gradient(#000,#000) center/5px 100% no-repeat; /*the center*/

}


.row {

  background-color: lime;

  display: flex;

  color: #fff;

}


.item:not(:nth-child(2)) {

  flex-basis: 0%;

  flex-grow: 1;

}


.item:last-child {

  text-align: right;

}


.item span{

  background-color: blue;

  display:inline-block;

  padding: 16px;

  height:100%;

  box-sizing:border-box;

}

<div class="container">

  <div class="row">

    <div class="item"><span>left, slightly longer</span></div>

    <div class="item"><span>center, this item is much longer</span></div>

    <div class="item"><span>right</span></div>

  </div>

</div>

您也可以通過使元素保持關閉來執行相同的操作。只需調整文本對齊即可:


.container {

  margin: 20px 0;

  padding-top:10px;

  background:linear-gradient(#000,#000) center/5px 100% no-repeat; /*the center*/

}


.row {

  background-color: lime;

  display: flex;

  color: #fff;

}


.item:not(:nth-child(2)) {

  flex-basis: 0%;

  flex-grow: 1;

}


.item:first-child {

  text-align: right;

}


.item span{

  background-color: blue;

  display:inline-block;

  padding: 16px;

  height:100%;

  box-sizing:border-box;

}

<div class="container">

  <div class="row">

    <div class="item"><span>left, slightly longer</span></div>

    <div class="item"><span>center, this item is much longer</span></div>

    <div class="item"><span>right</span></div>

  </div>

</div>


查看完整回答
反對 回復 2020-02-02
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

您能否給flex-grow:1作為商品類別并檢查


.item {

background-color: blue;

color: #fff;

padding: 16px;

flex-grow:1;

}

希望這是您要尋找的


查看完整回答
反對 回復 2020-02-02
  • 2 回答
  • 0 關注
  • 592 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號