Fixed position element inheriting width of flex item
Asked Answered
I

1

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!

Imbibe answered 17/12, 2015 at 21:32 Comment(6)
Won't calculating the width work for you? width: calc(100vw - 150px - 250px);Bromley
@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.Coxcombry
@Imbibe 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...Coxcombry
@NielsKeurentjes I'd love to hear your suggestions if there's a succinct way of getting the concept across!Imbibe
'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.Coxcombry
see also github.com/twbs/bootstrap/pull/18707Astylar
M
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

Majolica answered 18/12, 2015 at 8:53 Comment(7)
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.Imbibe
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.Majolica
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.Imbibe
I think I could accommodate that and make the scrollbar appear and disappear without that jumping as well.Majolica
@Imbibe Check the update, I think I did what I thought wasn't possible.Majolica
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 widthDonnenfeld
@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.Majolica

© 2022 - 2024 — McMap. All rights reserved.