Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place an item at a fixed position in a scrolling div

Tags:

html

css

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 of the div, which will be visible when someone is scrolling the div and will not visible after content disappears or window scrolls. But I am not getting how could I achieve my objective. As making it fixed It get visible to whole body scroll.

I want that element to be fixed at the div's bottom and will not visible if the body scrolls Below I am giving an example:

div { height: 100px;
    border: 1px solid; 
    overflow:auto;
    position:relative;
    
    }
span {
  background : red;
  position : fixed;
  
}
<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 botti=om 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>

As you can see the red strip content is visible to the whole body if I position it to be fixed. However, i just want it to be fixed at the div's bottom and visible only when div scrolling.

like image 971
Satyam Pathak Avatar asked Feb 04 '18 11:02

Satyam Pathak


People also ask

How do you make a div fixed position while scrolling?

This is done by adding the position of the element relative to the viewport with the current scroll position. The getBoundingClientRect() method returns a DOMReact object of which the 'top' value could be used to get the position relative to the viewport.

How do I give a fixed position to a div?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.

How do I fix scrolling position?

To make an element stick to the screen when you are scrolling, select it and just click the Fix Position When Scrolling checkbox. Select the artboard, set a Vertical Scrolling and set a value for the Viewpoint Height that's lower than the height of the entire artboard.

Which position will keep the element in the same place regardless of scrolling?

A fixed position element is positioned relative to the viewport, or the browser window itself. The viewport doesn't change when the window is scrolled, so a fixed positioned element will stay right where it is when the page is scrolled.


1 Answers

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>
like image 186
Roko C. Buljan Avatar answered Sep 28 '22 01:09

Roko C. Buljan