Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating div positioning

Tags:

html

css

I need a floating rectangle (100% width, 100px height) to appear exactly 20px above the bottom of a page. How can I do that?

The code below shows the rectangle at the bottom of the browser window not the page - so if the page is taller than what can fit in the screen, the rectangle appears somewhere in the middle of the page.

<div style="z-index: 1; position: absolute; right: 0px; bottom: 20px; background-color: #000000; width: 100%; height: 100px; padding: 0px; color: white; ">&nbsp;</div> 
like image 731
Michael Avatar asked Mar 23 '11 00:03

Michael


People also ask

Does float work with position?

How float and position work together? They don't. An absolutely positioned element cannot float. A floating element cannot be absolutely positioned.

How do I make a div float above everything?

Look for the highest and then give a higher z-index to your popup element. This way it will flow even over the other elements with z-index. If you don't have a z-index in any element on your page, you should give like z-index:2; or something higher. Save this answer.

How do you float a div?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

What is the position of the float?

FLOAT POSITION means a position for the purpose of providing coverage for approved absences of Employees. The position may or may not have a master rotation but will have scheduled hours within the posted and confirmed period in accordance with the letter of appointment.


2 Answers

  • As suggested by @Laxman13, you need to add position: relative to the parent of the "floating rectangle". The relevant parent in this case just happens to be the body element.
  • Read this / this / this to help understand the position property.

body {
  position: relative
}

#floatingRectangle {
  z-index: 1;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 20px;
  height: 100px;
  background-color: #000;
  color: white;
  padding: 0;
}
<div id="floatingRectangle">
  <h2>Hello</h2>
</div>
Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />Long content<br />

Live Demo

like image 87
thirtydot Avatar answered Sep 28 '22 18:09

thirtydot


Add position: relative; to the rectangle div's parent. This will position the div 20px from the bottom of the parent element.

like image 41
Laxman13 Avatar answered Sep 28 '22 18:09

Laxman13