Advertisement
frusso1337

complex selector html

Mar 27th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 48.56 KB | None | 0 0
  1. Complex Selectors
  2. 1 Complex Selectors
  3.  
  4. Selectors are one of, if not, the most important parts of CSS. They shape the cascade and determine how styles are to be applied to elements on a page.
  5.  
  6. Up until recently the focus of CSS never really touched on selectors. Occasionally there would be incremental updates within the selectors specification, but never any real ground breaking improvements. Fortunately, more attention has been given to selectors as of late, taking a look at how to select different types of elements and elements in different states of use.
  7.  
  8. CSS3 brought new selectors, opening a whole new world of opportunities and improvements to existing practices. Here we'll discuss selectors, old and new, and how to best put them to use.
  9. 2 Common Selectors
  10.  
  11. Before diving too deep into some of the more complex selectors, and those offered within CSS3, let's take a quick look at some of the more common selectors seen today. These selectors include the type, class, and ID selectors.
  12.  
  13. The type selector identifies an element based on its type, specifically how that element is declared within HTML. The class selector identifies an element based on its class attribute value, which may be reused on multiple elements as necessary to help share popular styles. Lastly, the ID selector identifies an element based on its ID attribute value, which is unique and should only be used once per page.
  14.  
  15. h1 {...}
  16. .tagline {...}
  17. #intro {...}
  18.  
  19. <section id="intro">
  20. <h1>...</h1>
  21. <h2 class="tagline">...</h2>
  22. </section>
  23.  
  24. 2.1 Common Selectors Overview
  25. Example Classification Explanation
  26. h1 Type Selector Selects an element by it's type
  27. .tagline Class Selector Selects an element by the class attribute value, which may be reused multiple times per page
  28. #intro ID Selector Selects an element by the ID attribute value, which is unique and to only be used once per page
  29. 3 Child Selectors
  30.  
  31. Child selectors provide a way to select elements that fall within one another, thus making them children of their parent element. These selections can be made two different ways, using either descendant or direct child selectors.
  32. 3.1 Descendant Selector
  33.  
  34. The most common child selector is the descendant selector, which matches every element that follows an identified ancestor. The descendant element does not have to come directly after the ancestor element inside the document tree, such as a parent-child relationship, but may fall anywhere within the ancestor element. Descendant selectors are created by spacing apart elements within a selector, creating a new level of hierarchy for each element list.
  35.  
  36. The article h2 selector is a descendant selector, only selecting h2 elements that fall inside of an article element. Notice, no matter where a h2 element lives, so long as it is within the article element, it will always be selected. Additionally, any h2 element outside of the article element is not selected.
  37.  
  38. Below, the headings are lines 3 and 5 are selected.
  39.  
  40. article h2 {...}
  41.  
  42. <h2>...</h2>
  43. <article>
  44. <h2>This heading will be selected</h2>
  45. <div>
  46. <h2>This heading will be selected</h2>
  47. </div>
  48. </article>
  49.  
  50. 3.2 Direct Child Selector
  51.  
  52. Sometimes descendant selectors go a bit overboard, selecting more than hoped. At times only the direct children of a parent element need to be selected, not every instance of the element nested deeply inside of an ancestor. In this event the direct child selector may be used by placing a greater than sign, >, between the parent element and child element within the selector.
  53.  
  54. For example, article > p is a direct child selector only identifying p elements that fall directly within an article element. Any p element placed outside of an article element, or nested inside of another element other than the article element, will not be selected.
  55.  
  56. Below, the paragraph on line 3 is the only direct child of it's parent article, thus selected.
  57.  
  58. article > p {...}
  59.  
  60. <p>...</p>
  61. <article>
  62. <p>This paragraph will be selected</p>
  63. <div>
  64. <p>...</p>
  65. </div>
  66. </article>
  67.  
  68. 3.2.1 Child Selectors Overview
  69. Example Classification Explanation
  70. article h2 Descendant Selector Selects an element that resides anywhere within an identified ancestor element
  71. article > p Direct Child Selector Selects an element that resides immediately inside an identified parent element
  72. 4 Sibling Selectors
  73.  
  74. Knowing how to select children of an element is largely beneficial, and quite commonly seen. However sibling elements, those elements that share a common parent, may also need to be selected. These sibling selections can be made by way of the general sibling and adjacent sibling selectors.
  75. 4.1 General Sibling Selector
  76.  
  77. The general sibling selector allow elements to be selected based on their sibling elements, those which share the same common parent. They are created by using the tilde character, ~, between two elements within a selector. The first element identifies what the second element shall be a sibling with, and both of which must share the same parent.
  78.  
  79. The h2 ~ p selector is a general sibling selector that looks for p elements that follow, and share the same parent, of any h2 elements. In order for a p element to be selected it must come after any h2 element.
  80.  
  81. The paragraphs on lines 5 and 9 are selected as they come after the heading within the document tree and share the same parent as their sibling heading.
  82.  
  83. h2 ~ p {...}
  84.  
  85. <p>...</p>
  86. <section>
  87. <p>...</p>
  88. <h2>...</h2>
  89. <p>This paragraph will be selected</p>
  90. <div>
  91. <p>...</p>
  92. </div>
  93. <p>This paragraph will be selected</p>
  94. </section>
  95.  
  96. 4.2 Adjacent Sibling Selector
  97.  
  98. Occasionally a little more control may be desired, including the ability to select a sibling element that directly follows after another sibling element, which is where the adjacent sibling element comes in. The adjacent sibling selector will only select sibling elements directly following after another sibling element. Instead of using the tilde character, as with general sibling selectors, the adjacent sibling selector uses a plus character, +, between the two elements within a selector. Again, the first element identifies what the second element shall directly follow after and be a sibling with, and both of which must share the same parent.
  99.  
  100. Looking at the adjacent sibling selector h2 + p only p elements directly following after h2 elements will be selected. Both of which must also share the same parent element.
  101.  
  102. The paragraph on line 5 is selected as it directly follows after its sibling heading along with sharing the same parent element, thus selected.
  103.  
  104. h2 + p {...}
  105.  
  106. <p>...</p>
  107. <section>
  108. <p>...</p>
  109. <h2>...</h2>
  110. <p>This paragraph will be selected</p>
  111. <div>
  112. <p>...</p>
  113. </div>
  114. <p>...</p>
  115. </section>
  116.  
  117. 4.3 Sibling Selectors Example
  118.  
  119. <input type="checkbox" id="toggle">
  120. <label for="toggle">&#9776;</label>
  121. <nav>
  122. <ul>
  123. <li><a href="#">Home</a></li>
  124. <li><a href="#">About</a></li>
  125. <li><a href="#">Services</a></li>
  126. <li><a href="#">Contact</a></li>
  127. </ul>
  128. </nav>
  129.  
  130. input {
  131. display: none;
  132. }
  133. label,
  134. ul {
  135. border: 1px solid #cecfd5;
  136. border-radius: 6px;
  137. }
  138. label {
  139. color: #0087cc;
  140. cursor: pointer;
  141. display: inline-block;
  142. font-size: 18px;
  143. padding: 5px 9px;
  144. transition: all .15s ease;
  145. }
  146. label:hover {
  147. color: #ff7b29;
  148. }
  149. input:checked + label {
  150. box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15);
  151. color: #9799a7;
  152. }
  153. nav {
  154. max-height: 0;
  155. overflow: hidden;
  156. transition: all .15s ease;
  157. }
  158. input:checked ~ nav {
  159. max-height: 500px;
  160. }
  161. ul {
  162. list-style: none;
  163. margin: 8px 0 0 0;
  164. padding: 0;
  165. width: 100px;
  166. }
  167. li {
  168. border-bottom: 1px solid #cecfd5;
  169. }
  170. li:last-child {
  171. border-bottom: 0;
  172. }
  173. a {
  174. color: #0087cc;
  175. display: block;
  176. padding: 6px 12px;
  177. text-decoration: none;
  178. }
  179. a:hover {
  180. color: #ff7b29;
  181. }
  182.  
  183. 4.3.1 Sibling Selectors Overview
  184. Example Classification Explanation
  185. h2 ~ p General Sibling Selector Selects an element that follows anywhere after the prior element, in which both elements share the same parent
  186. h2 + p Adjacent Sibling Selector Selects an element that follows directly after the prior element, in which both elements share the same parent
  187. 5 Attribute Selectors
  188.  
  189. Some of the common selectors looked at early may also be defined as attribute selectors, in which an element is selected based up on its class or ID value. These class and ID attribute selectors are widely used and extremely powerful but only the beginning. Other attribute selectors have emerged over the years, specifically taking a large leap forward with CSS3. Now elements can be selected based on whether an attribute is present and what its value may contain.
  190. 5.1 Attribute Present Selector
  191.  
  192. The first attribute selector identifies an element based on whether it includes an attribute or not, regardless of any actual value. To select an element based on if an attribute is present or not simply include the attribute name in square brackets, [], within a selector. The square brackets may or may not follow any qualifier such as an element type or class, all depending on the level of specificity desired.
  193.  
  194. a[target] {...}
  195.  
  196. <a href="#" target="_blank">...</a>
  197.  
  198. 5.2 Attribute Equals Selector
  199.  
  200. To identify an element with a specific, and exact matching, attribute value the same selector from before may be used, however this time inside of the square brackets following the attribute name, include the desired matching value. Inside the square brackets should be the attribute name followed by an equals sign, =, quotations, =""=, and inside of the quotations should be the desired matching attribute value.
  201.  
  202. a[href="http://google.com/"] {...}
  203.  
  204. <a href="http://google.com/">...</a>
  205.  
  206. 5.3 Attribute Contains Selector
  207.  
  208. When looking to find an element based on part of an attribute value, but not an exact match, the asterisk character, *, may be used within the square brackets of a selector. The asterisk should fall just after the attribute name, directly before the equals sign. Doing so denotes that the value to follow only needs to appear, or be contained, within the attribute value.
  209.  
  210. a[href*="login"] {...}
  211.  
  212. <a href="/login.php">...</a>
  213.  
  214. 5.4 Attribute Begins With Selector
  215.  
  216. In addition to selecting an element based on if an attribute value contains a stated value, it is also possible to select an element based on what an attribute value begins with. Using a circumflex accent, ^, within the square brackets of a selector between the attribute name and equals sign denotes that the attribute value should begin with the stated value.
  217.  
  218. a[href^="https://"] {...}
  219.  
  220. <a href="https://chase.com/">...</a>
  221.  
  222. 5.5 Attribute Ends With Selector
  223.  
  224. Opposite of the begins with selector, there is also an ends with attribute selector. Instead of using the circumflex accent, the ends with attribute selector uses the dollar sign, $, within the square brackets of a selector between the attribute name and equals sign. Using the dollar sign denotes that the attribute value needs to end with the stated value.
  225.  
  226. a[href$=".pdf"] {...}
  227.  
  228. <a href="/docs/menu.pdf">...</a>
  229.  
  230. 5.6 Attribute Spaced Selector
  231.  
  232. At times attribute values may be spaced apart, in which only one of the words needs to be matched in order to make a selection. In this event using the tilde character, ~, within the square brackets of a selector between the attribute name and equals sign denotes an attribute value that should be whitespace-separated, with one word matching the exact stated value.
  233.  
  234. a[rel~="tag"] {...}
  235.  
  236. <a href="#" rel="tag nofollow">...</a>
  237.  
  238. 5.7 Attribute Hyphenated Selector
  239.  
  240. When an attribute value is hyphen-separated, rather than whitespace-separated, the vertical line character, |, may be used within the square brackets of a selector between the attribute name and equals sign. The vertical line denotes that the attribute value may be hyphen-separated however the hyphen-separated words must begin with the stated value.
  241.  
  242. a[lang|="en"] {...}
  243.  
  244. <a href="#" lang="en-US">...</a>
  245.  
  246. 5.8 Attribute Selectors Example
  247.  
  248. <ul>
  249. <li><a href="#.pdf">PDF Document</a></li>
  250. <li><a href="#.doc">Word Document</a></li>
  251. <li><a href="#.jpg">Image File</a></li>
  252. <li><a href="#.mp3">Audio File</a></li>
  253. <li><a href="#.mp4">Video File</a></li>
  254. </ul>
  255.  
  256. ul {
  257. list-style: none;
  258. margin: 0;
  259. padding: 0;
  260. }
  261. a {
  262. background-position: 0 50%;
  263. background-repeat: no-repeat;
  264. color: #0087cc;
  265. padding-left: 22px;
  266. text-decoration: none;
  267. }
  268. a:hover {
  269. color: #ff7b29;
  270. }
  271. a[href$=".pdf"] {
  272. background-image: url("images/pdf.png");
  273. }
  274. a[href$=".doc"] {
  275. background-image: url("images/doc.png");
  276. }
  277. a[href$=".jpg"] {
  278. background-image: url("images/image.png");
  279. }
  280. a[href$=".mp3"] {
  281. background-image: url("images/audio.png");
  282. }
  283. a[href$=".mp4"] {
  284. background-image: url("images/video.png");
  285. }
  286.  
  287. 5.8.1 Attribute Selectors Overview
  288. Example Classification Explanation
  289. a[target] Attribute Present Selector Selects an element if the given attribute is present
  290. a[href="http://google.com/"] Attribute Equals Selector Selects an element if the given attribute value exactly matches the value stated
  291. a[href*="login"] Attribute Contains Selector Selects an element if the given attribute value contains at least once instance of the value stated
  292. a[href^="https://"] Attribute Begins With Selector Selects an element if the given attribute value begins with the value stated
  293. a[href$=".pdf"] Attribute Ends With Selector Selects an element if the given attribute value ends with the value stated
  294. a[rel~="tag"] Attribute Spaced Selector Selects an element if the given attribute value is whitespace-separated with one word being exactly as stated
  295. a[lang| ="en"] Attribute Hyphenated Selector Selects an element if the given attribute value is hyphen-separated and begins with the word stated
  296. 6 Pseudo-classes
  297.  
  298. Pseudo-classes are similar to regular classes in HTML however they are not directly stated within the markup, instead they are a dynamically populated as a result of users actions or the document structure. The most common pseudo-class, and one you've likely seen before, is :hover. Notice how this pseudo-class begins with the colon character, :, as will all other pseudo-classes.
  299. 6.1 Link Pseudo-classes
  300.  
  301. Some of the more basic pseudo-classes include two revolving around links specifically. The :link and :visited pseudo-classes define if a link has or hasn't been visited. 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.
  302.  
  303. a:link {...}
  304. a:visited {...}
  305.  
  306. 6.2 User Action Pseudo-classes
  307.  
  308. Based on a users actions different pseudo-classes may be dynamically applied to an element, of which include the :hover, :active, and :focus pseudo-classes. 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.
  309.  
  310. a:hover {...}
  311. a:active {...}
  312. a:focus {...}
  313.  
  314. 6.3 User Interface State Pseudo-classes
  315.  
  316. 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.
  317.  
  318. 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.
  319.  
  320. input:enabled {...}
  321. input:disabled {...}
  322.  
  323. 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 or unselected it lives in an indeterminate state, from which the :indeterminate pseudo-class can be used to target these elements.
  324.  
  325. input:checked {...}
  326. input:indeterminate {...}
  327.  
  328. 6.4 Structural & Position Pseudo-classes
  329.  
  330. 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.
  331. 6.4.1 :first-child, :last-child, & :only-child
  332.  
  333. 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. The :first-child pseudo-class will select an element if it's the first child within its parent, while the :last-child pseudo-class will select an element if it's the last element within its parent. These pseudo-classes are prefect 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.
  334.  
  335. 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.
  336.  
  337. li:first-child {...}
  338. li:last-child {...}
  339. div:only-child {...}
  340.  
  341. <ul>
  342. <li>This list item will be selected</li>
  343. <li>
  344. <div>This div will be selected</div>
  345. </li>
  346. <li>
  347. <div>...</div>
  348. <div>...</div>
  349. </li>
  350. <li>This list item will be selected</li>
  351. </ul>
  352.  
  353. 6.4.2 :first-of-type, :last-of-type, & :only-of-type
  354.  
  355. 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.
  356.  
  357. 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.
  358.  
  359. 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 reflex 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.
  360.  
  361. p:first-of-type {...}
  362. p:last-of-type {...}
  363. img:only-of-type {...}
  364.  
  365. <article>
  366. <h1>...</h1>
  367. <p>This paragraph will be selected</p>
  368. <p>...</p>
  369. <img src="#"><!-- This image will be selected -->
  370. <p>This paragraph will be selected</p>
  371. <h6>...</h6>
  372. </article>
  373.  
  374. 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.
  375.  
  376. 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.
  377. 6.4.3 Using Pseudo-class Numbers & Expressions
  378.  
  379. 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 forth item. When using a number outright it must be a positive number.
  380.  
  381. Expressions for pseudo-classes fall in the format of an, an+b, an-b, n+b, -n+b, and -an+b. The same expression may be translated and read as (a×n)±b. 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.
  382.  
  383. For example, the li:nth-child(3n) selector will identify every third list item within a list. Using the expression this equates to 3×0, 3×1, 3×2, and so forth. As you can see the results of this expression lead to the third, sixth, and every element a multiple of three being selected.
  384.  
  385. Additionally, the odd and even keyword values may be used. As expected, these will select odd or even elements respectively. Should keyword values not be appealing the expression of 2n+1 would select all odd elements while the expression of 2n would select all even elements.
  386.  
  387. Using the li:nth-child(4n+7) selector will identify every forth list item starting with the seventh list item. Again, using the expression this equates to (4×0)+7, (4×1)+7, (4×2)+7, and so forth. The results of this expression leading to the seventh, eleventh, fifteenth, and every element a multiple of four here on out being selected.
  388.  
  389. Using the n argument without being prefixed by a number results in the a variable being interpreted as 1. With the li:nth-child(n+5) selector every list item will be selected starting with the fifth list item, leaving the first four list items unselected. Within the expression this breaks down as (1×0)+5, (1×1)+5, (1×2)+5, and so forth.
  390.  
  391. To make things a bit more complicated negative numbers may also be used. For example, the li:nth-child(6n-4) selector will start counting every sixth list item starting at negative four, selecting the second, eighth, and fourteenth list items and so forth. The same selector, li:nth-child(6n-4), could also be written as li:nth-child(6n+2), without the use of a negative b variable.
  392.  
  393. A negative a variable, or a negative n argument, must be followed by a positive b variable. When preceded by a negative a variable or negative n argument the b variable identifies how high the counting will reach. For example, the li:nth-child(-3n+12) selector will select every third list item within the first twelve list items. The selector li:nth-child(-n+9) will select the first nine list items within a list as the n argument, without any stated a variable, is defaulted to -1.
  394. 6.4.4 :nth-child(n) & :nth-last-child(n)
  395.  
  396. With a general understanding of how the pseudo-class numbers and expressions work let's take a look at the actual pseudo-classes in which these numbers and expressions may be used, the first of which being the :nth-child(n) and :nth-last-child(n) pseudo-classes. These pseudo-classes work a bit like the :first-child and :last-child pseudo-classes in that they look, and count, all of the elements within a parent and only select the element specifically identified. The :nth-child(n) works from the beginning of the document tree while the :nth-last-child(n) works from the end of the document tree.
  397.  
  398. Using the :nth-child(n) pseudo-class, let's look at the li:nth-child(3n) selector. The selector here will identify every third list item, thus lines 4 and 7 are selected.
  399.  
  400. li:nth-child(3n) {...}
  401.  
  402. <ul>
  403. <li>...</li>
  404. <li>...</li>
  405. <li>This list item will be selected</li>
  406. <li>...</li>
  407. <li>...</li>
  408. <li>This list item will be selected</li>
  409. </ul>
  410.  
  411. Using a different expression within the :nth-child(n) pseudo-class will yield a different selection. The li:nth-child(2n+3) selector, for example, will identify every second list item starting with the third and then onward. As a result, the list items lines 4 and 6 are selected.
  412.  
  413. li:nth-child(2n+3) {...}
  414.  
  415. <ul>
  416. <li>...</li>
  417. <li>...</li>
  418. <li>This list item will be selected</li>
  419. <li>...</li>
  420. <li>This list item will be selected</li>
  421. <li>...</li>
  422. </ul>
  423.  
  424. Changing the expression again, this time with a negative value, yields new selection. Here the li:nth-child(-n+4) selector is identifying the top four list items, leaving the rest of the list items unselected, thus lines 2 through 5 are selected.
  425.  
  426. li:nth-child(-n+4) {...}
  427.  
  428. <ul>
  429. <li>This list item will be selected</li>
  430. <li>This list item will be selected</li>
  431. <li>This list item will be selected</li>
  432. <li>This list item will be selected</li>
  433. <li>...</li>
  434. <li>...</li>
  435. </ul>
  436.  
  437. Adding an negative integer before the n argument changes the selection again. Here the li:nth-child(-2n+5) selector identifies every second list item within the first five list items, thus the list items on lines 2, 4, and 6 are selected.
  438.  
  439. li:nth-child(-2n+5) {...}
  440.  
  441. <ul>
  442. <li>This list item will be selected</li>
  443. <li>...</li>
  444. <li>This list item will be selected</li>
  445. <li>...</li>
  446. <li>This list item will be selected</li>
  447. <li>...</li>
  448. </ul>
  449.  
  450. Changing from the :nth-child(n) pseudo-class to the :nth-last-child(n) pseudo-class switches the direction of counting, with counting starting from the end of the document tree using the :nth-last-child(n) pseudo-class. The li:nth-last-child(3n+2) selector, for example, will identify every third list item starting from the second to last item in a list, moving towards the beginning of the list. Here the list items on lines 3 and 6 are selected.
  451.  
  452. li:nth-last-child(3n+2) {...}
  453.  
  454. <ul>
  455. <li>...</li>
  456. <li>This list item will be selected</li>
  457. <li>...</li>
  458. <li>...</li>
  459. <li>This list item will be selected</li>
  460. <li>...</li>
  461. </ul>
  462.  
  463. 6.4.5 :nth-of-type(n) & :nth-last-of-type(n)
  464.  
  465. The :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes are very similar to that of the :nth-child(n) and :nth-last-child(n) pseudo-classes, however instead of counting every element within a parent the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes only count elements of their own type. For example, when counting paragraphs within an article, the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes will skip any headings, divisions, or miscellaneous elements that are not paragraphs, while the :nth-child(n) and :nth-last-child(n) would count every element, no matter it's type, only selecting the ones that match the element within the stated selector. Additionally, all of the same expression possibilities used within the :nth-child(n) and :nth-last-child(n) pseudo-classes are also available within the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes.
  466.  
  467. Using the :nth-of-type(n) pseudo-class within the p:nth-of-type(3n) selector we are able to identify every third paragraph within a parent, regardless of other sibling elements within the parent. Here the paragraphs on lines 5 and 9 are selected.
  468.  
  469. p:nth-of-type(3n) {...}
  470.  
  471. <article>
  472. <h1>...</h1>
  473. <p>...</p>
  474. <p>...</p>
  475. <p>This paragraph will be selected</p>
  476. <h2>...</h2>
  477. <p>...</p>
  478. <p>...</p>
  479. <p>This paragraph will be selected</p>
  480. </article>
  481.  
  482. As with the :nth-child(n) and :nth-last-child(n) pseudo-classes, the primary difference between the :nth-of-type(n) and :nth-last-of-type(n) pseudo-classes is that the :nth-of-type(n) pseudo-class counts elements from the beginning of the document tree and the :nth-last-of-type(n) pseudo-class counts elements from the end of the document tree.
  483.  
  484. Using the :nth-last-of-type(n) pseudo-class we can write the p:nth-last-of-type(2n+1) selector which identifies every second paragraph from the end of a parent element starting with the last paragraph. Here the paragraphs on lines 4, 7, and 9 are selected.
  485.  
  486. p:nth-last-of-type(2n+1) {...}
  487.  
  488. <article>
  489. <h1>...</h1>
  490. <p>...</p>
  491. <p>This paragraph will be selected</p>
  492. <p>...</p>
  493. <h2>...</h2>
  494. <p>This paragraph will be selected</p>
  495. <p>...</p>
  496. <p>This paragraph will be selected</p>
  497. </article>
  498.  
  499. 6.5 Target Pseudo-class
  500.  
  501. The :target pseudo-class is used to style elements when an element's ID attribute value matches that of the URI fragment identifier. The fragment identifier within a URI can be recognized by the hash character, #, and what directly follows it. The URL http://example.com/index.html#hello includes the fragment identifier of hello. When this identifier matches the ID attribute value of an element on the page, <section id"hello">= for example, that element may be identified and stylized using the :target pseudo-class. Fragment identifiers are most commonly seen when using on page links, or linking to another part of the same page.
  502.  
  503. Looking at the code below, if a user would visit a page with the URI fragment identifier of #hello the section with that same ID attribute value would be stylized accordingly using the :target pseudo-class. If the URI fragment identifier changes, and matches the ID attribute value of another section, that new section may be stylized using the same selector and pseudo-class from before.
  504.  
  505. section:target {...}
  506.  
  507. <section id="hello">...</section>
  508.  
  509. 6.6 Empty Pseudo-class
  510.  
  511. The :empty pseudo-class allows elements that do not contain children or text nodes to be selected. Comments, processing instructions, and empty text nodes are not considered children and are not treated as such.
  512.  
  513. Using the div:empty pseudo-class will identify divisions without any children or text nodes. Below the divisions on lines 2 and 3 are selected as they are completely empty. Even though the second division contains a comment it is not considered to be a child, thus leaving the division empty. The first division contains text, the third division contains one blank text space, and the last division contains a strong child element, thus they are all ruled out and are not selected.
  514.  
  515. div:empty {...}
  516.  
  517. <div>Hello</div>
  518. <div><!-- Coming soon --></div><!-- This div will be selected -->
  519. <div></div><!-- This div will be selected -->
  520. <div> </div>
  521. <div><strong></strong></div>
  522.  
  523. 6.7 Negation Pseudo-class
  524.  
  525. The negation pseudo-class, :not(x), is a pseudo-class that takes an argument which is filtered out from the selection to be made. The p:not(.intro) selector uses the negation pseudo-class to identify every paragraph element without the class of intro. The paragraph element is identified at the beginning of the selector followed by the :not(x) pseudo-class. Inside of the parentheses falls the negation selector, the class of .intro in this case.
  526.  
  527. Below both the div:not(.awesome) and :not(div) selectors use the :not(x) pseudo-class. The div:not(.awesome) selector identifies any division without the class of awesome, while the :not(div) selector identifies any element that isn't a division. As a result the division on line 1 is selected as well as the two sections on lines 3 and 4, thus they are marked bold. The only element not selected is the division with the class of awesome, as it falls outside of the two negation pseudo-classes.
  528.  
  529. div:not(.awesome) {...}
  530. :not(div) {...}
  531.  
  532. <div>This div will be selected</div>
  533. <div class="awesome">...</div>
  534. <section>This section will be selected</section>
  535. <section class="awesome">This section will be selected</section>
  536.  
  537. 6.8 Pseudo-classes Example
  538.  
  539. <table>
  540. <thead>
  541. <tr>
  542. <th>Number</th>
  543. <th>Player</th>
  544. <th>Position</th>
  545. <th>Height</th>
  546. <th>Weight</th>
  547. </tr>
  548. </thead>
  549. <tbody>
  550. <tr>
  551. <td>8</td>
  552. <td>Marco Belinelli</td>
  553. <td>G</td>
  554. <td>6-5</td>
  555. <td>195</td>
  556. </tr>
  557. <tr>
  558. <td>5</td>
  559. <td>Carlos Boozer</td>
  560. <td>F</td>
  561. <td>6-9</td>
  562. <td>266</td>
  563. </tr>
  564. ...
  565. </tbody>
  566. </table>
  567.  
  568. table {
  569. border-collapse: separate;
  570. border-spacing: 0;
  571. width: 100%;
  572. }
  573. th,
  574. td {
  575. padding: 6px 15px;
  576. }
  577. th {
  578. background: #42444e;
  579. color: #fff;
  580. text-align: left;
  581. }
  582. tr:first-child th:first-child {
  583. border-top-left-radius: 6px;
  584. }
  585. tr:first-child th:last-child {
  586. border-top-right-radius: 6px;
  587. }
  588. td {
  589. border-right: 1px solid #c6c9cc;
  590. border-bottom: 1px solid #c6c9cc;
  591. }
  592. td:first-child {
  593. border-left: 1px solid #c6c9cc;
  594. }
  595. tr:nth-child(even) td {
  596. background: #eaeaed;
  597. }
  598. tr:last-child td:first-child {
  599. border-bottom-left-radius: 6px;
  600. }
  601. tr:last-child td:last-child {
  602. border-bottom-right-radius: 6px;
  603. }
  604.  
  605. 6.8.1 Pseudo-classes Overview
  606. Example Classification Explanation
  607. a:link Link Pseudo-class Selects a link that has not been visited by a user
  608. a:visited Link Pseudo-class Selects a link that has been visited by a user
  609. a:hover Action Pseudo-class Selects an element when a user has hovered their cursor over it
  610. a:active Action Pseudo-class Selects an element when a user has engaged it
  611. a:focus Action Pseudo-class Selects an element when a user has made it their focus point
  612. input:enabled State Pseudo-class Selects an element in the default enabled state
  613. input:disabled State Pseudo-class Selects an element in the disabled state, by way of the disabled attribute
  614. input:checked State Pseudo-class Selects a checkbox or radio button that has been checked
  615. input:indeterminate State Pseudo-class Selects a checkbox or radio button that neither been checked or unchecked, leaving it in an indeterminate state
  616. li:first-child Structural Pseudo-class Selects an element that is the first within a parent
  617. li:last-child Structural Pseudo-class Selects an element that is the last within a parent
  618. div:only-child Structural Pseudo-class Selects an element that is the only element within a parent
  619. p:first-of-type Structural Pseudo-class Selects an element that is the first of it's type within a parent
  620. p:last-of-type Structural Pseudo-class Selects an element that is the last of it's type within a parent
  621. img:only-of-type Structural Pseudo-class Selects an element that is the only of it's type within a parent
  622. li:nth-child(2n+3) Structural Pseudo-class Selects an element that matches the given number or expression, counting all elements from the beginning of the document tree
  623. li:nth-last-child(3n+2) Structural Pseudo-class Selects an element that matches the given number or expression, counting all elements from the end of the document tree
  624. p:nth-of-type(3n) Structural Pseudo-class Selects an element that matches the given number or expression, counting only elements of it's type from the beginning of the document tree
  625. p:nth-last-of-type(2n+1) Structural Pseudo-class Selects an element that matches the given number or expression, counting only elements of it's type from the end of the document tree
  626. section:target Target Pseudo-class Selects an element whose ID attribute value matches that of the URI fragment identifier
  627. div:empty Empty Pseudo-class Selects an element that does not contain any children or text nodes
  628. div:not(.awesome) Negation Pseudo-class Selects an element not represented by the stated argument
  629. 7 Pseudo-elements
  630.  
  631. Pseudo-elements are dynamic elements that don't exist in the document tree, and when used within selectors these pseudo-elements allow unique parts of the page to be stylized. One important point to note, only one pseudo-element may be used within a selector at a given time.
  632. 7.1 Textual Pseudo-elements
  633.  
  634. The first pseudo-elements ever released were the :first-letter and :first-line textual pseudo-elements. The :first-letter pseudo-element will identify the first letter of text within an element, while the :first-line pseudo-element will identify the first line of text within an element.
  635.  
  636. In the demonstration below the first letter of the paragraph with the class of alpha is set in a larger font size and colored orange, as is the first line of the paragraph with the class of bravo. These selections are made by use of the :first-letter and :first-line textual pseudo-elements respectively.
  637.  
  638. .alpha:first-letter,
  639. .bravo:first-line {
  640. color: #ff7b29;
  641. font-size: 18px;
  642. }
  643.  
  644. <p class="alpha">Lorem ipsum dolor...</p>
  645. <p class="bravo">Integer eget enim...</p>
  646.  
  647. 7.1.1 Textual Pseudo-elements Demo
  648.  
  649. body {
  650. color: #666;
  651. font: 14px/24px "Open Sans", "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", Sans-Serif;
  652. }
  653. .alpha:first-letter,
  654. .bravo:first-line {
  655. color: #ff7b29;
  656. font-size: 18px;
  657. }
  658. p {
  659. margin-bottom: 24px;
  660. }
  661. p:last-of-type {
  662. margin-bottom: 0;
  663. }
  664. .alpha:first-letter,
  665. .bravo:first-line {
  666. color: #ff7b29;
  667. font-size: 18px;
  668. }
  669.  
  670. <p class="alpha">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vestibulum dignissim leo a interdum. Duis eu orci velit.</p>
  671. <p class="bravo">Integer eget enim pulvinar leo consectetur tincidunt ut sed erat. Etiam elit velit, molestie eu rutrum vel, vehicula feugiat augue. Vestibulum elementum dictum turpis ac blandit.</p>
  672.  
  673. 7.2 Generated Content Pseudo-elements
  674.  
  675. The :before and :after generated content pseudo-elements create new inline level pseudo-elements just inside the selected element. Most commonly these pseudo-elements are used in conjunction with the content property to add insignificant information to a page, however that is not always the case. Additional uses of these psuedo-elements may be to add user interface components to the page without having to clutter the document with unsemantic elements.
  676.  
  677. The :before pseudo-element creates an pseudo-element before, or in front of, the selected element, while the :after pseudo-element creates an pseudo-element after, or behind, the selected element. These pseudo-elements appear nested within the selected element, not outside of it. Below the :after pseudo-element is used to display the href attribute value of anchor links within parentheses after the actual links. The information here is helpful, but not ultimately necessary should a browser not support these pseudo-elements.
  678.  
  679. a:after {
  680. color: #9799a7;
  681. content: " (" attr(href) ")";
  682. font-size: 11px;
  683. }
  684.  
  685. <a href="http://google.com/">Search the Web</a>
  686. <a href="http://learn.shayhowe.com/">Learn How to Build Websites</a>
  687.  
  688. 7.2.1 Generated Content Pseudo-elements Demo
  689.  
  690. body {
  691. font: 14px/24px "Open Sans", "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", Sans-Serif;
  692. }
  693. a {
  694. color: #0087cc;
  695. text-decoration: none;
  696. }
  697. a:hover {
  698. color: #ff7b29;
  699. }
  700. a:after {
  701. color: #9799a7;
  702. content: " (" attr(href) ")";
  703. font-size: 11px;
  704. }
  705.  
  706. <ul>
  707. <li><a href="http://google.com/">Search the Web</a></li>
  708. <li><a href="http://learn.shayhowe.com/">Learn How to Build Websites</a></li>
  709. </ul>
  710.  
  711. 7.3 Fragment Pseudo-element
  712.  
  713. The ::selection fragment pseudo-element identifies part of the document that has been selected, or highlighted, by a user's actions. The selection may then be stylized, however only using the color, background, background-color, and text-shadow properties. It is worth noting, the background-image property is ignore. While the shorthand background property may be used to add a color, any images will be ignored.
  714. 7.3.1 Single Colon (:) versus Double Colons (::)
  715.  
  716. The fragment pseudo-element was added with CSS3 and in attempt to differentiate pseudo-classes from pseudo-elements the double colons were added to pseudo-elements. Fortunately most browsers will support both values, single or double colons, for pseudo-elements however the ::selection pseudo-element must always start with double colons.
  717.  
  718. When selecting any of the text within the demonstration below the background will appear orange and any text shadows will be removed thanks to the ::selection fragment pseudo-element. Also note, the ::-moz-selection Mozilla prefixed fragment pseudo-element has been added to ensure the best support for all browsers.
  719.  
  720. ::-moz-selection {
  721. background: #ff7b29;
  722. }
  723. ::selection {
  724. background: #ff7b29;
  725. }
  726.  
  727. 7.4 Pseudo-elements Example
  728.  
  729. <a class="arrow" href="#">Continue Reading</a>
  730.  
  731. .arrow {
  732. background: #2db34a;
  733. color: #fff;
  734. display: inline-block;
  735. height: 30px;
  736. line-height: 30px;
  737. padding: 0 12px;
  738. position: relative;
  739. text-decoration: none;
  740. }
  741. .arrow:before,
  742. .arrow:after {
  743. content: "";
  744. height: 0;
  745. position: absolute;
  746. width: 0;
  747. }
  748. .arrow:before {
  749. border-bottom: 15px solid #2db34a;
  750. border-left: 15px solid transparent;
  751. border-top: 15px solid #2db34a;
  752. left: -15px;
  753. }
  754. .arrow:after {
  755. border-bottom: 15px solid transparent;
  756. border-left: 15px solid #2db34a;
  757. border-top: 15px solid transparent;
  758. right: -15px;
  759. }
  760. .arrow:hover {
  761. background: #ff7b29;
  762. }
  763. .arrow:hover:before {
  764. border-bottom: 15px solid #ff7b29;
  765. border-top: 15px solid #ff7b29;
  766. }
  767. .arrow:hover:after {
  768. border-left: 15px solid #ff7b29;
  769. }
  770.  
  771. 7.4.1 Pseudo-elements Overview
  772. Example Classification Explanation
  773. .alpha:first-letter Textual Pseudo-elements Selects the first letter of text within an element
  774. .bravo:first-line Textual Pseudo-elements Selects the first line of text within an element
  775. div:before Generated Content Creates a pseudo-element inside the selected element at the beginning
  776. a:after Generated Content Creates a pseudo-element inside the selected element at the end
  777. ::selection Fragment Pseudo-element Selects the part of a document which has been selected, or highlighted, by a users actions
  778. 7.4.2 Selector Browser Support
  779.  
  780. While these selectors provide a variety of opportunity and the ability to do some truly amazing things with CSS, they are at times plagued by poor browser support. Before doing anything too critical check the selectors you are wishing to use across your visitors most common browsers, and then make the judgment call as to whether they are appropriate or not.
  781.  
  782. CSS3.info provides a CSS3 Selectors Test tool which will inform you as to which selectors are supported by the browser in use. It's also never a bad idea to check browser support directly from the vendor.
  783.  
  784. Additionally, Selectivizr, a JavaScript utility, provides great support for these selectors in Internet Explorer 6-8. More support, should it be necessary, can also be provided by jQuery selectors.
  785. 7.4.3 Selector Speed & Performance
  786.  
  787. It is important to pay attention the speed and performance of selectors, as using too many intricate selectors can slow down the rendering of a page. Be attentive and when a selector begins to look a bit foreign think about revisiting it, and seeing if a better solution can be found.
  788. 8 Introduction to the Cascade
  789. 8.1 Multiple Sources
  790.  
  791. The most common scenario authors will be concerned with are the style rules which are declared by the author, but a style sheet may originate from several different sources:
  792.  
  793. The Author may specify style rules for a document
  794. The User may also specify style behaviors for viewing one or more documents - possibly through a special user interface or through a special external stylesheet.
  795. The browser will also have its own set of default rendering behaviors which, whether specified using an ACTUAL style sheet or by some other means accomplishes the same goal - behaving as if the browser has its own "default style sheet." This default style sheet is the base set of rendering rules - applied whether the user and author specify their own style sheet rules or not.
  796.  
  797. 8.2 Multiple Methods
  798.  
  799. In addition, style sheet rules may be specified using several different methods, any of which are legal:
  800.  
  801. External Style Sheets (using the HTML LINK element)
  802.  
  803. <link rel="stylesheet" type="text/css" href="Path To stylesheet.css" />
  804.  
  805. Embedded Style Sheets (using the HTML STYLE element)
  806.  
  807. <style type="text/css">
  808. body {
  809. font-size: 14px;
  810. }
  811. </style>
  812.  
  813. Imported Style Sheets (@import)
  814.  
  815. <style type="text/css">
  816. @import url(Path To stylesheet.css)
  817. </style>
  818.  
  819. Inline Style Rules (using the HTML STYLE attribute])
  820.  
  821. <p style="font-size:14px;color:red;">Some text here...</p>
  822.  
  823. Non-CSS rendering rules (eg: the HTML FONT element, that is deprecated in HTML5)
  824.  
  825. <font size="3" color="red">This is some text!</font>
  826. <!-- Not Supported in HTML5. -->
  827.  
  828. When more than one style rule is specified for a single CSS selector or element, using any of these sources or methods, and they are in direct conflict with one another, what should happen? When all is said and done, CSS generates a single, cohesive "virtual" style sheet using all the style rules (explicitly stated or not) from all of these disparate sources; CSS "cascades" (combines) all of them together. When conflicts arise between style rules, resolution is determined using a weighted scale. Each style rule will carry a specific weight on this scale; when two or more rules are in conflict, the rule with the highest weight will "win" and be rendered.
  829. Lowest Importance Highest Importance
  830. Ownership Browser Defaults Reader's Style Sheet Author's Style Sheet
  831. Specification Method Linked Style Sheet Embedded Style Sheet Inline Styles
  832. Element Selector Specificity Contextual Selector Depth Class ID
  833. 9 Inheritance
  834.  
  835. Some values are inherited by the children of an element in the document tree, as described above. Each property defines whether it is inherited or not.
  836.  
  837. Suppose there is an H1 element with an emphasizing element (EM) inside:
  838.  
  839. <h1>The headline <em>is</em> important!</h1>
  840.  
  841. If no color has been assigned to the EM element, the emphasized "is" will inherit the color of the parent element, so if H1 has the color blue, the EM element will likewise be in blue.
  842.  
  843. When inheritance occurs, elements inherit computed values. The computed value from the parent element becomes both the specified value and the computed value on the child.
  844.  
  845. For example, given the following style sheet:
  846.  
  847. body { font-size: 10pt }
  848. h1 { font-size: 130% }
  849.  
  850. <body>
  851. <h1>A <em>large</em> heading</h1>
  852. </body>
  853.  
  854. the 'font-size' property for the H1 element will have the computed value '13pt' (130% times 10pt, the parent's value). Since the computed value of 'font-size' is inherited, the EM element will have the computed value '13pt' as well. If the user agent does not have the 13pt font available, the actual value.
  855. 9.1 The !important value
  856.  
  857. The important keyword makes a declaration take precedence over normal declarations—those that are not tagged with the important keyword. So p { color: red ! important } takes precedence over p { color: green }.
  858.  
  859. The syntax for an important declaration is property: value ! important
  860.  
  861. The relative importance of a rule also depends on its the source: whether it comes from a style sheet specified by the document author, the user or the user agent (default stylesheet).
  862.  
  863. The order of declarations from least important to most important:
  864.  
  865. user agent declarations
  866. user normal declarations
  867. author normal declarations
  868. author important declarations
  869. user important declarations
  870.  
  871. User important declarations take precedence over everything else. This allows users to override the page designer's presentation to meet their needs. For example a user could override the text colours to be yellow on black in a large font to make the text easier to read.
  872. 9.2 The inherit value
  873.  
  874. Each property may also have a cascaded value of 'inherit', which means that, for a given element, the property takes the same specified value as the property for the element's parent. The 'inherit' value can be used to enforce inheritance of values, and it can also be used on properties that are not normally inherited.
  875.  
  876. If the 'inherit' value is set on the root element, the property is assigned its initial value.
  877.  
  878. In the example below, the 'color' and 'background' properties are set on the body element. On all other elements, the 'color' value will be inherited and the background will be transparent. If these rules are part of the user's style sheet, black text on a white background will be enforced throughout the document.
  879.  
  880. body {
  881. color: black !important;
  882. background: white !important;
  883. }
  884.  
  885. * {
  886. color: inherit !important;
  887. background: transparent !important;
  888. }
  889.  
  890. 1. Complex Selectors
  891. 2. Common Selectors
  892. 3. Child Selectors
  893. 4. Sibling Selectors
  894. 5. Attribute Selectors
  895. 6. Pseudo-classes
  896. 7. Pseudo-elements
  897. 8. Introduction to the Cascade
  898. 9. Inheritance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement