Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do these values take precedence in CSS?

Tags:

css

I'm learning CSS and am confused by relative layout. What happens if you give conflicting property values for positioning? For example left: 50px; and right 50px;

I've tried myself and from what I can tell, right always gets dropped if there is both left and right. Also what about top vs bottom?

Example

<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
    position: relative;
    left: 30px;
    right: 30px;
    border: 10px solid #73AD21;
}
</style>
</head>
<body>

<h2>position: relative;</h2>

<p>Lorem Ipsum insert text here....</p>

<div class="relative">
This div element has position: relative;
</div>

</body>
</html>
like image 397
northerner Avatar asked Mar 11 '17 11:03

northerner


People also ask

How is CSS prioritized?

Summary. Now we can say that the priority of the CSS property in an HTML document is applied top to bottom and left to right. Values defined as Important will have the highest priority. Inline CSS has a higher priority than embedded and external CSS.

Which type of CSS has highest precedence?

Properties of CSS: Inline CSS has the highest priority, then comes Internal/Embedded followed by External CSS which has the least priority.

What is the CSS specificity order with highest to lowest precedence?

Specificity Rules include:CSS style applied by referencing external stylesheet has lowest precedence and is overridden by Internal and inline CSS. Internal CSS is overridden by inline CSS. Inline CSS has highest priority and overrides all other selectors.


1 Answers

From MDN:

When both the right CSS property and the left CSS property are defined, the position of the element is overspecified. In that case, the left value has precedence when the container is left-to-right (that is that the right computed value is set to -left), and the right value has precedence when the container is right-to-left (that is that the left computed value is set to -right).

So, when direction: ltr, left has precedence. When direction: rtl, right has precedence.

For top and bottom (MDN):

When both top and bottom are specified, as long as height is unspecified, auto or 100%, both top and bottom distances will be respected. Otherwise, if height is constrained in any way, the top property takes precedence and the bottom property is ignored.

like image 150
Dasar Avatar answered Oct 10 '22 23:10

Dasar