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 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.
.header {
background-color: #f4f4f4;
padding: 20px;
text-align: center;
}
.highlight {
color: red;
font-weight: bold;
}
header class applies a light gray background, padding, and centers the header text.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.<div> tag occupies all the available width of the page.<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.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>| Characteristic | <div> | <span> |
|---|---|---|
| Element type | Block | Inline |
| Main usage | Section organization | Text formatting |
| Layout interruption | Yes | No |
Create a simple web page containing a header, a main section and highlighted words.
<!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>
<div> and <span>.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 !