border的使用

 

border tag可以針對顏色(color)、樣式(style)、寬度(width)類別做控制,

也可以針對上下左右方向控制。

 

一、顏色(color)、樣式(style)、寬度(width)屬性

1.border-color

border-color可以使用name、 RGB、 Hex來表示

例如:border-color:blue;、border-color:rgb(0,0,255);、border-color:#0000ff;、

border-color:transparent

 

2.border-style

一個CSS寫法如p {border-style: none;}

以下列舉各border-style樣式

注意,當你要操作border的各種屬性時務必指定border-style樣式,

否則border就沒效果。

 

3.border-width

被用來設定border的粗細,

例如:border-width:5px;或預設的三種樣式

border-width:thin;、border-width:medium;、border-width:thick

<html>

<style type="text/css">
    .thin {
        border-style: solid;
        border-width: thin;
    }

    .medium {
        border-style: solid;
        border-width: medium;
    }

    .thick {
        border-style: solid;
        border-width: thick;
    }
</style>

<body>
    <p class="thin">aaa</p>
    <p class="medium">aaa</p>
    <p class="thick">aaa</p>
</body>
</html>

 

二、可控制上下左右(border-top、border-bottom、border-left、border-right)

的顏色(color)、樣式(style)、寬度(width)屬性

例如:border-top-color:red;、border-top-style:solid;、border-bottom-width:10px;...等等。

也可以省略方向中間詞

例如:

border-style: dotted; dotted影響範圍為上下左右

border-style:dotted solid; dotted影響範圍為上下,solid影響範圍為左右

border-style: dotted solid double;  dotted影響範圍為上,solid影響範圍為左右,double影響範圍為下

border-style: dotted solid double dashed; dotted影響範圍為上,solid影響範圍為右,double影響範圍為下,dashed影響範圍為左(順時鐘)

由上可知,border-color與border-width一樣類推

 

三、border shorthand property可控制顏色(color)、樣式(style)、寬度(width)屬性

例如:border: 5px solid red;

上例屬性可不必按照順序排列

另外一種樣式為包含方向中間詞

例如:border-top: thick double #ff0000;

 

四、範例

1、利用border屬性來做三角形

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        div {
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 200px 100px 0 100px;
            border-color: #bf00ff transparent transparent transparent;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

 

2、對話框

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <style>
        /* General CSS Setup */
        body {
            background-color: lightblue;
            font-family: "Ubuntu-Italic", "Lucida Sans", helvetica, sans;
        }

        /* container */
        .container {
            padding: 5% 5%;
        }

        /* CSS talk bubble */
        .talk-bubble {
            margin: 40px;
            display: inline-block;
            position: relative;
            width: 200px;
            height: auto;
            background-color: lightyellow;
        }

        .border {
            border: 8px solid #666;
        }

        .round {
            border-radius: 30px;
            -webkit-border-radius: 30px;
            -moz-border-radius: 30px;
        }

        /* Right triangle placed top left flush. */
        .tri-right.border.left-top:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: -40px;
            right: auto;
            top: -8px;
            bottom: auto;
            border: 32px solid;
            border-color: #666 transparent transparent transparent;
        }

        .tri-right.left-top:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: -20px;
            right: auto;
            top: 0px;
            bottom: auto;
            border: 22px solid;
            border-color: lightyellow transparent transparent transparent;
        }

        /* Right triangle, left side slightly down */
        .tri-right.border.left-in:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: -40px;
            right: auto;
            top: 30px;
            bottom: auto;
            border: 20px solid;
            border-color: #666 #666 transparent transparent;
        }

        .tri-right.left-in:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: -20px;
            right: auto;
            top: 38px;
            bottom: auto;
            border: 12px solid;
            border-color: lightyellow lightyellow transparent transparent;
        }

        /*Right triangle, placed bottom left side slightly in*/
        .tri-right.border.btm-left:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: -8px;
            right: auto;
            top: auto;
            bottom: -40px;
            border: 32px solid;
            border-color: transparent transparent transparent #666;
        }

        .tri-right.btm-left:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: 0px;
            right: auto;
            top: auto;
            bottom: -20px;
            border: 22px solid;
            border-color: transparent transparent transparent lightyellow;
        }

        /*Right triangle, placed bottom left side slightly in*/
        .tri-right.border.btm-left-in:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: 30px;
            right: auto;
            top: auto;
            bottom: -40px;
            border: 20px solid;
            border-color: #666 transparent transparent #666;
        }

        .tri-right.btm-left-in:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: 38px;
            right: auto;
            top: auto;
            bottom: -20px;
            border: 12px solid;
            border-color: lightyellow transparent transparent lightyellow;
        }

        /*Right triangle, placed bottom right side slightly in*/
        .tri-right.border.btm-right-in:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: 30px;
            bottom: -40px;
            border: 20px solid;
            border-color: #666 #666 transparent transparent;
        }

        .tri-right.btm-right-in:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: 38px;
            bottom: -20px;
            border: 12px solid;
            border-color: lightyellow lightyellow transparent transparent;
        }


        /*Right triangle, placed bottom right side slightly in*/
        .tri-right.border.btm-right:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: -8px;
            bottom: -40px;
            border: 20px solid;
            border-color: #666 #666 transparent transparent;
        }

        .tri-right.btm-right:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: 0px;
            bottom: -20px;
            border: 12px solid;
            border-color: lightyellow lightyellow transparent transparent;
        }

        /* Right triangle, right side slightly down*/
        .tri-right.border.right-in:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: -40px;
            top: 30px;
            bottom: auto;
            border: 20px solid;
            border-color: #666 transparent transparent #666;
        }

        .tri-right.right-in:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: -20px;
            top: 38px;
            bottom: auto;
            border: 12px solid;
            border-color: lightyellow transparent transparent lightyellow;
        }

        /* Right triangle placed top right flush. */
        .tri-right.border.right-top:before {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: -40px;
            top: -8px;
            bottom: auto;
            border: 32px solid;
            border-color: #666 transparent transparent transparent;
        }

        .tri-right.right-top:after {
            content: ' ';
            position: absolute;
            width: 0;
            height: 0;
            left: auto;
            right: -20px;
            top: 0px;
            bottom: auto;
            border: 20px solid;
            border-color: lightyellow transparent transparent transparent;
        }

        /* talk bubble contents */
        .talktext {
            padding: 1em;
            text-align: left;
            line-height: 1.5em;
        }

            .talktext p {
                /* remove webkit p margins */
                -webkit-margin-before: 0em;
                -webkit-margin-after: 0em;
            }
    </style>
</head>
<body>
    <div class="talk-bubble">
        <div class="talktext">
            <p>CSS Talk Bubble configured by classes. Defaults to square shape, no triangle. Height is auto-adjusting to the height of the text.</p>
        </div>
    </div>
    <div class="talk-bubble tri-right left-top">
        <div class="talktext">
            <p>This one adds a right triangle on the left, flush at the top by using .tri-right and .left-top to specify the location.</p>
        </div>
    </div>
    <div class="talk-bubble tri-right left-in">
        <div class="talktext">
            <p>This talk-bubble uses .left-in class to show a triangle on the left slightly indented. Still a blocky square.</p>
        </div>
    </div>
    <div class="talk-bubble tri-right round btm-left">
        <div class="talktext">
            <p>And now using .round we can smooth the sides down. Also uses .btm-left to show a triangle at the bottom flush to the left.</p>
        </div>
    </div>
    <div class="talk-bubble tri-right border round btm-left-in">
        <div class="talktext">
            <p>Now we add a border and it looks like a comic. Uses .border .round and .btm-left-in</p>
        </div>
    </div>
    <div class="talk-bubble tri-right border btm-right-in">
        <div class="talktext">
            <p>Now flipped the other way and square. Uses .border and .btm-right-in</p>
        </div>
    </div>
    <div class="talk-bubble tri-right btm-right">
        <div class="talktext">
            <p>Flush to the bottom right. Uses .btm-right only.</p>
        </div>
    </div>
    <div class="talk-bubble tri-right round right-in">
        <div class="talktext">
            <p>Moving our way back up the right side indented. Uses .round and .right-in</p>
        </div>
    </div>
    <div class="talk-bubble tri-right round border right-top">
        <div class="talktext">
            <p>And finally on the right flush at the top. Uses .round .border and .right-top</p>
        </div>
    </div>
</body>
</html>

 

3、麵包屑路徑

<html>
<head>
    <meta charset="utf-8">
    <title>CSS Breadcrumbs demo</title>

    <style>
        body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, input, textarea, blockquote {
            margin: 0;
            padding: 0;
            border: 0;
        }

        body {
            margin: 200px;
            font-family: Helvetica;
            background: #d4f2ff;
        }

        #crumbs {
            text-align: center;
        }

            #crumbs ul {
                list-style: none;
                display: inline-table;
            }

                #crumbs ul li {
                    display: inline;
                }

                    #crumbs ul li a {
                        display: block;
                        float: left;
                        height: 50px;
                        background: #3498db;
                        text-align: center;
                        padding: 30px 40px 0 80px;
                        position: relative;
                        margin: 0 10px 0 0;
                        font-size: 20px;
                        text-decoration: none;
                        color: #fff;
                    }

                        #crumbs ul li a:after {
                            content: "";
                            border-top: 40px solid transparent;
                            border-bottom: 40px solid transparent;
                            border-left: 40px solid #3498db;
                            position: absolute;
                            right: -40px;
                            top: 0;
                            z-index: 1;
                        }

                        #crumbs ul li a:before {
                            content: "";
                            border-top: 40px solid transparent;
                            border-bottom: 40px solid transparent;
                            border-left: 40px solid #d4f2ff;
                            position: absolute;
                            left: 0;
                            top: 0;
                        }

                    #crumbs ul li:first-child a {
                        border-top-left-radius: 10px;
                        border-bottom-left-radius: 10px;
                    }

                        #crumbs ul li:first-child a:before {
                            display: none;
                        }

                    #crumbs ul li:last-child a {
                        padding-right: 80px;
                        border-top-right-radius: 10px;
                        border-bottom-right-radius: 10px;
                    }

                        #crumbs ul li:last-child a:after {
                            display: none;
                        }

                    #crumbs ul li a:hover {
                        background: #fa5ba5;
                    }

                        #crumbs ul li a:hover:after {
                            border-left-color: #fa5ba5;
                        }
    </style>

</head>
<body>

    <div id="crumbs">
        <ul>
            <li><a href="#1">One</a></li>
            <li><a href="#2">Two</a></li>
            <li><a href="#3">Three</a></li>
            <li><a href="#4">Four</a></li>
            <li><a href="#5">Five</a></li>
        </ul>
    </div>

</body>
</html>

 

參考資料:

CSS三角形產生器

How To Create Flat Style Breadcrumb Links with CSS