Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Avoiding redundancy with selector groups

From: CSS: Visual Optimization

Video: Avoiding redundancy with selector groups

Another great way to avoid redundancy, and also to save yourself a lot of time in writing CSS is to use something called a Selector Group. I've got open here the meyer_reset.css document that's available to you in your Assets folder. If you don't have access to that, that's okay just take a look at what's on screen. And so basically, what we have here, as you can see is a bunch of different tags all grouped together separated by a comma, and then a declaration after that of all of these different properties that all of these things share. Now, I could have just written it out where HTML would be its own tag, and it would have all these properties, body be its own tag, have all these properties.

Avoiding redundancy with selector groups

Another great way to avoid redundancy, and also to save yourself a lot of time in writing CSS is to use something called a Selector Group. I've got open here the meyer_reset.css document that's available to you in your Assets folder. If you don't have access to that, that's okay just take a look at what's on screen. And so basically, what we have here, as you can see is a bunch of different tags all grouped together separated by a comma, and then a declaration after that of all of these different properties that all of these things share. Now, I could have just written it out where HTML would be its own tag, and it would have all these properties, body be its own tag, have all these properties.

But instead of writing all that out multiple times and having redundant CSS in my document, what I've done is created something called a Selector Group, so all of these are selectors, and this selector group now shares all of these properties. And since I've spelled them out, separated them by a comma, and then I put the curly brace up in front of them and a curly brace after there, that just means that all of these adopt all of these different properties all together. I don't have to separate them. They all just take on this one set of properties which is really, really handy.

So, let me show you a real world example of this. So, I'm just going to go ahead and close this document up for now, and I'll create a brand new blank CSS document. Let's assume that we are going to be styling some headers for our blog, let's say. And so, normally, what people do is they come in and they say, okay, H1, we're going to do the color, it's going to be black. And after we type out the color, we're going to say font-family, and we're going to call this arial,helvetica,sans-serif, something like that.

We're going to say font-size, and we'll make this 48 pixels, and there's a bunch of different properties go into this. So margin, let's put this at 0, and let's do 0, 5 pixels on the bottom, 0, something like that. And so, all of these different properties for the H1 tag, and then they would come down and they would type out H2, and they would do color:black, font-family:arial,helvetica,sans-serif.

You sort of get the idea here. I'm just being redundant as I go through here, creating a bunch of unnecessary lines in my CSS document. Whereas, if I were to come up here and just do this, where I eliminate anything that's specific to just the h1, so like the font-size, does not need to be in this declaration. I'll just do this, h1, h2, h3, h4. So all of these different tags are going to be black, they're going to inherit the font-family of Arial Helvetica or Sans-Serif, they are going to have this margin applied.

And then after that, I could type out h1, font-size:48. Then I could go down, and I could say h2, font-size:36. Then go down h3, font-size:24, and then h4, font size:, and let's do this at something like 18. There we go.

And so now, all of these different things are being inherited up here, and then I'm just defining the sizes one at a time right there. So, this Selector Group includes all four of my headers. They all are now black, they all have this font-family, they all have this margin, and then I have all their individual sizes declared down here. And so, what I've done is saved myself a lot of time, I've saved myself a lot of writing by typing out all this up here, and just adding the selector group instead of actually adding those individual properties to each one of these different header declarations. So, as you develop your CSS documents and you find similarities between certain tags, my recommendation is to create yourself some selector groups.

And it's not just these default tags like h1 and body, and things like that, you could add divs into this, you could add classes into this, you could add all different types of things into this as long as it's separated by a comma, and as long as they all share those specific properties that you define, everything should be cool. So just as you move forward, try using these Selector Groups to kind of compress your CSS, and to have certain things share certain properties so that it makes it easier on you, and easier on the person who comes in and edit it later on to find exactly what they're looking for.

Show transcript

This video is part of

Image for CSS: Visual Optimization
CSS: Visual Optimization

25 video lessons · 7017 viewers

Justin Seeley
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Visual Optimization.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.