Css
Css
The most common way to add CSS, is to keep the styles in external CSS files.
INLINE CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The
style attribute can contain any CSS property.
Example
Inline styles are defined within the "style" attribute of the relevant
element:
<html>
<body>
</body>
</html>
SOURCE CODE OUTPUT
ADVANTAGES OF INLINE CSS
DISADVANTAGES OF INLINE CSS
▪ They make a document difficult to maintain and
increase the download time. Inline styles must be
applied to every element on which you want to apply a
style. Therefore, if you want all your headings to have
the font family Arial, you have to add an inline style to
each heading element in your document.
• Adding CSS rules to every HTML element is time-consuming
and makes your HTML structure messy.
• Styling multiple elements can affect your page's size and
download time.
INTERNAL CSS
An internal style sheet may be used if one single HTML page has a unique style.
The internal style is defined inside the <style> element, inside the head section.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
SOURCE CODE OUTPUT
ADVANTAGES OF INTERNAL CSS
DISADVANTAGES OF INTERNAL CSS
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor, and must be
saved with a .css extension.
The external .css file should not contain any HTML tags.
Here is how the "mystyle.css" file looks like:
"mystyle.css"
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value and the unit
(such as margin-left: 20 px;). The correct way is: margin-left: 20px;
SOURCE CODE OUTPUT
ADVANTAGES OF EXTERNAL CSS
▪ Thus, they allow you to control the look and feel of several
documents in one go and do not need to define aa specific style
for every element.
▪ They allow you to easily group your styles in a more efficient way.
DISADVANTAGES OF EXTERNAL CSS