DIV and SPAN Tags
Introduction
In website creation, HTML tags play a crucial role in structuring and styling content. Among them, the <div>
and <span>
tags stand out for their flexibility and versatility, especially when combined with CSS classes. This course will introduce you to the interest of these tags and their use to build modern and adapted sites.
<div>
and <span>
tags, let's talk about CSS classes, their usefulness, and see an example that we will use below.CSS Classes and their usefulness
CSS classes are essential for styling <div>
and <span>
tags. A class is an HTML attribute that serves to identify one or more elements to apply custom styles to them.
Example of CSS declaration:
.header {
background-color: #f4f4f4;
padding: 20px;
text-align: center;
}
.highlight {
color: red;
font-weight: bold;
}
Effect of classes:
- The
header
class applies a light gray background, padding, and centers the header text. - The
highlight
class makes the text red and bold.
<div>
Tag
<div>
tag is a container-type tag that serves to group HTML elements. It is often used to structure sections of a web page.Main characteristics:
- Block: The
<div>
tag occupies all the available width of the page. - Organization: It allows creating logical divisions in the content.
- Usage: It is often used as an application point for CSS styles.
Example:
<div class="header">
<h1>Welcome to my site</h1>
<h2>Site made by myself!</h2>
</div>
Here, the header
class applied to the <div>
allows styling the page header.
<span>
Tag
<span>
tag is an inline tag that serves to style a specific portion of text or an HTML element without disrupting the content flow.Main characteristics:
- Inline: It does not interrupt the text flow.
- Flexibility: It is ideal for applying styles to specific words or phrases.
Example:
This site is extraordinary!
<p>This site is <span class="highlight">extraordinary</span>!</p>
In this example, the highlight
class applied to the <span>
allows highlighting the word "extraordinary".
<div>
and <span>
Comparison between
Characteristic | <div> | <span> |
---|---|---|
Element type | Block | Inline |
Main usage | Section organization | Text formatting |
Layout interruption | Yes | No |
Practical Workshop
Objective:
Create a simple web page containing a header, a main section and highlighted words.
HTML code to create:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DIV and SPAN Tags</title>
<style>
.header {
background-color: #333;
color: white;
text-align: center;
padding: 10px;
}
.highlight {
color: blue;
font-style: italic;
}
</style>
</head>
<body>
<div class="header">
<h1>Welcome to our site</h1>
</div>
<div class="content">
<p>Learn to use <span class="highlight">HTML</span> tags like a pro.</p>
</div>
</body>
</html>
Instructions:
- Create this code in an HTML file.
- Test it in your browser.
- Experiment by modifying the CSS styles or adding other
<div>
and<span>
.
Conclusion
The <div>
and <span>
tags are essential tools for structuring and styling your web pages. By associating them with CSS classes, you can create elegant and functional designs while maintaining a clear organization of your code. Try using them in your own projects to discover their full potential !