Professional Documents
Culture Documents
You’ll Learn
Various ways to provides CSS
Normalizing CSS
Selectors
Colors
Gradients
Borders
Shadows
If you want learn CSS well and become a new JOB DEVELOPER, don’t miss any lessons in this
section.
1- Providing CSS
Let’s talk about three different ways to providing styles.
So we have couple element to work with. Next we go to the style section element
<style> </style> and define a rule for our paragraphs.
<style>
p {
color: orange;
}
</style>
What we have here Embedded stylesheet there is two problems with this style,
a first problems is that they not scalable, so if we have multiple pages, we want our
paragraph to be orange with this approach we have to copy all the styles that we
have here and put them in to Avery single web page. And that is if tomorrow we
decide to change the color of paragraphs, we have to go to every single web page.
And make modification. So this approach is not scalable.
the second problems with this approach is that we have every import concept in
computer science called (Separation of Concerns). So we have different modules for
different concerns.
As we leaned (HTML & CSS) we use HTML to
structure web pages and we use CSS to style them.
These are two different concerns, so we should
separate them.
So we want to organize our code like well organize supermarket, this where we use
external stylesheets
External stylesheets, so we take all this style and put them in separate file.
So create a new file called (styles.css), we can call anything the name doesn’t
matter.
Now let’s go to index.html page and link HTML Document with CSS stylesheet, to do
that we use <link> element.
<link rel="stylesheet" href="" />
So these are two different ways provide styles, but they is third way we can use
(Inline styles) to apply styles to particular HTML elements.
So let’s say I added these two paragraph here.
<p>Lorem ipsum dolor sit amet.</p>
<p>Lorem ipsum dolor sit amet.</p>
I want make first paragraph (blue) but only the first paragraph, every HTML
element has style attribute and this where we can write one or more styles. So we
can set the color to blue and add (;) semicolon so we can type second CSS property,
so we can say font-weight to bold. Don’t worry about this we will talk it.
<p style="color: blue; font-weight: bold">Lorem ipsum dolor sit amet.</p>
2- Normalizing CSS
So as u probably know we have different kind of browsers out there, unfortunately
different browsers render some HTML elements differently.
Now to address this issue, we use popular stylesheet called Normalize CSS
(normalize.com), what this stylesheet does it provide basics default styles for some
HTML Elements,
so they exotically
the same way in
different
browsers.
Normalizing.css
makes browsers
render all
elements more
consistently and
in line with modern standards. It precisely targets only styles that need normalizing.
so go the https://necolas.github.io/normalize.css/ and download it
- Now back to our project we currently have our styles.css file
here. And we add another stylesheet.
- To organize CSS stylesheet make a new folder and add both of
them.
<link rel="stylesheet" href="css/styles.css" />
3- Basic Selectors
In CSS we have few different ways to select elements or styling proposes. We can
select by (Type, ID, Class, Attributes)
- Body: what we write here is called selector, because this is how we select an
element, now in this example we are selecting an element by its Type which is
the (Body) element. But we can also select an element by its ID.
#products{
}
<body>
<section id="products">
<article class="product"></article>
<article class="product"></article>
<article class="product"></article>
</section>
</body>
.product{
}
- And here is question for You. What is different between ID and CLASS?
So in the real world we cannot have two people with same passport
number.
- So these are the most common way to select elements by (ID, Class,
attributes).
4- Relational Selector
We have learned how to select element using basic selector, but we can also select
elements base on the relationship with other elements. Let me show what I mean.
Now let’s say we want to style this <p> paragraph, one way is to do it by
given ID or Class, but there is another way, we can start from <section>,
because this section has already id, we can start here and then select <p>
paragraph element here. So our markup will be cleaner.
We go to our stylesheet
(style.css) start with
product section then we
select the paragraph
element.
#products p {
color: orange;
}
- This rules will be applying to any paragraph elements, inside the product
section.
both
paragraph
are orange
- But what if we want to style only the first paragraph , to do that we have, to
type a greater then (>) symbol in between these two selector.
#products > p {
color: orange;
}
See only
First paragraph
Orange
If we want to style this new paragraph element, it’s come after the section.
color: orange;
}
This will select the paragraph element that comes immediately after the
products section.
See only
the third
paragraph
is orange
- But what if we want to style all paragraph come after the <section> we can
replace the pulse (+) symbol with Tilde (~) symbol, now this will select all the
paragraph that are general sibling of the products section.
#products ~ p {
color: orange;
}
1- They can be fragile, because they are depended on the location of this
elements in the DOM, so if we move elements around our styles will
break.
2- The second problems they are Not as fast as basic selectors, don’t think
too much about this.
Another group of selectors called pseudo selectors, the concept of pseudo selectors
is very useful.
- Let’s define an <article> with two <p> paragraph with some text
<article>
<p class="first">Lorem ipsum dolor sit amet.</p>
<p>Lorem ipsum dolor sit amet.</p>
</article>
Let’s say we want style the first paragraph and make the font bigger
and italic, here is one way to do it, we can give to this paragraph class
(first) name.
<article>
<p>Lorem ipsum dolor sit amet.</p>
<p>Lorem ipsum dolor sit amet.</p>
</article>
Look it the rules that we define here, this rules are applying to heading
because it is the first-child of the article element.
article :first-child {
font-size: 140%;
font-style: italic;
}
define
In this <article> we have one heading and two paragraph, this Pseudo
Class selector (:first-of-type) is apply the first element of the different
type, here we have first Heading that’s why this rules is apply to this
element, down the Heading we have two paragraph, and the first
paragraph is receive the style that we just define.
- Back to the stylesheet we want this rules to apply only to the first paragraph
in the article, so here we should specify the paragraph type.
article p:first-
of-type {
font-
size: 140%;
font-
style: italic;
}
We get the
Same result
- Sometimes we style elements base on the position in the container for
example, let’s say we have Table of List, and we want to highlight all the
(odd) or (even) items. So let’s create.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
color: deeppink;
}
color: deeppink;
}
Note that all the even items
Are pink
So this is all about Pseudo Class next we will learn about Pseudo Elements
and then we go to the stylesheet and define a rule for this class.
.first-letter {
font-size: 140%;
font-weight: bold;
}
Note: this first steps are completely unnecessary, because in CSS we have bench of
Pseudo elements for this reason.
We get same result before and we didn’t have to add <span> to our markup
- We have another Pseudo element for selectin the first line of the paragraph, so we
use
(p::first-line)
p::first-line {
font-weight: bold;
}
-
If
we select some text by default the background of the selected text become blue.
So we can change it by using the Pseudo element .
::selection {
background-color: palegoldenrod;
}
- We have another Pseudo element for inserting Content before the content of the
element. Let me show what I mean.
p::before {
content: "...";
}
p::after {
content: "...";
}
7- Selectors Specificity
We end of situation, where more than one rule can target a given element and these
rules can have conflict in styles, let me show how browser finger out, what rules
should be applying.
- So let’s define <article> and inside this <article> let’s define <h1> with class
of (highlight).
<article>
<h1 class="highlight">Heading Highlighting</h1>
</article>
h1 {
color: deeppink;
}
.highlight {
color: dodgerblue;
}
<article>
<h1 class="highlight">Heading Highlighting</h1>
</article>
- This is where the selector specificity (Weight) comes to play, each selector has
what we call selector specificity or (Weight). If multiply rules target the same
element the browser will apply the rule that is more Specific.
Element
Element
Lookup this triangle
this what we call the
Specificity Triangle at
the top of this triangle
the (ID selector),
because as you know
we cannot have two
element with same ID
so the ID selector is
very specific, we can
only target a single
element, below the ID
selector we have (Class & attribute selectors) these selector are less specific,
because we can have multiple element with same class or attributes, at next
level we have the (Element selector) because we can have many element a
given type.
- Back to our stylesheet which selector do things more specific the second
selector (. highlight) because we can have many <h1> element but not all this
<h1> element may have the class of (highlight).
h1 {
color: deeppink;
}
.highlight {
color: dodgerblue;
}
- Let me show you something go to the (Inspect), right click on the heading text,
go to inspect .
Apply selector
Overwrite selector
Here in the right side we can see the class selector apply to this element
that set color to (dodgerblue). Below this selector we have element
selector that set color to (deeppink) but this property declaration is cross out.
Because this is overwritten other property declaration.
- Another way to target this element is using the (ID) selecter, so here is our
markup, let’s give this element (ID) of (products) .
<article>
<h1 id="products" class="highlight">Heading Highlighting</h1>
</article>
Now back to our stlysheet, here we can deffine a ruels, with element with ID
of (#products),
#products {
color: green;
}
Now this selector is the most spicfice selector, so our (Heading) is green.
- What if
we
have
two
#products {
color: brown;
}
Which rules do you think will be apply, the ruls that come last . look
- Let me show you something very cool in (VSCode), if we hover our mouse
over a selector, we can see its weight or Specificity.
here we have three nu
h1 {
color: deeppink;
}
So the specificity of this element is One, and compare this with specificity of
others selectors.
The reason is our Heading is green is because of the third rules we have apply here
The specificity of this element is (1,0,0) it is
one hundred (100)
#products {
color: green;
}
- So here we can select an element with class of (.highlight) and the id of (#products), so
here we have one Class selector or one ID selector.
.highlight#products {
color: dodgerblue;
}
8- Inheritance
One of the important concept in CSS, you need to understand the concept of
Inheritance, which means some CSS property by-default inherit value from their
parents, let go a few examples.
- Let’s define a paragraph <p> with some text, now let’s add the
<strong>.
<p>Lorem ipsum <strong>dolor</strong> sit amet</p>
- Back to our stylesheet let’s define a rule for our paragraph <p> element
p {
color: dodgerblue;
}
look
our
<strong> element is blue even though, we didn’t set its color,
this color inheritance in the action.
- So with inheritance we didn’t come and set property on Strong element like
this.
strong {
color: dodgerblue;
}
Inheritance help us write less code, but what in this case, we want to stop
the inheritance, we don’t want to inherit this value. So we set value to
(initial). This set the color property to default color which is (black)
p {
color: dodgerblue;
}
strong {
color: initial;
}
- Now let’s look the property that doesn’t get inherited, so her in the paragraph
let’s set the border property.
p {
color: dodgerblue;
border: 1px solid black;
}
strong {
color: initial;
}
We have border
around our
paragraph, but there is no border around strong element, but if we want to
inherit the border to strong element, so we set border to (inherit). Now we can
say we inherit the border property.
p {
color: dodgerblue;
border: 1px solid black;
}
strong {
color: initial;
border: inherit;
}
9- Colors
Now let’s talk about colors, in CSS we have a few different ways to represent colors,
we can use by their (Named
colors, RGB Values, HSL Values,
and Hexadecimal Values).
Let’s see
these in action
- So I am going to define a <div> with class of (.box) , now let’s define a rules
give our (.box) width 200px and height 200px and background-color.
<div class="box"></div>
. box{
wi dth: 200px;
he ight: 200px;
background-color: rgb(230, 205, 16) ;
}
RGB (Red, Green, Blue) we can use RGB value to set background-color, and
RGB values start from zero (0) up to (255) (0-255)
Save the changes, and here is what we get.
- And we have another function is called RGBA() Which stand for lpha, so
with this value we can add an extra value and alpha channel used for
transparency. The value for the alpha channel is a decimal point number
between 0 mean (completely transparent) and 1 mean (completely opaque).
so if we have something under, for example image so that image will be visible.
This is very popular technique we use in CSS.
We can represent any number in the world using any of these systems.
Lookup this (#e6cd10) Hexadecimal value here we have six (6) characters and
every two characters represent, one of the numbers in the RGB system, so (e6)
represent (Red) and (cd) (Green) and (10) is (Blue), that is the concept behind
this. So instead of using RGB values we can use Hexadecimal value.
.box {
width: 200px;
height: 200px;
background-color: #e6cd10;
}
We have some others color systems also but more useful are
these, most people use Hexadecimal systems, and
Named Colors
because its short and more readable.
10-Gradients
We have learned about Colors, now let’s talk about Gradients, Using the linear-
gradient() and radial-gradient() functions, we can create gradients in CSS. Gradients
are images so they cannot be used as the value of background-color property. We
can use them as the value of background-image or background properties.
<div class="box"></div>
</body>
.box {
width: 200px;
height: 200px;
background: linear-gradient(dodgerblue, yellow);
}
Here I am using Name colors, but we can also use Hexadecimal colors and
RGB colors
- To change from Top to Bottom, back to our function as the first value or the
first argument, we supply the direction, we can say (to right).
.box {
width: 200px;
height: 200px;
background: linear-gradient(to right, dodgerblue, yellow);
}
.box {
width: 200px;
height: 200px;
background: linear-
gradient(to bottom right, dodgerblue, yell
ow);
}
Now look our transition start top left, and goes to the bottom right.
.box {
width: 200px;
height: 200px;
background: radial-gradient(white, orange);
}
11- Borders
Now let’s talk about Borders, this property takes three values, the first
value border size (10px) and the second value is the start of the border
(solid), the third value is to set the border color (dodblue)
.box {
width: 200px;
height: 200px;
background: orange;
border: 10px solid black;
}
- We
can
also
width: 600px;
height: 200px;
background: orange;
border: 10px solid black;
border-radius: 40px;
}
.box {
width: 200px;
height: 200px;
background: orange;
box-shadow: 10px 10px;
}
Basic Selectors
article All article elements
.product Elements with the product class
#products The element with the ID of products
a[href=“…”] Anchors with the given href
a[href*=“google”] Anchors whose href contains google
a[href^=“https”] Anchors whose href starts with https
a[href$=“.com”] Anchors whose href ends with .com
Relational Selectors
#products p All p elements inside #products
#products > p All p elements that are direct children of
#products + p #products
#products ~ p The p element immediately after #products
(sibling)
All p elements after #products (siblings)
Pseudo-class Selectors
article :first-child The first child of article elements
article :first-of-type The first occurrence of elements of different type
article p:first-of-type The first p element inside article elements
article :last-child
article :last-of-type
article :nth-child(odd)
article :nth-child(even)
Pseudo-element Selectors
p::first-letter The first letter of every p element
p::first-line The first line of every p element
::selection Any selected element
p::before To insert content before the content of p elements
p::after To insert content after the content of p elements
Colors
#fcba03 Hexadecimal value
rgb(252, 186, 3) RGB value
rgba(252, 186, 3, 0.5) Semi-transparent RGB value
hsl(44, 98%, 50%) HSL value
hsla(44, 98%, 50%, 0.5) Semi-transparent HSL value
Gradients
background: linear-gradient(blue, yellow);
background: linear-gradient(to bottom right, blue, yellow);
background: linear-gradient(45deg, blue, yellow);
background: linear-gradient(45deg, blue, yellow 30%);
background: radial-gradient(white, yellow);
background: radial-gradient(circle, white, yellow);
background: radial-gradient(circle at top left, white, yellow);
Borders
border: 10px solid blue;
border-width: 10px 20px 30px 40px; /* top right bottom left */
border-radius: 5px;
border-radius: 100%; /* full circle */
Shadows
box-shadow: 10px 10px;
box-shadow: 10px 10px grey;
box-shadow: 10px 10px 5px grey;
Terms
Embedded stylesheets Pseudo-element selectors
External stylesheets Radial gradients
Hexadecimal colors Relational selectors
HSL colors RGB colors
Inheritance Selectors
Inline styles Selectors specificity
Linear gradients Separation of concerns
Normalizing CSS
Pseudo-class selectors