温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS中以图换字的示例分析

发布时间:2021-08-03 10:14:16 来源:亿速云 阅读:123 作者:小新 栏目:web开发

这篇文章主要介绍CSS中以图换字的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

文字隐藏

在h2标签中,新增span标签来保存标题内容,然后将其样式设置为display:none

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

负缩进

通过使用text-index:-9999px,这样一个比较大的负缩进,使文本移到页面以外的区域

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

负margin

通过使用margin-left:-2000px,使盒模型向左偏移2000px,然后将宽度设置为2064px,从而页面中只显示2064px中64px的部分。将图片的背景设置为右对齐,且不重复

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

上padding

因为背景是显示在padding-box区域中的,而文本是显示在content-box区域中。所以,将height设置为0,用padding-top来替代height,并设置overflow:hidden。则,可以只显示背景不显示文本

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

0宽高

通过新增一个span标签来保存文本内容,并将该标签的宽高设置为0,再设置溢出隐藏即可

  <style>
    h2 {
      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>
  <h2><span>小火柴的蓝色理想</span></h2>

文本透明

设置文本的颜色为transparent,并设置font-size为1px,即减少行高的影响

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

伪元素

使用before伪元素,content设置为图片的URL,在h2元素上设置溢出隐藏

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

正缩进

设置text-indent:100%,使文本缩进到父元素宽度区域的右侧。然后配合设置white-space:nowrap和overflow:hidden,使文本不换行,并溢出隐藏。从而隐藏文本内容

  <style>
    h2 {
      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>
  <h2>小火柴的蓝色理想</h2>

字体大小

通过设置font-size:0,可以将字体大小设置为0

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

以上是“CSS中以图换字的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI