CSS用圖換字多種方法

來源:互聯網
上載者:User
這次給大家帶來CSS用圖換字多種方法,CSS用圖換字的注意事項有哪些,下面就是實戰案例,一起來看一下。

前面的話

CSS以圖換字的技術,很久都沒人提起了。它是一種在h1標籤內,使用映像替換文本元素的技術,使頁面在設計和可訪問性之間達到平衡。本文將詳細介紹CSS以圖換字的9種方法

文字隱藏

在h1標籤中,新增span標籤來儲存標題內容,然後將其樣式設定為display:none

  <style>    h1 {      width: 64px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      font: 12px/1 '微軟雅黑';    }    span {      display: none;    }  </style>  <h1>    <span>小火柴的藍色理想</span>  </h1>

負縮排

通過使用text-index:-9999px,這樣一個比較大的負縮排,使文本移到頁面以外的地區

  <style>    h1 {      width: 64px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      font: 12px/1 '微軟雅黑';      text-indent:-9999px;    }  </style>  <h1>小火柴的藍色理想</h1>

負margin

通過使用margin-left:-2000px,使盒模型向左位移2000px,然後將寬度設定為2064px,從而頁面中只顯示2064px中64px的部分。將圖片的背景設定為靠右對齊,且不重複

  <style>    h1 {      width: 2064px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico) right no-repeat;      font: 12px/1 '微軟雅黑';      margin-left:-2000px;    }  </style>  <h1>小火柴的藍色理想</h1>

上padding

因為背景是顯示在padding-box地區中的,而文本是顯示在content-box地區中。所以,將height設定為0,用padding-top來替代height,並設定overflow:hidden。則,可以只顯示背景不顯示文本

  <style>    h1 {      width: 64px;      padding-top: 64px;      height:0;      overflow:hidden;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      font: 12px/1 '微軟雅黑';    }  </style>  <h1>小火柴的藍色理想</h1>

0寬高

通過新增一個span標籤來儲存常值內容,並將該標籤的寬高設定為0,再設定溢出隱藏即可

  <style>    h1 {      width: 64px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      font: 12px/1 '微軟雅黑';    }    span{display:block;width: 0;height:0;overflow:hidden;}  </style>  <h1><span>小火柴的藍色理想</span></h1>

文本透明

設定文本的顏色為transparent,並設定font-size為1px,即減少行高的影響

  <style>    h1 {      width: 64px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      color:transparent;      font-size:1px;      }  </style>  <h1>小火柴的藍色理想</h1>

虛擬元素

使用before虛擬元素,content設定為圖片的URL,在h1元素上設定溢出隱藏

  <style>    h1 {      width: 64px;      height: 64px;      overflow: hidden;      font: 12px/1 '微軟雅黑';    }    h1:before {      content: url(https://static.xiaohuochai.site/icon/icon_64.ico);      display: block;    }  </style>  <h1>小火柴的藍色理想</h1>

正縮排

設定text-indent:100%,使文本縮排到父元素寬度地區的右側。然後配合設定white-space:nowrap和overflow:hidden,使文本不換行,並溢出隱藏。從而隱藏常值內容

  <style>    h1 {      width: 64px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      text-indent: 100%;      white-space: nowrap;      overflow: hidden;      font: 12px/1 '微軟雅黑';    }  </style>  <h1>小火柴的藍色理想</h1>

字型大小

通過設定font-size:0,可以將字型大小設定為0

  <style>    h1 {      width: 64px;      height: 64px;      background: url(https://static.xiaohuochai.site/icon/icon_64.ico);      font-size:0;    }  </style>  <h1>小火柴的藍色理想</h1>

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

CSS怪異盒模型和標準盒模型如何使用

CSS3中的transform功能詳解

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.