日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

css设置三角形

發布時間:2025/4/16 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 css设置三角形 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.在開發中,有時候會用到一些小三角形來強調或者標記元素,這樣以便區分不同的項目,然后把三角形繪制成一個比較明顯的顏色,就達到效果了,那怎么才能畫出三角形呢,之前我也不清楚,最近看到了有些網頁在使用,在進行標記的時候,都是使用的是背景圖片進行標記,這樣在網頁顯示的時候,感覺有點生硬,畢竟圖片的加載沒有css加載那么順暢

下面看一段代碼:這里設置了一個span 元素為塊級元素,分別設置border的四邊都為不同的顏色:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>div {width: 100%;margin: 50px 0;text-align: center;}span {position: relative;margin: 0 auto;display: block;width: 50px;height: 50px;border-style: solid;border-width: 50px;border-top-color: red;border-left-color: blue;border-bottom-color: yellow;border-right-color: black;}</style> </head> <body><div class="as">如何設置三角形?</div><div class="jindaobox"><span class="sanjiao"></span></div> </body> </html>

運行結果:發現四面的邊框,居然是這種梯形的結構,如果把梯形上底變為0,不就是我們想要的三角形了么,而且這是使用html 和css做不來的,不存在使用靜態頁面就可以實行,不存在圖片的不連續顯示問題;

接下來就是把梯形的上底變為0

上底變為0 很簡單,只要把元素的高和寬設置為0就可以了

width:----->0 得到上下兩種箭頭

height:------->0 得到左右兩種箭頭


1.當我們想要上箭頭的時候,就把元素的左右邊框和下邊框去掉

2.當我們想要下箭頭的時候,就把元素的左右邊框和上邊框去掉

3.當我們想要左箭頭的時候,就把上下邊框和右邊框去掉

4.當我們想要右箭頭的時候,就把上下邊框和左邊框去掉

想法是很好,但是不行,那我們換個方法:既然設置寬度不行,那我們就設置顏色吧,只要把上,左,右邊框的顏色設置為透明的,不就可以了么,css 中,剛好有一個設置顏色為透明的值

span {position: relative;margin: 0 auto;display: block;width: 0px;height: 0px;border-style: solid;border-width: 50pxborder-top-color: transparent;border-left-color: transparent;border-right-color: transparentborder-bottom-color: yellow;}

設置下箭頭:

span {position: relative;margin: 0 auto;display: block;width: 0px;height: 0px;border-style: solid;border-width: 50px;border-bottom-color: transparent;border-left-color: transparent;border-right-color: transparentborder-top-color: red}

設置左箭頭:

span {position: relative;margin: 0 auto;display: block;width: 0px;height: 0px;border-style: solid;border-width: 50px;border-top-color: transparent;border-bottom-color: transparentborder-right-color: transparent;border-left-color: blue;}


設置右箭頭:

span {position: relative;margin: 0 auto;display: block;width: 0px;height: 0px;border-style: solid;border-width: 50px;border-top-color: transparent;border-bottom-color: transparent;border-left-color: transparent;border-right-color: black;}


當然,css 還可寫在一起,這樣看起來要簡單一些:

span {position: relative;margin: 0 auto;display: block;width: 0px;height: 0px;/* 先后設置上右下左的border-color屬性都是一樣的,需要哪個箭頭,再設置哪個方向的顏色屬性,這樣,最后設置的屬性覆蓋了前面的屬性,就變成箭頭了 */border: 50px solid transparent;border-top-color: red;}


以上,是使用html和css兩項綜合起來設置的箭頭,可以不可以再設置簡單一點呢?

下面,我采用class 屬性來設置箭頭,當需要箭頭的時候,直接加上這個class 屬性就可以,當不想要箭頭的時候,去除調這個類就好了

下面來看一個例子:

<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>div {width: 100%;margin: 50px 0;text-align: center;}.jindaobox {position: relative;width: 980px;margin: 20px auto;}li {list-style: none;float: left;position: relative;border: 1px solid #eee;margin-right: 30px;padding: 10px 20px;border-radius: 5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;-ms-border-radius: 5px;-o-border-radius: 5px;}.active {border: 1px solid red !important;}.active::after {position: absolute;content: "";height: 0;width: 0;border: 8px solid transparent;border-top-color: red;top: 0;left: 0;right: 0;margin: auto;}</style> </head><body><div class="as">請選擇你喜歡的電影</div><ul class="jindaobox"><li class="lis">飛龍在天</li><li class="lis active">紫川</li><li class="lis">封神演義</li><li class="lis active">風云第一刀</li><li class="lis">天外飛仙</li></ul></body></html>


這樣,就實現了使用class 屬性控制箭頭的方式,當需要選中時,給li 元素加上一個active class 屬性即可,當不需要時,就去除active class 屬性

文章來源http://www.cnblogs.com/huanying2015

總結

以上是生活随笔為你收集整理的css设置三角形的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。