CSS Selectors

This page is also somewhat ugly by design, as it is designed to introduce different CSS selectors!

Basic Selectors: Universal and Element Selectors

ID Selectors

Class Selectors

Popular Posts

Posted by /u/not_funny

Lol look at this hilarious meme funny


Posted by /u/gooner4lyfe

Happy birthday to the strongest defender/bodyguard in the Prem! gunners


Posted by /u/dogfan

This Corgi got some good stuff from the vet. dogs


The Descendant Selector

The Combinator (Adjacent Selector/Adjacent Sibling Selected) and Direct Child/Direct Descendant Selector

The Attribute Selector

Examples

  1. This selects all <input> elements where the type attribute is set to text
                    
                        input[type="text"]{
                            color:blue;
                        }:
                    
                
  2. This selects all <input> elements where the type attribute is set to password:
                        
                            input[type="password"]{
                                color:blue;
                            }
                        
                    
  3. This selects all <input> elements where the class attribute is set to post:
                            
                                section[class="post"]{
                                    color:pink;
                                }
                            
                        
  4. This selects all <a> elements where the href attribute matches the www.rabogan.github.io website:
                        
                            a[href="https://www.rabogan.github.io"]{
                                color:black;
                            }
                        
                    

Pseudo-classes

  • A pseudo-class in CSS is a keyword added to a selector that specifies a special state of the selected element
  • Common examples include :hover, :visited, :nth-child(), and :active
  • A common example is to change the color of a button when the mouse is hovering over it! To do this, we need to use a pseudo-class in our CSS file. In my example, I used button:hover{...}, and changed the background-color and color of all buttons that don't have their specificity overridden!
  • So, to change all buttons so that when the mouse is over them, the background-color changes to red and text to white, we use button:hover{...}, where ... is the color changes
  • Another example is this: input[type="radio"]:checked{...} In this, all inputs of type radio that have been checked will have specific attributes adjusted (usually some kind of box-shadow)
  • Finally, :nth-of-type() is very useful. Plugging in a number, e.g. p:nth-of-type(2){...} will simply changed the 2nd p-element among siblings of type p. However, p:nth-of-type(2n){...} will change every 2nd element among siblings of type p
  • Briefly, pseudo-elements

  • A pseudo-element in CSS is a keyword added to a selector that allows you to style a specific part of the selected element(s)
  • Common examples include ::before, ::after, ::selection, ::first-line, and ::first-letter
  • Within this page, my example involves having the first letter of every h2 tag on the page be resized to 36px: h2::first-letter{...}
  • In addition, the ::selection{...} standalone code in my CSS file made it so that most selected text is red and gold
  • The parts that differ are for any text selected with a h2: inspect to see why!
  • Specificity

  • This is quite an easy concept. Basically:
  • One thing to note is that inline styles have higher specificity than ids! For example, something like <button> style="color:coral"> will trump any number of ids, classes or elements associated with a button in the CSS stylesheet. They're avoided because they can cause serious confusion
  • On the topic of confusion, the !important exception is an example of bad practice. When the !important rule is used on a style declaration, it signals to the browser that the specific declaration should override ANY other declarations. Why is it bad practice? Simply, it makes things difficult to debug, as it breaks the cascading between stylesheets!
  • Finally, this link which helps to compare specificity might be very useful: Compare Specificities Here
  • Inheritance

  • This should be an intuitive concept for you!
  • Move on to the CSS Box Model