Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

css - How to make position fixed item same width as flex parent

I want the fixed progress bar have same width as the yellow container and always positioned at the bottom of screen which overlapping on top of yellow container with CSS.

I have try to using width: inherit in the fixed child but the yellow parent do not have width so it doesn't work.

.container {
  display: flex;
  width: 500px;
  height: 1200px;
}

.left {
  flex: 1;
  background: blue;
}

.right {
  flex: 1;
  background: yellow;
}

.progress {
  position: fixed;
  background: gray;
  bottom: 0;
}
<div class="container">
  <div class="left">

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

      <div class="progress">I'm progress bar</div>
  </div>
</div>  
question from:https://stackoverflow.com/questions/65546469/how-to-make-position-fixed-item-same-width-as-flex-parent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Position fixed will be fixed to the page's scroll position and should not be used as a child of a static element.

Instead, you should set position to absolute on the progress bar, and position relative on the container.

.container {
  position: relative;
}

.child {
  position: absolute;
  width: 100%;
  left: 0;
}

.container {
  position: relative;
  display: flex;
  width: 500px;
  height: 100px;
}

.left {
  flex: 1;
  background: blue;
}

.right {
  flex: 1;
  background: yellow;
}

.progress {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background: gray;
}
<div class="container">
  <div class="left">
  </div>

  <div class="right">
    <div class="progress">I'm progress bar</div>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...