COURSE 03
CSS
lect. eng. Rajmond JÁNÓ, PhD
rajmond.jano@ael.utcl
uj.ro
fb.com/janorajmond
C03 – CSS
• Introduction
• Including CSS in HTML
pages
• CSS syntax
• CSS selectors
• IDs and classes
• Combining selectors
• Pseodo-element
selectors
• Selector priority
• Inheritance
• Styling color
• Styling font
• Specifying dimensions
• Borders, padding and
margins
• Positioning elements
• Adding opacity and
shadows
CSS
• Cascading Style Sheets (CSS) is a style sheet
language used for describing the presentation
of a document written in a markup language
like HTML
• CSS is a cornerstone technology of the World
Wide Web, alongside HTML and JavaScript
HTML + CSS
HTML HTML +
CSS
HTML + CSS
HTML + CSS
CSS
• The “cascade” part of CSS is a set of rules for
resolving conflicts with multiple CSS rules
applied to the same elements
• For example, if there are two rules defining the
color of an element, the rule that comes last in
the cascade order will “trump” the other
STYLING HTML
• HTML tags can be used to style HTML
• However, they shouldn’t be used
• Most formatting attributes have been deprecated in
HTML5
STYLING HTML
• Separation of concerns:
• HTML – structure of the webpage
• CSS – style of the webpage
STYLING HTML
HTML
CSS
Rendered
page
INCLUDING CSS IN WEB PAGES
• Inlining styles – style is defined when the element
is defined
INCLUDING CSS IN WEB PAGES
• Using the <style></style> tags in the head of
the HTML document
INCLUDING CSS IN WEB PAGES
• Using a separate linked stylesheet – most
commonly used
index.html myStyles.css
CSS SYNTAX
• Three terms for describing your styles
• CSS rule
• CSS selector
• CSS declaration
CSS RULE
• Every style is defined by a selector and a
declaration
• The declaration contains at least one property:
value pair. Together they are called a CSS rule
selector {
property1: value1;
property2: value2;
…
propertyn: valuen;
}
declaration
CSS SELECTOR
• The selector associates CSS rules with HTML
elements
CSS SELECTOR
• The selector is typed in front of the
declaration, with a space separating it and the
opening curly-bracket (aka curly-brace)
• Typically, extra indentation and returns are
added as shown for the sake of readability
CSS SELECTOR
• You can apply styles to multiple selectors in
the same rule by separating the selectors with
commas
CSS DECLARATION
• You can apply multiple declarations to a
selector by separating the declarations with
semi-colons.
CSS SELECTORS
• There are three categories of CSS selectors
• All (*)
• Type (element)
• ID
• Class
* SELECTOR
• Selects all elements on the web page
HTML CSS
TYPE (ELEMENT) SELECTORS
• The simplest selector is the type selector,
which targets an HTML element by name
ID SELECTORS
• An ID is an HTML attribute that is added to
your HTML markup
• You reference that ID in your CSS with a hash
(#)
• IDs should be unique in your HTML/CSS
HTML CSS
CLASS SELECTORS
• A class is an HTML attribute that is added to
your HTML markup
• You reference that class in your CSS with a
period (.)
HTML CSS
IDS VS. CLASSES
• The most important difference between IDs
and classes is that there can be only one ID
assigned to an element, but multiple classes
• An ID is more specific than a class
• An element can have both an ID and multiple
classes
IDS VS. CLASSES
IDS VS. CLASSES
HTML
CSS
IDS VS. CLASSES
HTML
CSS
What color will the paragraph
text be?
ID > class
MULTIPLE CLASSES
• Elements can have multiple classes, giving you
more control
HTML
CSS
SELECTOR COMBINATIONS
• Element+.class
HTML CSS
SELECTOR COMBINATIONS
• Element+.class
HTML CSS
SELECTOR COMBINATIONS
• .class+.class
HTML CSS
SELECTOR COMBINATIONS
• selector, selector
HTML CSS
DESCENDANT SELECTORS
• selector selector
HTML CSS
ADJACENT SELECTORS
• selector ~ selector (all adjacent)
HTML CSS
adjacent
ADJACENT SELECTORS
• selector + selector (directly adjacent)
HTML CSS
directly
adjacent
PSEUDO-ELEMENT SELECTORS
• :after
• :before
• :first-line
• :first-letter
• :selection
PSEUDO-ELEMENT SELECTORS
HTML CSS
PSEUDO-ELEMENT SELECTORS
• [attribute]
• [attribute=“exact-value”]
• [attribute*=“contains-value”]
• [attribute^=“begins-value”]
PSEUDO-ELEMENT SELECTORS
HTML
CSS
PSEUDO-CLASSES
• :active
• :link
• :visited
• :hover
• :focus
• … and many more
PSEUDO-CLASSES
• :first-child
• :last-child
• :only-child
• :nth-child(n)
• :nth-last-child(n)
PSEUDO-CLASSES
HTML CSS
SELECTOR PRIORITY
• Last loaded selector will trump any other
HTML CSS
SELECTOR PRIORITY
• Last loaded selector will trump any other
HTML CSS
SELECTOR PRIORITY
• Last loaded selector will trump any other
HTML CSS
CASCADING PRIORITY
Browser stylesheet
Linked (external) stylesheet
Embedded (internal) styles
Inline (internal) styles
INHERITANCE
• Most elements will inherit many style
properties from their parent elements by
default
!IMPORTANT SPECIFIER
• The !important specifier can be used to
override normal cascading and inheritance
• Normal use case: never!
HTML
CSS
!IMPORTANT SPECIFIER
• The !important specifier can be used to
override normal cascading and inheritance
• Normal use case: never!
HTML
CSS
APPLYING COLOR
• color – text color
• background-color – background color
(duuuh…) HTML CSS
APPLYING COLOR
• There are 3 main ways to set color:
• By name: red, white, blue, magenta, etc.
• By hex-RGB values: #006699, #123456, #FFAACC
• By RGB(A) values: rgb(0, 66, 99), rgba(123, 255, 0,
0.5)
• By HSL values: hsl(100, 66%, 99%), hsl(360, 55%, 0%)
APPLYING COLOR
• Use any color picker from the Internet (e.g.:
w3schools)
APPLYING COLOR
• Or just ask
Google
Assistant to
show you a
Color Picker
APPLYING COLOR
HTML CSS
FONT ATTRIBUTES
• For fonts, we generally want to style the
following:
• color: color
• size: font-size
• effect: font-weight, font-style, text-
decoration
• family: font-family
FONT-SIZE
• font-size
• pixels: px
• relative size of the element: em
HTML
FONT-SIZE
CSS
FONT-WEIGHT, FONT-STYLE,
TEXT-DECORATION
CSS
FONT-FAMILY
• For the font family you can (and should)
specify, in order:
• The exact font name
• A fallback font name or font family
• A generic font type (if none of the above are
available)
• You can also import font from other font
repositories (like Google Fonts)
FONT-FAMILY
CSS
Font to use
Fallback
Generic
(built-in
browser)
SPECIFYING DIMENSIONS
• You can specify dimensions (width and/or
height) to most HTML elements in:
• pixels
• percentages (of current viewport)
• You can also specify a maximum width of
length
SPECIFYING DIMENSIONS
HTML
CSS
SPECIFYING DIMENSIONS
HTML
CSS
The div will take up 50%
of the viewport until it
reaches 200px in width
and then it will stop
growing
MEASUREMENT UNITS IN CSS
Unit Description Example
% Defines a measurement as a percentage relative to another
value, typically an enclosing element.
p {
font-size: 16pt;
line-height: 125%;
}
px Defines a measurement in screen pixels. p {
padding: 25px;
}
em A relative measurement for the height of a font in em spaces.
Because an em unit is equivalent to the size of a given font, if
you assign a font to 12pt, each "em" unit would be 12pt; thus,
2em would be 24pt.
p {
letter-spacing: 7em;
}
rem Similar to “em” but while em is relative to the font-size of its
direct or nearest parent, rem is only relative to the html (root)
font-size.
p {
letter-spacing: 7rem;
}
cm/mm
/in
Defines a measurement in centimeters, millimeters or inches p {
word-spacing: 15mm;
}
pt Defines a measurement in points. A point is defined as
1/72nd of an inch.
body {
font-size: 18pt;
}
ex This value defines a measurement relative to a font's x-
height. The x-height is determined by the height of the font's
lowercase letter x.
p {
font-size: 24pt;
line-height: 3ex;
}
Margin
Border
Padding
BORDERS, PADDING, MARGINS
Conte
nt
BORDERS, PADDING, MARGINS
HTML CSS
BORDERS, PADDING, MARGINS
HTML CSS
BORDERS, PADDING, MARGINS
HTML CSS
Note: CSS comments are
just like C/C++ block
comments /* */
BORDERS, PADDING, MARGINS
HTML CSS
BORDER-RADIUS
HTML CSS
BORDER-RADIUS
HTML CSS
BORDER-RADIUS
HTML CSS
POSITIONING
• There are four basic types of positions in CSS
• Static
• Relative
• Absolute
• Fixed
POSITIONING
HTML CSS
default positioning is
POSITIONING
HTML CSS
With relative positioning you can
move an element from where it’s
POSITIONING
HTML CSS
Pay special attention when
using negative positioning
values!
POSITIONING
HTML CSS
Absolute positioning will place the element to the
specified location relative to the parent element (the
entire HTML document, in this case).
POSITIONING
HTML CSS
Fixed positioning will place the element relative to the
current viewport.
The element will stay in that position even if the page is
OPACITY
HTML CSS
OPACITY
HTML CSS
SHADOW
HTML CSS
Well, that looks like 💩, doesn’t it?
SHADOW
HTML CSS
Little bit better…
SHADOW
CSS
Blur
attribute
CHANGING CURSORS
• CSS can control the look of the cursor using the cursor
attribute CSS
HTML
BIBLIOGRAPHY
• https://www.w3schools.com/css/default.asp
• https://www.w3schools.com/css/css_syntax.as
p
• https://www.w3schools.com/cssref/css_selecto
rs.asp
• https://www.w3schools.com/css/css_pseudo_el
ements.asp
• https://www.w3schools.com/css/css_pseudo_cl
asses.asp
• https://www.w3schools.com/colors/default.asp
COURSES
Available online at:
http://www.ael.utcluj.ro/
Information for Students -> Courses -> Web
Technologies
Web technologies-course 03.pptx

Web technologies-course 03.pptx