Never use! Instead of using! This chunk of CSS The following CSS styles Specification Status Comment Selectors Level 4 The definition of 'Calculating a selector's specificity' in that specification. Working Draft Add the specificity adjustment selector :where.
Selectors Level 3 The definition of 'Calculating a selector's specificity' in that specification. Recommendation Add pseudo-elements. Recommendation Add pseudo-classes. Recommendation Initial definition.
Definitions of value syntax , shorthand properties and replaced elements. Selectors Level 4 The definition of 'Calculating a selector's specificity' in that specification. For example, if we wanted to make all paragraphs have green text, we would use the following CSS rule:.
For example, if we wanted all emphasized text in our paragraphs to be green text, we would use the following CSS rule:. To match a specific class attribute, we always start the selector with a period, to signify that we are looking for a class value. The period is followed by the class attribute value we want to match. For example, if we wanted all elements with a class of "highlight" to have a different background color, we would use the following CSS rule:.
To match a specific id attribute, we always start the selector with a hash symbol , to signify that we are looking for an id value.
The hash is followed by the id attribute value we want to match. To style an anchor which has not been visited the :link pseudo-class comes into play, where the :visited pseudo-class styles links that a user has already visited based on their browsing history.
The :hover pseudo-class is applied to an element when a user moves their cursor over the element, most commonly used with anchor elements. The :active pseudo-class is applied to an element when a user engages an element, such as clicking on an element. Lastly, the :focus pseudo-class is applied to an element when a user has made an element the focus point of the page, often by using the keyboard to tab from one element to another. As with the link pseudo-classes there are also some pseudo-classes generated around the user interface state of elements, particularly within form elements.
These user interface element state pseudo-classes include :enabled , :disabled , :checked , and :indeterminate. The :enabled pseudo-class selects an input that is in the default state of enabled and available for use, where the :disabled pseudo-class selects an input that has the disabled attribute tied to it.
Many browsers by default will fade out disabled inputs to inform users that the input is not available for interaction, however those styles may be adjusted as wished with the :disabled pseudo-class. The last two user interface element state pseudo-classes of :checked and :indeterminate revolve around checkbox and radio button input elements.
The :checked pseudo-class selects checkboxes or radio buttons that are, as you may expect, checked. When a checkbox or radio button has neither been selected nor unselected it lives in an indeterminate state, from which the :indeterminate pseudo-class can be used to target these elements.
A handful of pseudo-classes are structural and position based, in which they are determined based off where elements reside in the document tree. These structural and position based pseudo-classes come in a few different shapes and sizes, each of which provides their own unique function. Some pseudo-classes have been around longer than others, however CSS3 brought way of an entire new set of pseudo-classes to supplement the existing ones.
The first structural and position based pseudo-classes one is likely to come across are the :first-child , :last-child , and :only-child pseudo-classes. These pseudo-classes are perfect for selecting the first or last items in a list and so forth. Additionally, the :only-child will select an element if it is the only element within a parent. Alternately, the :only-child pseudo-class could be written as :first-child:last-child , however :only-child holds a lower specificity. Here the selector li:first-child identifies the first list item within a list, while the selector li:last-child identifies the last list item within a list, thus lines 2 and 10 are selected.
The selector div:only-child is looking for a division which is the single child of a parent element, without any other other siblings. In this case line 4 is selected as it is the only division within the specific list item. Finding the first, last, and only children of a parent is pretty helpful, and often all that is needed. However sometimes you only want to select the first, last, or only child of a specific type of element.
For example, should you only want to select the first or last paragraph within an article, or perhaps the only image within an article.
Fortunately this is where the :first-of-type , :last-of-type , and :only-of-type pseudo-selectors come into place. The :first-of-type pseudo-class will select the first element of its type within a parent, while the :last-of-type pseudo-class will select the last element of its type within a parent.
The :only-of-type pseudo-class will select an element if it is the only of its type within a parent. In the example below the p:first-of-type and p:last-of-type pseudo-classes select the first and last paragraphs within the article respectively, regardless if they are actually the first or last children within the article. Lines 3 and 6 are selected, reflecting these selectors. The img:only-of-type selector identifies the image on line 5 as it is the only image to appear within the article, thus also selected.
Lastly there are a few structural and position based pseudo-classes that select elements based on a number or an algebraic expression.
These pseudo-classes include :nth-child n , :nth-last-child n , :nth-of-type n , and :nth-last-of-type n. All of these unique pseudo-classes are prefixed with nth and accept a number or expression inside of the parenthesis, indicated by the character n argument. The number or expression that falls within the parenthesis determines exactly what element, or elements, are to be selected. Using a number outright will count individual elements from the beginning or end of the document tree and then select one element, while using an expression will count numerous elements from the beginning or end of the document tree and select them in groups or multiples.
As mentioned, using numbers outright within a pseudo-class will count from the beginning, or end, of the document tree and select one element accordingly. For example, the li:nth-child 4 selector will select the fourth list item within a list. Counting begins with the first list item and increases by one for each list item, until finally locating and selecting the fourth item.
When using a number outright it must be a positive number. The a variable stands for the multiplier in which elements will be counted in while the b variable stands for where the counting will begin or take place.
For example, the li:nth-child 3n selector will identify every third list item within a list. As you can see the results of this expression lead to the third, sixth, and every element a multiple of three being selected. Additionally, the odd and even keyword values may be used. As expected, these will select odd or even elements respectively. The results of this expression leading to the seventh, eleventh, fifteenth, and every element that is a multiple of four here on out being selected. Using the n argument without being prefixed by a number results in the a variable being interpreted as 1.
To make things a bit more complicated negative numbers may also be used. Shadow Effects Box Shadow. Note: An id name cannot start with a number! Note: A class name cannot start with a number! Report Error. Your message has been sent to W3Schools.
0コメント