如何让边框重叠?

时间:2016-12-18 04:18:02

标签: css html5 css3 css-selectors css-float

我正在尝试将标题的边框顶部设置为具有一种颜色,并且当在导航上悬停/活动时,其中一部分将更改为另一种颜色。

同样,为什么对于我的li:hover代码,当我执行border-top时,它仍会返回border-bottom

What I'm trying to achieve in a picture 如前所述,我也想在hover时,边界过渡将超出图中的绿色边界线。

我怎样才能做到这一点?

这是我到目前为止所拥有的

/*** Page ***/
html,body {
    margin: 0;
}

/*** Navigation ***/
.navbar {
    background-color: #ecf0f1;
}
.navbar > .main-nav > ul,
li {
    list-style-type: none;
    text-decoration: none;
}
.navbar > .main-nav > ul {
    min-height: 60px;
    margin: 0;
    padding: 0;
    overflow: hidden;
    box-sizing: border-box;
    border-top: 8px solid #d1d064;
}
.navbar > .main-nav > ul > li {
    text-align: center;
    min-width: 150px;
    position: relative;
    top: -2px;
    display: block;
    float: left;
}
.navbar > .main-nav > ul > li a {
    display: block;
    color: #737373;
    text-align: center;
    padding: 20px 16px;
    text-decoration: none;
    font-family: 'Lato', sans-serif;
    font-weight: 800;
}
.navbar > .main-nav > ul > li.active {
    border-top: 8px solid #0F9E5E;
}
.navbar > .main-nav > ul > li:after {
    display: block;
    content: '';
    border-top: solid 3px #a8a8b6;
    transform: scaleX(0);
    transition: transform 250ms ease-in-out;
}
.navbar > .main-nav > ul > li:hover:after {
    transform: scaleX(1);
}
.navbar > .main-nav {
    max-width: 1480px;
    margin: 0 auto;
}
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.1.0/css/hover-min.css" rel="stylesheet">
</head>

<body>
<br><br><br><br><br><br>
    <nav class="navbar">
        <div class="main-nav">
            <ul>
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">Second</a></li>
                <li><a href="#">Third</a></li>
            </ul>
        </div>
    </nav>
</body>

由于

4 个答案:

答案 0 :(得分:4)

边框影响布局,我建议只需切换到box-shadow即可更改颜色:

.navbar > .main-nav > ul > li.active,
.navbar > .main-nav > ul > li:hover {
  box-shadow: 0 -8px 0 #0F9E5E;
}

工作演示:

&#13;
&#13;
.navbar {
  background-color: #ecf0f1;
}
.navbar::after {
  content: '';
  clear: both;
  display: block;
}
.navbar > .main-nav > ul,
li {
  list-style-type: none;
  text-decoration: none;
}
.navbar > .main-nav > ul {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  border-top: 8px solid #d1d064;
}
.navbar > .main-nav > ul > li {
  text-align: center;
  min-width: 150px;
  position: relative;
  display: block;
  float: left;
}
.navbar > .main-nav > ul > li a {
  display: block;
  color: #737373;
  text-align: center;
  padding: 20px 16px;
  text-decoration: none;
  font-family: 'Lato', sans-serif;
  font-weight: 800;
}
.navbar > .main-nav > ul > li.active,
.navbar > .main-nav > ul > li:hover {
  box-shadow: 0 -8px 0 #0F9E5E;
}
&#13;
<br>
<br>
<br>
<nav class="navbar">
  <div class="main-nav">
    <ul>
      <li class="active"><a href="#">Home</a>
      </li>
      <li><a href="#">Second</a>
      </li>
      <li><a href="#">Third</a>
      </li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

  

同样,为什么我的李:悬停标签,当我做边框顶部时,它仍然返回我的边框底部?

这是由以下CSS引起的:

.navbar > .main-nav > ul > li:after {
  display: block;
  content: '';
  border-top: solid 3px #a8a8b6;
  transform: scaleX(0);
  transition: transform 250ms ease-in-out;
}

.navbar > .main-nav > ul > li:hover::after {
  transform: scaleX(1);
}

答案 1 :(得分:2)

用于清除 overflow:hidden的浮点数的ul上的li可以替换为:

.navbar > .main-nav > ul:after {
    display: block;
    clear: both;
    content: '';
}

因为overflow: hidden阻止了边框的重叠。

现在在li.active上添加一些负边距,并将边框宽度调整为获得所需效果。

  

我还想在悬停时,边界过渡会超过   图中的绿色边框线。

为此,请将.navbar > .main-nav > ul > li:after.navbar > .main-nav > ul > li:hover:after更改为之前的 psuedo元素,然后将margin-top投入其中。

见下面的演示:

&#13;
&#13;
/*** Page ***/

html,
body {
  margin: 0;
}
/*** Navigation ***/

.navbar {
  background-color: #ecf0f1;
}
.navbar > .main-nav > ul,
li {
  list-style-type: none;
  text-decoration: none;
}
.navbar > .main-nav > ul {
  min-height: 60px;
  margin: 0;
  padding: 0;
  /*overflow: hidden;*/
  box-sizing: border-box;
  border-top: 8px solid #d1d064;
}
.navbar > .main-nav > ul:after {
  display: block;
  clear: both;
  content: '';
}
.navbar > .main-nav > ul > li {
  text-align: center;
  min-width: 150px;
  position: relative;
  top: -2px;
  display: block;
  float: left;
}
.navbar > .main-nav > ul > li a {
  display: block;
  color: #737373;
  text-align: center;
  padding: 20px 16px;
  text-decoration: none;
  font-family: 'Lato', sans-serif;
  font-weight: 800;
}
.navbar > .main-nav > ul > li.active {
  border-top: 8px solid #0F9E5E;
  margin-top: -6px; /*ADDED THIS*/
}
.navbar > .main-nav > ul > li:before { /*CHANGED*/
  display: block;
  content: '';
  border-top: solid 3px #a8a8b6;
  transform: scaleX(0);
  transition: transform 250ms ease-in-out;
  margin-top: -9px; /*ADDED THIS*/
}
.navbar > .main-nav > ul > li:hover:before { /*CHANGED*/
  transform: scaleX(1);
}
.navbar > .main-nav {
  max-width: 1480px;
  margin: 0 auto;
}
&#13;
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.1.0/css/hover-min.css" rel="stylesheet">
</head>

<body>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <nav class="navbar">
    <div class="main-nav">
      <ul>
        <li class="active"><a href="#">Home</a>
        </li>
        <li><a href="#">Second</a>
        </li>
        <li><a href="#">Third</a>
        </li>
      </ul>
    </div>
  </nav>
</body>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您只需要为您的有效li添加一些负余量 - 顶部

.navbar > .main-nav > ul > li.active {
    border-top: 8px solid #0F9E5E;
    margin-top:-5px;
}

这是完整的工作代码

&#13;
&#13;
/*** Page ***/
html,body {
    margin: 0;
}

/*** Navigation ***/
.navbar {
    background-color: #ecf0f1;
}
.navbar > .main-nav > ul,
li {
    list-style-type: none;
    text-decoration: none;
}
.navbar > .main-nav > ul {
    min-height: 60px;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    border-top: 8px solid #d1d064;
}
.navbar > .main-nav > ul > li {
    text-align: center;
    min-width: 150px;
    position: relative;
    top: -2px;
    display: block;
    float: left;
}
.navbar > .main-nav > ul > li a {
    display: block;
    color: #737373;
    text-align: center;
    padding: 20px 16px;
    text-decoration: none;
    font-family: 'Lato', sans-serif;
    font-weight: 800;
}
.navbar > .main-nav > ul > li.active {
    border-top: 8px solid #0F9E5E;
    margin-top:-5px;
}
.navbar > .main-nav > ul > li:after {
    display: block;
    content: '';
    border-top: solid 3px #a8a8b6;
    transform: scaleX(0);
    transition: transform 250ms ease-in-out;
}
.navbar > .main-nav > ul > li:hover:after {
    transform: scaleX(1);
}
.navbar > .main-nav {
    max-width: 1480px;
    margin: 0 auto;
}
&#13;
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.1.0/css/hover-min.css" rel="stylesheet">
</head>

<body>
<br><br><br><br><br><br>
    <nav class="navbar">
        <div class="main-nav">
            <ul>
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">Second</a></li>
                <li><a href="#">Third</a></li>
            </ul>
        </div>
    </nav>
</body>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

您可以先修改此问题,然后首先需要删除此overflow-hidden

.navbar > .main-nav > ul {
  border-top: 8px solid #d1d064;
  box-sizing: border-box;
  margin: 0;
  min-height: 60px;
  /*overflow: hidden;*/ /* remove this overflow-hidden */
  padding: 0;
  z-index: 1;
}

第二次增加减去顶部的值

.navbar > .main-nav > ul > li {
   display: block;
   float: left;
   min-width: 150px;
   position: relative;
   text-align: center;
   top: -6px; /* i have changed this value -2px to -6px */
}

您的代码没有任何问题,请查看工作代码段

html,body {
    margin: 0;
}

/*** Navigation ***/
.navbar {
    background-color: #ecf0f1;
}
.navbar > .main-nav > ul,
li {
    list-style-type: none;
    text-decoration: none;
}
.navbar > .main-nav > ul {
    min-height: 60px;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    border-top: 8px solid #d1d064;
}
.navbar > .main-nav > ul > li {
    text-align: center;
    min-width: 150px;
    position: relative;
    top: -7px;
    display: block;
    float: left;
}
.navbar > .main-nav > ul > li a {
    display: block;
    color: #737373;
    text-align: center;
    padding: 20px 16px;
    text-decoration: none;
    font-family: 'Lato', sans-serif;
    font-weight: 800;
}
.navbar > .main-nav > ul > li.active {
    border-top: 8px solid #0F9E5E;
}
.navbar > .main-nav > ul > li:after {
    display: block;
    content: '';
    border-top: solid 3px #a8a8b6;
    transform: scaleX(0);
    transition: transform 250ms ease-in-out;
}
.navbar > .main-nav > ul > li:hover:after {
    transform: scaleX(1);
}
.navbar > .main-nav {
    max-width: 1480px;
    margin: 0 auto;
}
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/hover.css/2.1.0/css/hover-min.css" rel="stylesheet">

<nav class="navbar">
  <div class="main-nav">
    <ul>
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">Second</a></li>
      <li><a href="#">Third</a></li>
    </ul>
  </div>
</nav>