Start learning with our library of video tutorials taught by experts. Get started

CSS: Visual Optimization

Avoiding unnecessary tags and classes


From:

CSS: Visual Optimization

with Justin Seeley

Video: Avoiding unnecessary tags and classes

One of the things that get some people in trouble when they are developing CSS is the fact that they start to use unnecessary classes when they are developing their CSS. Chances are you are going to be doing things that you are going to find, you use certain styles that you are applying over and over and over again. For instance, you might have stuff that floats to the left, or stuffs that floats to the right. And chances are you are going to be declaring different classes of items, and you are just going to say okay, I want this to float left, I want this to float left, I want this to float right, and et cetera, et cetera. But why not just develop your own set of generic classes called .left, .right or whatever, and then use those as a secondary class when you are defining something.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS: Visual Optimization
1h 11m Beginner Feb 12, 2013

Viewers: in countries Watching now:

Take a tour of a workflow that optimizes CSS code for easier navigation, organization, and readability. In this course, author Justin Seeley covers best practices for writing CSS in an easy-to-read format, commenting code, developing a table of contents, and adopting other methods that help produce "cleaner" code. The course also contains tips for speeding up development with some online tools and simplification techniques.

Topics include:
  • Exploring good versus bad style sheets
  • Creating section headings
  • Indenting code for enhanced readability
  • Creating single- and multi-line comments
  • Avoiding unnecessary tags and classes
  • Creating master color and typography guides
  • Simplifying comments
Subjects:
Web Web Graphics Web Development
Software:
CSS
Author:
Justin Seeley

Avoiding unnecessary tags and classes

One of the things that get some people in trouble when they are developing CSS is the fact that they start to use unnecessary classes when they are developing their CSS. Chances are you are going to be doing things that you are going to find, you use certain styles that you are applying over and over and over again. For instance, you might have stuff that floats to the left, or stuffs that floats to the right. And chances are you are going to be declaring different classes of items, and you are just going to say okay, I want this to float left, I want this to float left, I want this to float right, and et cetera, et cetera. But why not just develop your own set of generic classes called .left, .right or whatever, and then use those as a secondary class when you are defining something.

So for instance, let us go in and create a new document here, and I will show you exactly what I mean by this. The first that people do when they start defining stuff, so they will do something like button-left, and then they will tell that to float:left. And then they will do something like column-left, and then they will tell that to float:left. Again, it's just more redundancy that you don't need, you don't necessarily need a column-left or a button-left. You might need a button class, you might need a column class.

You might also need a left or right class, but you don't need individual columns for this. Or you might even see people do something like this, column-left-blue, or button-left-blue, and you would see something that says button-left-red, button-left-whatever. And so you grind up with all of these unnecessary classes where you could just do something like .button. And so this could be something like, you know, webkit-border-radius:6px, border-radius:6px and six pixels for that. And so that's just your button class.

And then you can have something here like generic classes that will be like .green, which is background:green. And then you can have something like .left which you just float:left and then .right which would just be float:right. And so you can make up these generic classes and then when you go in, and you are defining something in HTML, let' just create a blank HTML document, you would do something like

just like that.

And so that way it absorbs the button class, the green class, and the left class all at one time, and you can also go back, and you can make changes to the button class by itself. You can make changes to the green class, the left class or right class just makes it a little bit more organized, keeps these things as generic as possible. Remember you want to design your CSS based on what something is not what it looks like. And so in this case, I am just defining the basic properties of the border-radius to maybe a width and height value. Whereas the green, the left, the right, that's all controlled generically.

That way, if I wanted to have something later on that's like a column, and we will do .column-a here, and I can just say I want this to be 90% of the width, so let us do width:90%, and then whether or not it floats left or right is irrelevant, because I can then go over to my HTML document, and I can say something like,

. So that way it is going to float right, because it's adopting that right class, and inside of here I have got .right. So that's what I am defining right there.

It's using the column-a class it's also using the right class, and I didn't have to tell this to float right, I just said to tell it how wide it needs to be. So in this case, I am avoiding these unnecessary tags by developing a set of generic tags that I can then apply to different classes in my HTML markup later on. So go through, find some commonalities between things, like whether or not it floats left, it floats right, colors, et cetera. Develop yourself some generic tags that you put somewhere in your CSS document. Maybe even have your own section of quote generic tags, and so you use those generic classes to apply them to different things like buttons and columns and so forth.

I think you will find this as a great way to help you keep yourself organized and a great way to help avoid unnecessary class creation and unnecessary tag usage as well.

There are currently no FAQs about CSS: Visual Optimization.

Share a link to this course
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.
Upgrade now


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.

Upgrade now

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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