【问题标题】:Why is CSS only calling part of a class name?为什么 CSS 只调用类名的一部分?
【发布时间】:2021-11-11 03:47:36
【问题描述】:

所以我目前正在学校研究幻灯片。他们指给我的地方是 W3Schools.com,下面的代码就是源自这里。

我感到困惑的是以下内容。在 div 标签中是“class="mySlides fade"。然而,在 CSS 文件中,该类没有在任何地方提及。但是提到的是 .mySlides 和 .fade

CSS 中的这些类是否与 div 标签中的相同?据我了解,您必须使用全名( .mySlides fade )。

以下是 HTML 代码:

<!DOCTYPE html>
<head>
    <meta charset = "utf-8">
    <script src="./js/main.js"></script>
    <link rel="stylesheet" href="./styles.css">
</head>

<body>

            <!-- Slideshow container -->
        <div class="slideshow-container">

            <!-- Full-width images with number and caption text -->
            <div class="mySlides fade">
            <div class="numbertext">1 / 3</div>
            <img src="img1.jpg" style="width:100%">
            <div class="text">Caption Text</div>
            </div>
        
            <div class="mySlides fade">
            <div class="numbertext">2 / 3</div>
            <img src="" style="width:100%">
            <div class="text">Caption Two</div>
            </div>
        
            <div class="mySlides fade">
            <div class="numbertext">3 / 3</div>
            <img src="img3.jpg" style="width:100%">
            <div class="text">Caption Three</div>
            </div>
        
            <!-- Next and previous buttons -->
            <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
            <a class="next" onclick="plusSlides(1)">&#10095;</a>
        </div>
        <br>
        
        <!-- The dots/circles -->
        <div style="text-align:center">
            <span class="dot" onclick="currentSlide(1)"></span>
            <span class="dot" onclick="currentSlide(2)"></span>
            <span class="dot" onclick="currentSlide(3)"></span>
        </div> 

</body>

下面是 CSS:

* {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Hide the images by default */
.mySlides {
  display: none;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4}
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4}
  to {opacity: 1}
}

【问题讨论】:

标签: html css


【解决方案1】:

你假设它是一个类名是错误的。空格允许您为同一个元素指定多个类名。

【讨论】:

    【解决方案2】:

    回答您的问题:不,您不必同时使用两者,而且它们不一样。基本上,它用于 CSS/HTML 中的可扩展性和可重用性。所以 CSS 所做的就是调用所有带有“fade”类的元素并给它一个特定的动作,“mySlides”类也是如此。每个在 CSS 中的样式不同,但应用于相同的元素。这有助于使代码更具可读性,还允许您将淡入淡出效果应用到其他元素而无需 mySlides 类,反之亦然。

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2021-11-25
      相关资源
      最近更新 更多