Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override position : absolute in css?

I have a menu on top of my page, and after that a div tag that uses a class as below:

<div class="a">
   Hello!
</div>

a is a general class that has position: absolute; in style.
I want to disable this absolute; since the div content not shown completely. So I decided to use another class that overrides the position setting.

<div class="a overridden-a">

What should I set for position: in .overridden-a{ position: ???? !important } in my other style?

Update: I don't want to edit the a class styles, It is common and general in project.

like image 971
Elnaz Avatar asked Dec 08 '22 22:12

Elnaz


2 Answers

The default value of position is static.

The use of !important is not best practice and should be avoided where possible. Instead, to override a CSS rule you need to use a selector of a higher specificity. Try this:

.a.overridden-a { 
    position: static;
}

position: relative; would achieve what you require as well.

like image 94
Rory McCrossan Avatar answered Dec 10 '22 10:12

Rory McCrossan


What you set it to instead depends on how you want to display it instead. There are 4 possible values (including absolute, which you're trying to override).

From http://www.w3schools.com/css/css_positioning.asp:

position: static; An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page. This is the default.

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. Other content will not be adjusted to fit into any gap left by the element.

position: fixed; An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.

position: absolute; An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

like image 28
ADyson Avatar answered Dec 10 '22 12:12

ADyson