Css - Cascading Style Sheets: An Inline CSS Uses The Style Attribute of An HTML Element
Css - Cascading Style Sheets: An Inline CSS Uses The Style Attribute of An HTML Element
Internal or Embedded CSS: This can be used when a single HTML document must be
styled uniquely. The CSS rule set should be within the HTML file in the head section i.e the
CSS is embedded within the HTML file.
External CSS: External CSS contains separate CSS file which contains only style property
with the help of tag attributes (For example class, id, heading, … etc). CSS property written
in a separate file with .css extension and should be linked to the HTML document using link
tag. This means that for each element, style can be set only once and that will be applied
across web pages.The file which contains CSS property must be save with .css extension.
For Ex: styles.css
• link tag is used to link the external style sheet with the html webpage.
• href attribute is used to specify the location of the external style sheet file.
Properties of CSS:
Inline CSS has the highest priority, then comes Internal/Embedded followed by External
CSS which has the least priority. Multiple style sheets can be defined on one page. If for an
HTML tag, styles are defined in multiple style sheets then the below order will be followed.
• As Inline has the highest priority, any styles that are defined in the internal and
external style sheets are overridden by Inline styles.
• Internal or Embedded stands second in the priority list and overrides the styles in the
external style sheet.
• External style sheets have the least priority. If there are no styles defined either in
inline or internal style sheet then external style sheet rules are applied for the HTML
tags.
CSS Syntax
For Eg;
AIM : To create a web page with a <h1> and <p> element, which shows a Blue Heading
and a red paragraph using Inline CSS.
PROGRAM
<!DOCTYPE html>
<html>
<body>
</body>
</html>
OUTPUT
RESULT
PROGRAM
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
OUTPUT
RESULT
PROGRAM
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
"styles.css":
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
}
OUTPUT
RESULT
The symbol used for class selectors is . and comes before the name of the class. In the
example, lines 2 and 3 now make use of the mainbody class.
<body>
<p id="introduction">All text for paragraph 1 would go
here</p>
<p class="mainbody">All text for paragraph 2 would go
here</p>
<p class="mainbody"=>All text for paragraph 3 would go
here</p>
</body>
Paragraph one would display on screen in the style defined for the introduction id and
paragraphs two and three would appear on screen with the style defined for the mainbody
class.