Lab 02 PDF
Lab 02 PDF
2
Goal: To understand CSS
Inline Vs in-file Vs external CSS
Selectors and specificity rules
Box Model, Animation
Responsiveness, media queries
Inline CSS
Create a file as follows. Tinker with different values of the styles and observe their effects. You may use the browser’s
built in development tool to speed up.
File name:my_inline.html
On every line style information is added inside the style attribute of the html element. This is hard to manage for a
large html file. Therefore, a better and more manageable approach becomes necessary. In-file CSS is better. Why is it a
better approach? Is it more efficient? Does the browser treat these two approaches differently? You may now save the
above file as the following one. (Given in next page)
External CSS
Now split the file above into two files as follows. Make sure you save them in the same folder.
File name: style.css
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
<!DOCTYPE html>
<html> <head>
</body> </html>
Now browse to this html file (external.html). Do you see any difference? Your code surely looks more
organized.
What is the purpose of this new line <link rel="stylesheet" href="style.css"> inside
the <head>?
Using external CSS file has other advantages. These external files get cached by default at the browser. If any other
html
page from your website uses the same set of styles by linking to these same CSS files your browser will use them from
its cache. This is a great advantage. This enhances performance web applications.
style.css and external.html
CSS Selectors
Now let’s play with the following files to understand CSS selector better. Modify the
files as follows
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style1.css">
</head>
<body>
<h1>My First CSS at CSE482</h1>
<h1 id="myID">My First CSS at CSE482</h1>
<p>This is the 1st para.</p>
<p class="class1">This is the 2nd para with class1.</p>
<p class="class2">This is the 2nd para with class2.</p>
</body>
</html>
Now, browse to external.html file. What differences do you see? Why the <h1> tags differ? Why do the tags
<p>
nd
differ in looks? Why did the style on the 2 <h1> tag <h1 id="myID">My First CSS at CSE482</h1> not
display white color text? Why was the first rule h1{color: white; text-align: center;} overridden?
CSS Specificity
If there are two or more CSS rules that point to the same element, the selector with the highest specificity value will
a
"win", and its style declaration will be applied to that HTML element. Find this rule at W3schools / MDN.
<!DOCTYPE html>
<html> <head>
<style>
#demo { color: blue; }
.test { color: green; }
p { color: red; }
</style>
</head>
<body>
<p id="demo" class="test" style="color: pink;">Ki ache duniay?</p>
</body> </html>
Why does it show pink? If you remove the inline style which style will
apply?
Authors: [email protected], [email protected] Lab-2-Page-4
Padding, Margin, Border, and Dimension Units
File name: box.html
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 30rem;
border: 15px solid green;
border-radius: 2rem;
padding: 50px;
margin: 20px;
}
</style>
</head>
<body>
<h2>Padding, margin, border and border radius</h2>
<div>
This is box’s content. 50px padding, 20px margin, 15px green border
and a border radius of 2rem.
</div>
</body>
</html>
b
px to rem, em and percentage? Google or search W3schools.com / MDN
Try changing the parameter of the styles from
to find what do rem , em mean.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h2>Animation Test Lab</h2>
<div>I am animated!</div>
</body>
</html>
Change animation parameters and try to understand. Can you make this box spin? What changes does it need to spin?
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="responsive.css">
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<div class="col-3 menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="col-6">
/* For desktop: */
.col-1 { width: 8.33%; }
.col-2 { width: 16.66%; }
.col-3 { width: 25%; }
.col-4 { width: 33.33%; }
.col-5 { width: 41.66%; }
.col-6 { width: 50%; }
.col-7 { width: 58.33%; }
.col-8 { width: 66.66%; }
.col-9 { width: 75%; }
.col-10 { width: 83.33%; }
.col-11 { width: 91.66%; }
.col-12 { width: 100%; }
A. https://www.w3schools.com/css/css_rwd_grid.asp
B. https://www.w3schools.com/css/css_rwd_mediaqueries.asp
C. https://www.w3schools.com/css/css_rwd_images.asp
D. https://www.w3schools.com/css/css_rwd_videos.asp
E. https://www.w3schools.com/css/css_rwd_frameworks.asp
a
W3Schools: https://www.w3schools.com/
b
MDN: Mozilla Developers Network. https://developer.mozilla.org/en-US/