【问题标题】:Show border triangle in navbar using CSS使用 CSS 在导航栏中显示边框三角形
【发布时间】:2015-08-17 05:51:07
【问题描述】:

我正在尝试在悬停的项目下方创建一个带有箭头的导航栏。这是我想要做的:

对于箭头,我使用了伪元素beforeafter。以下是部分代码:

body {
  background: #FFFFFF;
  color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  height: 100%;
}
.clear {
  clear: both;
}
.page-wrap {
  width: 980px;
  margin: 0px auto;
  height: 100%;
}
#main-menu {
  background: white;
  height: 55px;
  width: 100%;
  padding: 0px;
  margin: 0px;
  border-bottom: 1px solid black;
}
ul {
  font-family: Arial, Verdana;
  font-size: 18px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul {
  display: none;
}
ul li a {
  display: block;
  text-decoration: none;
  color: black;
  padding: 0 9px 0 9px;
  background: white;
  margin-left: 1px;
  white-space: nowrap;
  line-height: 55px;
  font: 18px;
  font-family: Arial, Helvetica, sans-serif;
  outline: none;
}
ul li a:hover {
  color: black;
}
#menu a:hover:after {
  content: "";
  position: absolute;
  top: 40px;
  left: 50%;
  margin-left: -15px;
  width: 0px;
  height 0px;
  xxmargin: 0px auto;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid black;
}
<header id="main-menu">
  <div class="page-wrap">
    <ul id="menu">
      <li><a href="#">Recommended</a></li>
      <li><a href="#">Recent</a></li>
    </ul>
  </div>
</header>

Fiddle Link

由于边框颜色,箭头是黑色的。如何只显示箭头的边框?

【问题讨论】:

  • 使用here 的方法可以帮助您:) 这不是您想要的,但可以采用该方法。

标签: html css css-shapes


【解决方案1】:

只需添加 before 伪元素就像您添加 :after 一样

#menu a:hover:after {
  content: "";
  position: absolute;
  top: 41px;
  left: 50%;
  margin-left: -15px;
  width: 0px;
  height 0px;
  margin: 0px auto;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid white;
}
#menu a:hover:before {
  content: "";
  position: absolute;
  top: 40px;
  left: 50%;
  margin-left: -15px;
  width: 0px;
  height 0px;
  margin: 0px auto;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid black;
}

我已经更新了你的笔,请检查

http://codepen.io/anon/pen/vOxmGZ

【讨论】:

  • 加一个,完美:)
【解决方案2】:

您可以使用带边框的旋转伪元素:

body {
  background: #FFFFFF;
  color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  height: 100%;
}
.page-wrap {
  width: 980px;
  margin: 0px auto;
  height: 100%;
}
#main-menu {
  background: white;
  height: 55px;
  width: 100%;
  padding: 0px;
  margin: 0px;
  border-bottom: 1px solid black;
}
ul {
  font-family: Arial, Verdana;
  font-size: 18px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul {
  display: none;
}
ul li a {
  display: block;
  text-decoration: none;
  color: black;
  padding: 0 9px 0 9px;
  background: white;
  margin-left: 1px;
  white-space: nowrap;
  line-height: 55px;
  font: 18px;
  font-family: Arial, Helvetica, sans-serif;
  outline: none;
}
ul li a:hover {
  color: black;
}
#menu a:hover:after {
  content: "";
  position: absolute;
  bottom:-10px;
  left: 50%;
  margin-left:-10px;
  width: 20px;
  height: 20px;
  background:#fff;
  -webkit-transform:rotate(45deg);
  -ms-transform:rotate(45deg);
  transform:rotate(45deg);
  border-left:1px solid #000;
  border-top:1px solid #000;
  box-sizing:border-box;
}
<header id="main-menu">
  <div class="page-wrap">
    <ul id="menu">
      <li><a href="#">Recommended</a>
      </li>

      <li><a href="#">Recent</a>
      </li>
    </ul>
  </div>
</header>

【讨论】:

    【解决方案3】:

    使用课前课和课后课可以轻松完成。将您的伪元素更改为之前和之后使用伪元素在黑色火车头内绘制一个白色三角形。 对于您的代码,css 可以是这样的

    #menu a:hover:before {
        content: "";
        position: absolute;
        top: 40px;
        left: 50%;
        margin-left: -15px;
        width: 0px;
        height 0px;
        xxmargin: 0px auto;
        border-left: 15px solid transparent;
        border-right: 15px solid transparent;
        border-bottom: 15px solid black;
    
        }
    #menu a:hover:after {
        content: "";
        position: absolute;
        top: 40px;
        left: 50%;
        margin-left: -15px;
        width: 0px;
        height 0px;
        xxmargin: 0px auto;
        border-left: 12px solid transparent;
        border-right: 12px solid transparent;
        border-bottom: 12px solid white;
    

    }

    【讨论】:

    • 您可以在继续添加另一个答案之前检查已接受的答案。除了选择器顺序和几个边框值之外,这是相同的,并没有真正添加任何额外内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多