CSS Basics Day 1
CSS Basics Day 1
CSS Basics Day 1
Basics
Day 1
Agenda
● HTML was NEVER intended to contain tags for formatting a web page!
● HTML was created to describe the content of a web page, like:
● <h1>This is a heading</h1>
● <p>This is a paragraph.</p>
● When tags like <font>, and color attributes were added to the HTML 3.2 specification, it
started a nightmare for web developers. Development of large websites, where fonts and color
information were added to every single page, became a long and expensive process.
● To solve this problem,On December 17, 1996, the World Wide Web Consortium (W3C)
created CSS.
● CSS removed the style formatting from the HTML page
How to use CSS!
● We can write it as inline styles.
● We can use internal style element to write our styles in html files.
● We can use external style sheet.
Inline style
● An inline style may be used to apply a
<style>
h2{
color:red;
}
</style>
External CSS
● With an external style sheet, you can change the
look of an entire website by changing just one file!
● Each HTML page must include a reference to the
external style sheet file inside the <link> element,
inside the head section.
* {
color: red;
}
Selector Types : Element Selector
The element selector selects HTML elements based on the element name.
Here, all <p> elements on the page will be center-aligned, with a red text color:
p {
text-align: center;
color: red;
}
Selector Types : Class Selector
A HTML element can have one or more items defined in their class attribute. The class selector matches
any element that has that class applied to it.
color: red;
}
Selector Types : Class Selector
● Notice how the . is only present in CSS and not the HTML. This is because the . character instructs
the CSS language to match class attribute members. This is a common pattern in CSS, where a
● The value of a class attribute can be almost anything you want it to be. One thing that could trip you
up, is that you can't start a class (or an ID) with a number, such as .
1element.
Selector Types : ID Selector
● An HTML element with an id attribute should be the only element on a page with that ID value.
color: red;
Similarly to the class selector ., use a # character to instruct CSS to look for an element that
matches the id that follows it.
CSS Attribute Selectors
CSS [attribute="value"]
● input[type="text"]
● input[type="button"]
● a[target="_blank"]
What are Pseudo-classes?
A pseudo-class is used to define a special state of an element,For example, it can be used to:
● :active -> selector is used to select and style the active link ,link becomes active when you click on it.
● :hover -> selector is used to select elements when you mouse over them.
● :focus -> selector is used to select the element that has focus.
● :first-child -> selector is used to select the specified selector, only if it is the first child of its parent.
● :last-child -> selector is used to select the specified selector, only if it is the last child of its parent.
● :not(selector) -> selector matches every element that is NOT the specified element/selector.
What are Pseudo-elements?
● ::first-line -> used to add a special style to the first line of a text.
● ::first-letter -> used to add a special style to the first letter of a text.
● ::before -> can be used to insert some content before the content of an element.
● ::after -> can be used to insert some content after the content of an element.
CSS stands for Cascading Stylesheets. The cascade is the algorithm for solving conflicts
where multiple CSS rules apply to an HTML element.
The cascade algorithm
The cascade algorithm is split into 4 distinct stages :
● Position and order of appearance: the order of which your CSS rules appear
● Specificity: an algorithm which determines which CSS selector has the strongest match
● Origin: the order of when CSS appears and where it comes from, whether that is a
browser style, CSS from a browser extension, or your authored CSS
● Importance: some CSS rules are weighted more heavily than others, especially with the
!important rule type
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the following rules,
where number one has the highest priority:
So, an inline style has the highest priority, and will override external and internal styles and browser
defaults.
Specificity
● If there are two or more conflicting CSS rules that point to the same element, the browser
follows some rules to determine which one is most specific and therefore wins out.
● Think of specificity as a score/rank that determines which style declarations are ultimately
applied to an element.
● The universal selector (*) has low specificity, while ID selectors are highly specific!
Specificity Hierarchy
Every selector has its place in the specificity hierarchy. There
are four categories to define the specificity level of a selector:
We can use color option to change the text color like p { color : red } , so all paragraphs will be changed to be red.
● Background-color
We can use background color option to change the background color like div { background-color : red } , so all divs
We can use width option to change the element like img { width: 10px } , so image will have width 10 pixels
● Height
We can use height option to change the element like img { height: 10px } , so image will have height 10 pixels
Spacing
● Padding
Instead of creating space on the outside of your box, like margin does, the padding property creates space on the
inside of your box instead: like insulation.
● Margin
If you want to add space to the outside of an element, you use the margin property. Margin is like adding a cushion
around your element. The margin property is shorthand for margin-top, margin-right, margin-bottom and
margin-left.
Box Model
ُExample
ANALOGY FOR THE CSS BOX MODEL
border
Content area
margin
padding
Sizing
Units
● Px [Pixels]
● % [Percentage]
The order of values in the border shorthand are border-width, border-style and then, border-color.
● Border-radius : 25px;
● Text-align:center;
● Font-size:10px;
● Text-decoration:underline
● Text-transform:capitalize
● Compared to display: inline, the major difference is that display: inline-block allows to set a
● Compared to display: block, the major difference is that display: inline-block does not add a
line-break after the element, so the element can sit next to other elements.
Thank You!
Lab 1 : Pricing cards style