Lecture 3 CSS
Lecture 3 CSS
CSS
HELLO!
I’M Tran Dung [email protected]
Lecturer in Faculty
of Information
Technology
INTERESTS
0388 389 579
Course Objectives
In this lecture, you will
learn about
● CSS Styles
● CSS Selectors
● Pseudo Selectors
● CSS Values
● Style Priority
TABLE OF CONTENTS
01 02 03
CSS OVERVIEW NESTED SELECTORS CSS VALUES
04 05 06
CSS FONT/ PSEUDO CSS PRIORITIES
TEXT PROPERTIES SELECTORS
1.
CSS OVERVIEW
WHAT IS CSS?
No CSS
CSS
WHAT IS CSS?
CSS defines styling of
the HTML elements
Fonts
Colors
Margins
Sizes
Positioning
Floating
…
Attaching the styles to the document ?
Property Value
Selector h1 {
font-size: 42px; Declaration
Select all
<span> elements
span {
background: DodgerBlue;
color: #ffffff;
}
Primary Selectors: Select by ID
<span id="top">Here's a span with some text.</span>
<span>Here's another.</span>
Select <span>
with id="top"
span#top {
background: DodgerBlue;
}
Primary Selectors: Select by Class
<span class="sky">Here's a span with some text.</span>
<span>
Another <span class="code"><span></span>.
</span>
<span> with
class="sky"
span.sky {
background: DodgerBlue;
}
.code { Elements with
font-family: class="code"
Consolas;
}
Nested Selectors: Descendant
div.items a { <a> inside <div class="items">
color: green;
font-weight: bold; <div class="items">
} <a href="#">Item1</a>
<a href="#">Item2</a>
<a href="#">Item3</a>
<ul>
<li><a href="#">Item4</a></li>
<li><a href="#">Item5</a></li>
<li><a href="#">Item6</a></li>
</ul>
</div>
Nested Selectors: Adjacent Sibling
<p> coming after <p> p + p {
font-style: italic;
font-weight: bold;
}
<div>
<p>I'm a paragraph</p>
<p>I am selected!</p>
</div>
<div>
<p>I'm a paragraph</p>
<h2>Monkey hair</h2>
<p>I am NOT selected</p>
</div>
Nested Selectors: Direct Child
<span> directly contained in a <div>
div > span {
background: DodgerBlue;
}
span { background: #fff; }
Direct child of <div>
<div>
<span>Span #1, in the div.
<span>Span #2, in the span that's…</span>
</span>
</div>
<span>Span #3, not in the div at all.</span>
Nested Selectors: Multiple Classes
.apple { color: red }
.orange { color: orange }
.small { font-size: 16px }
.apple.orange { Elements with
font-style: italic; both classes
}
Element with
multiple classes
a[title="menu"] {
text-decoration: none;
color: #962103;
font-size: 22px;
}
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#" title="menu">Menu</a></li>
</ul>
Multiple Selectors (Element, Element)
<h1>Welcome…</h1>
<h2>My name is…</h2> h1, h2, p {
<p>I live in Duckburg.</p> background: yellow;
<p>My best friend is…</p> }
2
Combining Multiple Selectors
h1#header.intro {
text-decoration:
underline;
color: #C00;
}
#rgb {
color: rgb(192, 41, 2)
}
<red>, <green>, <blue>
#hex-rgb {
color: #C02902;
#RRGGBB or #RGB where
} R, G, B are hex digits (0…F)
CSS Color Values (RGBA and HSLA)
<div id="wrapper">
<div class="opacity">Some
text</div>
</div>
<red>, <green>,
<blue>, <alpha>
#wrapper, .opacity {
background: rgba(87, 76, 83, 0.9)
}
.opacity {
background: hsla(344, 73%, 52%, 0.5); The opacity values are
} from 0.0 to 1.0
Size Values (px)
Pixels: fixed size units (logical pixels, not physical)
<div>
Lorem ipsum dolor
sit adipisicing elit.
</div>
div {
font-size: 24px
}
Size Values (em)
Em's: scalable unit (based on the parent element)
<div>
<div class="nested-div">
Lorem ipsum dolor
sit adipisicing elit…
</div>
</div> Parent <div>
font: 24px 1.8em = 1.8 *
24px = 43.2px
div { font-size: 24px }
.nested-div { font-size: 1.8em http://pxtoem.com
}
Size Values (rem)
Rem's: scalable unit (based on the html element)
<div>
<div class="nested-div">
Lorem ipsum dolor
sit adipisicing elit…
</div>
</div>
font-style: italic;
font-variant: normal;
font-weight: bold;
font-size: 12px;
line-height: 16px;
font-family: Verdana;
Font Embedding
<h1>HTML & CSS Course</h1>
<h2>Normal Text</h2>
@font-face {
font-family: SketchRockwell;
src: url('fonts/SketchRockwell.ttf')
}
h1 {
font-family: SketchRockwell;
font-size: 3.5em
}
Importing "Lato" from Google Font Directory
@import
url('https://fonts.googleapis.com/css?family=Lato');
body {
font-family:
Lato, Arial, sans-serif;
}
sans-serif
serif font font
CSS Font Rules: Color
<h1>color</h1>
<p>Text with default color</p>
<p class="color-ddd">Text…</p>
<p class="color-coral">Text…</p>
<p class="color-rgba">Text…</p>
.weight-light {
font-weight: light;
}
.weight-bold {
font-weight: bold;
}
CSS Font Rules: Font-Style
<p class="font-style-italic">Text…</p>
<p class="font-style-oblique">Text…</p>
.font-style-italic {
font-style: italic;
}
.font-style-oblique {
font-style: oblique;
}
CSS Font Rules: Text-Align
<p class="text-align-center">Text…</p>
<p class="text-align-justify">Text…</p>
.text-align-center {
text-align: center;
}
.text-align-justify {
text-align: justify;
}
CSS Font Rules: Text-Transform
<p class="uppercase">Text…</p>
<p class="capitalize">Text…</p>
.uppercase {
text-transform: uppercase;
}
.capitalize {
text-transform:
capitalize;
}
Text Shadow
<blur <shadow
radius> color>
p {
text-shadow: 2px 2px 7px #000; }
<horizontal <vertical
distance> distance>
Word Wrapping
<p>This paragraph…</p>
p {
word-wrap: normal;
}
p {
word-wrap: break-word;
}
5.
Pseudo Selectors
Pseudo Selector (:hover)
a:hover {
color: #D48F29;
text-decoration:
underline overline;
}
Pseudo Selector (:visited)
a:visited {
color:#D48F29;
}
Pseudo Selector (:active)
<h2>click anywhere</h2>
html:active {
background: yellow;
}
Pseudo Selector (:first-line)
tr:nth-child(2n) {
background: #95313b;
color: #fff;
}
Structural Pseudo-Classes: nth-child(n+1)
tr:nth-child(2n+1) {
background: #95313b;
color: #fff;
}
5
Structural Pseudo-Classes: nth-of-type(n)
<p>This is first paragraph</p>
<p>This is second paragraph</p>
<p>This is third paragraph</p>
<p>This is fourth paragraph</p>
<div>
<p>This is first paragraph</p>
<p>This is second paragraph</p>
</div>
p:nth-of-type(2) {
background: #95313b;
color: #fff;
}
Structural Pseudo-Classes: first-of-type
<p>This is first paragraph</p>
<p>This is second paragraph</p>
<p>This is third paragraph</p>
<p>This is fourth paragraph</p>
<div>
<p>This is first paragraph</p>
<p>This is second paragraph</p>
</div>
p:first-of-type {
background: #95313b;
color: #fff;
}
6.
CSS Priorities
Default Browser Styles
Reset the browser styles (partially or fully)
* margin: 0; padding:0; }
Order of Style Definitions
Priorities of the CSS style definitions:
External <link rel="stylesheel" href="…"> lowest
Styles in the <head><style>…</style></head>
Inline style attributes: <p style="…">
Using !important
highest
h1.big { color:red !important }
Selector Priority (Specificity)
Selector priorities depend on:
0 0 0 0
inline styles number of number of number of
id selectors class element
selectors selectors
Selector Priority (Specificity) – Example
p { color: #FFF } 0, 0, 0, 1