固定位置元素继承flex项的宽度

时间:2021-08-07 20:05:13

I'm building out a UI that requires a fixed position/sticky element at the bottom of the viewport with a width constrained by a main content area. The main content area is optionally flanked by (sibling) left and/or right sidebars with fixed widths, so I'm using Flexbox to build the three column structure with flex-grow: 1 on the main content.

我正在构建一个UI,它需要在视口底部有一个固定位置/粘性元素,其宽度受主要内容区域的约束。主要内容区域可选择由(兄弟)左侧和/或右侧边栏固定宽度,因此我使用Flexbox在主要内容上使用flex-grow:1构建三列结构。

I've learned from @Marc Audet's accepted answer at How can I make a fixed positioned div inherit width of parent? that setting width: inherit on the fixed element is typically how to solve this problem, but it only seems to work when there's a specified width on its parent, which doesn't help me considering I need the main content area to fill the remaining width of the page.

我从@Marc Audet的接受答案中学到了如何使固定的div继承父级的宽度?设置宽度:继承固定元素通常是如何解决这个问题,但它似乎只有在其父节点上有指定宽度时才能工作,这对我无需考虑我需要主内容区域来填充剩余宽度的页面。

Does anyone have any ideas for getting around this? Check out my Fiddle for the code/example. Any help would be appreciated!

有没有人有任何想法绕过这个?查看我的小提琴代码/示例。任何帮助,将不胜感激!

1 个解决方案

#1


2  

CSS

html {
  box-sizing: border-box;
  font: 400 16px/1.45 'Source Code Pro';
}

*,
*:before,
*:after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
  border: 0;
  outline: none;
  overflow-x: hidden;
}

body {
  background: #121;
  color: #FEF;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100vw;
  height: 100vh;
}

.container {
  display: flex;
  color: #fff;
  height: -moz-fit-content;
  height: -webkit-fit-content;
  height: fit-content;
  background: blue;
}

.left {
  background: blue;
  min-height: 100vh;
  min-width: 150px;
  flex-shrink: 0;
}

.middle {
  background: green;
  min-height: 100vh;
  overflow: hidden;
  width: calc(100vw - 400px);
  padding-bottom: 60px;
  flex-grow: 1;
  flex-shrink: 0;
}

.middle .fixed-footer {
  background: orange;
  position: fixed;
  bottom: 0;
  width: 100vw;
  width: inherit;
  padding: 16px 0;
  overflow-x: hidden;
}

.right {
  background: blue;
  min-height: 100vh;
  min-width: 250px;
  flex-shrink: 0;
}

@media screen and (min-width: 640px) {
  html {
    margin-left: calc(100vw - 100%);
    margin-right: 0;
    overflow-y: auto;
  }
}

Added Star Wars ipsum content to demonstrate .middle's vertical flexibility and how .fixed-footer is stationary and is .middle's width.

添加了星球大战的ipsum内容,以展示.middle的垂直灵活性以及.fixed-footer是静止的,并且是.middle的宽度。

DEMO

DEMO

#1


2  

CSS

html {
  box-sizing: border-box;
  font: 400 16px/1.45 'Source Code Pro';
}

*,
*:before,
*:after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
  border: 0;
  outline: none;
  overflow-x: hidden;
}

body {
  background: #121;
  color: #FEF;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  width: 100vw;
  height: 100vh;
}

.container {
  display: flex;
  color: #fff;
  height: -moz-fit-content;
  height: -webkit-fit-content;
  height: fit-content;
  background: blue;
}

.left {
  background: blue;
  min-height: 100vh;
  min-width: 150px;
  flex-shrink: 0;
}

.middle {
  background: green;
  min-height: 100vh;
  overflow: hidden;
  width: calc(100vw - 400px);
  padding-bottom: 60px;
  flex-grow: 1;
  flex-shrink: 0;
}

.middle .fixed-footer {
  background: orange;
  position: fixed;
  bottom: 0;
  width: 100vw;
  width: inherit;
  padding: 16px 0;
  overflow-x: hidden;
}

.right {
  background: blue;
  min-height: 100vh;
  min-width: 250px;
  flex-shrink: 0;
}

@media screen and (min-width: 640px) {
  html {
    margin-left: calc(100vw - 100%);
    margin-right: 0;
    overflow-y: auto;
  }
}

Added Star Wars ipsum content to demonstrate .middle's vertical flexibility and how .fixed-footer is stationary and is .middle's width.

添加了星球大战的ipsum内容,以展示.middle的垂直灵活性以及.fixed-footer是静止的,并且是.middle的宽度。

DEMO

DEMO