【问题标题】:empty space before li when adding justify-content:space-around添加 justify-content:space-around 时 li 之前的空白
【发布时间】:2021-02-26 06:16:35
【问题描述】:

我正在尝试使用 ul/li 元素创建一个选项卡,我使用了 justify-content: space-around,所以它当然会在元素之间添加空格,但是当我添加时我无法设置 li 的样式例如边框,边框是在元素上而不是在空间上添加的,所以你知道如何解决这个问题吗?

这是有效的sn-p

ul.tab1 {
    /* reset unordered list to not use default list styles */
    padding: 0;
    margin: 0;
    list-style:none;
    /* add display flex with space around and vertical centering...also stop wrap */
    display: flex;
    justify-content: space-around;
    align-items: center;
    flex-wrap: nowrap;
    border: 1px solid #8A4DEE;
    border-bottom: none;
    border-top-left-radius: 12px 12px;
    border-top-right-radius: 12px 12px;
    background-image: linear-gradient(to right, #F9F9F9 , #DDCDFD);
  }

  ul.tab1 li::before {
    padding: 0;
    margin: 0;
    content: none;
  }
  ul.tab1 li a {
    height: 51px;
    color: #8A4DEE;
    border-right: 1px solid #8A4DEE;
    border-bottom: 1px solid #8A4DEE;
    display: table-cell;
    vertical-align: middle;
    padding-right: 30px;
  }
  ul.tab1 li:first-child a {
    padding-left:20px;
  }
  ul.tab1 li:last-child a {
    padding-right:10px;
  }
  ul.tab1 li a.active {
      border-bottom: none;
  }
<ul class="tab1">
    <li><a href="#" class="tablinks active">aaaa</a></li>
    <li><a href="#" class="tablinks">bbbb</a></li>
    <li><a href="#" class="tablinks">cccc</a></li>
    <li><a href="#" class="tablinks">dddd</a></li>
                                                                
                                                                
                                                                
                                                            </ul>

【问题讨论】:

    标签: html css tabs justify


    【解决方案1】:

    您是否正在寻找类似的东西

    ul {
          padding: 0;
        margin: 0;
    }
    ul.tab1 {
        /* reset unordered list to not use default list styles */
        list-style:none;
        /* add display flex with space around and vertical centering...also stop wrap */
        display: flex;
        justify-content: space-around;
        align-items: center;
        flex-wrap: nowrap;
        border: 1px solid #8A4DEE;
        border-bottom: none;
        border-top-left-radius: 12px 12px;
        border-top-right-radius: 12px 12px;
        background-image: linear-gradient(to right, #F9F9F9 , #DDCDFD);
      }
    
      ul.tab1 li::before {
        padding: 0;
        margin: 0;
        content: none;
      }
      ul.tab1 li a {
        color: #8A4DEE;
      }
    li {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 51px;
      width: 100%;
    }
      ul.tab1 li {
        color: #8A4DEE;
        border-right: 1px solid #8A4DEE;
      }
      ul.tab1 li a.active {
          border-bottom: none;
      }
      ul li:last-child {
        border-right: none;
      }
    <ul class="tab1">
        <li><a href="#" class="tablinks active">aaaa</a></li>
        <li><a href="#" class="tablinks">bbbb</a></li>
        <li><a href="#" class="tablinks">cccc</a></li>
        <li><a href="#" class="tablinks">dddd</a></li>
        </ul>

    【讨论】:

    • 好的,谢谢。我刚刚添加了border-bottom,就完成了。所以解决方案是添加这个显示: flex;justify-content: center;align-items: center;在 li 元素上。
    • @Khadija_ 如果答案对您有帮助,请接受/投票,谢谢。编码快乐!
    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2021-06-17
    • 1970-01-01
    • 2012-08-07
    • 2018-08-28
    • 2018-11-15
    • 2020-11-15
    • 2021-04-17
    相关资源
    最近更新 更多