简体   繁体   English

可以使用CSS设置alt属性吗?

[英]Can an alt attribute be set using CSS?

I'm setting an image using a <div> tag like this: 我正在使用<div>标签设置图像,如下所示:

<div id="logo"></div>

and here's the CSS: 这是CSS:

    #logo {
      background: url(../images/logo.png) no-repeat center;
      width: 180px;
      height: 82px;
      float: right; 
      padding: 15px 15px 0 10px;
      position: relative;
    }

How do I set an alt="" attribute for the image? 如何为图像设置alt =“”属性?

thanks! 谢谢!

That's not an image in the HTML sense - which is the only type of image an alt attribute (not tag) can be applied to. 从HTML的角度来看,这不是图像-这是alt属性(不是标签)可以应用的唯一图像类型。

You can't apply attributes of any kind via CSS - it's for styling, not applying mark-up. 您不能通过CSS应用任何类型的属性-用于样式设置,而不是应用标记。

It is impossible to set an <img alt=""> with css . css设置<img alt="">是不可能的。 But this can be done with JavaScript Example 但这可以通过JavaScript示例完成

<div class="logo><img src="../image.png"></div>

document.getElementByTagName('img').alt = "Anything";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM