Handout 03. CSS
Handout 03. CSS
Content
❑ Basic CSS
❑ Advanced CSS rendering
❑ Responsible webpage design
1
10/3/2024
Basic CSS
❑ Content vs. Presentation
❑ Inline CSS
❑ Document CSS
❑ CSS syntax & declaration
❑ CSS class & subclass
Cascading Style ❑ CSS pseudo-class
Sheets (CSS) ❑ div & span with CSS
❑ External CSS
2
10/3/2024
<body>
❖ external style sheets can be linked and applied to numerous <h1>My First CSS Example</h1>
documents, might also specify how things should be presented on <p>This is a paragraph.</p>
</body>
screen or in print lower-level style sheets can override higher-level </html>
style sheets
❑ User-defined style sheets can also be used to override
the specifications of the webpage designer. These might
be used, say, to make text larger (e.g. for visually-
impaired users).
font-family:Courier,monospace <body>
font-style:italic <p style="font-family:Arial,sans-serif;
font-weight:bold text-align:right">This is a
font-size:12pt font-size:large font-size:larger right-justified paragraph in a sans serif
font (preferably Arial), with some
<span style="color:green">green text</span>.
color:red color:#000080 </p>
background-color:white
<p>And <a style="color:red;
text-decoration:underline text-decoration:none;
text-decoration:none font-size:larger;"
text-align:left text-align:center href="page01.html">here</a>
text-align:right text-align:justify is a formatted link.
</p>
vertical-align:top vertical-align:middle
</body>
vertical-align:bottom </html>
view page
3
10/3/2024
<head>
margin-left:0.1in margin-right:5% <title>Inline Style Sheets</title>
margin:3em </head>
padding-top:0.1in padding-bottom:5%
<body>
padding:3em <p>Here is an image
<img src="VictoriaBldg.jpeg"
alt="image of Victoria Building"
border-width:thin border-width:thick style="margin-left:0.3in;
border-width:5 margin-right:0.3in;
border-color:red vertical-align:middle;
border-style:double;
border-style:dashed border-style:dotted border-color:blue" />
border-style:double border-style:none embedded in text.
</p>
<body>
<table style="font-family:Arial,sans-serif">
<caption style="color:red;
font-style:italic;
text-decoration:underline">
Student data. </caption>
<tr style="background-color:red">
<th> name </th> <th> age </th>
</tr>
<tr>
<td> Chris Smith </td> <td> 19 </td>
</tr>
<tr>
<td> Pat Jones </td> <td> 20 </td>
</tr>
<tr>
<td> Doogie Howser </td> <td> 9 </td>
</tr>
</table>
</body>
</html>
view page
view page
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
4
10/3/2024
Layout in a Table
<html>
❑Border on tables using the “style” attribute <!-- CS443 page12.html 17.10.14 -->
<table style= "border: 1px solid;"> <head>
<title>Table Layout</title>
</head>
❑Horizontal & vertical layout within cells
<td style= "text-align:center"> <body>
<table style="border: 1px solid;">
<td style= "vertical-align: bottom"> <tr style="text-align: center;">
<td style="border: 1px solid;">
Left<br/>Column</td>
❑Layout to an entire row <td style="border: 1px solid;
<tr style="text-align: center"> vertical-align: top;">
Right Column</td>
<tr style="vertical-align: top"> </tr>
<tr>
<td style="border: 1px solid;">
Some data</td>
<td style="border: 1px solid;">
Some data</td>
</tr>
</table>
</body> view page
10
10
5
10/3/2024
CSS Syntax
❑ Selectors:
❖ Points to the HTML element(s) to apply style
❖ Simple (select elements based on name, id, class)
❖ Combinator (select elements based on a specific
relationship between them)
The CSS rule
❑ Declaration applied to the HTML
element with
❖ Specify the style to apply to HTML element(s) id="para1":
❖ Style consisted of property name and value
❖ Can declare 1 or many styles with block
The CSS rule
separated by a colon applied to the HTML
element with
class="center"
11
11
12
12
6
10/3/2024
CSS Selector
13
13
Selector: Combination
14
14
7
10/3/2024
Selector: Pseudo-Class
<html>
❑ Remember the HTML events!!! <head>
<style>
❖ Attached to HTML tags a.highlight:hover {
color: #ff0000;
❖ Event building and capturing font-size: 22px;
}
15
15
Selector: by Attribute
❑ Styling Forms
❖ attribute selectors can be useful for
styling forms without class or ID
❖ input[type="button"] {
width: 120px;
margin-left: 5px;
display: inline;
}
16
16
8
10/3/2024
❑ Problem: font properties apply to whole elements, which are <style type = "text/css">
17
17
.div2 {
❑ CSS box-sizing width: 300px;
height: 100px;
❖ include the padding and border in an element's total width and height padding: 50px;
border: 1px solid red;
❖ box-sizing: border-box; - padding and border are included in the width and height }
.div1 {
width: 300px; height: 100px;
border: 1px solid blue;
box-sizing: border-box;
}
.div2 {
width: 300px; height: 100px;
padding: 50px; border: 1px solid red;
box-sizing: border-box;
}
18
18
9
10/3/2024
19
19
20
20
10
10/3/2024
<head>
❑ All Web pages link to that same style sheet for a <title>Title for Page</title>
uniform look. <link rel="stylesheet"
type="text/css"
❑ simplifies Web pages since only need to specify href="myStyle.css"
title="myStyle“ />
structure/content tags </head>
<h1>The End</h1>
/* myStyle.css CS443 02.09.05 */
h1 {color : blue; text-align : center} </body> view page
21
21
Advaned CSS
❑ Rounded Corners
❑ Background by image and multi-image
❑ Shadow
❑ Advanced color
❑ Transition
Cascading Style ❑ Transformation
Sheets (CSS)
22
22
11
10/3/2024
Rounded Corners
❖ height
❑ Specify Each Corner
❖ border-top-left-radius
❖ border-top-right-radius
❖ border-bottom-right-radius
❖ border-bottom-left-radius
23
23
24
24
12
10/3/2024
image
<div id="example1">
❖ Properties declaration separated by “,” <h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor sit amet, consectetuer
❑ Background Size adipiscing elit, sed diam nonummy nibh euismod
tincidunt ut laoreet dolore magna aliquam erat
❖ background-size property to specify the size of background volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci
images. tation ullamcorper suscipit lobortis nisl ut aliquip
25
25
Box shadows
26
26
13
10/3/2024
Text shadows
blur effect
h1 {
❑ color: red;
text-shadow: 2px 2px black;
❖ h1 { }
text-shadow: 5px 5px 5px gray; </style>
</head>
} <body>
<h1>Text-shadow effect!</h1>
❖ h1 {
color: white;
text-shadow: 2px 2px 5px black;
}
❑ More:
❖ Multiple Shadows
❖ Border around
27
27
Advanced coloring
28
28
14
10/3/2024
29
29
30
30
15
10/3/2024
31
31
❑ Viewport concept
❖ Viewport is the area that the browser actually renders webpage Zoom out to fit viewport
with screen size
❖ Viewport has its own width and height
pad
Phone Unfortunately,
no lunch free!!!
https://www.w3schools.com/css/tryit.asp?filename=tryresponsive_col-s
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
32
32
16
10/3/2024
❑ Viewport setting
❖ <meta name="viewport" content="width=device-width, initial-scale=1.0">
❖ width=device-width - sets the width to follow the screen-width of the device (depending on the device)
❖ initial-scale=1.0 - sets the initial zoom level
❑ Media query
❖ CSS technique to define style rules for different media types
❖ Query to check width and height of the viewport, or orientation of the viewport,
then to define style by some SCC code:
@media not|only mediatype and (media feature) and (media feature) {
CSS-Code;
}
Layout change
❖ https://www.w3schools.com/css/css3_mediaqueries.asp when viewport goes
to small size
❑ Page layout should also consider the viewport size:
.left { width: 20%; }
.main { width: 60%; }
.right { width: 20%; }
@media screen and (max-width: 800px) {
.left, .main, .right {
width: 100%;
}
}
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_responsive_media_query
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
33
33
<style>
div.example {background-color: lightgrey; padding: 20px;}
@media screen and (max-width: 600px) { Checkout viewport width > 600px to
div.example { Checkout
font-size: 30px;
redefine “font-size”: 80px viewport
} width less
} than 600px
</style>
34
34
17
10/3/2024
<style>
* { box-sizing: border-box;}
Define CSS menu style for
.topnav { background-color: #333;}
“.topnav a”: left, inline, color… Checkout
.topnav a { float: left; viewport
color: #f2f2f2; text-align: center; width less
padding: 14px 16px; text-decoration: none; than 600px
}
35
35
<picture>
<source media="(min-width:650px)" srcset="img_pink_flowers.jpg">
<source media="(min-width:465px)" srcset="img_white_flower.jpg">
<img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>
Checkout viewport
width less than 650px
Checkout viewport
width less than 465x
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
36
36
18
10/3/2024
❑ CSS Flexbox
Box container
❖ https://www.w3schools.com/css/css3_flexbox.asp Box elements
❖ Box container: Place the box elements and arrange them by flex-direction
.flex-container { display: flex; flex-direction: row; }
❖ Box elements: To display the contents inside, and link in between them
.flex-container > div { width: 100px; margin: 10px;
text-align: center; line-height: 75px; font-size: 30px; }
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
37
37
38
38
19
10/3/2024
❑ Grid
➢ https://www.w3schools.com/css/css_rwd_grid.asp
❑ W3.CSS
➢ https://www.w3schools.com/w3css/default.asp
❑ Bootstrap
➢ https://www.w3schools.com/bootstrap/bootstrap_ver.asp
39
39
40
40
20
10/3/2024
41
41
21