DEV Community

Cover image for HTML Tutorials: Enhancing Web Pages with CSS #2
Zahir Hadi Athallah
Zahir Hadi Athallah

Posted on • Updated on

HTML Tutorials: Enhancing Web Pages with CSS #2

Introduction:

Welcome to Part 2 of our HTML tutorials! In this article, we will explore the world of CSS (Cascading Style Sheets) and learn how to enhance the visual presentation of our web pages. CSS allows us to apply styles, such as colors, fonts, and layouts, to our HTML elements. By combining HTML with CSS, we can create visually appealing and engaging web pages. Let's dive in!

A. Linking CSS to HTML:

To apply CSS styles to an HTML page, we need to link our CSS file to the HTML document. We do this by using the tag within the

section of our HTML file. Here's an example:
<!DOCTYPE html>
<html>
<head>
    <title>My Web Page</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- HTML content goes here -->
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In the example above, we have linked an external CSS file called "styles.css" to our HTML page. The CSS file contains all the styling instructions for our HTML elements.

B. Applying Styles:

CSS styles can be applied to HTML elements using selectors. Selectors target specific elements on the page and define the styles to be applied. Here are some commonly used CSS selectors:

  • Element Selector: Selects elements by their HTML tag name. For example, to style all paragraphs, we use the p selector.
p {
    color: blue;
    font-size: 16px;
}
Enter fullscreen mode Exit fullscreen mode
  • Class Selector: Selects elements based on their assigned class attribute. To apply a class to an HTML element, we use the class attribute. Multiple elements can share the same class. For example, to style all elements with the class "highlight", we use the .highlight selector.
.highlight {
    background-color: yellow;
    font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode
  • ID Selector: Selects a single element based on its unique ID attribute. To apply an ID to an HTML element, we use the id attribute. For example, to style an element with the ID "logo", we use the #logo selector.
#logo {
    width: 200px;
    height: 100px;
}
Enter fullscreen mode Exit fullscreen mode

C. Common CSS Properties:

CSS offers a wide range of properties to control the appearance of HTML elements. Here are some commonly used properties:

  • color: Sets the text color.
  • background-color: Sets the background color.
  • font-size: Sets the font size.
  • font-family: Sets the font family.
  • padding: Sets the padding (space) inside an element.
  • margin: Sets the margin (space) outside an element.
  • border: Sets the border properties.

Example :

h1 {
    color: red;
    font-size: 24px;
}

.container {
    background-color: lightgray;
    padding: 10px;
    margin: 20px;
    border: 1px solid black;
}
Enter fullscreen mode Exit fullscreen mode

D. CSS Box Model:

The CSS Box Model is an important concept to understand. It describes the structure of an HTML element, which consists of content, padding, border, and margin. By adjusting these properties, we can control the spacing and layout of elements on the page.

Example:

.box {
    width: 200px;
    height: 200px;
    padding: 20px;
    border: 1px solid black;
    margin: 10px;
}
Enter fullscreen mode Exit fullscreen mode

E. CSS Layouts:

CSS provides various layout techniques to arrange elements on the page. Some commonly used layout properties are:

  • display: Controls how elements are displayed (e.g., block, inline, flex).
  • position: Positions elements on the page (e.g., relative, absolute, fixed).
  • float: Floats elements to the left or right of their container.
  • flexbox: Provides flexible box layout capabilities.
  • grid: Enables grid-based layout.

Example:

.container {
    display: flex;
    justify-content: center;
    align-items: center;
}
Enter fullscreen mode Exit fullscreen mode

Closing:

CSS is a powerful tool that allows us to style our HTML pages and create visually appealing websites. In this tutorial, we learned how to link CSS to HTML, apply styles using selectors, use common CSS properties, understand the CSS Box Model, and explore CSS layouts. With this knowledge, you can now take your web development skills to the next level and create stunning web pages. Happy coding!

Top comments (0)