Best way to replicate the 8 little square of Photoshop transform tool with CSS

后端 未结 1 908
醉酒成梦
醉酒成梦 2021-01-15 17:44

I\'d like some containers on my page to have a 1px stroke with eight little squares just like the Photoshop selection tool below (but without the cross at the center). I wan

相关标签:
1条回答
  • 2021-01-15 18:22

    If you don't want a lot of elements or a compicate way with mulitple gradient here is another idea using special character:

    .box {
      width: 200px;
      height: 200px;
      margin: 20px;
      background: linear-gradient(lightblue, lightblue) 5px 0/calc(100% - 10px) 100% no-repeat;
      position: relative;
      line-height: 180px;
      letter-spacing: 180px;
    }
    
    .box:before {
      content: "\25A1\25A1\25A1";
      letter-spacing: 85px;
      display: block;
      line-height: 0px;
    }
    
    .box:after {
      content: "\25A1\25A1\25A1";
      position: absolute;
      bottom: 2px;
      letter-spacing: 85px;
      display: block;
      line-height: 0px;
    }
    <div class="box">
      □□
    </div>

    Here is anoher more generic solution using multiple background, SVG and CSS variable to make the code light:

    .overlay {
      position: relative;
      pointer-events: none;
      --square: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 20 20"><rect width="20" height="20" fill="transparent" stroke="%23000" stroke-width="3"/></svg>');
    }
    
    .overlay:before {
      content: "";
      position: absolute;
      top: -5px;
      bottom: -5px;
      left: -5px;
      right: -5px;
      background-image: 
        var(--square), var(--square), var(--square), 
        var(--square),                var(--square), 
        var(--square), var(--square), var(--square);
      background-size: 10px 10px;
      background-position: 
        top    left, top center   , top    right,
        center left,                center right,
        bottom left, bottom center, bottom right;
      background-repeat: no-repeat;
    }
    
    .box {
      height: 200px;
      width: 200px;
      background: lightblue;
      display: inline-block;
      vertical-align: top;
      margin: 10px;
    }
    
    p {
      border: 1px solid;
      padding: 50px;
    }
    <div class="box overlay"></div>
    
    <div class="box overlay" style="width:100px;"></div>
    <div class="box overlay" style="height:100px;"></div>
    
    <p class="overlay"> some text</p>

    0 讨论(0)
提交回复
热议问题