width and height doesn't seem to work on :before pseudo-element

后端 未结 5 1557
小鲜肉
小鲜肉 2020-12-08 00:20

Here is a fiddle.

foo bar baz

and

a.infolink::before
{
         


        
相关标签:
5条回答
  • 2020-12-08 00:30

    ::before and ::after pseudo-elements are laid inline by default, so width and height won't take effect.

    Set it to display: inline-block and it should work.

    0 讨论(0)
  • 2020-12-08 00:32

    Use this if you have image in container or CSS white-space: nowrap; property

    body::before{ 
        content:url(https://i.imgur.com/LJvMTyw.png);
        transform: scale(.3);
    }
    
    0 讨论(0)
  • 2020-12-08 00:40

    Note: The ::before and ::after pseudo-elements are actually laid display: inline; by default.

    Change the display value to inline-block for the width & height to take effect while maintaining inline formatting context.

    a.infolink::before {
        content: '?';
        display: inline-block;
        background: blue;
        color: white;
        width: 20px;
        height: 20px;
    }
    

    http://jsfiddle.net/C7rSa/3/

    0 讨论(0)
  • 2020-12-08 00:40

    I can get it to work but not using a percentage in width. Pixels work fine

    visibility: visible;
    content: "stuff";
    min-width: 29px;
    width: 100%;
    float: left;
    position: relative;
    top: -15px;
    left: 0;
    
    0 讨论(0)
  • 2020-12-08 00:42

    add display:block;

    demo

    p > a.infolink::before {
        display:block;
        content:'?';
        background: blue;
        color: white;
        width: 20ex;
        height: 20ex;
        border:1px solid #000;
    }
    
    0 讨论(0)
提交回复
热议问题