Styling HTML With CSS
Styling HTML With CSS
Styling HTML With CSS
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
h1, h2, p { text-align: center; color: red;
}
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a
later date.
Comments are ignored by browsers.
Example
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
p{
color: red;
/* This is a single-line comment */ text-
align: center;
}
CSS can be added to HTML in the following ways:
Inline - using the style attribute in HTML elements
Internal - using the <style> element in the <head> section
External - using an external CSS file
The preferred way to add CSS to HTML is to put CSS syntax in separate CSS files.
Inline Styles
An inline style can be used if a unique style is to be applied to one single occurrence of
an element.
To use inline styles, use the style attribute in the relevant tag. The style attribute can contain
any CSS property.
The example below shows how to change the text color and the left margin of a paragraph:
<p style="color:blue;margin-left:20px;">This is a paragraph.</p>
HTML Style Example - Background Color
The background-color property defines the background color for an element: Example
<!DOCTYPE html>
<html>
<body style="background-color:yellow;">
<h2 style="background-color:red;">This is a heading</h2>
<p style="background-color:green;"> This is a paragraph.</p>
</body>
</html>
Internal Style Sheet
An internal style sheet can be used if one single document has a unique style. Internal
styles are defined in the <head> section of an HTML page, by using the <style> tag, like
this:
<head> <style> body {background-
color:yellow;} p {color:blue;}
</style>
</head>
External Style Sheet
An external style sheet is ideal when the style is applied to many pages.
External Style Sheets can save a lot of work
External Style Sheets are stored in CSS files
With an external style sheet, you can change the look of an entire Web site by changing
one file.
Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the
<head> section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>