HTML 中的<span>标签被视为内联元素。它类似于 div 标记,但 div 标记特意用于块级元素,而 span 用于内联元素。它主要用于用户想要将内联元素分组到其代码结构中。HTML 中的 Span 标记用于通过使用元素类或 id 属性为特定内容提供样式。使用 HTML 文档中的 span 标记本身无法进行视觉更改。它用作 HTML 文档中的内联标记。在代码中使用 span 标记有助于减少代码和 HTML 属性。

语法与示例

定义 span 标记的语法如下:

语法:

<span class=""> Contents </span>

例:

<!DOCTYPE html>
<html>
<head>
<title>Span tag in HTML </title>
<style>
.demo {
color: blue;
font-size: 200%;
position: relative;
top: 5px;
}
</style>
</head>
<body>
<p><span class="demo">O</span>Pride make us artificial and Humility make us real.</p>
<p>True fact about life </p>
</body>
</html>

输出:

span标记在 HTML 中的属性

下面是一些用于应用<span>样式的属性。具体如下:

HTML中的span标记示例

下面给出了 html 中 span 标记的示例:

示例 #1

代码:

<!DOCTYPE html>
<html>
<head>
<title>HTML Span Tag</title>
</head>
<style>
.imgdemo {
padding-left:25px;
background:url(./Content/data/2.jpg) no-repeat top left;
display: inline-block;
height: 150px;
width: 150px;
}
</style>
<body>
<!-- span tags with inline style/css  -->
<h2>Span tag with text color</h2>
<span style="color:brown;">
Do those things which makes your soul happy </span>
<br>
<h2> Span tag with background color</h2>
<span style="background-color:lightblue;">
Everybody have natural beauty. Try look at your picture when you are a baby. Your     eyes, eyebrows, lips, body. When you are smiling, crying, staring, etc. That it, you got     it.</span>
<br>
<h2> Span tag with background image</h2>
<br>
<span class="imgdemo" style="color: azure; font-size: 16px; font-display: block;">
Image as background</span>
</body>
</html>

输出:

示例 #2

代码:

<!--Example 2-->
<!DOCTYPE html>
<html>
<head>
<title>HTML Span Tag</title>
</head>
<body>
<h2> Span tag Examples</h2>
<p>Good, Better, Best Never let it rest.
<span style="color:crimson;"> "Till your good is better and your better is best" </span></p>
<p>Everyday you have a choice -<span style="font-family: cursive; font-weight: 200; font-   size: 18px;">
STAY THE SAME OR CHANGE    </span>
</p>
<p>Whenever you see a successful person you only see the public glories, never the private sacrifices to reach them."<span style="background-color: aqua ; color:darkblue;">
"Opportunities don't happen, you create them" </span></p>
</body>
</html>

输出:

示例 #3

代码:

<!DOCTYPE html>
<html>
<style>
.spandemo {
background:url(./Content/data/3.jpg) no-repeat top left;
display: inline-block;
padding-top: 20px;
width: 1800px;
height: 500px;
}
</style>
<body>
<h2> Span tag for image</h2> <br>
<span class="spandemo" style="color: aliceblue; font-weight: bold;">
<p> Nature always wears<br> Color of SPIRIT. </p> <br>
Heaven is under our feet <br>as well as over our heads <br>
<p> Deep in their roots,<br>all flowers Keep the light </p>
<p> My soul steers me<br> into nature's silence</p>
</span>
</body>
</html>

输出:

结论

从上述所有信息中,我们了解到HTML中的<span>标记用于为内联元素提供样式。可以对此样式属性进行分组,并以内联方式指定它们。Span标签主要用于在内联CSS的帮助下在我们的网页上排列结构部分和适当的布局部分。

发表回复