2016-08-12 107 views
0

我有一個完美的導航欄(使用navbar.css)。但是當我添加頁面上另一個元素所需的bootstrap.css時,導航欄會中斷。這就是我一直試圖解決衝突:bootstrap.css文件 navbar css被bootstrap.css覆蓋

  • navbar.css添加!important到每一個項目後

    • 引用navbar.css和包圍<!-------------------->
    • 這個其他元素不利用鏈路參考整個bootstrap.css文件,所以我用螢火蟲,以消除所有未使用的CSS(至< 500行擴展CSS)
    • 通過我修改bootstrap.css文件逐行並刪除任何引用我的n avbar(導航標籤等)

    但導航欄仍然打破。下面是導航欄的代碼和我的CSS:

    HTML

    <nav class='sidebar sidebar-menu-collapsed'> 
        <ul> 
        <li> 
         <a class='expandable' href='#'> 
         <span><i class="fa fa-align-justify fa-2x" aria-hidden="true" id='justify-icon'></i></span> 
         </a> 
        </li> 
        <li> 
         <a class='expandable' href='myshares.php' title='My Archives'> 
         <span><i class="fa fa-archive fa-2x" aria-hidden="true"></i></span> 
         <span class='expanded-element'>My Archives</span> 
         </a> 
        </li> 
        <li> 
         <a class='expandable' href='sharedwithme.php' title='Shared w/ Me'> 
         <span><i class="fa fa-inbox fa-2x" aria-hidden="true"></i></span> 
         <span class='expanded-element'>Shared w/ Me</span> 
         </a> 
        </li> 
        <li class="active"> 
         <a class='expandable' href='#' title='Add a Share'> 
         <span><i class="fa fa-plus-square fa-2x" aria-hidden="true"></i></span> 
         <span class='expanded-element'>Add Share</span> 
         </a> 
        </li> 
        <li> 
         <a class='expandable' href='tools.html' title='Tools'> 
         <span><i class="fa fa-wrench fa-2x" aria-hidden="true"></i></span> 
         <span class='expanded-element'>Tools</span> 
         </a> 
        </li> 
        <li> 
         <a class='expandable' href='howitworks.html' title='How this Works'> 
         <span><i class="fa fa-question fa-2x" aria-hidden="true"></i></span> 
         <span class='expanded-element'>How This Works</span> 
         </a> 
        </li> 
        <li> 
         <a class='expandable' href='logout.php' title='Logout'> 
         <span><i class="fa fa-power-off fa-2x" aria-hidden="true"></i></span> 
         <span class='expanded-element'>Logout</span> 
         </a> 
        </li> 
        </ul> 
    </nav> 
    

    CSS

    @import "https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css"; 
    @import "https://www.sonoclipshare.com/public/bootstrap.css"; 
    body { 
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
        background: none repeat scroll 0 0 white; 
    } 
    
    nav.sidebar-menu-collapsed { 
        width: 34px; 
    } 
    
    nav.sidebar-menu-expanded { 
        width: 116px; 
    } 
    
    nav.sidebar { 
        position: fixed; 
        top: 0px; 
        left: 0px; 
        height: 100%; 
        background: none repeat scroll 0 0 #0099ff; 
        color: white; 
        padding: 20px 10px; 
        text-align: left; 
    } 
    
    nav.sidebar a#justify-icon { 
        outline: 0; 
        color: white; 
        font-size: 24px; 
        font-style: normal; 
    } 
    
    nav.sidebar a#logout-icon { 
        outline: 0; 
        color: white; 
        font-size: 24px; 
        font-style: normal; 
        position: absolute; 
        bottom: 10px; 
        left: 10px; 
    } 
    
    nav.sidebar ul { 
        margin: 0; 
        padding: 0; 
        margin-top: 60px; 
    } 
    
    nav.sidebar ul li { 
        margin: 0; 
        padding-left: 3px; 
        margin-top: 20px; 
        list-style-type: none; 
    } 
    
    nav.sidebar ul li a.expandable { 
        outline: 0; 
        color: white; 
        text-decoration: none; 
    } 
    
    nav.sidebar ul li a.expandable:hover { 
        color: #bbbbbb; 
    } 
    
    nav.sidebar ul li a.expandable span.expanded-element { 
        margin-left: 2px; 
        display: none; 
        font-size: 11px; 
        position: relative; 
        bottom: 2px; 
    } 
    
    nav.sidebar ul li.active { 
        background: none repeat scroll 0 0 black; 
        border-radius: 4px; 
        padding: 4px; 
    } 
    
    nav.sidebar ul li.active a.expandable { 
        color: white; 
    } 
    
    nav.sidebar ul li.active a.expandable:hover { 
        color: white; 
    } 
    
    #justify-icon { 
        margin-bottom: 40px; 
    } 
    
    .fa-power-off { 
        margin-top: 40px; 
    } 
    

    JQuery的

    $(document).ready(function() { 
        function expandMyMenu() { 
        $("nav.sidebar").removeClass("sidebar-menu-collapsed").addClass("sidebar-menu-expanded"); 
        }; 
    
        function collapseMyMenu() { 
        $("nav.sidebar").removeClass("sidebar-menu-expanded").addClass("sidebar-menu-collapsed"); 
        }; 
    
        function showMenuTexts() { 
        $("nav.sidebar ul a span.expanded-element").show(); 
        $("nav.sidebar ul a span.expanded-element").css('display', 'inline'); 
        }; 
    
        function hideMenuTexts() { 
        $("nav.sidebar ul a span.expanded-element").hide(); 
        }; 
        $("#justify-icon").click(function(e) { 
        if ($("nav.sidebar").hasClass("sidebar-menu-collapsed")) { 
         expandMyMenu(); 
         showMenuTexts(); 
         $(this).css({ 
         color: "#000" 
         }); 
        } else if ($("nav.sidebar").hasClass("sidebar-menu-expanded")) { 
         collapseMyMenu(); 
         hideMenuTexts(); 
         $(this).css({ 
         color: "#FFF" 
         }); 
        } 
        }); 
    }); 
    

    如果我刪除了違規行@import "https://www.sonoclipshare.com/public/bootstrap.css"; navbar看起來應該是。這是一個codepen玩耍。我在此空出了500行bootstrap.css。這是一個jsfiddle與所有的css

  • 回答

    0

    違規行,是隻是一個引導CSS的CDN?嘗試從官方源代碼中將其放入代碼的頭部分;

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
    
    +0

    添加此行,完整的引導程序CSS打破我的菜單一樣。通過將該行添加到上面的codepen可以看出。 – UltrasoundJelly

    0

    我刪除CSS

    nav.sidebar-menu-collapsed { 
        width: 34px; 
    } 
    

    的這部分和導航欄看起來更好。

    +0

    我也建議遵循A. Cawlan的建議並使用首選的CDN來源。 –

    +0

    同意,它看起來更好。但是從原始的Navbar仍然有變化。圖標較小,文本現在在導航欄展開時包裝。我需要導航欄不變,因爲我在整個站點使用它。 – UltrasoundJelly

    +0

    你可以用你的代碼創建一個JSFiddle,我們可以編輯你的代碼。您只提供了導航欄代碼,因此我們無法挑選出其他已損壞的內容。 –

    0

    嗯,我能夠縮小CSS,我的其他代碼使用bootstrap,只包括。仍然不能100%確定bootstrap的哪個部分影響了我的代碼,但這是一個簡單的消除過程,讓我回答了我的問題。蠻力再次襲擊。感謝大家的建議。