Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position: absolute without setting top/left/bottom/right?

Case #1:

I want to put a logo above the photo in the header in the default WordPress theme ( http://twentyelevendemo.wordpress.com/ )

My solution: add the logo before the photo, and set position: absolute on it, without setting any of the top/left/bottom/right properties:

http://jsfiddle.net/TsAJp/

Html:

<a id="header">   <img id="logo">    <img id="photo"> </a> 

Css:

#logo {   position: absolute;   margin: 10px;   /* or padding: 10px; */   /* or border: 10px solid transparent;      only this works with my elderly iPhone Simulator.app */ } 

Case #2:

Another example is a horizontal multi-level menu which is 100% wide and laid out with display: table-*, but table-cells don't support position: relative, so my only solution was this: http://jsfiddle.net/pCe49/

It works on IE6-7, Firefox1.5, not working on Firefox 0.8, etc.

Do you think it is a good solution, or is it a non-standard piece of hack, which can fall apart any minute?

like image 686
biziclop Avatar asked Apr 20 '12 09:04

biziclop


People also ask

What's the point of positioning an element in relative without setting top bottom right left properties?

position: static;Static positioned elements are not affected by the top, bottom, left, and right properties.

What can I use instead of position absolute?

Fixed. The fixed value is similar to absolute as it can help you position an element anywhere relative to the document, however this value is unaffected by scrolling.

How do you position an absolute element?

Absolute An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element. If it doesn't have any parent elements, then the initial document <html> will be its parent.

How do you override absolute position in CSS?

position: relative; An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position.


2 Answers

The standard generally says if top/bottom, left/right are auto, then default them to their position: static values:

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-width

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-height

like image 70
biziclop Avatar answered Oct 11 '22 18:10

biziclop


AFAIK, you should pay attention of hierarchical css rules, beacuse if you don't specify top, left and other attributes, they are inherited from parent element, or are set by defaults in browser's css. IMHO, it's better to initialize elements with your values.

like image 29
Johnny_D Avatar answered Oct 11 '22 17:10

Johnny_D