【问题标题】:Bootstrap absolutely positioned navbar will not lay on top of relatively positioned banner imageBootstrap 绝对定位的导航栏不会位于相对定位的横幅图像之上
【发布时间】:2017-09-21 18:00:31
【问题描述】:

我有一个使用 position: relative; 的横幅图片。在具有位置的包装器内部:相对;我添加了一个 Bootstrap navbar-static-top。我的目标是让导航栏位于横幅图像的顶部。到目前为止,我已经尝试改变一些元素的位置,尽管我尽了最大的努力,但最终还是在导航栏和横幅图像之间出现了空白。我相信这与我正在使用的导航栏类之一有关,但我不确定是哪一个。下面是相关的 HTML5 和 CSS3 代码。

.wrapper {
	position: relative;
	padding: 0 15px;
}

#banner {
	height: 50vh;
	width: 100vw;
	position: relative;
}

#responsive-nav {
	background-color: transparent;
	position: absolute;
	z-index: 2;
	
}

#logo {
	position: absolute;
	top: 15px;
	left: 60px;
	z-index: 1;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mock Up</title>
    <!-- Custom CSS -->
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <!-- Bootstrap Link -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>
	<div class="nav-wrapper">
     <nav class="navbar navbar-static-top">
        <!--Navbar-->
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mainNavBar">
                    <!-- Collapsing Hamburger Buttons for mobile -->
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>         
            </div>
            <!--Menu Items--> 
            <div class="collapse navbar-collapse navbar-right" id="mainNavBar">
                <ul class="nav navbar-nav">
                    <li class="nav-item">
                        <a href="#about">Home</a>
                    </li>
                    <li class="nav-item">
                        <a href="#work">Projects</a>
                    </li>
                    <li class="nav-item">
                        <a href="#contact">Contact</a>
                    </li>
                 
                </ul>
            </div>
            </div>
        </div>
                <div class="wrapper">
                <img class="img-responsive" id="logo" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2ZS1qSS1wOGJCVzg=w1046-h653-iv1">
                <img class="img-responsive" id="banner" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2SDRjaVpyY3FITFk=w1366-h653-iv1">
            </div>
            </div>
        </div>
        
    </nav>
    <script src="https://use.fontawesome.com/bd8b80bd9d.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>

</html>

【问题讨论】:

    标签: html twitter-bootstrap css css-position navbar


    【解决方案1】:

    navbar 引导程序中的类提供 margin-bottom:20px。您可以覆盖 margin-bottom,使其变得重要,以删除导航栏和图像之间的空间

    .wrapper {
    	position: relative;
    	padding: 0 15px;
    }
    
    #banner {
    	height: 50vh;
    	width: 100vw;
    	position: relative;
    }
    
    #responsive-nav {
    	background-color: transparent;
    	position: absolute;
    	z-index: 2;
    	
    }
    
    #logo {
    	position: absolute;
    	top: 15px;
    	left: 60px;
    	z-index: 1;
    }
    .navbar {
      margin-bottom: 0px;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Mock Up</title>
        <!-- Custom CSS -->
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <!-- Bootstrap Link -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    
    <body>
    	<div class="nav-wrapper">
         <nav class="my-class navbar navbar-static-top">
            <!--Navbar-->
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#mainNavBar">
                        <!-- Collapsing Hamburger Buttons for mobile -->
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>         
                </div>
                <!--Menu Items--> 
                <div class="collapse navbar-collapse navbar-right" id="mainNavBar">
                    <ul class="nav navbar-nav">
                        <li class="nav-item">
                            <a href="#about">Home</a>
                        </li>
                        <li class="nav-item">
                            <a href="#work">Projects</a>
                        </li>
                        <li class="nav-item">
                            <a href="#contact">Contact</a>
                        </li>
                     
                    </ul>
                </div>
                </div>
            
                    <div class="wrapper">
                    <img class="img-responsive" id="logo" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2ZS1qSS1wOGJCVzg=w1046-h653-iv1">
                    <img class="img-responsive" id="banner" src="https://lh3.google.com/u/0/d/0B7B-ke12S7B2SDRjaVpyY3FITFk=w1366-h653-iv1">
                </div>
               
           
            
        </nav>
        <script src="https://use.fontawesome.com/bd8b80bd9d.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </body>
    
    </html>

    【讨论】:

    • 使用 !important 是不是有些令人不快?
    • 结束标签有一些不匹配,这就是为什么不使用 !important 就不会选择更改的原因。我已经在上面的 sn-p 中更新了你的代码。如果在引导 css 文件之前加载 css,则 style.css 中的样式将覆盖引导样式。
    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多