【问题标题】:Can't change element properties in css using tag selectors [duplicate]无法使用标签选择器更改 CSS 中的元素属性 [重复]
【发布时间】:2021-07-22 00:21:59
【问题描述】:

我是 Web 开发领域的新手,我参加了 Angela Yu 的 Web 开发训练营。 我目前正在使用 bootstrap v5html5CSS3

我遇到了一个问题,当我使用标签选择定位时,我无法更改正文标签的属性

这是我的 index.html

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>TinDog</title>

  <!-- External style Sheet -->
  <link rel="stylesheet" href="css/styles.css">

  <!-- bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
  <!-- Google fonts -->
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Montserrat&family=Ubuntu:wght@500&display=swap" rel="stylesheet">
  <!-- font awesome -->
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous">
</head>

<body>
  <section id="title">
    <!-- Nav Bar -->
    <nav class="navbar navbar-expand-lg navbar-dark">
      <div class="container-fluid p-0 title-container">
        <a href="#" class="navbar-brand">tinCat</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
          <ul class="nav navbar-nav">
            <li class="nav-item"><a class="nav-link" href="">Contact</a></li>
            <li class="nav-item"><a class="nav-link" href="">Pricing</a></li>
            <li class="nav-item"><a class="nav-link" href="">Download</a></li>

          </ul>
        </div>
      </div>

    </nav>
    <!-- Nav Bar end -->


    <!-- Download Section -->
    <div class="container-fluid p-0 title-things">
      <div class="row">
        <div class="col-lg-6 ">
          <h1>Meet new and interesting cats nearby.</h1>
          <button type="button" class="btn btn-light download-button"><i class="fab fa-apple"></i> Download</button>
          <button type="button" class="btn btn-dark download-button"><i class="fab fa-google-play"></i> Download</button>
        </div>
        <div class="col-lg-2">
          <img class="cat-profile"src="images/iphone6.png" alt="iphone-mockup">
        </div>
      </div>

  </section>
  </div>



</body>

</html>

这是我的 Styles.css

body{
  font-family: 'Montserrat', sans-serif;
 
}

#title{
  padding: 3% 15%;
  background-color: #ff4c68;
  font-family: 'Montserrat', sans-serif;
  color: #ffffff;
}
.navbar-brand{
  font-family: 'Ubuntu', sans-serif;
  font-weight: bold;
  font-size: 2.5rem !important;

}

.cat-profile
{
  transform: rotate(10deg);
  width: 360px;
  margin-top: 20px;
}

.download-button{
  margin-top:  20px;

}

.title-things{
  margin-top: 100px ;
}

.title-container {
padding: 0% 15%;
}

/* .col{
  padding: 0px;
} */
/* .title-text
{
font-family: 'Montserrat', sans-serif;
font-weight: bold;
size : 3rem;
line-height: 1.5;
} */

相反我在标签中添加了一个新类,在外部样式表中,我使用类选择器定位它并且它起作用了!谁能解释一下原因?

【问题讨论】:

  • 你不应该使用 !important 除非你真的必须这样做。这看起来像您首先导入样式表。这样它们可能会被引导样式覆盖。尝试在引导 css 之后包含您的自定义样式。
  • 是的,我刚开始学习它 2 天,这是我想知道的事情,我们最后必须链接样式表,这给予了它最大的重要性。谢谢

标签: html css twitter-bootstrap bootstrap-5


【解决方案1】:

这完全取决于你的样式出现的顺序,所以如果你在 bootstrap 下添加你的样式,它将覆盖 bootstrap styles 。像这样更改代码,

        <!-- bootstrap -->
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
        <!-- Google fonts -->
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=Montserrat&family=Ubuntu:wght@500&display=swap" rel="stylesheet">
        <!-- font awesome -->
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous">

       <!-- External style Sheet -->
      <link rel="stylesheet" href="css/styles.css">

【讨论】:

  • 非常感谢,它对我有用,我很难弄清楚这件事,为什么它没有覆盖,再次感谢!
【解决方案2】:

将您的样式表放在其他样式表的下方

          <head>
            <meta charset="utf-8">
            <title>TinDog</title>

            <!-- bootstrap -->
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
            <!-- Google fonts -->
            <link rel="preconnect" href="https://fonts.gstatic.com">
            <link href="https://fonts.googleapis.com/css2?family=Montserrat&family=Ubuntu:wght@500&display=swap" rel="stylesheet">
            <!-- font awesome -->
            <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous">

          <!-- External style Sheet -->
          <link rel="stylesheet" href="css/styles.css">
          </head>

【讨论】:

  • 非常感谢!我才知道这玩意儿!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 2010-10-19
  • 2010-12-04
  • 2016-01-13
  • 1970-01-01
  • 2012-04-04
相关资源
最近更新 更多