CSS101 - Introduction to CSS
Henry Uzor, GMNSE
Software Engineer | Frontend Web Developer | AI Applications Engineer | Data Annotator | Frontend Development Writer | JavaScript | TypeScript | React.js | Vue.js
Hello Senior Devs!
This is Henry Uzor, and it is my pleasant delight to bring you my weekly newsletter once again on Frontend Development. I recently concluded the HTML module with the last edition of the newsletter and starting this week, the newsletter will be commencing a new module, the CSS module. And what better way to kickstart the module, than with the Introduction to CSS?
As you are aware, the HTML module was completed with the last edition of this newsletter. The module has 14 editions and in those 14 editions, we got to learn about a whole lot of things there is to learn on HTML. We covered topics like HTML Elements, Page Structure, and Classifications of HTML Elements where we categorized HTML elements into inline and block elements. We also got to learn about how to embed HTML multimedia content such as images, audio, and videos into a webpage. We spent about 2 editions on HTML Tables alone, and then we went on to learn about Lists, Layout Elements & Techniques, and Semantic Elements and then we concluded the module by learning about HTML Forms.
But as we must have observed, becoming a Frontend Developer doesn’t stop at learning only HTML. Though it is very important to understand how contents are embedded in a webpage using HTML, there just must be more to it. How do we add life, colour, style, and beautification to whatever content we have added to the webpage? That, my friends, is where CSS comes in!
If this is your first time reading my article and you don’t really have a full understanding of what HTML is all about, I’d suggest you take the time to learn about it, or you can read my previous 14 articles on the subject. I took time to simplify the concept, providing comprehensive lessons and I did this using practical examples. Learning HTML is very important for you if you want to really understand this new module on CSS, so I suggest you go check it out, otherwise, you’d pretty much be wasting your time trying to learn this one. Below is a link to my very first article and you can take it up from there. Trust me, you will appreciate it:
If you recall, when we were learning HTML, there were a few times when I had to make use of CSS to add more visual meaning to our content, a very good example is the very last edition of the HTML module where we learnt about HTML forms. Now we need to understand what CSS all is about, and how to use it to add style to our web pages.
INTRODUCTION
CSS is short for Cascading Style Sheet, and it is a styling language used to define the appearance and layout of HTML documents on a screen. Like HTML, it is not a programming language, but it does allow you to control various aspects of web pages, such as colours, fonts, spacing, positioning, and more. CSS works by selecting HTML elements and applying specific styles to them.
So how does a typical CSS code look? Have a look at the anatomy of a CSS ruleset below:
This picture is labelled and from the picture, you will observe that the CSS syntax basically comes in two halves – the “selector” half and the “declaration” half.
From this example, the element with the class name “container” is selected for styling, and five styling ruleset is written for it. All five declarations have the property–value pair, each separated by a colon (:) and ending with a semi-colon (;).
It is important to note that the entire ruleset can be written on a single line and the code will still work, provided it is written properly, just like this below:
However, for the sake of your sanity and that of every other developer you might be working with on the project, I would advise against this, for various reasons. You see, when you write the entire declaration on a single line, if there is an error in the code, it can become very difficult to detect the error, especially if the number of declarations is a whole lot more than just five rules. Besides, you don’t want to have to be scrolling your code horizontally just to see the rest of it, that doesn’t really make a lot of sense now, does it? And lastly, just look at the two snippets, you can’t help but agree that the previous one where the declarations are written on different lines, certainly looks better and well arranged to the eye, than the latter.
The CSS Universal Selector (*)
The CSS Universal Selector (*) is used to select all elements in an HTML document. It matches every single element type in the HTML document and applies the written ruleset to all of them. The universal selector is often used to apply global styles or to reset default styles for all elements in the HTML. Here is what it looks like using the CSS Universal Selector:
In this example, the universal selector is used to select all elements in the HTML document, and the declaration within the ruleset block set the margin, padding, and box-sizing properties to specific values. While the universal selector has this behaviour of selecting every single element in the HTML document as a group and styling all of them together, you can also select different elements and style them with one ruleset in a practice that is called “CSS Selectors Grouping”.
领英推荐
Let me try and make this more understandable. Take for instance the following piece of code:
As you can see from the code, the <h1> element, another element with “id” attribute – “para1”, and all elements with the “class” name – “center”, all have the same styling ruleset. So, it will only be optimally better, or it will only make more logical sense to group these selectors and write the same declaration to style them. Just like this:
Looks better and simpler, doesn’t it? Sure, it does! Grouping CSS Selectors helps to minimize your code, and when doing this, ensure to separate each selector with a comma (,).
There is something else I’d just like to touch on before we move on, and that is - how to select elements that are nested in varying kinds of ways.
Other categories of CSS Selectors are:
In this example, the “:hover”pseudo-class is used to change the background-color and font-size of the <button> element when it is hovered over with the mouse cursor. The “:first-child” pseudo-class is used to change the text color and decoration of all <p> elements that are the first child of any element. And the “:last-child” pseudo-class is used to change the text alignment and color of all <li> elements that are the last child inside the element they exist in.
There is so much to learn about “CSS Pseudo-classes” that I can not really cover it all. But you can definitely read all about it on the following link:
And that’s a wrap, Folks! There you have it for this week! I hope you had fun. I always do!
Next week, we will be looking at how we can add or link CSS to our HTML code.
?
Feel free to drop your comments in the comments section and share this Newsletter with anyone you know that might be interested in learning about Frontend Development. If you have any questions that you’d like to ask on any of the subjects I’ve discussed so far or anything at all that has to do with Frontend Development, I am just a DM away.
Today is Friday, so TGIF and I hope you had a great week. Until I come your way again next week, have a blessed and productive weekend. See you in a bit!