Can't scroll to top of flex item that is overflowing container

后端 未结 7 2390
再見小時候
再見小時候 2020-11-21 05:08

So, in attempting to make a useful modal using flexbox, I found what seems to be a browser issue and am wondering if there is a known fix or workaround -- or ideas on how to

相关标签:
7条回答
  • 2020-11-21 05:13

    I also managed to do it using extra container

    HTML

    <div class="modal-container">
      <div class="modal">
        <div class="content-container">
           <div class="content">
             <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
          </div>
      </div>  
    </div>
    

    CSS

    .modal-container {
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background-color: black;
    }
    
    .modal {
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #aaa;
      height: 80%;
      width: 90%;
    }
    
    .content-container {
      background-color: blue;
      max-height: 100%;
      overflow: auto;
      padding:0;
    }
    
    .content {
      display: flex;
      background-color: red;
      padding: 5px;
      width: 900px;
      height: 300px;
    }
    

    in jsfiddle > https://jsfiddle.net/Nash171/cpf4weq5/

    change .content width/height values and see

    0 讨论(0)
  • 2020-11-21 05:15

    I think I found a solution. It works with lots of text and a little text. You don't need to specify the widths of anything, and it should work in IE8.

    .wrap1 {
      position: fixed;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background: rgba(0, 0, 0, 0.5);
      overflow-y: auto;
    }
    .wrap2 {
      display: table;
      width: 100%;
      height: 100%;
      text-align: center;
    }
    .wrap3 {
      vertical-align: middle;
      display: table-cell;
    }
    .wrap4 {
      margin: 10px;
    }
    .dialog {
      text-align: left;
      background-color: white;
      padding: 5px;
      border-radius: 3px;
      margin: auto;
      display: inline-block;
      box-shadow: 2px 2px 4px rgba(0, 0, 0, .5);
    }
    <div class="wrap1">
      <div class="wrap2">
        <div class="wrap3">
          <div class="wrap4">
            <div class="dialog">
              <p>Lorem ipsum dolor sit amet.</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 05:23

    2 Container Flex Method with Table fallback tested IE8-9, flex works in IE10,11. Edit: Edited to ensure vertical centering when minimal content, added legacy support.

    The issue stems from height being inherited from the viewport size which causes children to overflow, as Michael answered. https://stackoverflow.com/a/33455342/3500688

    something more simple and use flex to maintain the layout within the popup container(content):

    #popup {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    min-height: 100vh;
    background-color: rgba(0,0,0,.25);
    margin: auto;
    overflow: auto;
    height: 100%;
    bottom: 0;
    display: flex;
    align-items: flex-start;
    box-sizing:border-box;
    padding:2em 20px;
    }
    .container {
    background-color: #fff;
    margin: auto;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    /* width: 100%; */
    max-width: 500px;
    padding: 10px;
        /* display: flex; */
        /* flex-wrap: wrap; */
    }
    		<!--[if lt IE 10]>
    <style>
    	  #popup {
    			display: table;
    			width:100%;
    		}
    		.iewrapper {
    			display: table-cell;
    			vertical-align: middle;
    		}
    	</style>
    	<![endif]-->
    <div id="popup">
    	<!--[if lt IE 10]>
    <div class="iewrapper">
    <![endif]-->
        <div class="container">
            <p class="p3">Test</p>
        <p class="p3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <p class="p3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        </div>
    	<!--[if lt IE 10]>
    <div class="iewrapper">
    <![endif]-->
    </div>

    0 讨论(0)
  • 2020-11-21 05:31

    According to MDN, the safe value can now be provided to properties like align-items and justify-content. It's described as follows:

    If the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode were start.

    So, it might be used as follows.

    .rule
    {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: safe center;
    }
    

    However, it's unclear how much browser support it has, I could not find any examples of its use, and I have been having some issues with it myself. Mentioning it here to draw more attention to it.

    0 讨论(0)
  • 2020-11-21 05:34

    I managed to pull this off with just 3 containers. The trick is to separate the flexbox container from the container that controls the scrolling. Lastly, put everything into a root container to center it all. Here are the essential styles to create the effect:

    CSS:

    .root {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .scroll-container {
      margin: auto;
      max-height: 100%;
      overflow: auto;
    }
    
    .flex-container {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    

    HTML:

    <div class="root">
      <div class="scroll-container">
        <div class="flex-container">
          <p>Lorem ipsum dolor sit amet.</p>
        </div>
      </div>
    </div>
    

    I've created a demo here: https://jsfiddle.net/r5jxtgba/14/

    0 讨论(0)
  • 2020-11-21 05:36

    Well, as Murphy's Law would have it, the reading I did after posting this question resulted in a few results -- not completely resolved, but somewhat useful nonetheless.

    I played around with min-height a bit before posting, but was not aware of the intrinsic sizing constraints that are fairly new to the spec.

    http://caniuse.com/#feat=intrinsic-width

    Adding a min-height: min-content to the flexbox area does resolve the issue in Chrome, and with vendor prefixes also fixes Opera and Safari, though Firefox remains unresolved.

    min-height: -moz-min-content; // not implemented
    min-height: -webkit-min-content // works for opera and safari
    min-height: min-content // works for chrome
    

    Still looking for ideas on Firefox, and other potential solutions.

    0 讨论(0)
提交回复
热议问题