How can I show only corner borders?

后端 未结 16 1643
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 10:06

I\'m wondering if it\'s possible in CSS or jQuery to make a border but only for corner. Something like this:

****                         ****
*                     


        
16条回答
  •  感情败类
    2020-11-22 10:28

    Here are a couple of methods to create this effect without using any extra pseudo/real elements. One thing to note is that both these approaches would work only in modern browsers because they use CSS3 properties.

    Using border-image: The border-image property makes it pretty easy to create such effects. The approach is as follows:

    • Create a transparent image which has borders just in the corner like here.
    • Set this image as the border-image-source and let the browser take care of the rest :) Since the default value for border-image-repeat is stretch, the browser would stretch the original image to fit the container even if the container becomes large.
    • The value set for the border-image-width property determines how thick the borders are.

    .bordered {
      background-color: beige;
      border-image-source: url("http://i.stack.imgur.com/s2CAw.png");
      border-image-slice: 1;
      border-image-width: 5px;
    }
    .square {
      height: 150px;
      width: 150px;
    }
    .large-square {
      height: 350px;
      width: 350px;
    }
    
    /* Just for demo */
    
    div {
      margin-bottom: 10px;
    }

    Advantages:

    • Needs no extra elements (pseudo or real) which means less cluttered markup, pseudo elements can be used for other needs.
    • Is reasonably responsive. That is browser will adapt the borders even if container's dimensions change.

    Drawbacks:

    • Relatively lower browser support. If IE10- support is needed then this is a no-go.
    • Since the border image is getting stretched, if the original image's canvas is a square and the container is a rectangle then the borders would look wider at top and bottom than left and right.

      .bordered {
        background-color: beige;
        border-image-source: url("http://i.stack.imgur.com/s2CAw.png");
        border-image-slice: 2;
        border-image-width: 5px;
      }
      .small-square {
        height: 75px;
        width: 75px;
      }
      .square {
        height: 150px;
        width: 150px;
      }
      .large-square {
        height: 350px;
        width: 350px;
      }
      .rectangle {
        height: 150px;
        width: 250px;
      }
      .large-rectangle {
        height: 150px;
        width: 350px;
      }
      
      /* Just for demo */
      
      div {
        margin-bottom: 10px;
      }


    Using background-image: The background-image property can also be used with linear-gradient images to produce the effect. The approach is as follows:

    • Create four linear-gradient images (two for top, bottom and two for left, right). These gradients would start with required color and continue to be that color for as many pixels as the width/height of the border image. After that it should be transparent.
    • For top and bottom borders, gradient's direction should be to right. For left and right borders, it should be to bottom.
    • The background-size value determines the thickness of the border. For top and bottom borders, the size of the gradient image would be 100% in X-axis and 5px (thickness) in Y-axis. For left and right borders, the size would 5px (thickness) in X-axis and 100% in Y-axis.
    • The background-repeat should be set to repeat-x for the top, bottom borders and to repeat-y for left and right borders.
    • The background-position is set to (-1 * half the size of the color in gradient) in the X or Y-axis as appropriate. This is to make half of the colored area appear on one side of the element while the other half appears on the other side (because gradient is repeating).

    .bordered.square {
      height: 150px;
      width: 150px;
    }
    .bordered.rectangle {
      height: 150px;
      width: 250px;
    }
    .bordered {
      background-color: beige;
      background-image: linear-gradient(to right, black 30px, transparent 30px), linear-gradient(to right, black 30px, transparent 30px), linear-gradient(to bottom, black 30px, transparent 30px), linear-gradient(to bottom, black 30px, transparent 30px);
      background-size: 100% 5px, 100% 5px, 5px 100%, 5px 100%;
      background-position: -15px 0%, -15px 100%, 0% -15px, 100% -15px;
      background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
    }
    
    /* Just for demo */
    
    div {
      margin-bottom: 10px;
    }

    Advantages:

    • Needs no extra elements (pseudo or real) which means less cluttered markup, pseudo elements can be used for other needs.
    • Is reasonably responsive as the width of the color in gradient is fixed. If the width of the borders dashes need to change according to the container's dimensions then we can change the pixels value in gradient to percentage (with a few more minor changes) like in below snippet.

      .bordered.square {
        height: 150px;
        width: 150px;
      }
      .bordered.large-square {
        height: 250px;
        width: 250px;
      }
      .bordered {
        background-color: beige;
        background-image: linear-gradient(to right, black 10%, transparent 10%), linear-gradient(to right, black 10%, transparent 10%), linear-gradient(to bottom, black 10%, transparent 10%), linear-gradient(to bottom, black 10%, transparent 10%);
        background-size: 90% 5px, 90% 5px, 5px 90%, 5px 90%;
        background-position: 0% 0%, 0% 100%, 0% 0%, 100% 0%;
        background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
      }
      
      /* Just for demo */
      
      div {
        margin-bottom: 10px;
      }

    Drawbacks:

    • Relatively better browser support. If IE9- support is needed then this is a no-go.
    • If percentage based gradient is used then the same drawback with rectangles as mentioned for border-image would be applicable here also.

提交回复
热议问题