鍍金池/ 問答/HTML/ ul與li高度不一致

ul與li高度不一致

有以下html代碼:

 <ul>
    <li>
        <span>aaaaaaa</span>
        <i class="delete"></i>
    </li>
    <li>
        <span>bbbbbbb</span>
        <i class="delete"></i>
    </li>
</ul>

css代碼如下:

 ul
    padding: 15px
    font-size: 0
    width: 857px
    border: 1px solid $color-background
    li
        position: relative
        display: inline-block
        width: 80px
        height 24px
        line-height: 22px
        border: 1px solid #e4e4e4
        border-radius: 2px
        padding: 0 5px
        background-color: #f5f9ff
        font-size: $font-size-medium
        &:nth-child(9n+1)
            margin-left: 0
        .delete
            position: absolute
            display: inline-block
            width: 12px
            height: 12px
            right: -(@width/2)px
            top: -(@height/2)px
            background: url("./close.png") center no-repeat
            background-size: contain
            background-color: $color-background-blue
            border-radius: 50%
            cursor: pointer
        & + li
            margin-left: 15px
        span
            display: inline-block
            width: 100%
            overflow: hidden
            white-space: nowrap
            text-overflow: ellipsis
            

在chrome中看到ul高度為74,但是li的高度只有24,請問是什么原因呢?

回答
編輯回答
下墜

因為瀏覽器默認ul上面有padding和margin,設置一下即可:

ul{
padding: 0;
margin: 0;
}
2017年11月26日 21:39
編輯回答
舊時光

把ul的padding屬性設置為0,然后再添加一個margin為0,我已經嘗試過了,可以的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <style type="text/css">
         ul{
            margin: 0;
            padding: 0;
            font-size: 0;
            width: 857px;
            border: 1px solid $color-background;
         }
        li{
            position: relative;
            display: inline-block;
            width: 80px;
            height: 24px;
            line-height: 22px;
            border: 1px solid #e4e4e4;
            border-radius: 2px;
            padding: 0 5px;
            background-color: #f5f9ff;
            font-size: $font-size-medium;
            &:nth-child(9n+1){
                        margin-left: 0}

        }
        .delete{
            position: absolute;
            display: inline-block;
            width: 12px;
            height: 12px;
            right: -(@width/2)px;
            top: -(@height/2)px;
            background: url("./close.png") center no-repeat;
            background-size: contain;
            background-color: $color-background-blue;
            border-radius: 50%;
            cursor: pointer;
        }
        & + li{
            margin-left: 15px;
        }
        span{
            display: inline-block;
            width: 100%;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
            
    </style>
</head>
<body>
    <ul>
        <li>
            <span>aaaaaaa</span>
            <i class="delete"></i>
        </li>
        <li>
            <span>bbbbbbb</span>
            <i class="delete"></i>
        </li>
    </ul>
</body>
</html>
2017年6月8日 00:24