Introduction To CSS: There Are 3 Ways To Apply CSS To HTML
Introduction To CSS: There Are 3 Ways To Apply CSS To HTML
CSS (cascading style sheets) is a stylesheet language that is used to define the presentation of a document written in
a markup language such as HTML and XHTML, but the language can be applied to any kind of XML document. For
example, it can be used to define the colors, fonts and layout of a web page. A cascading style sheet has a file
extension of ".css".
The CSS specifications are maintained by the World Wide Cosortium (W3C).
An internal style sheet is only placed within the web page which they are intended for.
The styles are placed within the openning and closing <head> tags and the openning and closing <style> tags enclose
all of the styles for the web page.
The following HTML and CSS code is an example of an internal style sheet that will make the text within the
paragraphs on that web page red.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
p
{
color: red;
}
</style>
</head>
<body>
<p>
the text in this paragraph will be red
</p>
<p>
the text in this paragraph is also going to be red
</p>
</body>
</html>
External Style Sheets
External style sheets are styles that can be applied to as many web pages as you like that are on the same website.
With external style sheets there are at least 2 documents: the document that the styles are on and the the web page
that the styles apply to (of course you can apply the styles to multiple web pages on the same site).
The web page(s) need to link to the style sheet by using the following code within the openning and closing <head>
tags.
Now open a new (blank) document and copy and paste below HTML code into the document and save it as
"whatever.html".
<head>
<title>this is the title of the web page</title>
</head>
<body>
<p>
the text in this paragraph is red
</p>
<p>
I now know HTML and CSS
</p>
</body>
</html>
After you have saved the above HTML code as "whatever.html", open another new (blank) document and copy and
paste the following CSS code.
p
{
color: red;
}
Now upload both files to your website. The web page should have the following 2 paragraphs in it and the color of
the text should be red.
note: if you are not connected to the internet, you should link to the style sheet something like:
file:///C:/Documents/mystylesheet.css depending on where on your computer the file is located.
To use in-line styles you place the "style" attribute within the relevant HTML tag. The style attribute can contain any
CSS property.
An in-line style can apply to a whole HTML element or it can apply to a selected area within that element. For
example it can apply to a whole paragraph or it can apply to only a string of text within the paragraph.
The following HTML and CSS code shows you how to apply in-line styles.
The above HTML and CSS code will make a web page with the following 2 paragraphs.
this is the best CSS and HTML tutorial available on the world wide web
The following HTML and CSS code example shows you how to set the background color of an element.
The HTML and CSS code example below shows you how to set an image as the background.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
body
{
background-image:
url('csshtmltutorial-image.jpg')
}
</style>
</head>
<body>
</body>
</html>
The HTML and CSS code below shows you how to repeat a background image.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
body
{
background-image:
url('csshtmltutorial.jpg');
background-repeat: repeat;
}
</style>
</head>
<body>
</body>
</html>
The following link is to the web page that the above HTML and CSS code makes:
HTML and CSS background example
How to repeat an image vertically only
The HTML and CSS code example below shows you how to repeat a background image vertically only.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
body
{
background-image:
url('csshtmltutorial.jpg');
background-repeat: repeat-y;
}
</style>
</head>
<body>
</body>
</html>
The HTML and CSS code example below shows you how to repeat a background image horizontally only.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
body
{
background-image:
url('csshtmltutorial.jpg');
background-repeat: repeat-x;
}
</style>
</head>
<body>
</body>
</html>
How to repeat one background image at a time
The HTML and CSS code example below shows you how to display one background image at a time.
<style type="text/css">
body
{
background-image:
url('csshtmltutorial.jpg');
background-repeat: no-repeat;
}
</style> </head> <body> </body> </html>
The values that can be used for the background-position property are:
• top left
• top center
• top right
• center left
• center center
• center right
• bottom left
• bottom center
• bottom right
• x% y%
• xpos ypos
The HTML and CSS code example below shows you how to place a background image on a web page.
The HTML and CSS code example below shows you how to place a background image on a web page using percent.
The HTML and CSS code example below shows you how to place a background image on a web page using pixels.
The HTML and CSS code example below shows you how to place a fixed background image.
The below HTML and CSS code example shows you how to set the color of text with CSS.
The above HTML and CSS code makes the level 1 and 2 headings and the paragraph below.
The below HTML and CSS code example shows you how to set the background color of text with CSS.
This is a text. This is more text. This is more text. This is more text. This is more text. This is more text. This is more
text. This is more text. This is more text. This is more text. This is more text. This is more text. This is more text.
This is more text. This is more text. This is more text. This is more text. This is a text.
The HTML and CSS code example below shows you how to increase or decrease the space between characters.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
h1
{letter-spacing: -3px;}
h3
{letter-spacing: 0.5cm;}
</style>
</head>
<body>
<h1>This is a level 1 header</h1>
<h3>This is a level 3 header</h3>
</body>
</html>
The above HTML and CSS code make the level 1 and 2 headings below.
The HTML and CSS code example below shows you how to specify the space between lines.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
p.one
{
line-height: 90%; }
p.two
{
line-height: 200%;
}
</style>
</head>
<body>
<p>
This is a paragraph with a standard line-height. The default line height in most browsers is about 110% to 120%. This
is a paragraph with a standard line-height. This is a paragraph with a standard line-height.
</p>
<p class="one">
This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a
paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This
is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.
</p>
<p class="two">
This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a
paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This
is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.
</p>
</body>
</html>
The above HTML and CSS code example makes the 3 paragraphs below.
This is a paragraph with a standard line-height. The default line height in most browsers is about 110% to 120%. This
is a paragraph with a standard line-height. This is a paragraph with a standard line-height.
This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a
paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This
is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.
This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a
paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This
is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.
CSS Font Code Tutorial
You can define the font of text with the use of CSS font properties.
The below HTML and CSS code example shows you how to set the font of text with CSS.
The above HTML and CSS code produce the following examples, with each having a different font style.
This is a level 3 header
<head>
<title>this is the title of the web page</title>
<style type="text/css">
</style>
</head>
<body>
<p>
This is a normal paragraph </p>
<p style="font: caption">
This is a paragraph with a "caption" font
</p>
</body>
</html>
The above HTML and CSS code makes the below paragraph.
<head>
<title>this is the title of the web page</title>
<style type="text/css">
h3 {font-size: 150%;}
h4 {font-size: 130%;}
p {font-size: 100%;}
</style>
</head>
<body>
<h3>
This is a level 3 header
</h3>
<h4>
This is a level 4 header
</h4>
<p>
This is a paragraph
</p>
</body>
</html>
The above HTML and CSS code produce the following examples.
The HTML and CSS code above will produce the paragraphs below.
The HTML and CSS code example above make the following paragraphs.
The above HTML and CSS code produce the 2 paragraphs below.
The above HTML and CSS code example produces the 3 paragraphs below.
This example shows you how to set all of the padding properties in one declaration in an HTML element.
This example shows you how to set all of the padding properties in one declaration in an HTML element.
</head>
<body>
<table border="1">
<tr>
<td class="padding1">
This is a tablecell with equal padding of 1cm on each side.
</td>
</tr>
</table>
<table border="1">
<tr>
<td class="padding2">
This is a tablecell with equal padding of 2cm on each side.
</td>
</tr>
</table>
<table border="1">
<tr>
<td class="padding3">
This tablecell has a top and bottom padding of 0.5cm and a left and right padding of 2cm.
</td>
</tr>
</table>
<table border="1">
<tr>
<td class="padding4">
This tablecell has a top and bottom padding of 0.5cm and a left and right padding of 4cm.
</td>
</tr>
</table>
</body>
</html>