使用css设置图像标题属性的样式?

时间:2012-07-26 15:38:56

标签: html css

我想在将鼠标悬停在图片上时看到的文字样式。我尝试了以下但不起作用:

<body>
    <img src="img.jpg" title = "<span class='title'> title </span>
</body>

我的风格在head。我想知道为什么它被显示为纯文本以及为什么样式不起作用。

7 个答案:

答案 0 :(得分:20)

没有什么是不可能的。已编辑the solution by Andres Ilich问题:How to change the style of Title attribute inside the anchor tag?

a {
  color: #900;
  text-decoration: none;
}

a:hover {
  color: red;
  position: relative;
}

a[data]:hover:after {
  content: attr(data);
  padding: 4px 8px;
  color: rgba(0,0,0,0.5);
  position: absolute;
  left: 0;
  top: 100%;
  white-space: nowrap;
  z-index: 2;
  border-radius: 5px ;
  background: rgba(0,0,0,0.5);
}
<a data="This is the CSS tooltip showing up when you mouse over the link"href="#" class="tip">Link</a>

答案 1 :(得分:5)

IMG标记中的标题属性不能包含HTML,因此您无法执行此操作。

答案 2 :(得分:2)

直接在html中是不可能的,将来会使用html5 figurefigcaption元素,或者可以使用jquery! 有关这些元素的更多信息,请参阅HtmlDoctor

答案 3 :(得分:1)

你可以这样做 -

&#13;
&#13;
a.tip {
    border-bottom: 1px dashed;
    text-decoration: none
}
a.tip:hover {
    cursor: help;
    position: relative
}
a.tip span {
    display: none
}
a.tip:hover span {
    border: #c0c0c0 1px dotted;
    padding: 5px 20px 5px 5px;
    display: block;
    z-index: 100;
    left: 0px;
    margin: 10px;
    width: 250px;
    position: relative;
    top: -150px;
    text-decoration: none
}
&#13;
<a href="#" class="tip">
   <img src="http://www.w3schools.com/html/pic_mountain.jpg">
   <span>This is the CSS tooltip showing up when you mouse over the link</span>
</a>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

来自HTML spec(强调我的):

  

title属性表示元素的建议信息,例如适用于工具提示的信息。在链接上,这可以是目标资源的标题或描述;在图像上,它可以是图像信用或图像的描述;关于一个段落,它可以是对案文的脚注或评注;在引用中,它可能是关于来源的进一步信息;在交互式内容上,它可以是元素使用的标签或说明;等等。 值为文字。

因此,title属性中仅允许 text 。对于自定义HTML工具提示,您必须使用自定义解决方案,例如默认隐藏内容并使用CSS在:hover上显示内容,或使用JavaScript从data-...属性中提取。

答案 5 :(得分:0)

如果您想要做的是在鼠标悬停图像时显示文本,您可以使用CSS :hover状态执行某些操作。

这种解决方案在this blog post中描述。

答案 6 :(得分:0)

另一个重要的小提琴是http://jsfiddle.net/tDQWN/129/ (我不能相信,但在同一搜索中我找到了两个帖子)。这是一个类似的解决方案,但造型有点发烧友。

a {
  color: #900;
  text-decoration: none;
}

a:hover {
  color: red;
  position: relative;
}

a[data-title]:hover:after {
  content: attr(data-title);
  padding: 4px 8px;
  color: #333;
  position: absolute;
  left: 0;
  top: 100%;
  white-space: nowrap;
  z-index: 20px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-box-shadow: 0px 0px 4px #222;
  -webkit-box-shadow: 0px 0px 4px #222;
  box-shadow: 0px 0px 4px #222;
  background-image: -moz-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #eeeeee),color-stop(1, #cccccc));
  background-image: -webkit-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -moz-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -ms-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -o-linear-gradient(top, #eeeeee, #cccccc);
}

和HTML:

<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. <a href="#" data-title="Hello, i am a link">Vestibulum</a> tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. <a href="#" data-title="This is another link">Mauris placerat</a> eleifend leo.</p>

希望这有助于某人!

相关问题