Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
For the most part, your style sheets will consist of specific styles targeting elements on the page; However, it's extremely helpful to have a global serve classes that can assist when needing to do common tasks, such as floating elements, clearing floats and basic elements styling. These classes can then be used to add styling without needing to write extraneous markup all the way throughout your styles. So here I have the main.css, and I want to go down to my global classes sections. I am just going to scroll down through my sections until I find this one, about line 129.
Now, these are going to be what I consider to be utility styles, meaning I'm not going to rely on them to do this every single time, but if I'm working and maintaining my site later on, and all of sudden, I am like, hey, I need to float this element to left, or float it to right, and I don't want to change its parent selector, then I'm going to apply the class, alright. So the first class I want to do is .floatRight. Notice that's all one word, and the dot is in the front of it because this is a class selector. I'm going to go ahead and open up my curly brace and come down and choose floatRight, just like that.
So now anytime I need to float an image, or some text, or an element to the right, and it's not part of my overall stylessheet, I can simply select it and apply this class to it. So that's why I kind of think of it as a little utility. I can just go ahead, anytime I need it, go ahead apply it. Let's keep going. Let's do a .floatLeft, again notice that I'm preceding that little period, and for that one I want to choose float, and I want to choose left of the value. There we go. Let's keep going. I'm also going to do a .clearRight, and its property is indeed going to be clear right.
Now this is actually a more common need, frankly, than the classes above it. Anytime we do in a float-based layout, a lot of times you have an element that you really don't want to wrap around another element, or an element that you don't want to float above to affect. Well, if that's the case, then I can just go ahead and apply this, and again, it's going to eliminate the need for me to have this property as part of the parent class. Let's go ahead and do .clearLeft, all one word, while we're at it, and indeed do clear left on this one as well as a property. So you will notice that these are a single property and very descriptive as to what they do: clearRight, clearLeft and in case of the next one .clearBoth.
Now, did you see how I left that period off? It's so easy to do when you are hand-coding that. So always get in the habit of putting those on there. Sometimes, like me, you'll just kind of forget them. clearBoth, and now anything that we don't want any floating, left or right, we can use that class to apply. We are going to do one more, and here I'm going to go ahead and do .quote and under that, I'm going to do font style, so font-style, and we are going to do italic. So those are our global classes, very simple, very easy: floatLeft, floatRight, clearRight, clearLeft, clearBoth, and the just a quote so when we want to italicize some text and we don't need to use the emphasis tag, then we will use a Span tag and apply this class to it.
Now, by no means is that all of the global classes that you can write. I have seen css styles out there where they have hundreds and hundreds of lines of global classes that they can apply anytime they need it. You have to be very, very careful about that. Global classes can really speed up your site development, but you can also become too dependent upon them. Overusing classes is one of the most common mistakes made when writing css, and it results in a lot of unnecessary code and harder styles to edit and maintain later on. Imagine that if you want all of yours headings to be a certain color, instead of just writing a heading selector, you apply that class.
Now, you have got to go in and if you delete the class, really make them, just really large edits to your styles. Be careful about that. My advice is to add global classes that won't conflict with any specific rules, because that's the other thing. You want to avoid style conflicts, and you can turn to them and use them when you need that particular styling without having to affect or write a brand-new parent style for it.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.