导航栏顶部的 Bootstrap 警报

iszxjhcz  于 2023-04-18  发布在  Bootstrap
关注(0)|答案(3)|浏览(174)

我在我们的布局文件中添加了以下代码,但在导航栏顶部的alert和导航栏之间有一些间距。我试图添加一些CSS,margin bottom:0,但该余量随后应用于整个站点上的所有警报。

<div class="alert alert-danger" role="alert">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<strong>Moving Servers</strong> You will experience some downtime as we move all of our files onto a new server. This move should be finished on 12/16.
	</div>
   
  
  <!-- Navbar Starts -->
  <header>
  <nav class="navbar navbar-default navbar-static-top">
    <div class="container">
      <div class="navbar-header">

我们只想让警报出现在导航栏的最上方。

qeeaahzv

qeeaahzv1#

如果你只是想把它放在navbar之上,那么创建一个类添加到它,然后使用margin-bottom: 0,这样它就不会改变默认的警报。
请参见工作代码段。

.alert.alert-server {
  margin-bottom: 0;
  border-radius: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="alert alert-danger alert-server" role="alert">
  <button type="button" class="close" data-dismiss="alert">×</button>
  <strong>Moving Servers</strong> You will experience some downtime as we move all of our files onto a new server. This move should be finished on 12/16.
</div>
<nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav" aria-expanded="false"> <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

      </button> <a class="navbar-brand" href="#">BRAND</a>

    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-nav">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home <span class="sr-only">(current)</span></a>

        </li>
        <li><a href="#">About</a>

        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>
holgip5t

holgip5t2#

你从来没有真正提供任何CSS示例,所以我不完全确定你是如何实现这一点的,但是我确实看到你使用navbar-static-top类来固定它的位置,你说你希望在导航栏顶部的警报,所以我会去。
你需要给你的提醒分配一个唯一的id或类,这样你就可以把它放在导航栏的顶部,而不影响页面上的其他提醒。例如,我应用了以下样式来固定位置,并给它一个z索引,这样导航栏就不会出现在提醒的顶部:

#top-alert {
  position: fixed;
  top: 0;
  z-index: 99999;
}

然后我将#top-alert id分配给alert,如下所示:

<div id="top-alert" class="alert alert-danger" role="alert">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>Moving Servers</strong> You will experience some downtime as we move all of our files onto a new server. This move should be finished on 12/16.
</div>

总之,这将导致警报出现在导航栏的顶部。

这里是一个小提琴链接:https://jsfiddle.net/of91yuhu/

q3qa4bjr

q3qa4bjr3#

为此挣扎了一段时间,最终这个简单的解决方案结束了我的头痛...

<div class="container-fluid fixed-top p-0">
    <div id="alerts">
        <div class="alert alert-info alert-dismissible m-0 py-1">
            <strong>Alert title</strong> Alert description
            <button type="button" class="btn-close btn-sm p-2" data-bs-        
                dismiss="alert" aria-label="Close"></button>
        </div>
    </div>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            Your menu content...
        </div>
    </nav>
</div>

相关问题