css (1)
css (1)
<!DOCTYPE html>
<html>
<head>
<style>
.heading1 { text-align: center; color: Green; } </style>
</head>
<body>
<h1 class="heading1">This heading is green with centered
align.</h1>
</body>
</html>
output
CSS Universal Selector
<!DOCTYPE html>
<html>
<head>
<style>
* { color: blue; text-align:center; border:1px solid #000; }
</style>
</head>
<body>
<h2>This is my second heading</h2>
<h4>This is my fourth heading</h4>
</body>
</html>
output
Adding CSS
There are several choices for adding CSS to the
page
• Inline style
• Embedded style
• Import style
• External style
Inline style
<p style="color:olive;font-size:24px;">HTML
Styles with CSS</p>
embedded
<!DOCTYPE html>
<html>
<head> <body>
<title>My Example</title>
<style>
<h1>Embedded
body { Styles</h1>
background-color: darkslategrey; <p id="intro">Allow
color: azure; you to define styles for the
font-size: 1.1em;
}
whole document.</p>
h1 { <p
color: coral; class="colorful">This has a
} style applied via a
#intro {
font-size: 1.3em;
class.</p>
} </body>
.colorful { </html>
color: orange;
}
External style
• Commonly used style
• Style and html are in separate file
• To add an external stylesheet to a web page,
use the <link> tag, providing the URL of the
style sheet in the href attribute, as well
as rel="stylesheet“ is then linked to from
External style sheet example
external_p1.html
external_p1.css
<!DOCTYPE html>
<html>
<head> body {
<title>My Example</title> background-color: darkslategrey;
<link rel="stylesheet" href="C:\Users\ color: azure;
r_anisha\Desktop\external_p1.css"> font-size: 1.1em;
</head> }
<body>
<p id="intro">Allow you to define styles #intro {
for the whole document.</p> font-size: 1.3em;
<p class="colorful">This has a style appl }
ied via a class.</p>
.colorful {
</body>
color: orange;
</html>
}
• Note: href should refer the path of CSS
File
output
Import style
• CSS @import rule to import an external style
sheet.
• To do this, use the <style> tag.
<style>
@import "imported_style_sheet.css";
@import url("imported_style_sheet.css");
</style>
Import style
<!DOCTYPE html>
<html>
<head>
<title>My Example</title>
<style>
@import "external_p1.css";
</style>
</head>
<body>
<p id="intro">Allow you to define styles for the whole document.</p>
<p class="colorful">This has a style applied via a class.</p>
</body>
</html>