样式表可以实现哪些HTML无法实现的功能?


开发者可以使用CSS来描述网页内容的呈现方式。样式表包含CSS代码,我们将它与网页链接起来,以更好地呈现网页。

为什么我们需要在HTML网页中使用样式表?

对于初学者来说,首先想到的问题是为什么我们需要使用样式表?我们能否不使用样式表创建网页?答案是肯定的。你可以不使用样式表创建网页。

HTML仅用于向网页添加内容,它准备网页的结构。我们需要描述网页内容以更好地呈现并吸引访问者,这可以通过CSS实现。

通过在样式表中添加CSS代码,我们可以设置网页的字体样式、div元素样式,以及创建网页卡片等等。

语法

用户应该遵循以下语法在样式表中编写CSS代码。

Selector {
   /* CSS code */
}

在这里,我们需要在CSS代码的声明块中编写CSS选择器和CSS代码。此外,用户可以为多个HTML元素编写CSS代码。

示例1(基本样式)

在下面的示例中,我们演示了如何将样式表与HTML内容一起使用。这里,我们使用了嵌入式样式表来更改HTML元素的表示。

这里,我们创建了两个div元素并为其设置样式。用户可以移除与div元素相关的样式,并观察其外观。

<html>
<head>
   <style>
      body {background-color: aliceblue;}
      .first {
         height: 150px;
         width: 300px;
         font-size: 1.3rem;
         font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
         color: white;
         background-color: blueviolet;
      }
      .second {
         height: 100px;
         width: 400px;
         font-size: 1.3rem;
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
         color: red;
         margin: 10px;
         background-color: green;
      }
   </style>
</head>
<body>
   <h2>Using the <i> stylesheet </i> to apply basic styles to the webpage content </h2>
   <div class = "first"> Hello! How are you? </div>
   <div class = "second"> Welcome to the TutorialsPoint! </div>
</body>
</html>

示例2(动画和过渡)

在下面的示例中,我们演示了如何使用样式表向HTML元素添加动画。我们创建了一个div元素并使用CSS对其进行样式设置。

此外,我们还为动画添加了“bounce”关键帧。在“bounce”关键帧中,我们更改元素的水平位置,使其具有动画效果。

<html>
<head>
   <style>
      .animate {
         width: 300px;
         height: 300px;
         background-color: burlywood;
         border: 2px dotted olive;
         border-radius: 10px;
         animation: bounce 2s ease-in-out 0.1s infinite;
      }
      @keyframes bounce {
         0% {transform: translateX(0px);}
         40% {transform: translateX(30px);}
         80% {transform: translateX(20px);}
         90% {transform: translateX(15px);}
         100% {transform: translateX(00px);}
      }
   </style>
</head>
<body>
   <h2>Using the <i> stylesheet </i> to add animation to HTML content </h2>
   <div class = "animate"> </div>
</body>
</html>

示例3(响应式设计)

在下面的示例中,我们演示了如何通过向HTML内容添加样式表来创建响应式设计。这里,我们为父元素设置了尺寸和一些基本样式。此外,我们还为子div元素设置了尺寸。

之后,我们使用媒体查询使网页具有响应性。如果屏幕尺寸小于720像素,我们将更改div元素的尺寸,用户可以在输出中观察到这一点。

<html>
<head>
   <style>
      .parent {
         width: 50%;
         height: 70%;
         background-color: blueviolet;
         border: 2px dotted green;
         border-radius: 12px;
         margin: 0 auto;
      }
      .child {
         width: 50%;
         height: 50%;
         background-color: yellow;
         border-radius: 12px;
         margin: 15% auto;
      }
      @media screen and (max-width: 720px) {
         .parent { width: 100%; height: 70%;}
         .child { width: 80%; height: 50%;}
      }
   </style>
</head>
<body>
   <h2> Using the <i> stylesheet </i> to make a responsive design </h2>
   <div class = "parent">
      <div class = "child">
      </div>
   </div>
</body>
</html>

示例4(布局控制)

在下面的示例中,我们演示了如何通过样式表控制HTML元素的布局。这里,我们有一个“container” div元素,其宽度为屏幕总宽度的80%。此外,我们使用了“display: flex” CSS属性。

对于“block1”元素,我们使用了“flex: 1”;对于“block2”元素,我们使用了“flex: 2” CSS属性。这里,用户可以观察到block1占据33.33%的空间,而block2占据66.67%的总空间。

<html>
<head>
   <style>
      .container {
         width: 80%;
         height: 300px;
         display: flex;
         border: 3px solid pink;
      }
      .block1 {
         flex: 1;
         background-color: blue;
      }
      .block2 {
         flex: 2;
         background-color: red;
      }
   </style>
</head>
<body>
   <h2> Using the <i> stylesheet </i> to make responsive design </h2>
   <div class = "container">
      <div class = "block1"> </div>
      <div class = "block2"> </div>
   </div>
</body>
</html>

用户学习了将样式表与HTML一起使用的益处。我们可以更好地呈现HTML内容,而这仅使用普通的HTML是无法实现的。此外,我们还可以使用样式表使网页更具吸引力。

更新于:2023年4月27日

89 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.