4

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.

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.

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

Community
  • 1
  • 1
jabram
  • 653
  • 1
  • 6
  • 8
  • Won't calculating the width work for you? `width: calc(100vw - 150px - 250px);` – Anton Yakushev Dec 17 '15 at 21:54
  • @AntonYakushev the point is that the middle section grows automatically if the sidebars are left out, otherwise he could just use `left:150px` and `right:250px` instead. – Niels Keurentjes Dec 17 '15 at 21:56
  • @jabram the reason `width:inherit` doesn't work is that the default value for `width` is `auto`, which is being inherited just fine, it's just not helping you in this case. I know possible solutions to your issue but they're highly complex involving multiple sibling selectors... – Niels Keurentjes Dec 17 '15 at 21:58
  • @NielsKeurentjes I'd love to hear your suggestions if there's a succinct way of getting the concept across! – jabram Dec 17 '15 at 22:17
  • 'succinct' is the part I'm worried about :) I built a setup exactly like this a while ago (sadly for an internal site so can't share a link) but the CSS was so complex that even my colleagues barely understood it hehe. – Niels Keurentjes Dec 17 '15 at 22:20
  • see also https://github.com/twbs/bootstrap/pull/18707 – Bass Jobsen Dec 28 '15 at 10:31

1 Answers1

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.

DEMO

zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • interesting solution, thanks! I'll play around with this a bit, but one negative for me is the the overflow: scroll which will always display a scrollbar on PCs, which I'm trying to get away from. – jabram Dec 18 '15 at 16:02
  • If it doesn't have a scrollbar how would you access the content that stretches below the fixed footer? If you use `auto` it jumps the width of the scrollbar thereby making a unsightly offset. – zer00ne Dec 18 '15 at 16:16
  • I'd just rather scroll the page than the element itself. If the left and right sidebars had a lot of content as well, then we'd be dealing with three independently scrolling columns. – jabram Dec 18 '15 at 16:23
  • I think I could accommodate that and make the scrollbar appear and disappear without that jumping as well. – zer00ne Dec 18 '15 at 16:33
  • @jabram Check the update, I think I did what I thought wasn't possible. – zer00ne Dec 18 '15 at 16:58
  • This is not a good solution and doesn't answer the question asked "Fixed position element inheriting width of flex item" the proposed solution inherits width from something with fixed width – Ross Johnson Mar 14 '19 at 10:29
  • @RossJohnson The OP explained: *"...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."* So the OP's real issue is fixed widths to which my solution uses `min-width` thereby imbuing flexibility the OP needs. – zer00ne Mar 15 '19 at 11:18