国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > CSS > 正文

css實(shí)現(xiàn)各種0.5px的線(小結(jié))

2024-07-11 08:42:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在PC端用1px的邊框線,看起來(lái)還好,但在手機(jī)端看起來(lái)就很難看了,而0.5px的分割線會(huì)有種精致的感覺(jué)。用普通寫(xiě)法border:solid 0.5px red;iPhone可以正常顯示,android下幾乎所有的瀏覽器都會(huì)把0.5識(shí)別為0,即無(wú)邊框狀態(tài).

原理

原理就是給需要加邊框的元素插入一個(gè)偽類(lèi),偽類(lèi)采用絕對(duì)定位,然后對(duì)偽類(lèi)添加1px邊框,最后進(jìn)行0.5倍縮放。

transform的縮放和旋轉(zhuǎn)默認(rèn)都是按照元素的中心點(diǎn)來(lái)操作的

outline元素在縮放0.5之前尺寸就是紅框元素,縮放后,位置到了紅框中心,為了使之依然在左上角,縮放之前我們需進(jìn)行l(wèi)eft:-50%;top:-50%的位移。

0.5px邊框

<div class="first">  <div class="first-div">    HELLO WORLD  </div></div><style>.first{  position: relative;  font-size: 16px;}.first .first-div:before{  content: "";  position: absolute;  top: -50%;  bottom: -50%;  left: -50%;  right: -50%;  width: 200%;  height: 200%;  -webkit-transform: scale(0.5);  transform: scale(0.5);  border: solid 1px red;  box-sizing:border-box;}</style>

副作用

當(dāng)用偽類(lèi)的絕對(duì)定位來(lái)實(shí)現(xiàn)了邊框后,我們?cè)趂irst類(lèi)和first-div類(lèi)上的點(diǎn)擊事件會(huì)失效,因?yàn)榇藭r(shí)的偽類(lèi)是絕對(duì)定位,而且長(zhǎng)寬等于父類(lèi)元素的長(zhǎng)寬,是脫離了文檔流覆蓋在父類(lèi)上的,偽類(lèi)不是真實(shí)的DOM元素,沒(méi)有js點(diǎn)擊事件

解決方案

再寫(xiě)一個(gè)絕對(duì)定位元素,覆蓋在父元素上,層級(jí)優(yōu)先級(jí)要高一點(diǎn)

<div class="first">  <div class="first-div">    HELLO WORLD  </div>  <div class="click-able" onclick="alert('click')"></div></div><style>.first{  position: relative;  font-size: 16px;}.first .first-div:before{  content: "";  position: absolute;  top: -50%;  bottom: -50%;  left: -50%;  right: -50%;  width: 200%;  height: 200%;  -webkit-transform: scale(0.5);  transform: scale(0.5);  border: solid 1px red;  box-sizing:border-box;}.click-able{  position: absolute;  top: 0;  bottom: 0;  left: 0;  right: 0;  z-index: 10;}  </style>

0.5px圓角邊框

<div class="round">  <div class="round-div">    HELLO WORLD  </div></div><style>.round{   position: relative;   font-size: 16px; }.round .round-div:before{  content: "";  position: absolute;  top: -50%;  bottom: -50%;  left: -50%;  right: -50%;  width: 200%;  height: 200%;  -webkit-transform: scale(0.5);  transform: scale(0.5);  border: solid 1px red;  border-radius: 22px;  box-sizing:border-box;}</style>

0.5px左邊線

<div class="left">  <div class="left-div">    HELLO WORLD  </div></div><style>.left{  position: relative;  font-size: 16px;}.left .left-div:before{  content: " ";  position: absolute;  left: 0;  bottom: 0;  width: 1px;  height: 100%;  border-left: 1px solid red;  -webkit-transform-origin: 0 0;  transform-origin: 0 0;  -webkit-transform: scaleX(0.5);  transform: scaleX(0.5);}</style>

0.5px右邊線

<div class="right">  <div class="right-div">    HELLO WORLD  </div></div><style>.right{  position: relative;  font-size: 16px;  display: inline-block;}.right .right-div:before{  content: " ";  position: absolute;  right: 0;  bottom: 0;  width: 1px;  height: 100%;  border-right: 1px solid red;  -webkit-transform-origin: 0 0;  transform-origin: 0 0;  -webkit-transform: scaleX(0.5);  transform: scaleX(0.5);}</style>

0.5px底部線

<div class="bottom">  <div class="bottom-div">    HELLO WORLD  </div></div><style>.bottom{  position: relative;  font-size: 16px;}.bottom .bottom-div:before{  content: " ";  position: absolute;  left: 0;  bottom: 0;  width: 100%;  height: 1px;  border-top: 1px solid red;  -webkit-transform-origin: 0 0;  transform-origin: 0 0;  -webkit-transform: scaleY(0.5);  transform: scaleY(0.5);}</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 井研县| 九龙城区| 瑞金市| 随州市| 茌平县| 察隅县| 宁陕县| 岳阳县| 鄂托克旗| 扎鲁特旗| 连江县| 龙山县| 马龙县| 旬阳县| 阿拉善右旗| 沈阳市| 汶上县| 灯塔市| 阿拉善盟| 龙海市| 柯坪县| 崇阳县| 会宁县| 黔江区| 西城区| 昆山市| 杨浦区| 济宁市| 泰安市| 化德县| 集安市| 柏乡县| 南和县| 金昌市| 南安市| 四川省| 莫力| 五大连池市| 扬中市| 合川市| 靖西县|