3. CSS Intro
3. CSS Intro
● You learned about chrome developer tools. Many modern browsers that you
see today have developer tools built into them, these are called browser
developer tools. They all work in a similar way. For example Mozilla Firefox,
Microsoft Edge, Brave, etc.
● These are powerful tools for web developers for a variety of purposes.
● They help you understand a web page structure (basic HTML), and styling
(CSS) of various elements by inspecting them.
● They help in modifying the HTML and CSS of the page to experiment with
the looks of the webpage for trial purposes.
● They help in debugging code.
Note: They also help you understand the javascript code and functionality
associated with it. You’ll learn more about this in the upcoming lectures.
Debugging
Let’s say you have a simple web page as shown below. The text color of the list
items is blue. But you coded it to be green. Now you wish to understand why it is
appearing blue while you coded it to be green. Here we have used Microsoft Edge
as our browser. Similarly, you can use developer tools in other browsers as well.
● Step 1: Select the HTML element that you want to fix, then right-click and
select “inspect”
● Step 2: Under ‘Elements’ you’ll be able to see the HTML code highlighted
with respect to the HTML element that you selected to inspect. Here one
<li>..</li> is selected. You can click on it to expand and see its contents.
Below you can see ‘Styles’ where you’ll be able to see all the styles applied to
the selected element.
- In Step 3, you can see some styles have strikethroughs. For example in
<ul>, color: green;
This means you coded <ul> contents to have the color green, but the
browser didn’t apply this color, since some other more specific styling
(that is applied to <li>) overrode this value. Hence developer tools help
us figure out such scenarios helping in debugging.
Note:
You don’t have to master using Browser Developer tools in one go. As you keep
practising you’ll learn with time.
In general, if you ever face any issues with Chrome Developer Tools or if you want to
learn more, you can refer to its documentation whenever required.
● Documentation
● MDN Article on Chrome Dev Tools
Introduction to CSS
Adding CSS to your websites will make them presentable and somewhat like the
sites you see on the internet.
● CSS is a shorthand for Cascading Style Sheets.
A CSS comment initiates with /* and ends with */. Comments can also span multiple
lines.
The browser formats the HTML document based upon the information in the
stylesheet. The browser will access the stylesheets in the HTML document
itself. There are 3 ways to add CSS styles to your document:
● Inline Styles
● External Styles
● Internal Styles
The browser treats both internal and external CSS equally, but the order in which
they are defined, determines which property gets priority.
● If the link to internal CSS is defined before the external CSS, then
properties of external CSS will get preference over internal CSS, i.e.
external CSS > internal CSS.
● If the link to internal CSS is defined after the external CSS, then properties
of internal CSS will get the preference, i.e internal CSS > external CSS.
Inline Styles
Internal Styles
External styles
● You should use an external style sheet if you want to apply styling to a
website using just one file.
● Although the syntax is similar to internal stylesheets, it is implemented
using a separate CSS file.
● The '.css' extension is used to save it. Eg. 'styles.css'.
Attribute Value
NOTE: The link is placed within the head. CSS syntax code is contained in the
'styles.css' file only. The name of the file could be anything followed by .css
extension, and the same should be specified while linking it to the html file using
<link> tag.
SELECTORS
When multiple styles are applied to an element, specificity determines which style
will be used.
Element Selector
The element selector will help us to select all elements with the same mentioned
element name. This will select all the elements in the HTML document with the
given name, but most of the time this is not our requirement. So, to apply styles to
only some specific elements we need to have some restrictions. We will take a
look at this later in this section only.
Class Selector
Multiple elements with a specific class attribute are selected using the class
selector. To select elements with a specific class, type a period (.) followed by the
class name.
To use the class selector, the class attribute is used in the element's opening
tag. The value of the class attribute contains the name of the class. There can be
multiple classes added to the tag by giving space in between.
Id selector
The id selector will help us to select only one element with that specific id. We need
to write a hash(#) character and then id name to select an element with a specific
id.
To make use of the id selector, the id attribute is defined in the element's opening
tag. The value of the id attribute will have the name of the id. The id is unique on an
HTML page.
There can only be one id in the tag. If another element is having the same
id, the styles would not be applied by the browser.
#one {
color: blue;
#two {
background-color: teal;
#three {
color: green;
#four {
background-color: lightgrey;
Grouping Selectors
We usually use the same CSS for multiple elements, and we can't have too
many classes,as too many classes would become difficult to manage.
So, CSS helps us with a grouping feature where you can define the CSS rules
to multiple elements with the use of a combination of either class, tag, or id.
We need to use a comma separator for the different selectors for grouping
Nesting Selectors
To use nesting, you need to add space between the selectors. Hence the
sequence formed represents a hierarchy starting from the top.
Chaining Selectors
There are times when we want to have the same class for multiple elements and
we want to apply styles to them. In this scenario, we can use chaining selectors.
To use chaining we take the help of the combination of selectors without putting any
space in between them.
Eg., we have a class 'header-style' applied to every heading. We can apply different
styles to them like this:
<html>
<head>
<style>
.header-style{
background-color: aqua;
display: inline-block;
}
</style>
</head>
<body>
<h1 class="header-style">Hi</h1>
<h1 class="header-style">Hello</h1>
</body>
</html>
Now, as the class header-style is given to both <h1> tags, we can apply styles to
both of them together as done in the above example.