设置边框的高度

时间:2017-05-04 03:05:35

标签: html css

我有两个问题:

  1. 我可以设置右边的高度吗?

  2. 我可以隐藏最后一个边界吗?

  3. enter image description here

    
    
    body {
      margin: 0;
      padding: 0;
    }
    
    #banner {
      height: 30px;
      width: 750px;
      margin: 0 auto;
      background-color: aquamarine;
    }
    
    #banner ul {
      list-style: none;
      height: 30px;
      padding: 0;
    }
    
    #banner ul li {
      float: left;
      margin: auto 0;
      height: 30px;
      line-height: 30px;
      width: 100px;
      text-align: center;
      border-right: 1px solid #aba;
    }
    
    <div id="banner">
      <ul>
        <li>Home</li>
        <li>link</li>
        <li>product</li>
        <li>phone</li>
        <li>cat</li>
        <li>about</li>
      </ul>
    </div>
    &#13;
    &#13;
    &#13;

5 个答案:

答案 0 :(得分:2)

隐藏最后一个边框

这是你的边界:

li { border-right: 1px solid #aba; }

但是你不希望它出现在最后一个项目上。所以试试这个:

li + li {
  border-left: 1px solid #aba;
}

新规则将左侧边框应用于紧跟在另一个li之后的li个元素。这将排除第一个li上的左边框和上一个li上的右边框。

缩短边框

您希望边框小于全高。您可以使用绝对定位的伪元素实现效果:

li {
  position: relative; /* establish the containing block for abspos children */
}

li+li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 50%;
  width: 2px;
  background-color: red;
}

&#13;
&#13;
body {
  margin: 0;
  padding: 0;
}

#banner {
  height: 30px;
  width: 750px;
  margin: 0 auto;
  background-color: aquamarine;
}

#banner ul {
  list-style: none;
  height: 30px;
  padding: 0;
}

#banner ul li {
  float: left;
  margin: auto 0;
  height: 30px;
  line-height: 30px;
  width: 100px;
  text-align: center;
  /* border-right: 1px solid #aba; */
  position: relative;
}

li+li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  height: 50%;
  width: 2px;
  background-color: red;
}
&#13;
<div id="banner">
  <ul>
    <li>Home</li>
    <li>link</li>
    <li>product</li>
    <li>phone</li>
    <li>cat</li>
    <li>about</li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个:

li:not(:last-child):after {
  content: "";
  display: inline-block;
  margin-left: 40px;
  border-right: 3px solid orange;
  height: 20px;
  top: 5px;
  position: absolute;
}

完整代码:

&#13;
&#13;
  body {

    margin: 0;
    padding:0;
}

#banner {

    height:30px;
    width: 750px;
    margin: 0 auto;
    background-color: aquamarine;
    vertical-align: middle;
}
#banner ul{

    list-style: none;
    height:30px;
    padding: 0;
    position: relative;

}

#banner ul li {

    float: left;
    margin: auto 0;
    height: 30px;
    line-height: 30px;
    width: 100px;
    text-align:center;
}
li:not(:last-child):after {
  content: "";
  display: inline-block;
  margin-left: 40px;
  border-right: 3px solid orange;
  height: 20px;
  top: 5px;
  position: absolute;
}
&#13;
 <div id="banner">
    <ul >
        <li>Home</li>
        <li>link</li>
        <li>product</li>
        <li>phone</li>
        <li>cat</li>
        <li>about</li>
    </ul>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

  1. 您可以尝试:: before和:: after
  2. 而不是边框

    &#13;
    &#13;
    ul {
      display: flex;
    }
    li {
      position: relative;
      display: flex;
      justify-content: center;
      flex: 1;
      padding: 10px 0;
      background-color: gray;
      list-style-type: none;
    }
    li::after {
      content: "";
      position: absolute;
      top: 5px; /* control the 'height' with top/bottom */
      bottom: 5px;
      right: 0;
      width: 5px; /*same as what your border was */
      background-color: black; /* instead of border-color */
    }
    li:last-child::after { /*hides last 'border' */
      display: none;
    }
    &#13;
    <div>
      <ul>
        <li>one</li>
        <li>two</li>
        <li>three</li>
      </ul>
    </div>
    &#13;
    &#13;
    &#13;

    1. 尝试#banner ul li:last-child {border-right:0;或border-right-color:hsla(0,0%,0%,0);}
    2. <强>拨弄

      https://jsfiddle.net/Hastig/kkx33des/

答案 3 :(得分:0)

要移除上一个li的边框,请使用伪类:last-child

我在display:flex的顶部和底部添加边距以使其居中后使用了align-item:centerli

body {

    margin: 0;
    padding:0;
}

#banner {

    height:30px;
    width: 750px;
    margin: 0 auto;
    background-color: aquamarine;
}
#banner ul{

    list-style: none;
    height:30px;
    padding: 0;
    display: flex;
    align-items: center;
}

#banner ul li {

    float: left;
    width: 100px;
    text-align:center;
    border-right:1px solid #aba;
    margin-top:10px;
    margin-bottom: 10px;
}
#banner ul li:last-child {
  border-right: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>title</title>
    <link rel="stylesheet" href="styles/index.css" type="text/css">
</head>
<body>
<div id="banner">
    <ul >
        <li>Home</li>
        <li>link</li>
        <li>product</li>
        <li>phone</li>
        <li>cat</li>
        <li>about</li>
    </ul>
</div>

</body>
</html>

答案 4 :(得分:-2)

  1. U可以使用:在每个li的选择器之后尝试使用管道符号作为替代
  2. 你需要:last-child伪Css选择器来隐藏最后一个li的边框