Border Corners: Triangle

前端 未结 3 1693
不知归路
不知归路 2021-01-20 04:44

Is it possible to get rid of the \"triangle\" shape in the border corners? (when using different color borders)

See this example:

http://jsfiddle.net/GLsqV/

相关标签:
3条回答
  • 2021-01-20 05:00

    Not everywhere with a single element. (Actually, the default triangular shape of borders is what makes great things possible: The Shapes of CSS)

    However, what you are asking for is easily possible everywhere with another child element.

    CSS:

    .borders {
      width: 520px;
      height: 500px;
      border-top: 10px solid red;
      border-bottom: 10px solid green;
    }
    
    .borders2 {
      background: #efefef;
      width: 500px;
      height: 500px;    
      border-left: 10px solid black;
      border-right: 10px solid black;
    }
    

    ​ HTML:

    <div class="borders">
      <div class="borders2">
      </div>
    </div>​
    

    With these values, the outer DIV's border-box will still have a size of 520×520 pixels.

    See also this fiddle.

    0 讨论(0)
  • 2021-01-20 05:18

    One option using generated content:

    .borders {
        width:500px;
        height:500px;
        position: relative;
        background:#efefef;
        border-top:10px solid red;
        border-bottom:10px solid green;
    }
    
    .borders::before,
    .borders::after {
        content: '';
        position: absolute;
        width: 10px;
        top: 0;
        bottom: 0;
        background-color: #000;
    }
    
    .borders::before {
        left: 0;
    }
    
    .borders::after {
        right: 0;
    }
    

    JS Fiddle demo.

    Or with nested HTML (if you really must):

    <div class="borders">
        <div class="innerBorder left"></div>
        <div class="innerBorder right"></div>
    </div>​
    
    .borders {
        width:500px;
        height:500px;
        position: relative;
        background:#efefef;
        border-top:10px solid red;
        border-bottom:10px solid green;
    }
    
    .borders .innerBorder{
        content: '';
        position: absolute;
        width: 10px;
        top: 0;
        bottom: 0;
        background-color: #000;
    }
    
    .borders .left {
        left: 0;
    }
    
    .borders .right {
        right: 0;
    }
    

    JS Fiddle demo.

    And a single-nested-element solution in which the left, and right, border-color is the background-color of the wrapping element, and the width controlled by the margin of the descendant:

    <div class="borders">
        <div class="inner"></div>
    </div>​
    

    CSS:

    .borders {
        width:500px;
        height:500px;
        background-color: #000;
        border-top:10px solid red;
        border-bottom:10px solid green;
    }
    
    .borders .inner {
        background-color: #efefef;
        height: 100%;
        margin: 0 10px;
    }
    

    JS Fiddle demo.​

    0 讨论(0)
  • 2021-01-20 05:19

    That's how borders work. How would the browser decide which ones overlap the corners otherwise?

    You can achieve this effect using a nested DIV, or using a trick with :before and :after with some absolute positioning.

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