微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们如何使用分割标签来为HTML元素设置样式?

标记用作 HTML 元素的容器。借助此标签,我们可以轻松定义 HTML 文档的一部分。它还用于将大部分 HTML 元素分组在一起并轻松格式化它们。 标签与块级元素一起使用。

标记接受所有 CSS 属性,并使用 classid属性设置其中元素的样式。

我们如何使用分割标签来为HTML元素设置样式?

语法

以下是 标记的语法。

<div class='division'>Content…</div>

示例 1

下面给出了一个在 HTML 中向 div 标签添加样式的示例。

<!DOCTYPE html>
<html>
<head>
   <Meta charset="UTF-8">
   <Meta http-equiv="X-UA-Compatible" content="IE=edge">
   <Meta name="viewport" content="width=device-width, initial-scale=1.0">
   <style>
      .parent {
         border: 1rem solid green;
         margin: 1rem;
         padding: 1rem 1rem;
         text-align: center;
         Box-shadow: 2px 2px 20px 23px aquamarine;
      }
      .division {
         display: inline-block;
         border: 1px solid aquamarine;
         padding: 1rem 1rem;
         background-color: #2ecc71;
         color: white;
      }
   </style>
</head>
<body>
   <div class='parent'>
      <div class='division'>div tag 1</div>
      <div class='division'>div tag 2</div>
      <div class='division'>div tag 3</div>
   </div>
</body>
</html>

以下是上述示例程序的输出

我们可以向标签添加更多样式。

示例 2

下面给出了在 HTML 中向 div 标记添加样式的另一个示例。

<!DOCTYPE html>
<html>
<head>
   <Meta charset="UTF-8">
   <Meta http-equiv="X-UA-Compatible" content="IE=edge">
   <Meta name="viewport" content="width=device-width, initial-scale=1.0">
   <style>
      .parent {
         border: 1rem solid green;
         margin: 1rem;
         padding: 1rem 1rem;
         text-align: center;
         Box-shadow: 2px 2px 20px 23px aquamarine;
      }
      .division {
         display: inline-block;
         border: 1px solid aquamarine;
         padding: 1rem 1rem;
         background-color: #2ecc71;
         color: white;
         text-transform: uppercase;
         text-decoration: underline;
         font-family: cursive;
         font-size: 1.2rem;
         font-weight: bolder;
         font-style: italic;
      }
   </style>
</head>
<body>
   <div class='parent'>
      <div class='division'>div tag 1</div>
      <div class='division'>div tag 2</div>
      <div class='division'>div tag 3</div>
   </div>
</body>
</html>

以下是上述示例程序的输出

示例 3

您可以尝试运行以下代码以使用 标记设置 HTML 元素的样式。添加的样式规则将应用于 id=”content” 的元素。这里的 id 是 CSS 选择器。

<!DOCTYPE html>
<html>
<head>
   <style>
      #container p {
         line-height: 15px;
         margin: 20px;
         padding-bottom: 15px;
         text-align: justify;
         width: 130px;
         color: blue;
      }
   </style>
   <title>HTML div Tag</title>
   <link rel = "stylesheet" href = "style.css">
</head>
<body>
   <div id = "container">
      <p>Welcome to our website. We provide tutorials on varIoUs subjects.</p>
   </div>
</body>
</html>

以上就是我们如何使用分割标签来为HTML元素设置样式?的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐