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

链接到单个 html 页面的两个 css 导致元素属性之间的覆盖

如何解决链接到单个 html 页面的两个 css 导致元素属性之间的覆盖

我有两个 css 文件 说 sidebar.css 和 topbar.css

当我将这些链接一个 html 文件中时,属性会被交换 假设如果我指定边栏的边距:0 和顶部栏的边距:30px,顶部栏也会被放置在 0px 处。并且内部的许多其他属性被覆盖等。我应该如何克服这个问题,有没有一种方法可以将这些代码单独链接到 html 文件中而不会出现此类问题?如果不是,我应该对代码进行哪些更改。

我有一个包含导航元素的网页,我在代码中插入了一个侧边栏,其中还包含一些导航和其他元素,这些元素对我的原始网页进行了更改。当我们链接两个css文件时,我们如何使它们独立和独立。

这是我的侧边栏代码

    <!DOCTYPE html>
    
    <html lang="en" dir="ltr">
       <head>
          <Meta charset="utf-8">
          <title>Sidebar Menu with sub-menu</title>
          <link rel="stylesheet" href="sidebarnewstyle.css">
          <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
          <link rel="stylesheet" href="sidebarnewstyle.css"/> <!-- href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"-->
       </head>
       <body>
    
        
          <div class="btn">
             <span class="fas fa-bars"></span>
          </div>
          <nav class="sidebar">
             <div class="text">
                Side Menu
             </div>
             <ul>
                <li class="active"><a href="#">Dashboard</a></li>
                <li>
                   <a href="#" class="feat-btn">RESULT
                   <span class="fas fa-caret-down first"></span>
                   </a>
                   <ul class="feat-show">
                      <li><a href="#">CSE</a></li>
                      <li><a href="#">ECE</a></li>
                      <li><a href="#">EEE</a></li>
                      <li><a href="#">IT</a></li>
                   </ul>
                </li>
    
                <li>
                   <a href="#" class="serv-btn">Subject-wise
                   <span class="fas fa-caret-down second"></span>
                   </a>
                   <ul class="serv-show">
                      <li><a href="#">CSE</a></li>
                      <li><a href="#">ECE</a></li>
                      <li><a href="#">EEE</a></li>
                      <li><a href="#">IT</a></li>
                   </ul>
                </li>
                <li><a href="#">Subject</a></li>
                <li><a href="#">Change Password</a></li>
                <li><a href="#">About</a></li>
             </ul>
          </nav>
          <div class="content">
             <div class="header">
               SYstem
            
          <script >
             $('.btn').click(function(){
               $(this).toggleClass("click");
               $('.sidebar').toggleClass("show");
             });
               $('.feat-btn').click(function(){
                 $('nav ul .feat-show').toggleClass("show");
                 $('nav ul .first').toggleClass("rotate");
               });
               $('.serv-btn').click(function(){
                 $('nav ul .serv-show').toggleClass("show1");
                 $('nav ul .second').toggleClass("rotate");
               });
               $('nav ul li').click(function(){
                 $(this).addClass("active").siblings().removeClass("active");
               });
          </script>
    
       </body>
    </html>

这是侧边栏对应的css文件

    @import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap');
    *{
      margin: 0;
      padding: 0;
      user-select: none;
      Box-sizing: border-Box;
      font-family: 'Poppins',sans-serif;
      z-index: 0;
    }
    
    .btn{
      position: absolute;
      top: 15px;
      left: 45px;
      height: 45px;
      width: 45px;
      text-align: center;
      background: #1b1b1b;
      border-radius: 3px;
      cursor: pointer;
      transition: left 0.4s ease;
    }
     .btn.click{
      left: 260px;
    }
     .btn span{
      color: white;
      font-size: 28px;
      line-height: 45px;
    }
     .btn.click span:before{
      content: '\f00d';
    }
     .sidebar{
      position: fixed;
      width: 250px;
      height: 100%;
      left: -250px;
      background: #1b1b1b;
      transition: left 0.4s ease;
    }
     .sidebar.show{
      left: 0px;
    }
     .sidebar .text{
      color: white;
      font-size: 25px;
      font-weight: 600;
      line-height: 65px;
      text-align: center;
      background: #1e1e1e;
      letter-spacing: 1px;
    }
     nav ul{
      background: #1b1b1b;
      height: 100%;
      width: 100%;
      list-style: none;
    }
    nav ul li{
      line-height: 60px;
      border-top: 1px solid rgba(255,255,0.1);
    }
    nav ul li:last-child{
      border-bottom: 1px solid rgba(255,0.05);
    }
    nav ul li a{
      position: relative;
      color: white;
      text-decoration: none;
      font-size: 18px;
      padding-left: 40px;
      font-weight: 500;
      display: block;
      width: 100%;
      border-left: 3px solid transparent;
    }
    nav ul li.active a{
      color: cyan;
      background: #1e1e1e;
      border-left-color: cyan;
    }
    nav ul li a:hover{
      background: #1e1e1e;
    }
    nav ul ul{
      position: static;
      display: none;
    }
    nav ul .feat-show.show{
      display: block;
    }
    nav ul .serv-show.show1{
      display: block;
    }
    nav ul ul li{
      line-height: 42px;
      border-top: none;
    }
    nav ul ul li a{
      font-size: 17px;
      color: #e6e6e6;
      padding-left: 80px;
    }
    nav ul li.active ul li a{
      color: #e6e6e6;
      background: #1b1b1b;
      border-left-color: transparent;
    }
    nav ul ul li a:hover{
      color: cyan!important;
      background: #1e1e1e!important;
    }
    nav ul li a span{
      position: absolute;
      top: 50%;
      right: 20px;
      transform: translateY(-50%);
      font-size: 22px;
      transition: transform 0.4s;
    }
    nav ul li a span.rotate{
      transform: translateY(-50%) rotate(-180deg);
    }
    .content{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      color: #202020;
      z-index: -1;
      text-align: center;
    }
    .content .header{
      font-size: 45px;
      font-weight: 600;
    }
    .content p{
      font-size: 30px;
      font-weight: 500;
    }

这是一个顶部栏,它的样式表中还有导航和其他 css 元素,导致重叠等。

<!DOCTYPE html>
    <html lang="en" dir="ltr">
    <head>
        <Meta charset="utf-8">
        <title>NAv menu</title>
        <link rel="stylesheet" type="text/css" href="topbarstyle.css">
    </head>
    <body>
        <div class="navigation">
        <nav>
            <a href="#">I</a>
            <a href="#">II</a>
            <a href="#">III</a>
            <a href="#">IV</a>
            <div class="animation start-home">
                
            </div>
        </nav>
        </div>
    
    </body>
    </html>

这是topbar样式表的代码

    @import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap')
    body {
        font-family: 'Open Sans',sans-serif;
        background: #2c3e50; 
    }
    
    nav{
        position: relative;
        margin: 30px auto 0;
        width: 420px;
        height: 45px;
        background: #000;
        border-radius: 8px;
        font-size: 0;
        Box-shadow: 0 2px 3px 0 rgba(0,.1); 
    }
    
    nav a{
        font-size: 15px;
        color: white;
        text-decoration: none;
        line-height: 45px;
        position: relative;
        z-index: 1; 
        display: inline-block;
        text-align: center;
    
    }
    nav .animation{
        position: absolute;
        height: 100%;
        top: 0;
        z-index: 0;
        background: #4dff4d;
        border-radius: 8px; 
        transition: all .5s ease 0s;
    }
    
    a:nth-child(1){
        width: 100px;
    }
    
    nav .start-home,a:nth-child(1):hover~.animation{
        width: 100px;
        left: 0;
    
    }
    
    a:nth-child(2){
        width: 110px;
    }
    
    nav .start-II,a:nth-child(2):hover~.animation{
        width: 110px;
        left: 100px;
    }
    
    a:nth-child(3){
        width: 100px;
    }
    
    nav .start-III,a:nth-child(3):hover~.animation{
        width: 100px;
        left: 210px;
    }
    
    a:nth-child(4){
        width: 100px;
    }
    
    nav .start-IV,a:nth-child(4):hover~.animation{
        width: 120px;
        left: 300px;
    }

我已经尝试将 z-index 和 position 设为固定/绝对。但是没有其他方法可以将这些样式表单独链接一个 html 文件中。

解决方法

当你链接两个 css 文件时,它们都被浏览器加载并解释为一个大文件,这是没有办法的。

(请注意,例如 css 重置是如何工作的:第一个文件将“重置”所有 css 以对其进行标准化,然后您的 css 文件会出现并被应用)

由于您在两个文件中都有 nav 的定义,因此最终结果将是两个定义的组合,最后一个优先。

我看到的唯一方法是实际使用类而不是元素作为选择器。

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