html - 将元素置于导航栏内的顶部

标签 html css twitter-bootstrap bootstrap-4

我正在尝试装饰我的导航栏,让它看起来更好一些,我使用 bootstrapv4 作为基础,我只想在导航栏顶部添加 2 个“条纹”,但出于某种原因,bootstrap 使我的装饰居中,有些东西我想阻止。

这是我的代码(有点简化)

HTML:

<nav class="navbar navbar-expand-lg navbar-green">
  <div class="navbar-top-bg"></div>
  <div class="container">
    <div class="navbar-top-fg"></div>
  </div>
</nav>

CSS:

.navbar-top-bg {
  position: absolute;
  content: "";
  width: 100%;
  height: 10px;
  background: rgba(67, 107, 65, 0.5);
  margin: 0 auto;
  z-index: 111111;
  border-bottom: 1px solid rgba(87, 136, 89, 0.4);
  vertical-align: top;
}

.navbar-top-fg {
  position: relative;
  content: "";
  width: 288px;
  height: 10px;
  -webkit-transform: perspective(40px) rotateX(-16deg);
  transform: perspective(40px) rotateX(-16deg);
  background: rgba(0, 0, 0, 0.5);
  margin: 0 auto;
  z-index: 1111111;
  /*border-bottom: 2px solid rgba(234, 234, 234, 0.4);*/
  border-right: 2px solid rgb(65, 100, 62);
  border-left: 2px solid rgb(65, 100, 62);
  vertical-align: top;
}

.navbar-green {
  padding-left: 0;
  padding-right: 0;
  background: rgba(37, 48, 27, 1);
  background: -moz-linear-gradient(top, rgba(39, 41, 37, 1) 0%, rgba(0, 0, 0, 0.60) 100%);
  background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(39, 41, 37, 1)), color-stop(100%, rgba(0, 0, 0, 0.60)));
  background: -webkit-linear-gradient(top, rgba(39, 41, 37, 1) 0%, rgba(0, 0, 0, 0.60) 100%);
  background: -o-linear-gradient(top, rgba(39, 41, 37, 1) 0%, rgba(0, 0, 0, 0.60) 100%);
  background: -ms-linear-gradient(top, rgba(39, 41, 37, 1) 0%, rgba(0, 0, 0, 0.60) 100%);
  background: linear-gradient(to bottom, rgba(39, 41, 37, 1) 0%, rgba(0, 0, 0, 0.60) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#25301b', endColorstr='#79bd64', GradientType=0);
  border-bottom: 1px solid rgba(91, 115, 89, 0.08) !important;
  -webkit-box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.75);
  height: 75px;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 1030;
}

如您所见,2 个绿色条纹位于导航栏的中心(垂直),但我希望它们位于顶部(要查看该布局,只需删除 navbarnavbar- expand-lg 类),但我也想使用常见的“导航栏”功能来添加链接等。

这里还有一个 jsfiddle 的链接:

https://jsfiddle.net/Insax/apzb42b9/

最佳答案

在“条纹”的两个部分上使用 position: absolute 并使用 top: 0 将它们定位到顶部。使用 left: 0right: 0 将它们居中。

JSFiddle

.navbar-top-bg {
  width: 100%;
  height: 10px;
  background: rgba(67, 107, 65, 0.5);
  margin: 0 auto;
  z-index: 111111;
  border-bottom: 1px solid rgba(87, 136, 89, 0.4);
}

.navbar-top-fg {
  width: 288px;
  height: 10px;
  -webkit-transform: perspective(40px) rotateX(-16deg);
  transform: perspective(40px) rotateX(-16deg);
  background: rgba(0, 0, 0, 0.5);
  margin: 0 auto;
  z-index: 1111111;
  /*border-bottom: 2px solid rgba(234, 234, 234, 0.4);*/
  border-right: 2px solid rgb(65, 100, 62);
  border-left: 2px solid rgb(65, 100, 62);
}

.navbar-top-bg,
.navbar-top-fg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
}

关于html - 将元素置于导航栏内的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50115909/

相关文章:

html - CSS 方形背景 - 图像

css - Bootstrap 轮播 : start css animation after slide is complete

html - react 中的 Bootstrap 列偏移不起作用

css - 将链接设为白色

html - CSS HTML 根据字体大小制作图片

javascript - 如何替换http ://with Tel:

javascript - tinymce 4 如何将光标放在文本末尾

javascript - iOS webview 将 var 发送到 javascript html 文件

css - 当 &lt;input/> 文本溢出时,iOS 6 中的页面移动

javascript - 当我第二次打开特定页面时,导航栏消失