Hope you did well on your exam! ๐
If this site helped, please leave feedback, check out the project on GitHub, or follow me on GitHub.
F4: CSS code examples
Code examples from the course Google Drive for the CSS lecture. View the source and run live previews where available.
Element selector โ styling paragraphs
css_1.html
HTML18 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
p {6
text-align: center;7
color: green;8
}9
10
</style>11
</head>12
<body>13
<h1> This is my header</h1>14
<p id="para1">Hello World! This is a paragraph</p>15
<p id="para2">This is a paragraph</p>16
17
</body>18
</html>Class selector โ .center
css_2.html
HTML25 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
6
7
.center {8
text-align: center;9
color: red;10
}11
12
13
</style>14
</head>15
<body>16
17
<h1 class="myclass">Red and center-aligned heading</h1>18
<h1 id="h1blue" class="center"> This will not be red </h1>19
<h1>This is the third h1</h1>20
<h1 class="center">This is the third h1</h1>21
<p class="center">Red and center-aligned paragraph.</p>22
<div class="blue">This is a division</div> 23
24
</body>25
</html>Specificity โ p.center vs .center
css_3.html
HTML24 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
p.center {6
color:red; 7
text-align:center;8
}9
.center {10
color: blue;11
text-align:right;12
}13
14
15
</style>16
</head>17
<body>18
19
<p>This is the first paragraph</p>20
<p class="center">This is the second paragraph</p> 21
<h1 class="center"> This is a heading</h1>22
23
</body>24
</html>Multiple classes โ .center and .large
css_4.html
HTML25 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
6
.center {7
text-align: center;8
color: red;9
}10
11
12
13
.large {14
font-size: 300%;15
}16
</style>17
</head>18
<body>19
20
<h1 class="center">This is a heading</h1>21
<p class="large">This is the first paragaraph</p>22
<p class="center large">This is the second paragraph</p> 23
24
</body>25
</html>Universal and ID selectors
css_5.html
HTML24 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
* {6
text-align: center;7
background-color: white;8
color: green;9
}10
#para1{11
color:blue;12
}13
</style>14
</head>15
<body>16
17
<h1>Hello world!</h1>18
19
<p>Every element on the page will be affected by the style.</p>20
<p id="para1">Me too!</p>21
<p>And me!</p>22
23
</body>24
</html>Grouped selectors โ h1, h2, p
css_6.html
HTML25 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
h1{6
text-align:left;7
background-color: blue;8
}9
h1, h2, p {10
text-align: center;11
color: red;12
13
}14
15
</style>16
</head>17
<body>18
19
<h1>Hello World!</h1>20
<h2>Smaller heading!</h2>21
<h3> This shouldn't be styled </h3>22
<p>This is a paragraph.</p>23
24
</body>25
</html>General sibling combinator (div ~ p)
css_7.html
HTML32 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
div~p{6
background-color: yellow;7
}8
</style>9
</head>10
<body>11
12
<h2>Descendant Selector</h2>13
<p>Paragraph 0 - The descendant selector matches all elements that are descendants of a specified element.</p>14
15
<div>16
<p>Paragraph 1</p>17
<p>Paragraph 2</p>18
<section>19
<p>Paragraph 3</p>20
</section>21
</div>22
<p>Paragraph 4</p>23
<p>Paragraph 5</p>24
<h1>this is the heading</h1>25
New heading26
<div>mydiv</div>27
<p>Paragraph 6</p>28
<p>Paragraph 7</p>29
30
31
</body>32
</html>Link pseudo-classes (:link, :visited, :hover, :active)
css_8.html
HTML39 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
/* unvisited link */6
a:link {7
color: red;8
}9
10
/* visited link */11
a:visited {12
color: green;13
}14
15
/* mouse over link */16
a:hover {17
color: yellow; 18
}19
20
21
/* selected link */22
a:active {23
color: blue;24
}25
p::first-line{26
color: rgb(255, 55, 0);27
font-weight: bold;28
}29
</style>30
</head>31
<body>32
33
<h2>CSS Links</h2>34
<p><b><a href="#" target="_blank">This is a link</a></b></p>35
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p>36
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>37
<a href="#">Testing anchor </a>38
</body>39
</html>:hover pseudo-class
css_9.html
HTML28 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
div {6
background-color: green;7
color: white;8
padding: 25px;9
text-align: center;10
}11
12
div:hover {13
background-color: blue;14
}15
div.myclass:hover{16
background-color:brown;17
}18
</style>19
</head>20
<body>21
22
<p>Mouse over the div element below to change its background color:</p>23
<div class="myclass">This is a new div</div>24
25
<div>Mouse Over Me</div>26
27
</body>28
</html>Reveal child on parent hover
css_10.html
HTML24 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
p {6
display: none;7
background-color: yellow;8
padding: 20px;9
}10
11
div:hover p {12
display: block;13
}14
</style>15
</head>16
<body>17
18
<div>Hover over me to show the p element19
<p>Tada! Here I am!</p>20
</div>21
<div>hello this is another div</div>22
23
</body>24
</html>::first-line pseudo-element
css_11.html
HTML19 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
p::first-line {6
color: rgb(72, 0, 255);7
font-variant: small-caps;8
}9
</style>10
</head>11
<body>12
13
<p>You can use the ::first-line pseudo-element to add a special14
effect to the first line of a text. Some more text. 15
And even more, and more, and more, and more, and more, 16
and more, and more, and more, and more, and more, and more, and more.</p>17
18
</body>19
</html>Attribute selector and box model
css_12.html
HTML51 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<style>5
a[target] {6
background-color: yellow;7
color:red;8
}9
h1{10
border:2px solid Tomato;11
width:50%;12
}13
h2{14
border:5px dotted Tomato;15
margin-top:50px;16
margin-right:50px;17
margin-left:50px;18
19
}20
h3{21
border:10px dashed Tomato;22
margin-top:20px;23
padding-top:20px;24
padding-bottom: 100px;25
}26
27
p[title*=fl]{28
background-color: red;29
}30
31
32
</style>33
</head>34
<body>35
36
<h2>CSS [attribute] Selector</h2>37
<p title="flower dd .. sdf">The links with a target attribute gets a yellow background:</p>38
39
<a href="https://www.google.com">google.com</a>40
<a href="http://www.disney.com" target="_blank">disney.com</a>41
<a href="http://www..wikipedia.org" target="_top">wikipedia.org</a>42
43
44
45
<h1>Hello World</h1>46
<h2>This is heading 2</h2>47
<h3>This is heading 3</h3>48
49
50
</body>51
</html>Responsive column layout
css_13.html
HTML141 lines
1
<!DOCTYPE html>2
<html>3
<head>4
<meta name="viewport" content="width=device-width, initial-scale=1.0">5
<style>6
* {7
box-sizing: border-box;8
}9
10
.row::after {11
content: "";12
clear: both;13
display: table;14
}15
16
[class*="col-"] {17
float: left;18
padding: 15px;19
}20
21
html {22
font-family: "Lucida Sans", sans-serif;23
}24
25
.header {26
background-color: #9933cc;27
color: #ffffff;28
padding: 15px;29
}30
31
.menu ul {32
list-style-type: none;33
margin: 0;34
padding: 0;35
}36
37
.menu li {38
padding: 8px;39
margin-bottom: 7px;40
background-color: #33b5e5;41
color: #ffffff;42
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);43
}44
45
.menu li:hover {46
background-color: #0099cc;47
}48
49
.aside {50
background-color: #33b5e5;51
padding: 15px;52
color: #ffffff;53
text-align: center;54
font-size: 14px;55
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);56
}57
58
.footer {59
background-color: #0099cc;60
color: #ffffff;61
text-align: center;62
font-size: 12px;63
padding: 15px;64
}65
66
/* For mobile phones: */67
[class*="col-"] {68
width: 100%;69
}70
71
@media only screen and (min-width: 600px) {72
/* For tablets: */73
.col-s-1 {width: 8.33%;}74
.col-s-2 {width: 16.66%;}75
.col-s-3 {width: 25%;}76
.col-s-4 {width: 33.33%;}77
.col-s-5 {width: 41.66%;}78
.col-s-6 {width: 50%;}79
.col-s-7 {width: 58.33%;}80
.col-s-8 {width: 66.66%;}81
.col-s-9 {width: 75%;}82
.col-s-10 {width: 83.33%;}83
.col-s-11 {width: 91.66%;}84
.col-s-12 {width: 100%;}85
}86
@media only screen and (min-width: 768px) {87
/* For desktop: */88
.col-1 {width: 8.33%;}89
.col-2 {width: 16.66%;}90
.col-3 {width: 25%;}91
.col-4 {width: 33.33%;}92
.col-5 {width: 41.66%;}93
.col-6 {width: 50%;}94
.col-7 {width: 58.33%;}95
.col-8 {width: 66.66%;}96
.col-9 {width: 75%;}97
.col-10 {width: 83.33%;}98
.col-11 {width: 91.66%;}99
.col-12 {width: 100%;}100
}101
</style>102
</head>103
<body>104
105
<div class="header">106
<h1>Chania</h1>107
</div>108
109
<div class="row">110
<div class="col-3 col-s-3 menu">111
<ul>112
<li>The Flight</li>113
<li>The City</li>114
<li>The Island</li>115
<li>The Food</li>116
</ul>117
</div>118
119
<div class="col-6 col-s-9">120
<h1>The City</h1>121
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>122
</div>123
124
<div class="col-3 col-s-12">125
<div class="aside">126
<h2>What?</h2>127
<p>Chania is a city on the island of Crete.</p>128
<h2>Where?</h2>129
<p>Crete is a Greek island in the Mediterranean Sea.</p>130
<h2>How?</h2>131
<p>You can reach Chania airport from all over Europe.</p>132
</div>133
</div>134
</div>135
136
<div class="footer">137
<p>Resize the browser window to see how the content respond to the resizing.</p>138
</div>139
140
</body>141
</html>Multi-stylesheet demo (W3Schools)
css_demo.html
HTML445 lines
1
<!DOCTYPE html>2
<!-- saved from url=(0046)https://www.w3schools.com/css/demo_default.htm -->3
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">4
<style>/* Stylesheet 1: */5
body {6
font: 100% Lucida Sans, Verdana;7
margin: 20px;8
line-height: 26px;9
}10
11
.container {12
xmin-width: 900px;13
}14
15
.wrapper {16
position: relative;17
overflow: auto;18
}19
20
#top, #sidebar, #bottom, .menuitem {21
border-radius: 4px;22
margin: 4px;23
}24
25
#top {26
background-color: #4CAF50;27
color: #ffffff;28
padding: 15px;29
}30
31
#menubar {32
width: 200px;33
float: left34
}35
36
#main {37
padding: 10px;38
margin: 0 210px;39
}40
41
#sidebar {42
background-color: #32a4e7;43
color: #ffffff;44
padding: 10px;45
width: 180px;46
bottom: 0;47
top: 0;48
right: 0;49
position: absolute;50
}51
52
#bottom {53
border: 1px solid #d4d4d4;54
background-color: #f1f1f1;55
text-align: center;56
padding: 10px;57
font-size: 70%;58
line-height: 14px;59
}60
61
#top h1, #top p, #menulist {62
margin: 0;63
padding: 0;64
}65
66
.menuitem {67
background-color: #f1f1f1;68
border: 1px solid #d4d4d4;69
list-style-type: none;70
padding: 2px;71
cursor: pointer;72
}73
74
.menuitem:hover {75
background-color: #ffffff;76
}77
78
.menuitem:first-child {79
background-color:#4CAF50;80
color: white;81
font-weight:bold;82
}83
84
a {85
color: #000000;86
text-decoration: underline;87
}88
89
a:hover {90
text-decoration: none;91
}92
93
94
@media (max-width: 800px) {95
#sidebar {96
width: auto;97
position: relative;98
} 99
#main {100
margin-right: 0;101
} 102
103
}104
105
@media (max-width: 600px) {106
#menubar {107
width: auto;108
float: none;109
}110
#main {111
margin: 0;112
} 113
}114
</style>115
116
<style>/* Stylesheet 2: */117
body {118
font-family: Arial;119
background-color: #d14836;120
line-height: 20px;121
}122
123
.container {124
xmin-width: 900px;125
}126
127
.wrapper {128
position: relative;129
overflow: auto;130
}131
132
#top {133
color: #ffffff;134
padding: 15px;135
font-size: 30px;136
line-height: 26px; 137
}138
139
#top h1 {140
margin:0;141
line-height: 50px;142
}143
144
#menubar {145
width: 190px;146
float: right;147
}148
149
#main {150
padding: 10px;151
background-color: #ffffff;152
font: 80% Verdana;153
}154
155
#main h1, #main h2 {156
color: #d14836;157
}158
159
#sidebar {160
background-color: #F6DAD7;161
color: #d14836;162
padding: 10px;163
}164
165
#bottom {166
text-align: center;167
padding: 10px;168
font-size: 70%;169
color: #ffffff;170
}171
172
#menulist {173
padding:0;174
font: 16px verdana;175
}176
177
.menuitem {178
width: 155px;179
background-color: #d14836;180
border: 1px solid #d14836;181
border-radius: 40px;182
color: #ffffff;183
list-style-type: none;184
margin: 10px;185
padding: 5px;186
text-align: center;187
cursor: pointer;188
}189
190
.menuitem:nth-child(2) {191
background-color:white;192
color: #d14836;193
font-weight:bold;194
}195
196
.menuitem:hover {197
background-color: #ffffff;198
color: #d14836;199
}200
201
a {202
color: #d14836;203
text-decoration: none;204
}205
206
a:hover {207
text-decoration: underline;208
}209
</style>210
211
<style>/* Stylesheet 3: */212
body {213
font: 100% Verdana;214
margin: 20px;215
line-height: 26px;216
}217
218
.container {219
xmin-width: 900px;220
}221
222
.wrapper {223
position: relative;224
overflow: auto;225
}226
227
#sidebar {228
background-color: #f1f1f1;229
border: 1px solid #d4d4d4;230
padding-left: 10px;231
}232
233
#bottom {234
text-align: center;235
padding: 10px;236
font-size: 70%;237
line-height: 14px;238
}239
240
h1, h2, h3 {241
color: #4CAF50;242
}243
244
#menulist {245
padding: 0;246
position: relative;247
overflow: auto;248
}249
250
.menuitem {251
width: 165px;252
float: left;253
background-color: #555555;254
color: #ffffff;255
list-style-type: none;256
margin: 4px;257
padding: 2px;258
text-align: center;259
cursor: pointer;260
}261
262
.menuitem:nth-child(3) {263
background-color:#4CAF50;264
}265
266
.menuitem:hover {267
background-color: #999999;268
}269
270
a {271
color: #000000;272
}273
274
a:hover {275
color: #84c754;276
}277
</style>278
<style>/* Stylesheet 4: */279
body {280
font: 100% Courier New;281
margin: 20px;282
line-height: 26px;283
background-color: #000000;284
}285
286
.container {287
xmin-width: 900px;288
}289
290
.wrapper {291
position: relative;292
overflow: auto;293
}294
295
#top {296
color: #84c754;297
padding: 15px;298
}299
300
#main {301
padding: 10px;302
color: #84c754;303
}304
305
#sidebar {306
color: #ffffff;307
border: 1px solid #ffffff;308
border-radius: 4px;309
padding: 10px;310
width: 320px;311
top: 0;312
right: 0;313
position: absolute;314
font-size: 80%;315
line-height: 20px;316
}317
318
#bottom {319
border: 1px solid #ffffff;320
border-radius: 4px;321
color: #ffffff;322
text-align: center;323
padding: 10px;324
font-size: 70%;325
line-height: 14px;326
}327
328
#top h1,#top p {329
margin: 0;330
}331
332
.menuitem {333
color: #84c754;334
cursor: pointer;335
}336
337
.menuitem:nth-child(4) {338
color:white;339
font-weight:bold;340
}341
342
.menuitem:hover {343
color: #ffffff;344
}345
346
a {347
color: #ffffff;348
}349
350
a:hover {351
color: #84c754;352
}353
@media (max-width: 600px) {354
#sidebar {355
width: auto;356
margin-bottom:10px; 357
position: relative;358
} 359
}360
361
</style>362
363
</head>364
<body>365
366
<div class="container wrapper">367
<div id="top">368
<h1>Welcome to My Homepage</h1>369
<p>Use the menu to select different Stylesheets</p>370
</div>371
<div class="wrapper">372
<div id="menubar">373
<ul id="menulist">374
<li class="menuitem" onclick="reStyle(0)">Stylesheet 1375
</li><li class="menuitem" onclick="reStyle(1)">Stylesheet 2376
</li><li class="menuitem" onclick="reStyle(2)">Stylesheet 3377
</li><li class="menuitem" onclick="reStyle(3)">Stylesheet 4378
</li><li class="menuitem" onclick="noStyles()">No Stylesheet379
</li></ul>380
</div>381
<div id="main">382
<h1>Same Page Different Stylesheets</h1>383
<p>This is a demonstration of how different stylesheets can change the layout of your HTML page. You can change the layout of this page by selecting different stylesheets in the menu, or by selecting one of the following links:<br>384
<a href="https://www.w3schools.com/css/demo_default.htm#" onclick="reStyle(0);return false">Stylesheet1</a>,385
<a href="https://www.w3schools.com/css/demo_default.htm#" onclick="reStyle(1);return false">Stylesheet2</a>,386
<a href="https://www.w3schools.com/css/demo_default.htm#" onclick="reStyle(2);return false">Stylesheet3</a>,387
<a href="https://www.w3schools.com/css/demo_default.htm#" onclick="reStyle(3);return false">Stylesheet4</a>.388
</p>389
<h2>No Styles</h2>390
<p>This page uses DIV elements to group different sections of the HTML page. Click here to see how the page looks like with no stylesheet:<br><a href="https://www.w3schools.com/css/demo_default.htm#" onclick="noStyles();return false">No Stylesheet</a>.</p>391
</div>392
<div id="sidebar">393
<h3>Side-Bar</h3>394
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>395
</div>396
</div> 397
398
399
<div id="bottom">400
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.401
</div>402
</div>403
404
<script>405
function noStyles() {406
document.styleSheets[0].disabled = true;407
document.styleSheets[1].disabled = true;408
document.styleSheets[2].disabled = true;409
document.styleSheets[3].disabled = true;410
}411
412
function reStyle(n) {413
noStyles()414
document.styleSheets[n].disabled = false;415
}416
417
function closeBlackdiv() {418
var blackdiv, stylediv;419
blackdiv = document.getElementById("blackdiv")420
blackdiv.parentNode.removeChild(blackdiv);421
stylediv = document.getElementById("stylediv")422
stylediv.parentNode.removeChild(stylediv);423
}424
425
function showStyle(n) {426
var div, text, blackdiv;427
blackdiv = document.createElement("DIV");428
blackdiv.setAttribute("style","background-color:#000000;position:absolute;width:100%;height:100%;top:0;opacity:0.5;margin-left:-20px;");429
blackdiv.setAttribute("id","blackdiv");430
blackdiv.setAttribute("onclick","closeBlackdiv()");431
document.body.appendChild(blackdiv);432
div = document.createElement("DIV");433
div.setAttribute("id","stylediv");434
div.setAttribute("style","background-color:#ffffff;padding-left:5px;position:absolute;width:auto;height:auto;top:100px;bottom:50px;left:200px;right:200px;overflow:auto;font-family: monospace; white-space: pre;line-height:16px;");435
text = document.createTextNode(document.getElementsByTagName("STYLE")[n].innerHTML);436
div.appendChild(text);437
document.body.appendChild(div);438
//alert(document.getElementsByTagName("STYLE")[n].innerHTML);439
}440
reStyle(0);441
</script>442
443
444
445
</body></html>