Vertically centering content of :before/:after pseudo-elements

前端 未结 6 1224
闹比i
闹比i 2020-11-29 17:51

I\'m trying to achieve something similar to this picture:

\"enter

I have an im

6条回答
  •  一个人的身影
    2020-11-29 18:35

    Assuming your element is not an (because pseudo elements are not allowed on self-closing elements), let's suppose it's a

    , so a way could be:

    div {
        height: 100px ;
        line-height: 100px;
    }
    div:before, div:after {
        content: "";
        ...
        display: inline-block;
        vertical-align: middle;
        height: ...;
        line-height: normal;
    }
    

    If you cannot change the line-height of the div, another way is:

    div {
        position: relative;
    }
    div:before, div:after {
        position: absolute;
        display: block;
        top: 50%;
        -webkit-transform: translateY(-50%);
        -moz-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        transform: translateY(-50%);
        content: "";
        width: ...
    }
    

    Otherwise, just place the indicators as a background in center position.

提交回复
热议问题