Place an item at a fixed position in a scrolling div

后端 未结 2 954
耶瑟儿~
耶瑟儿~ 2021-01-04 09:11

I have a page full of content and window is scrollable, in which there is scrollable div where my objective is to place an icon at the very bottom

相关标签:
2条回答
  • 2021-01-04 09:17

    A standardly supported way is to edit the HTML markup and add a wrapper element to both the scrollable and the "fixed" element - which will now have position: absolute; instead:

    #wrapper {  /* ADDED */
      position: relative;
      width: 200px;
    }
    
    #scrollable {
      height: 100px;
      border: 1px solid;
      overflow: auto;
      position: relative;
    }
    
    #wrapperFooter {
      background: red;
      position: absolute; /**/
      bottom:0;
    }
    <div id="wrapper">
      <div id="scrollable">
        <p style="border: 4px dashed #000; height: 200px;"></p>
      </div>
      <div id="wrapperFooter">ABSOLUTELY! :)</div>
    </div>

    0 讨论(0)
  • 2021-01-04 09:33

    I think you are looking for sticky position like this

    div {
      height: 100px;
      border: 1px solid;
      overflow: auto;
      position: relative;
    }
    
    span {
      background: red;
      position: sticky;
      bottom: 0;
    }
    <div>
      <p>I have something</p>
      <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
      <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
      <span>this need to fixed at the bottom and visible while scrolling the div</span>
    </div>
    
    <p>I have something</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
    <p>I have something</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>

    Simply pay attention to browser support as this property is not yet supported everywhere

    Here is another trick using fixed position but more a hack than a generic solution.

    body {
      transform:translate(0,0); /* Block fixed position from scrolling*/
    }
    div {
      height: 100px;
      border: 1px solid;
      overflow: auto;
      position: relative;
    }
    
    span {
      background: red;
      position: fixed;
      margin-top:-110px; /*this need to be calculated depending on content*/
    }
    <div>
      <p>I have something</p>
      <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
      <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
      <span>this need to fixed at the bottom and visible while scrolling the div</span>
    </div>
    
    <p>I have something</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
    <p>I have something</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>
    <p>I have something shdbfs jsdfjhs d sdfhsjdgf dsb usbsd shdshdhsdbs ds dvhsd sdshd vcsgdvshuc sd chjsdcsshdcbshudcuysdchusd cuyssdc shd cscyusdc gsdcsducsgcs uycsucs sdusdyu</p>

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