how to create inline style with :before and :after

前端 未结 5 1020
时光说笑
时光说笑 2021-01-31 01:19

I generated a bubble chat thingy from http://www.ilikepixels.co.uk/drop/bubbler/

In my page I put a number inside of it

.bubble {
  position: relative;
          


        
5条回答
  •  旧巷少年郎
    2021-01-31 01:47

    You can, using CSS variables (more precisely called CSS custom properties).

    • Set your variable in your style attribute: style="--my-color-var: orange;"
    • Use the variable in your stylesheet: background-color: var(--my-color-var);

    Browser compatibility

    Minimal example:

    div {
      width: 100px;
      height: 100px;
      position: relative;
      border: 1px solid black;
    }
    
    div:after {
      background-color: var(--my-color-var);
      content: '';
      position: absolute;
      top: 0;
      bottom: 0;
      right: 0;
      left: 0;
    }

    Your example:

    .bubble {
      position: relative;
      width: 30px;
      height: 15px;
      padding: 0;
      background: #FFF;
      border: 1px solid #000;
      border-radius: 5px;
      text-align: center;
      background-color: var(--bubble-color);
    }
    
    .bubble:after {
      content: "";
      position: absolute;
      top: 4px;
      left: -4px;
      border-style: solid;
      border-width: 3px 4px 3px 0;
      border-color: transparent var(--bubble-color);
       display: block;
      width: 0;
      z-index: 1;
      
    }
    
    .bubble:before {
      content: "";
      position: absolute;
      top: 4px;
      left: -5px;
      border-style: solid;
      border-width: 3px 4px 3px 0;
      border-color: transparent #000;
      display: block;
      width: 0;
      z-index: 0;
    }
    100

提交回复
热议问题