How to make a pure css triangle which has a white center

前端 未结 4 1125
囚心锁ツ
囚心锁ツ 2021-02-20 17:45

I want to create an upward and downward facing arrow with css like the following: http://apps.eky.hk/css-triangle-generator/

However, instead of a solid color, I want to

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-20 18:23

    If you want to create a triangle with borders (or box shadow look-alike) in pure CSS, you should use pseudo-elements :before and :after.

    In my example, I added display:inline-block; to the element .arrow-dropdown to be able to create some kind of dropdown menu with a drop shadow. It is followed by .arrow-only which is a a basic triangle with a red border.

    body {
        margin: 10px;
        background: #1670c4;
    }
    .text {
        display: inline-block;
        font-size: 15px;
        color: #fff;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.4);
        cursor: default;
    }
    .arrow-dropdown {
        display: inline-block;
        position: relative;
        vertical-align: middle;
        margin: 1px 0 0 8px;
        width: 8px;
        height: 7px;
    }
    .arrow-dropdown:after {
        content: '';
        position: absolute;
        border-style: solid;
        border-width: 7px 4px 0;
        border-color: #fff transparent transparent transparent;
        display: block;
        width: 0;
        z-index: 1;
    }
    .arrow-dropdown:before {
        content: '';
        position: absolute;
        border-style: solid;
        border-width: 8px 5px 0;
        border-color: rgba(0,0,0,0.3) transparent transparent transparent;
        display: block;
        width: 0;
        z-index: 0;
    }
    .arrow-only {
        position: relative;
        vertical-align: middle;
        margin: 10px 0 0 8px;
        width: 8px;
        height: 7px;
    }
    .arrow-only:after {
        content: '';
        position: absolute;
        border-style: solid;
        border-width: 12px 9px 0;
        border-color: #fff transparent transparent transparent;
        display: block;
        width: 0;
        z-index: 1;
    }
    .arrow-only:before {
        content: '';
        position: absolute;
        border-style: solid;
        border-width: 15px 11px 0;
        border-color: #f00 transparent transparent transparent;
        display: block;
        width: 0;
        z-index: 0;
        margin:-1px 0 0 -2px;
    }
    Dropdown text

提交回复
热议问题