In the previous task, we learned how to add interactivity to our HTML page using JavaScript. In this task, we will learn how to style our HTML page using CSS.
CSS, which stands for Cascading Style Sheets, is a styling language used to describe how colors, fonts, and layouts are presented in markup languages like HTML. It plays a crucial role in web development by allowing developers to add visual styling to their web pages.
You can include CSS directly in your HTML file using the style
attribute, or you can create a separate CSS file and link it to your HTML file using the <link>
tag. We will explore both methods in this task.
Inline styles can be added to an HTML element using the style
attribute. Here’s an example:
This code centers the text of the entire body and makes the text color of the heading red.
Aside: I again added the sample sleep cycles to the HTML.
Notice the syntax used to declare the styles: style="text-align: center;"
and style="color: red;"
. This is inline CSS and is used to apply styles directly to specific HTML elements.
Inline styles can be used for quick and simple styling changes, but they can become difficult to manage as the page grows in complexity. It is better to use a separate CSS stylesheet for larger projects.
Remove the style attributes added to the <body>
and <h1>
tags previously. Then, define the styles inside <style></style>
tags, inside the <head>
section of HTML:
This stylesheet defines styles for the body, h1, and p elements. The background-color
style sets the background color of the body to a light gray, while the color
style sets the text color to a dark gray. The text-align
style centers the text, and the line-height
style sets the line height of the p element to 1.5 times the font size.
To select different HTML elements in CSS, you use a selector followed by curly braces containing one or more property-value pairs.
The selector can be an element name, class name, ID, or a combination of these. In the previous example, we used the element names body
, h1
, and p
. For instance, the style declared for the p
element will apply to all p
elements in our HTML page.
Add the following to the style
tags:
The CSS code above defines the styles for two selectors: #calc-btn
and .cycle
.
#calc-btn
styles the “Calculate” button, setting its font size, padding, border, background color, and color. It also sets the cursor to “pointer” to indicate that it is clickable. Pay attention to the leading #
in this case. It is used to select an element with a specific id
attribute. In this case, the id
is calc-btn
.
#calc-btn:hover
styles the button on hover, changing its background color and text color. .cycle
styles the wake-up times, setting their font size, padding, border, background color, color, and margin. Pay attention to the leading .
in this case. It is used to select elements with a specific class
attribute. In this case, the class
is cycle
.
Refresh the index.html
file in your browser to see the updated styles.
Style tag is a good way to add styles to your HTML document. However, it is still better to separate the styles into a separate CSS file for better organization and maintainability.
CSS stylesheets can also be defined in a separate file with a .css
extension and linked to an HTML document using a <link>
tag in the <head>
section.
Create an index.css
file with the following content:
This CSS file contains the same styles as the previous example, but they are defined in a separate file.
Now remove the style
element from the index.html
file and instead add the following link
element:
The <link>
tag is used to link an external resource, such as a CSS stylesheet, to an HTML document. In this case, the rel
attribute specifies the relationship between the HTML and the linked resource. rel="stylesheet"
indicates that the linked resource is a stylesheet. The type
attribute specifies the MIME type of the linked resource, which is text/css
in this case. The href
attribute specifies the location of the linked resource. In this example, the linked resource is a file named index.css
in the same directory as the HTML document.
Using a separate CSS stylesheet is generally a better practice than using inline styles or styles defined within a <style>
tag. It allows for easy maintenance and updates to the styling of the page and improves the separation of content and presentation.
There are best practices and coding conventions when it comes to authoring HTML documents. A good starting point is given on W3School’s HTML Style Guide. You can search online for more resources. (You will find many).
We’ve already followed many of the points made in W3School’s HTML Style Guide. We will polish our work by incorporating a few that we’ve left out.
lang
attribute to the <html>
tag:<meta>
tag to declare the character encoding used in your HTML document. (Place it right after the <head>
opening tag.)To ensure proper interpretation and correct search engine indexing, both the language and the character encoding should be defined as early as possible in an HTML document.
<meta>
tag for setting the viewport.The viewport gives the browser instructions on how to control the page’s dimensions and to scale. It is needed for responsive web design, which aims to make your web page look good on all devices.
With these additions, our index.html
file should look like this:
With these changes, our HTML document is now more structured and follows best practices for authoring HTML documents.