Pointed/angled arrow-style borders in CSS

前端 未结 3 2099
我寻月下人不归
我寻月下人不归 2021-02-05 21:46

I\'m making a breadcrumb menu and attempting to do it in pure CSS so I don\'t have to use a background image to get an arrow shape. Is it possible to achieve this angled border

相关标签:
3条回答
  • 2021-02-05 22:11

    Stealing from CSS Tricks - CSS Triangle, you can do something like

    li.breadcrumb:after {
        content: "";
        display: inline-block;
        width: 0;
        height: 0;
        border-top: 20px solid #eee;
        border-bottom: 20px solid #eee;
        border-left: 20px solid #ccc;
    }
    

    li.breadcrumb:after {
        content: "";
        display: inline-block;
        width: 0;
        height: 0;
        border-top: 20px solid #eee;
        border-bottom: 20px solid #eee;
        border-left: 20px solid #ccc;
    }
    li.first-crumb:after {
        border-top: 20px solid #ccc;
        border-bottom: 20px solid #ccc;
        border-left: 20px solid #aaa;
    }
    li.last-crumb:after {
        border-top: 20px solid #fff;
        border-bottom: 20px solid #fff;
        border-left: 20px solid #eee;
    }
    
    li.breadcrumb {
        list-style-type: none;
        background-color: #ccc;
        display: inline-block;
        float: left;
        line-height: 0;
    }
    li.first-crumb {
        background: #aaa;
    }
    li.last-crumb {
        background: #eee;
    }
    li.breadcrumb a {
        text-decoration: none;
    }
    <div class="breadcrumb">
        <ul class="breadcrumb">
            <li class="breadcrumb first-crumb"><a href="#">Hurr</a>
            </li>
            <li class="breadcrumb"><a href="#">Durr</a>
            </li>
            <li class="breadcrumb last-crumb"><span>Furr</span>
            </li>
        </ul>
    </div>
    <div class="arrow-right"></div>


    Original JSFiddle

    0 讨论(0)
  • 2021-02-05 22:14

    you can get those borders too if you want (I have improved the previous answer by Olaf Dietsche):

    HTML:

    <div class="breadcrumb">
        <ul class="breadcrumb">
            <li class="breadcrumb first-crumb"><a href="#">Hurr</a></li>
            <li class="breadcrumb"><a href="#">Durr</a></li>
            <li class="breadcrumb last-crumb"><span>Furr</span></li>
        </ul>
    </div>
    

    CSS:

    li.breadcrumb:before {
        content:'';
        width: 28.28427px; /* sqrt(40*40 / 2) */
        height: 28.28427px;
        background:transparent;
        position:absolute;
        -moz-transform:rotate(45deg);
        -webkit-transform:rotate(45deg);
        transform:rotate(45deg);
        transform-origin: top right;
        top: 20px;
        margin:0;
        right: 0;
        border-right: #000 solid 1px;
        border-top: #000 solid 1px;
    }
    
    li.breadcrumb:after {
        content: "";
        display: inline-block;
        width: 0;
        height: 0;
        border-top: 20px solid #eee;
        border-bottom: 20px solid #eee;
        border-left: 20px solid #ccc;
    }
    li.first-crumb:after {
        border-top: 20px solid #ccc;
        border-bottom: 20px solid #ccc;
        border-left: 20px solid #aaa;
    }
    li.last-crumb:after {
        border-top: 20px solid #fff;
        border-bottom: 20px solid #fff;
        border-left: 20px solid #eee;
    }
    
    li.breadcrumb {
        list-style-type: none;
        background-color: #ccc;
        display: inline-block;
        float: left;
        line-height: 0;
        position: relative;
        height: 40px;
    }
    li.first-crumb {
        background: #aaa;
    }
    li.last-crumb {
        background: #eee;
    }
    li.breadcrumb a {
        text-decoration: none;
    }
    

    and the fiddle: http://jsfiddle.net/piotku/9cs1zy4h/

    0 讨论(0)
  • 2021-02-05 22:20

    Another way to do it is to use an :after element and make this a rectangle with two borders and rotate this.

    Here is an example: Breadcrumb

    Main CSS:

    li-item:after {
      content: "";
      width: 5rem;
      height: 100%;
      display: inline-block;
      position: absolute;
      background: transparent;
      right: 0;
      top: 0;
      transform: translateX(-1rem) rotate(45deg); 
      border-right: 4px solid white;
      border-top: 4px solid white;
    }
    
    0 讨论(0)
提交回复
热议问题