Giter Club home page Giter Club logo

css-specificity's Introduction

A platform integration developer works with CSS (professionally) for the first time: selector logic and specificity

Originally written in 2016.

I’ve worked with CSS before and have done the brilliant CSS selector tutorial at http://flukeout.github.io/, but I am definitely not an expert. Coming from a product where UX was not a priority doesn’t help. Here’s what I’ve learned in my first week of being a MEAN (stack, not personality) web developer.

Basic selector logic

a and b are CSS classes.

  • .a.b = select an element with both classes on it
  • .a, .b = select an element with a or b on it
  • .a .b = select an element with class b, which is a descendant of an element with a
  • .a > .b = select an element with class b, which is a direct descendant of an element with a

Elements which are direct descendants are exactly one level below, no more.

Avoiding !important with CSS specificity

Putting !important at the end of CSS properties is bad. It brings the priority of that property to be the highest, i.e. it will override every other time that property is set, whether sensible or not. Arbitrarily overriding stuff will make your overall CSS messy and confusing. It’s useful for testing in developer tools, but don’t commit it.

How to not use important: wrap the class containing the property which is being overridden, by a class in a more specific parent element/a class with higher specificity (try to choose one without variables and other junk).

e.g. properties within b are being overridden by something or the other. a has high specificity:

.a {
  .b { … }
}

The above is equivalent to .a.b. Don’t worry about the enclosing class: only the styling in .b will be applied.

Why does this work? At the heart of it all is:

Precedence of CSS selectors

The least complicated explanation I could find is the section Calculating CSS Specificity Value in https://css-tricks.com/specifics-on-css-specificity/.

TL;DR version of CSS selector precedence:

  1. Inline styling: there is no way to override this, except using !important, or maybe some jQuery/JS.
  2. #id
  3. class
  4. Native HTML tag/styling - e.g. <h1>, etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.