Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is position:absolute; left:-10000px; better than display:none;?

Tags:

css

I have seen people set an element's style to:

element{
    position:absolute;
    left:-10000px;
}

rather than setting:

element{
    display:none;
}

Does the first method have better browser support, or why do they do it that way?

like image 350
Web_Designer Avatar asked Apr 17 '11 06:04

Web_Designer


People also ask

Is it good practice to use position absolute?

As long as you structure your HTML so that the elements follow a logical order that makes sense when rendered without CSS, there is no reason why using absolute positioning should be considered bad practice. Save this answer.

What can I use instead of position absolute?

We can use CSS absolute positioning to overlap elements, but there's some advantages to using CSS grid instead. Let's explore using CSS grid as an alternative to position absolute.

What is difference between position absolute and relative?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

What does position absolute mean?

Absolute positioning defines the position of a given bounding box from the top and left side margins of the web page. This not only allows objects to be placed in an exact location, it also allows objects to be placed one on top of another.


1 Answers

An element whose display style property is none cannot be measured. Properties like offsetWidth and offsetHeight will always return 0 in that case.

On the other hand, an element that's visible but displaced outside the document area can be measured. So, if you want to hide an element but still be able to determine its width and height, the first solution is the way to go.

like image 102
Frédéric Hamidi Avatar answered Oct 21 '22 11:10

Frédéric Hamidi