CSS Selectors Flashcards
What is universal selector?
- {
color: black;
}
Selects every possible element on page.
What is element selector?
Selects everything of a given type.
img {
width: 100px;
height: 200px;
}
This will select all img elements.
How do you combine multiple selectors?
Use a comma.
h1, h2 {
color: magenta;
}
How do you write an ID selector?
Use # along with the id name. ID specific to one element ID.
#search { color: blue; }
This will style the element with the ‘search’ id only.
What is a class selector?
.tag {
color: red;
}
You can create a class on an element to specify/pinpoint styling (can be used on any element). For example, if you have multiple <span> elements but only want to style certain ones, you can add a class tag to those <span class="tag">. then you would write </span></span>
Write a descendant selector for all anchor tags nested in a list styled with text color blue with a wavy underline.
li a {
color: blue;
text-decoration: wavy underline;
}
**A space is nested but a comma will select both ‘li’ and ‘a’ elements.
Write an adjacent or combinator selector for a paragraph immediately preceded by a heading.
h1 + p {
color: red;
}
Write a direct child combinator to select only li’s that are direct children of a div element.
div > li {
color: white;
}
Write an attribute selector where all input type text is 300px wide and font color yellow.
input [type=”text”] {
width: 300px;
color: yellow;
}
- *Applies to all attributes even class attributes.
- **Check attribute selector MDN for variations.
What are Pseudo Classes? Give examples.
Keyword/modifiers added to a selector that specifies a special state of the selected element(s). \:active \:checked \:first \:first-child \:hover \:not ( ) \:nth-child ( ) \:nth-of-type ( )
Style a button element that when hovered over the color changes to pink.
button:hover {
color: pink;
}
Style the item class with every other being green background with yellow text.
.item:nth-of-type(2n) {
background-color: green;
color: yellow;
}
Define Pseudo Elements and give examples.
Keywords added to a selector that lets you style a particular part of selected element(s). \::after \::before \::first-letter \::first-line \::selection
Define ‘The Cascade’
The order your styles are declared in and linked to matters. If multiple styles are declared in a sheet the styling follows the latest. Also if there are multiple style.css sheets the last on ‘link’ is followed.
What happens when you have conflicting styles targeting the same element?
Specificity will dictate which style will ultimately be applied to the page.
Remember ICE (ID>Class>Element)!!
What is the hierarchy of specificity in regards to selectors? Specificity score?
ID > Class > Element
I-C-E
What are Inline styles?
Inline styles are used within the html doc and takes highest priority. Generally not best use.
What does !important declaration do?
!important takes highest priority by ignoring specificity. Not best use.
What is CSS inheritance?
Some CSS properties will be inherited by child elements. For instance, if I style the body element, everything within the body will be styled as such.
What is CSS inheritance?
Some CSS properties will be inherited by child elements. For instance, if I style the body element, everything within the body will be styled as such. Inheritance will be from the closest parent element that is styled.
How do you force certain elements to inherit?
button, input {
color: inherit;
}
**Not all properties are inheritable.