montrealist
montrealist

Reputation: 5693

How can I make wider list children spill out of their absolutely-positioned parent?

I have a list with dynamically-populated items, I need it to be wider than its absolutely-positioned parent (it's a custom <select> element implementation).

#wrapper {
    position: relative;
    border: 1px dashed black;
    width: 300px;
    margin: 0 auto;
}

#testContainer {
    display: inline-flex;
    position: absolute;
    right: 0px;
    background-color: fuchsia;
    list-style: none;
    padding: 0;
    border: 5px dashed orange;
}

#testLabel {
    width: 300px;
    background-color: yellow;
}

.testItem {
    width: 200px;
    height: 50px;
    background-color: #aaa;
}
<div id="wrapper">
  <div id="testLabel">label</div>
  <ul id="testContainer">
    <li class="testItem">aaaaaaaaaaaaaaaaaaaaaaaaaaapppppppppp</li>
    <li class="testItem">www</li>
  <li class="testItem">cccccccccccccccccc</li>
  </ul>
</div>

It works everywhere (screenshot 1) except IE11 (screenshot 2). How can I achieve this? Here's a Codepen: https://codepen.io/montrealist/pen/VrrYem

Chrome/Firefox/Safari

IE11

Upvotes: 3

Views: 187

Answers (3)

Ruslan
Ruslan

Reputation: 1303

Let's work without the flex, as our grandfathers taught us!

#wrapper {
  border: 1px dashed black;
  margin: 0 auto;
  position: relative;
  width: 300px;
}
#testLabel {
  background: yellow;
}
#testContainer {
  background: fuchsia;
  border: 5px dashed orange;
  font-size: 0;
  list-style: none;
  padding: 0;
  position: absolute;
  right: 0;
  white-space: nowrap;
}
.testItem {
  background: #aaa;
  display: inline-block;
  font-size: 16px;
  height: 50px;
  min-width: 200px;
}
<div id="wrapper">
  <div id="testLabel">label</div>
  <ul id="testContainer">
    <li class="testItem">aaaaaaaaaaaaaaaaaaaaaaaaaaapppppppppp</li>
    <li class="testItem">www</li>
    <li class="testItem">cccccccccccccccccc</li>
  </ul>
</div>

Upvotes: 1

Andrew Ice
Andrew Ice

Reputation: 841

Unless i'm understanding this wrong, I would simply add an overflow-x: auto; to the css for your #testContainer.

This will allow your List Items to be fully viewed the way they should be, just with the user having to scroll instead.

So just:

#testContainer {
  display: flex;
  position: absolute;
  right: 0;
  background-color: fuchsia;
  list-style: none;
  overflow-x: auto; /* Added this here */
  padding: 0;
  border: 5px dashed orange;
  width: calc(50vw - 50%)
}

Upvotes: 1

dippas
dippas

Reputation: 60563

you can do like this :

  • use display:flex instead of inline-flex in #testContainer
  • use width: calc(50vw - 50%)
  • [optional for tablet/mobile] - use left:-50% and width: calc(100vw - 50%)
  • use flex: 1 instead of width:200px in .testIem
  • to avoid overlapping letters (in IE at least) use word-wrap: break-word

#wrapper {
  position: relative;
  border: 1px dashed black;
  width: 300px;
  margin: 0 auto;
}

#testContainer {
  display: flex;
  position: absolute;
  right: 0;
  background-color: fuchsia;
  list-style: none;
  padding: 0;
  border: 5px dashed orange;
  width: calc(50vw - 50%)
}

#testLabel {
  width: 300px;
  background-color: yellow;
}

.testItem {
  flex: 1;
  height: 50px;
  background-color: #aaa;
  word-wrap: break-word
}

@media (max-width: 800px) {
  #testContainer {
    right: auto;
    left: -50%;
    width: calc(100vw - 50%)
  }
}
<div id="wrapper">
  <div id="testLabel">label</div>
  <ul id="testContainer">
    <li class="testItem">aaaaaaaaaaaaaaaaaaaaaaaaaaapppppppppp</li>
    <li class="testItem">www</li>
    <li class="testItem">cccccccccccccccccc</li>
  </ul>
</div>

Upvotes: 3

Related Questions