Chapter 14: Using HTML with CSS

CSS provides styles to HTML elements on the page. Inline styling involves usage of the style attribute in tags, and is highly discouraged. Internal stylesheets use the <style> tag and are used to declare rules for directed portions of the page. External stylesheets may be used through a <link> tag which takes an external file of CSS and applies the rules to the document. This topic covers usage of all three methods of attachment.

External Stylesheet Use

Use the link attribute in the document’s head:

<head>
 <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

You can also use stylesheets provided from websites via a content delivery network, or CDN for short. (for example,
Bootstrap):

Advertisement
<head>
 <link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-
BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

Generally, you can find CDN support for a framework on its website.

Internal Stylesheet

You can also include CSS elements internally by using the <style> tag:

<head>
 <style type="text/css">
 body {
 background-color: gray;
 }
 </style> 
</head>

Multiple internal stylesheets can be included in a program as well.

Advertisement
<head>
 <style type="text/css">
 body {
 background-color: gray;
 }
 </style>
 <style type="text/css">
 p {
 background-color: blue;
 }
 </style> 
</head>

Inline Style

You can style a specific element by using the style attribute:

<span style="color: red">This text will appear in red.</span>

Note: Try to avoid this — the point of CSS is to separate content from presentation.

Multiple Stylesheets

It’s possible to load multiple stylesheets:

Advertisement
<head>
 <link rel="stylesheet" type="text/css" href="general.css">
 <link rel="stylesheet" type="text/css" href="specific.css">
</head>

Note that later files and declarations will override earlier ones. So if general.css contains:

body {
 background-color: red;
}

and specific.css contains:

body {
 background-color: blue;
}

if both are used, the background of the document will be blue.

Advertisement

Recent Posts

Securing phpMyAdmin Like a Pro: Essential Tips and Tricks

Securing phpMyAdmin is crucial to prevent unauthorized access and protect your databases. Here's a guide…

5 months ago

Pasqal raises $100M to build a neutral atom-based quantum computer

Pasqal, a Paris-based quantum computing startup, today announced that it has raised a $100 million…

1 year ago

Apple in talks with Disney, others on VR content for new headset: Report

Developed with Sony Group Corp, the headset will have two ultra-high-resolution displays to handle the…

1 year ago

Microsoft, Amazon results to highlight softening cloud business

After years of blistering growth, most recently fuelled by remote working and studying during the…

1 year ago

Intel chairman Omar Ishrak steps down

Omar Ishrak had stepped down and the chipmaker appointed board director Frank Yeary as his…

1 year ago

Canada to commercialise world's first photonic-based quantum computer

Canadian Prime Minister Justin Trudeau has announced a new federal investment to build and commercialise…

1 year ago