Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When it comes to readability one of the most important things you can do in terms of CSS code is to indent certain areas of your code to add a little bit of distinguishing characteristics to that area of the code. When we talk about CSS rules, what we need to understand is that there's some sort of an unwritten rule when it comes to writing these and one I try to follow. If there's only one rule in you CSS declaration, you keep that on one single line. However, if you have multiple rules, you want to put that on multiple lines, and you also want to make sure that you indent the properties that you're working on.
So let's take a look at how that works right now. So I'm just going to go here underneath Universal Styles, and I'm going to start off with a new declaration here. I'm just going to type out just a fictitious classname. So I'll just type out .classname. Let's assume that this one is going to be just a one single line kind of thing. So I'm just going to do open curly braces there and a close curly brace and in-between those we're going type out border, and we are going to set it to 0 and semicolon like that. Since there is only one rule here, I'm going to keep that right there on one line.
However, let's say that I had another classname, .classname2, and we'll do open curly braces here. If I had more than one set of rules that were going to be applied here, I would just create a new line, and I would make sure that the second line is indented. In most modern code editors like Coda and others, they will automatically indent this second line for you. If they do not just press the Tab key over, and you will be able to Tab over like this. So I'll just type out something like Background, and we'll make that white and then border set to 0.
Then let's just say the color was something like that, and we have float left, margin 0, and let's do padding 0. So there's an example of how that would work. So in this case we have classname with only one rule so that gets put on one line with no indent right there, but in this case look how much easier to read this is right here. Everything is nice and concise. It is in order, and I actually put all of the different properties that I was working on, I put those in order as well as alphabetically.
So B for background, B for border, C for color, F for float, et cetera. I try to follow these unwritten rules to make my code a little bit more readable to other people, but also making sure that I use the indentation for these, because making that indent just lets me know, here is the start of this class, and it doesn't stop until the outdent happens towards the end. Everything that's indented now belongs to that class and everything that's not indented does not. This is a great way to help you distinguish between blocks and declarations of CSS and also a great way to help keep yourself organized as you continue to create your CSS documents.
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.