【问题标题】:Keep div in the dead center of the page, no matter the size of neighbouring divs [duplicate]无论相邻 div 的大小如何,都将 div 保持在页面的中心位置 [重复]
【发布时间】:2020-07-31 05:06:58
【问题描述】:

我试图将 3 个元素置于页脚中间。无论他的邻居有多大,他们中的一个都需要留在页脚的死点。现在的问题是,整个团队都居中,但我希望中间的那个在中间,而左右的应该“粘”在它旁边,一个在左边,一个在右边。 JSFiddle https://jsfiddle.net/xbqz8od2/

<div class=container>
  <div id=left>No matter how big the left or right text gets</div>
  <div id=center>KEEP ME IN THE DEAD CENTER</div>
  <div id=right>foobar</div>
</div>
.container {
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  position: fixed;
  bottom: 0;
  width: 100%;
  background-color: lightgray;
}

#left,
#right,
#center {
  display: flex;
  margin: 1px;
  padding: 5px;
  background-color: lightblue;
}

#left {
  background-color: salmon;
}
#center {
  background-color: lightgreen;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    在左右元素中添加flex: 1;

    .container {
      padding: 0;
      margin: 0;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      position: fixed;
      bottom: 0;
      width: 100%;
      background-color: lightgray;
    }
    
    #left,
    #right,
    #center {
      display: flex;
      margin: 1px;
      padding: 5px;
      background-color: lightblue;
    }
    
    #left {
      background-color: salmon;
    }
    #center {
      background-color: lightgreen;
    }
    
    #left, #right {
      flex: 1;
    }
    <div class=container>
      <div id=left>No matter how big the left or right text gets</div>
      <div id=center>KEEP ME IN THE DEAD CENTER</div>
      <div id=right>foobar</div>
    </div>

    【讨论】:

    • 谢谢!我会接受答案,但它缺少一些东西,左侧元素上的display: block; text-align: right;,所以它使文本“粘”在它旁边jsfiddle.net/v3dfqpzm
    • 从技术上讲,您提到 elements,而不是 text,应该粘在中间元素上?
    • 你是对的,很难解释整个事情。无论如何,我为后代留下了评论。
    猜你喜欢
    • 2013-09-01
    • 2021-08-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多