{"id":1671,"date":"2015-08-31T11:04:43","date_gmt":"2015-08-31T16:04:43","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=1671"},"modified":"2015-08-31T11:04:43","modified_gmt":"2015-08-31T16:04:43","slug":"pseudoclasscss","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/pseudoclasscss\/","title":{"rendered":"The case for using the Pseudo Class in CSS"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Pseudo Classes are a crucial part of CSS. They allow developers to apply styles to elements under certain conditions. For example, if there is a &lt;div&gt; tag with 3 &lt;p&gt; tags inside of it, the :nth-child pseudo selector is able to specify which one of the &lt;p&gt; tags to apply a style to without needing a bunch of ids. Selecting elements without using ids is also really useful in dynamically generated websites, where a developer may want to style the first and last elements differently than the middle ones.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Some pseudo classes are more than just a way to avoid an overload of ids, such as :hover. :hover allows developers to customize an element when the cursor is hovering on top of it. The :hover is a common part of customizing the look of standard links, which already have :hover events and may look ugly without customization.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Here is a demo of what can be achieved with pseudo classes. It will use a google font called Roboto, which can be added to most projects using the methods provided on https:\/\/www.google.com\/fonts . This demo won\u2019t contain a single id or class, yet it will still look nice and have basic functionality in the end with the power of pseudo classes. To start, create a &lt;div&gt; and 3 child &lt;p&gt; elements with &lt;div&gt; tags in between the &lt;p&gt; tags like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1672\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.40.48-PM-300x152.png\" alt=\"Screen Shot 2015-08-27 at 3.40.48 PM\" width=\"300\" height=\"152\" \/><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>It should look something like this:<\/p>\n<p><strong><strong>\u00a0<a href=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.41.20-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1673\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.41.20-PM-300x136.png\" alt=\"Screen Shot 2015-08-27 at 3.41.20 PM\" width=\"300\" height=\"136\" \/><\/a><\/strong><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Now the HTML is complete and we can move onto the CSS.<\/span><\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Start by styling the p tags with this:<\/span><\/p>\n<p><strong><strong>\u00a0<a href=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.48.26-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1674\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.48.26-PM-300x135.png\" alt=\"Screen Shot 2015-08-27 at 3.48.26 PM\" width=\"300\" height=\"135\" \/><\/a><\/strong><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Then try out the pseudo selectors :first-child, and :nth-child() like this:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.53.19-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1675\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-3.53.19-PM-300x166.png\" alt=\"Screen Shot 2015-08-27 at 3.53.19 PM\" width=\"300\" height=\"166\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Now, it may seem odd that :nth-child() needed a 3 and 5 to select the correct elements versus 2 and 3. After all; it looks like the CSS is describing the third &lt;p&gt; tag inside of a &lt;div&gt;. It turns out that :nth-child() actually is describing the 3rd element of the parent element that happens to be a &lt;p&gt; tag. If we had typed \u201cdiv p:nth-child(2)\u201d, that would be interpreted as find a &lt;p&gt; that is the 2nd child of a &lt;div&gt;. It wouldn\u2019t have found anything because the second child of our &lt;div&gt; isn\u2019t a &lt;p&gt;.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Next, style the &lt;div&gt; tags inside of the parent &lt;div&gt; like this:<\/span><\/p>\n<p><strong><strong>\u00a0<a href=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-4.18.55-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1676\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-4.18.55-PM-300x108.png\" alt=\"Screen Shot 2015-08-27 at 4.18.55 PM\" width=\"300\" height=\"108\" \/><\/a><\/strong><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Which should produce this:<\/span><\/p>\n<p><strong><strong>\u00a0<a href=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-4.19.54-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1677\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-4.19.54-PM-300x147.png\" alt=\"Screen Shot 2015-08-27 at 4.19.54 PM\" width=\"300\" height=\"147\" \/><\/a><\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Doesn\u2019t that look nice and customizeable? Now lets add some :hover functionality to really make it shine.<\/span><\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Its this easy to capture the hover event:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-1678\" src=\"http:\/\/cssnewbie.com\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-27-at-4.29.30-PM-300x76.png\" alt=\"Screen Shot 2015-08-27 at 4.29.30 PM\" width=\"300\" height=\"76\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Now when a &lt;p&gt; tag is hovered over it will turn black. :hover can make a site feel much more alive when used properly.<\/span><\/p>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">View and customize\u00a0the result here: <\/span><a href=\"https:\/\/jsfiddle.net\/MorganMeliment\/zk4u5f8j\/\"><span style=\"font-weight: 400;\">https:\/\/jsfiddle.net\/MorganMeliment\/zk4u5f8j\/<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Pseudo Classes are a crucial part of CSS. They allow developers to apply styles to elements under certain conditions. For example, if there is a &lt;div&gt; tag with 3 &lt;p&gt; tags inside of it, the :nth-child pseudo selector is able [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/pseudoclasscss\/\" title=\"Click to read 'The case for using the Pseudo Class in CSS'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":1672,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168,173,192],"tags":[62,79,82,49,107,105,155,156],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1671"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=1671"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1671\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/1672"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=1671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=1671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=1671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}