Welcome to our comprehensive guide on HTML Styles! In this lesson, we'll dive deep into the world of styling your web pages with CSS, a language that works hand-in-hand with HTML. By the end of this tutorial, you'll be able to transform your static HTML pages into visually appealing, interactive websites! 💡
In HTML, structure is king, but style is the crown that makes your web pages stand out. HTML provides the content's skeleton, while CSS adds the color, layout, fonts, and animations that bring your pages to life.
To add styles to your HTML pages, you'll use the <style> tag. Here's a simple example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Styled Page</title>
<style>
body {
background-color: lightblue;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is some example text.</p>
</body>
</html>In this example, the <style> tag is used to define the styling for our web page. The body element is selected, and we set its background color and font family.
Selectors are used to target HTML elements to apply styles. The most common types of selectors are:
div, p, or h1.class="my-class".id="my-id".Which selector would you use to style all `p` elements on a page?
There are numerous CSS properties to style your web pages. Here are some of the most common:
color: red;background-color: blue;font-family: Arial, sans-serif;margin: 10px;padding: 10px;What does the `margin` property do in CSS?
Styling links can make your navigation more engaging. Here's an example:
<style>
a {
color: white;
text-decoration: none;
background-color: blue;
padding: 5px 10px;
border-radius: 5px;
}
a:hover {
background-color: red;
}
</style>In this example, we've created a custom look for our links by targeting the a element. The :hover pseudo-class lets us style the link when it's being hovered over.
Responsive design ensures that your web pages look great on various devices, from phones to desktops. The @media rule helps with this:
<style>
@media (max-width: 600px) {
body {
font-size: 16px;
}
}
</style>In this example, the @media rule applies the styles within it only when the viewport width is 600 pixels or less.
What does the `@media` rule do in CSS?
By now, you should have a solid understanding of styling your HTML pages with CSS. Happy coding, and remember to keep learning and experimenting! 🤗
What are some best practices to follow when writing CSS?