Html bootstrap - 如何将导航栏放在背景图像的顶部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35154284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:53:39  来源:igfitidea点击:

bootstrap - how to put your navbar on top of your background image?

htmlcsstwitter-bootstrapbootstrap-4

提问by LandonZeKepitelOfGreytBritn

I am new to bootstrap. This is the design I am trying to make: enter image description here

我是引导程序的新手。这是我正在尝试的设计: 在此处输入图片说明

As you can see the navbar is on top op the background image. This is what I have:

如您所见,导航栏位于背景图像的顶部。这就是我所拥有的:

 <!DOCTYPE html>
    <html lang="en">
    <head>
               <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<style>
        <style>
.navbar-default {
background: none;
border: none;
}

body{
    height:100%;
}
        html{
            background: url('sun.jpg') no-repeat center center fixed;
            -webkit-background-size:cover;
            -moz-background-size:cover;
            -o-background-size:cover;
            background-size:cover;
            background-position: 0 0;

        }
        </style>

    </head>
<body>

        <nav class="navbar navbar-default">
            <div class="container">
                <ul class="nav navbar-nav"> 
                    <li class="active"><a href="#">home</a></li>
                    <li><a href="#">ask a question!</a></li>
                    <li><a href="#">learn</a></li>
                    <li><a href="#">contact</a></li>
                </ul>
            </div>

        </nav>

</body>
</html>

gives:

给出:

enter image description here

在此处输入图片说明

I don't know how to put it on the image itself instead of above.

我不知道如何将它放在图像本身而不是上面。

short sidequestion: what makes it that the picture from vimeo makes it look so much better than the image I use from the sun? on my image you can see all the pixels etc... Is this due to the fact that it might be a too small image and that it has to be stretched a lot to fit the whole page?

简短的附带问题:是什么让 vimeo 的图片看起来比我使用的太阳图像好得多?在我的图像上,您可以看到所有像素等......这是因为它可能是一个太小的图像并且它必须被拉伸很多才能适合整个页面吗?

EDIT:

编辑:

this is what I tried but didn't work

这是我尝试过但没有用的方法

    <nav class="navbar navbar-default">
        <div class="container">
            <ul class="nav navbar-nav"> 
                <li class="active"><a href="#">home</a></li>
                <li><a href="#">ask a question!</a></li>
                <li><a href="learn.html">learn</a></li>
                <li><a href="#">contact</a></li>
            </ul>
            <style>
            .navbar{
                background:transparent;
            }
            </style>
        </div>

    </nav>

采纳答案by Zim

Bootstrap 4

引导程序 4

The Navbar has no background color, so it's transparent by default. Just remember to use navbar-lightor navbar-darkso the link colorswork with the contrast of the background image. Display of the toggleris also based on navbar-(dark or light).

导航栏没有背景颜色,因此默认情况下它是透明的。只要记住使用navbar-light或者navbar-dark所以链接的颜色与背景图像的对比度工作。所述的显示toggler也是基于navbar-(暗或亮)

https://www.codeply.com/go/FTDyj4KZlQ

https://www.codeply.com/go/FTDyj4KZlQ

<nav class="navbar navbar-expand-sm fixed-top navbar-light">
    <div class="container">
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbar1">
            <span class="navbar-toggler-icon"></span>
        </button>
        <a class="navbar-brand" href="#">Brand</a>
        <div class="collapse navbar-collapse" id="navbar1">
            <ul class="navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

Bootstrap 3

引导程序 3

Use navbar-fixed-topand then change the CSS to make it transparent. Here's an example of a custom transparent navbar. http://www.codeply.com/go/JNy8BtYSem

使用navbar-fixed-top然后更改 CSS 以使其透明。这是自定义透明导航栏的示例。 http://www.codeply.com/go/JNy8BtYSem

  <nav id="nav" class="navbar navbar-fixed-top" data-spy="affix">
    <div class="container">
        <div class="navbar-header">
      <a href="#" class="navbar-brand">Brand</a>
      <a class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <i class="fa fa-bars"></i>
      </a>
      </div>
      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li><a href="#">Link</a></li>
          <li><a href="#">Link</a></li>
        </ul>
        <ul class="nav pull-right navbar-nav">
          <li>
           <a href="#">Link</a>
          </li>
        </ul>
      </div>        
    </div>
  </nav>


This question is only to make the transparent Navbar.To change style after scrolling or at some Navbar position see: Animate/Shrink NavBar on scroll using Bootstrap 4

这个问题只是为了制作透明的Navbar。要在滚动后或在某些导航栏位置更改样式,请参阅:使用 Bootstrap 4 滚动滚动时动画/收缩导航栏

回答by Turnip

You need to remove the background from your nav-bar:

您需要从导航栏中删除背景:

.navbar-default {
    background: none;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<style>
html,
body {
  height: 100%;
 background: red;
}

.navbar-default {
    background: none;
   border: none;
}
</style>

<!-- Wrap all page content here -->
<div id="wrap">
  
  <!-- Fixed navbar -->
  <div class="navbar navbar-default navbar-fixed-top">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">Project name</a>
      </div>
      <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#contact">Contact</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li class="divider"></li>
              <li class="dropdown-header">Nav header</li>
              <li><a href="#">Separated link</a></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
      </div><!--/.nav-collapse -->
    </div>
  </div>
  
</div>

回答by Atilla Arda A??kg?z

.navbar{
    background:transparent;
}

This will make your background transparent.

这将使您的背景透明。

回答by Saad Mirza

Just take the backgound image in Body and then use this code

只需在 Body 中获取背景图像,然后使用此代码

             <div class="navbar navbar-inverse navbar-fixed-top">
              <div class="container">
                <div class="navbar-header">
                  <button type="button" class="navbar-toggle" data-         
                toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                  </button>
                  <a class="navbar-brand" href="#">Brand</a>
                </div>
                <div class="collapse navbar-collapse">
                  <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#about">About</a></li>
                  </ul>
                  <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">Username</a></li>
                  </ul>
                </div>
              </div>
            </div>

            <div class="wide">
                <div class="col-xs-5 line"><hr></div>
                <div class="col-xs-2 logo">Logo</div>
                <div class="col-xs-5 line"><hr></div>
            </div>

            <div class="container">
              <div class="text-center">
                <h1>Content</h1>
              </div>
            </div>

Also used styling style="opacity:0.6;" in Navbar div

还使用了样式 style="opacity:0.6;" 在导航栏 div

回答by elmarko

The background colour is set on the .navbar-default class so make sure your css is using that class to target turning the bar transparent. e.g.

背景颜色设置在 .navbar-default 类上,因此请确保您的 css 正在使用该类来将条形图变为透明。例如

.navbar-default{background-color: transparent;}

As Turnip states, this needs to be applied after the bootstrap styles in your <head>

正如 Turnip 所述,这需要在您的引导程序样式之后应用 <head>

If you really want to force it whilst you are getting it to work add the following attribute to your <nav>element

如果您真的想在让它工作时强制它工作,请将以下属性添加到您的<nav>元素中

style="background-color: transparent;"

So your navelement looks like this:

所以你的nav元素看起来像这样:

 <nav class="navbar navbar-default" style="background-color: transparent;">

However, it obviously better to have this within a CSS file, rather than as inline HTML

然而,将它放在一个 CSS 文件中显然更好,而不是作为内联 HTML