html - 關(guān)于CSS實(shí)現(xiàn)border的0.5px設(shè)置?
問(wèn)題描述
網(wǎng)上看到的代碼,有些不理解的地方:
.custom-border{ width:200px; margin:10px auto; height:100px; border:1px solid #333; background-color:#eee; padding:10px;}.scale-border{ margin:10px auto; height:100px; position:relative; padding:10px; width: 200px;}.border{ -webkit-transform:scale(0.5); transform:scale(0.5); position:absolute; border:1px solid #333; top:-50%; right:-50%; bottom:-50%; left:-50%; border-radius: 10px; background-color:#eee;}.content{ position:relative; z-index:2;}<p class='custom-border border-color'>邊框?qū)挾?px</p><p class='scale-border'> <p class='content'>邊框?qū)挾?.5px</p> <p class='border border-color'></p></p>
請(qǐng)問(wèn)在這里CSS代碼中的
top:-50%;right:-50%;bottom:-50%;left:-50%;
是什么意思?實(shí)現(xiàn)這個(gè)0.5px的邊框的原理是什么?btw,transform:scale是不是在項(xiàng)目中挺少用到的?百度了好久關(guān)于scale 的詳細(xì)用法甚少。。
問(wèn)題解答
回答1:其實(shí)主要是scale(0.5)把它縮小到0.5px;然后利用
top:-50%;right:-50%;bottom:-50%;left:-50%;
去把它變大到原來(lái)的大小。但是這個(gè)變大并不影響邊框的大小;
回答2:首先 transform:scale(0.5); 表示縮放1/2的意思,就會(huì)變成這樣(黑色外邊框是特意加上去對(duì)比的):
因?yàn)閷?duì)于縮放而言是整體縮小。所以呢,縮小以后,又需要把她拉回原來(lái)的大小,這樣看起來(lái)才像0.5px的邊框,即:
top:-50%;right:-50%;bottom:-50%;left:-50%;
感覺(jué)多加一個(gè) <p> 來(lái)表示0.5px的大小,并不優(yōu)雅,于是改寫這樣:
.custom-border{ width:200px; margin:10px auto; height:100px; border:1px solid #333; background-color:#eee; padding:10px;}.scale-border{ margin:10px auto; height:100px; position:relative; padding:10px; width: 200px;}.scale-border::after{ content: ’ ’; -webkit-transform:scale(0.5); transform:scale(0.5); position:absolute; border:1px solid #333; top:-50%; right:-50%; bottom:-50%; left:-50%; border-radius: 10px; background-color:#eee;}.content{ position:relative; z-index:2;}
<p class='custom-border border-color'>邊框?qū)挾?px</p><p class='scale-border'> <p class='content'>邊框?qū)挾?.5px</p></p>回答3:
是為了放大到原始.scale-border的兩倍大小。因?yàn)?border是絕對(duì)定位(position:absolute;),所以其定位是根據(jù)其最近的非position:static來(lái)定的,而.scale-border是相對(duì)定位的(position:relative;),所以
top:-50%;right:-50%;bottom:-50%;left:-50%;
就是.border以.scale-border的中心為中心,放大到兩倍,然后再ransform:scale(0.5);縮小到1/2,那就和.scale-border一樣大小了。此時(shí)的 1px border,就變?yōu)?0.5px。
transform應(yīng)該可以放心使用。
回答4:兄弟,看這個(gè)你就明白了。https://developer.mozilla.org...
相關(guān)文章:
1. 視頻文件不能播放,怎么辦?2. mysql - 把一個(gè)表中的數(shù)據(jù)count更新到另一個(gè)表里?3. 請(qǐng)教使用PDO連接MSSQL數(shù)據(jù)庫(kù)插入是亂碼問(wèn)題?4. mysql 查詢身份證號(hào)字段值有效的數(shù)據(jù)5. visual-studio - Python OpenCV: 奇怪的自動(dòng)補(bǔ)全問(wèn)題6. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來(lái)哪些效率或者其他方面的好處7. node.js - nodejs開發(fā)中常用的連接mysql的庫(kù)8. python bottle跑起來(lái)以后,定時(shí)執(zhí)行的任務(wù)為什么每次都重復(fù)(多)執(zhí)行一次?9. python - 爬蟲模擬登錄后,爬取csdn后臺(tái)文章列表遇到的問(wèn)題10. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?
