總結一下各類0.5px的線

在PC端用1px的邊框線,看起來還好,但在手機端看起來就很難看了,而0.5px的分割線會有種精緻的感受。用普通寫法border:solid 0.5px red;iPhone能夠正常顯示,android下幾乎全部的瀏覽器都會把0.5識別爲0,即無邊框狀態.android

原理

原理就是給須要加邊框的元素插入一個僞類,僞類採用絕對定位,而後對僞類添加1px邊框,最後進行0.5倍縮放。transform的縮放和旋轉默認都是按照元素的中心點來操做的
outline元素在縮放0.5以前尺寸就是紅框元素,縮放後,位置到了紅框中心,爲了使之依然在左上角,縮放以前咱們需進行left:-50%;top:-50%的位移
圖片描述web

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>

反作用

當用僞類的絕對定位來實現了邊框後,咱們在first類和first-div類上的點擊事件會失效,由於此時的僞類是絕對定位,並且長寬等於父類元素的長寬,是脫離了文檔流覆蓋在父類上的,僞類不是真實的DOM元素,沒有js點擊事件瀏覽器

  • 解決方案

再寫一個絕對定位元素,覆蓋在父元素上,層級優先級要高一點spa

<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>

圖片描述

相關文章
相關標籤/搜索