Yesterday, I created an article about HTML. Today, let me create an article about CSS. The queen for HTML. I recommend you W3Schools to learn CSS more and CSS-Tricks, the famous website for CSS tips and tricks. We're here to learn some common problems about CSS. This article can be a start for you in coding CSS.
Today, I found that a lot of developers worked with CSS preprocessors or frameworks, including new developers. There are a lot of front-end frameworks out there that come with SASS/Less. They can't be avoided and of course, those tools are good. But, it is also important to be good in native CSS too. Because that is what preprocessors produce.
CSS stands for Cascading Style Sheets. It is for HTML or document's look. Proposed in 1994 and released on 1996 by Håkon Wium Lie. Today, CSS has the fourth version, but it is still debated.
Start
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html><html><head><title>CSS</title><linkrel="stylesheet"href="app.css"><style>.error{color:red;}</style></head><body><p>This is <spanstyle="color:red">inline CSS</span>.</p><p>This is <spanclass="error">internal CSS</span>.</p><p>This is <spanclass="success">external CSS</span>.</p></body></html>
1
2
3
.success{color:green;}
There are three ways to insert CSS into the document; inline, internal, and external.
Inline
An inline style may be used to apply a unique style for a single element. To use inline styles, add the style attribute to the relevant element.
Most WYSIWYG editors produce inline styles which is not recommended. Because it will create many repetition and hard to override the styles, you may need !important as a suffix for the value of attributes.
Internal
An internal style may be used if one single HTML page has a unique style. You need to put them inside <style> before </head>. The web browser will load the styles along with the content. It is not recommended if you have many styles because it will make the article heavy to load.
External
An external style is a CSS file that will be loaded with <link> as long with rel attribute equal to stylesheet. It is common in many websites. The web browser will load the CSS file and cache it after the HTML loaded.
ID vs Class
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html><html><head><title>ID vs Class</title><linkrel="stylesheet"href="app.css"></head><body><pid="id"class="gradient">ID is unique.</p><pid="class"class="gradient">Class is not unique.</p></body></html>
The most common question in front-end interviews is that many new developers fail. What is the difference between ID and class? Well, the answer is simple. ID is unique, which means only one, while class is not. ID is to identify an element while class is to group several elements. We can select the element with # for ID and . for the class in CSS. It has the same meaning in JavaScript too. We can select the element with getElementById() for ID and getElementsByClassName() for the class in JavaScript.
HTML tags come with default styles. Some tags have different styles depending on the web browser. We need to reset them to make them consistent on every web browser. We need to remove those default styles.
The easiest way is to select all elements using *. But, that is not the recommended way. Because you will need to override the tag again. The code above can be your reference. There are also many reset CSS out there. The famous one is normalize.css.
While slicing design to HTML, the design comes with typography. That is the base of every style. The best way to code typography in CSS is to override the default tags with the typography.
The CSS above is the copy from the previous one. I just updated some tags with some styles instead of resetting them.
Screen Reader
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html><html><head><title>Screen Reader</title><linkrel="stylesheet"href="app.css"></head><body><p><ahref="#"><spanclass="sr-only">Go to the next page!</span>→</a></p></body></html>
The content should be easy to read by machines or screen readers, not just humans. If there is some content that you need to hide from humans, not machines, you can use the screen reader class like above from Tailwind CSS.
<!DOCTYPE html><html><head><title>Navigation</title><linkrel="stylesheet"href="app.css"></head><body><nav><ul><li><ahref="#">Menu 1</a></li><li><ahref="#">Menu 2</a><ul><li><ahref="#">Sub Menu 1</a></li><li><ahref="#">Sub Menu 2</a><ul><li><ahref="#">Sub Sub Menu 1</a></li><li><ahref="#">Sub Sub Menu 2</a></li></ul></li><li><ahref="#">Sub Menu 3</a></li></ul></li><li><ahref="#">Menu 3</a></li><li><ahref="#">Menu 4</a></li></ul></nav></body></html>
Some developers, especially new developers, failed to make nested navigation, especially main navigation. Technically, navigation is just a list of links, not a bunch of <div>. Let me give you a simple example of nested navigation here.
Take a look at some selectors that I used above. More information about combination selectors can be found on W3Schools CSS Combinators.
Responsiveness is the important thing today. The web should adapt to many screen sizes of desktops, tablets, and smartphones. We can use CSS Media Queries.
I copied the previous code and added a button, CSS media queries, and JavaScript because the only CSS is not enough.
Closing
The most important is to master CSS syntaxes. But they are a lot. We can't put them all in a single article like this. Understanding how selectors work is also important. I love WordPress CSS Coding Standards. That is neat. I set them to be my CSS coding standards too. You can clone the repository of examples above here, https://github.com/aristorinjuang/css.