css 图像替换技术

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了css 图像替换技术相关的知识,希望对你有一定的参考价值。

TECHNIQUE 1

<h3 class="visuallyhidden">
  CSS-Tricks
</h3>

h3.visuallyhidden {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

This doesn't really replace a single element with an image, it just does a good job at hiding an element from view while remaining accessible to screen readers. 
So you could use whatever you wanted to display the image, then put an <h3 class="visuallyhidden">Title</h3> next to it.

--------------------------------------------------------------------------------

Instead of setting a large negative text-indent, you can set it to 100%.
This way the browser won’t need to create a large box and the performance will improve. 
Text is pushed on the side.
Setting overflow to hidden hides the text and whitespace: nowrap keeps it from moving onto the next line. 
The text will still be read by screen readers in this method. 

.replace-scott {
  width: 264px;
  height: 106px;
  background: url("assets/logo.png");
  text-indent: 100%;
  white-space: nowrap;
  overflow: hidden;
}

--------------------------------------------------------------------------------
!! Elements with display set to none are ignored by screen readers. The exact same issue occurs with visibility set to hidden. 
!! What you can do is set opacity to 0. This will hide the element while still keeping it accessible to screen readers.

<h1 class="replace-display">
  <span>SitePoint</span>
</h1>

.replace-display {
  width: 264px;
  height: 106px;
  background: url("assets/logo.png");
}

.replace-display span{
  // display: none;   //DON'T USE!!!
  
  opacity: 0; // used instead
}

--------------------------------------------------------------------------------

We set the width and height of our span element to zero. 
This forces the text inside the element to move out of its box. 
The text is then hidden by the overflow: hidden property.

.replace-overflow {
  width: 264px;
  height: 106px;
  background: url("assets/logo.png");
}

.replace-overflow span {
  display: block;
  width: 0;
  height: 0;
  overflow: hidden;
}

The text is still accessible to screen readers. Hence, there are no accessibility issues.

--------------------------------------------------------------------------------

What not to do on with the text to not get penalized by Google
https://support.google.com/webmasters/answer/66353

以上是关于css 图像替换技术的主要内容,如果未能解决你的问题,请参考以下文章

CSS 摆脱FF中链接的虚线边框;使用图像替换技术

CSS面试题总结2(转)

css Css图像替换

css CSS:图像替换

css Css:图像替换

CSS 标准CSS图像替换