Guide: CSS3 Pseudo-Classes For Web Designers: Complete Guide
- The pseudo-classes are used to target elements without adding additional classes, attributes or ID; therefore it is called pseudo-class that way. In our previous posts we covered a few new CSS3 pseudo classes including: not ,: before and: after ,: first-of-type and we also use pseudo classes in some of our tutorials
- And in this post, we’re going to walk through the other new CSS3 pseudo classes that haven’t been covered yet. Let’s take a look.
- nth-child is used to target elements in a specific order from their parent. This pseudo class is used in conjunction with the following parameters: a number (1, 2, 3, etc.), a keyword (strange even) or an equation (an, an + b, an-b, etc.). In the following example, we have three paragraphs within one Section 2: Sweet icing application. Apple pie application bonbon cake danish biscuits jujubes. Topping croissant tiramisu tart jelly pudding. Section 3: Oat cake pastry chocolate dessert brownie wafer candy. Caramel chocolate cake marzipan. Biscuit candy pudding dessert brownie dragee halvah donut gummy bears. p: nth-child (2) {padding: 15px; background-color: # 333; color: #fff;} We can also use an equation to select, say, paragraphs 2, 4, 6 and the next numbers in that range. We can write the style rule like this. li: nth-child (2n) {padding: 5px; background-color: # 333; color: #fff; smargin: 10px 0;} The n notation in the equation above represents the sequence number (0, 1, 2, 3, etc.). In the example above, 2 is multiplied by n, yielding the following results:
2 (0) = 0 (select none) 2 (1) = 2 (select 2nd child element) 2 (2) = 4 (select 4th child element) 2 (3) = 6 (select 6th child element) … Etc.
To see how it works in another equation, you can use this tool called the nth tester. However, for this to accurately select the child elements for this pseudo-class, the order of the elements should not be preceded by other element types. Given the following HTML markup as an example, we have three paragraphs. We add it Now if we select the second paragraph with the following style rule.
Section 2: Sweet icing application. Apple pie application bonbon cake danish biscuits jujubes. Topping croissant tiramisu tart jelly pudding. Section 3: Oat cake pastry chocolate dessert brownie wafer candy. Caramel chocolate cake marzipan. Biscuit candy pudding dessert brownie dragee halvah donut gummi bears. p: nth-child (2) {padding: 15px; background-color: # 333; color: #fff;} Nothing is not applied as the second child element has now been replaced with The: nth-last-child pseudo-class works the same as: nth-child, except the sequence now starts from the last child. So if we apply this style below: p: nth-last-child (1) {padding: 15px; background color: # 333; color: #fff;} The element that is applied is the very last paragraph element of the parent element.
- Watch the demo below.
- nth-last-of-type works in the same way as: first-of-type we discussed in our previous post.
It selects the specified child, even when the sequence is interrupted by other element types. And similar to: nth-last-child the sequence starts from the very last element. In the example below, we’ve packed paragraphs, block quotes, and sub-ordered lists into one
Section 2: Sweet icing application. Apple pie application bonbon cake danish biscuits jujubes. Topping croissant tiramisu pie jelly-o pudding.
Section 3: Oatmeal pastry chocolate dessert brownie waffle candy. Caramel chocolate cake marzipan. Biscuit candy pudding dessert brownie dragee halvah donut gummy bears.
List 1: cotton candy apple pie topping. List 2: Biscuit gummy bears sweet List 3: Jujubes fruitcake hogweed chocolate bar. List 4: Tart carrot cake cookie marzipan pastry toffee. < strong> List 5: Wafer tiramisu marzipan pie. List 6: Halvah chocolate bar. List 7: sterk> Toffee sugar plum. List 8: Caramel pie.
- p: n-the-last-of-type (2) {padding: 15px; background color: # 333; color: #fff;}
- Unlike: nth-child or: nth-last-child which selects the element strictly by their string, the -or-type pseudo-class will also find the element by their specified type. To see our example in action above, check out the demo at the link below.
- The: only-child is used to select the specified element which is the only child of its parent. Imagine a parent who has only one child in the family. Likewise, in the following example, we have only one paragraph within one
- p: only-child {padding: 15px; background color: # 333; color: #fff;}
- However, if we have more children of any type, this pseudo-class will not work, because the specified element no longer has the only child under his parent.
- only-of-type pseudo-class works in the same way as: only-child. But apart from the series, it also finds the element of their type. The: only-of-type will target and add styles. Imagine a parent with 3 children consisting of two girls and a boy. We can attack the boy with this pseudo class because he is the only boy in his family.
In the following example, we have two paragraphs and one block quote.
Section 1: Sweet icing application. Apple pie application bonbon cake danish biscuits jujubes. Topping croissant tiramisu pie jelly-o pudding.
Section 2: Sweet icing application. Apple pie application bonbon cake danish biscuits jujubes. Topping croissant tiramisu pie jelly-o pudding. To blockquote: only-of-type {padding: 15px; background-color: # 333; color: #fff;} Click on the demo below to see it in action.
- empty specified element targets that are empty. In others words, this element has no content or child, it has nothing, not even a space. In the following example, we have an empty paragraph within a p: empty {padding: 15px; background color: # 333; color: #fff;} The: target pseudo-class is used to add styles to elements with fragment ID. Snippet ID is an extra string in the last part of the URL that is started with a hash (#). It looks like the following http://www.domain.com/#fragmentid To apply styles with: target, we must first assign a unique id or name attribute to the element as follows. To select and apply styles when this paragraph is targeted with a snippet ID, probably something like http://www.domain.com/#target, we can write the style rule like this. p: target {padding: 15px; background color: # 333; color: #fff;} Paul Underwood has shown how to use this: target pseudo class in real case in this post How to make CSS based content accordion.
- disabled is used to target elements that do not accept data input and, on the contrary: enabled, is used to target elements that accept data input. In the following example, we have two types of text input, one of which is assigned with a disabled attribute.
Next, we want to change the disabled input border color to red and green for another input. input: enabled {background-color: #fff; border: 2px solid # 79be6a;} input: disabled {background-color: # f3f3f3; border: 2px solid # c33a36;} Visit the demo page below to see this example in action. The last pseudo class we’ll discuss is: checked. This pseudo class will target it element when it does selected or checkedSo this pseudo class only applies to the radio input type and the input type with check boxes.
Final Thought
We’ve covered a lot in this post. We hope this is a good reference for working with pseudo classes. Finally, you can download the demo resource from the following link.
CSS3 Pseudo-Classes For Web Designers: Complete Guide: benefits
Faq
Final note
I hope you like the guide CSS3 Pseudo-Classes For Web Designers: Complete Guide. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends. For our visitors: If you have any queries regards the CSS3 Pseudo-Classes For Web Designers: Complete Guide, then please ask us through the comment section below or directly contact us. Education: This guide or tutorial is just for educational purposes. Misinformation: If you want to correct any misinformation about the guide “CSS3 Pseudo-Classes For Web Designers: Complete Guide”, then kindly contact us. Want to add an alternate method: If anyone wants to add more methods to the guide CSS3 Pseudo-Classes For Web Designers: Complete Guide, then kindly contact us. Our Contact: Kindly use our contact page regards any help. You may also use our social and accounts by following us on Whatsapp, Facebook, and Twitter for your questions. We always love to help you. We answer your questions within 24-48 hours (Weekend off). Channel: If you want the latest software updates and discussion about any software in your pocket, then here is our Telegram channel. Compsmag has Proudly Shared Free Information Since 2014.