4 Basics of CSS Class Note
4 Basics of CSS Class Note
Basics of CSS
4.1 Introduction
the browser what to do with your HTML elements, it translates it to the right way
and renders it in the way you want it.
You use the combination of HTML and CSS to send instructions to the browser. It is
like sending a written instruction to someone who is supposed to rearrange your
house in a way you want it without showing them pictures or videos, just written
instruction. Let's say the person rearranging your house only speaks English. You
don't have to know or use every single English word to effectively communicate with
this person. You just use the words you know effectively. In this case, you are directly
communicating with the browser. Same concept when using CSS to instruct the
browser to style our HTML. You just use the CSS rules you know to instruct the
browser.
• There are multiple ways of adding CSS instructions into your HTML. We will
discuss the three ways below.
1. Internal (adding CSS instructions on the HTML file/ on-file)
• An internal CSS is defined in the <head> section of an HTML page, within a <style>
element. Ex: add an on-file style that changes the background of the body tag to red
2. Inline
• The main difference between the inline styling and internal styling is that inline
styling is directly applied inside the opening tag of the HTML element. For that, we
use the "style" attribute of an HTML element. Ex: add an inline style on the <body>
tag to change the background color to blue 3. External CSS file
• This is the recommended way of including your CSS instructions on your HTML
file. The main concept is to create a separate CSS file containing all the CSS
instructions and to link this CSS file to the HTML file. We add a <link> tag in the
<head> section to link our CSS file to our HTML file. Ex: add an external CSS file
to change the background color to purple
4.4 CSS units
• Having a clear understanding of the measuring units is the bases for any
design and styling job. There are a couple of commonly used measurement
units in CSS.
The most common one is the pixel (px). It is the size of 1/96th of
an inch (0.26mm).
You must train your brain to think in terms of pixels from now on.
Other commonly used units include:
• Percentage %: this is when you want to describe your element in relation
with other elements, usually the containing elements
• em and rem: these are usually used to define font sizes, and both are scalable
units of size o The em unit is relative to the font size of its parent element.
In the example below, the child will have a font-size of 36px (2 * 18px =
36px)
• The rem unit, short for root em, is a relative unit that’ll always be based upon
the font-size value of the root element, which is the <html> element. And if
the <html> element doesn’t have a specified font-size, the browser default
of 16px is used. So that means that, by using the rem unit, the values of
parent elements are ignored, and only the value of the root is taken into
consideration. If look at the following example, the child will have a font
size of 32px (2 * 16px = 32px)
{font-size: 2rem;}
• This concept of wrapping elements in containers is key in mastering CSS. Note that the
wrappers or containers are HTML elements. The styling is mostly applied on them.
Commonly used HTML containers are <header>, <section>, <div> and <footer>
• Before you even start writing a single CSS instruction, make sure to focus on organizing your
HTML elements. Organizing your HTML means bundling related elements together with in
a container. Try to think of the best way to group your elements together
• To help you understand what we mean by "thinking in terms of containers" let's work on a
small project called "The Puppy Lover’s Page" together. First thing first, every web page
development starts with a design. Designers are responsible to provide that to you Look at
the Puppies page design: https://mizantechinstitute.com/courses/Term1-docs/css-class/fruit-
Lovers-Designes.zip )
It is based on the design that you start to think about the structure of your HTML
Look at the conceptual containers you should think of before building your HTML
Then you build your HTML matching the containers you imagined
Look at the demo video on this section
• One way of styling specific elements in CSS is to name your HTML elements. Naming is
applied on the HTML elements. When you name, make sure to name every container in a
way that is uniquely identifiable. It is these names we later use to specifically identify the
element we want to style. We use classes and ids for naming elements. Classes and ids are
always placed in the opening HTML tag. Please note that you can give any name you think
is appropriate
• Naming elements with classes
We use the class attribute to style multiple elements on our web page. Classes are
like family names. Each element can have more than one class attribute value
• Like we have said earlier, CSS is just a set of instructions the browser should follow to
render the HTML elements. To write CSS instructions for an HTML element, we need to
first correctly identify the element, this is what selecting means. There are multiple ways of
selecting a specific HTML element. Here is a useful link listing out the 30 most used CSS
selectors: https://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--
nethttps://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-
1604816048
• Class selector
This is a way of selecting an HTML element using their class name. We use the dot
(.) identifier for that
Ex:
.className {
.bold-title {
bold;
}
• ID selector
This is a way of selecting an HTML element using their id. We use the hashtag (#)
identifier for that
Ex:
#id {
#header-wrapper{ background-color:
black;
a{
color: red;
}
4.8 Selecting elements using star (wild selector), hover and descendant selectors
* { margin: 0;
}
• Descendant selector
Sometimes, just using the class name or the element type might not provide a specific
selection as we want. In those cases, we use descendant selector style for that.
The descendant selector matches all elements that are descendants of a specified
element. You can assume this as using the full name of a person. If we have 2 friends
with similar first name, we can use their last name to identify one from the other.
The Parent and Child concept
• The outer wrapper of an HTML tag is called the parent
• The one in the inside the parent HTML is the child. Look at the example below to make
every anchor link on the website red, but if the anchor link is inside a div, to make the
that specific link white
a{
color: red;
} div a { color:
white;
}/* Note: CSS here uses parent div first and the child next */
o Hover selector
• This is more of an action selector. It gets activated when you hover your cursor on an
HTML element. For example, look at the following example to change the color of
the anchor link to green and underline it when people hover over the lin.
decoration: underline;