styling an image title attribute using css?

前端 未结 7 1347
猫巷女王i
猫巷女王i 2020-12-14 13:07

I want to style the text we see when we hover over the image. I tried the following but it won’t work:


         


        
相关标签:
7条回答
  • 2020-12-14 13:22

    It is not possible directly in html, it will be in the future with the html5 figure and figcaption element, or it's possible using jquery ! See HtmlDoctor for more informations on these elements !

    0 讨论(0)
  • 2020-12-14 13:23

    From the HTML spec (emphasis mine):

    The title attribute represents advisory information for the element, such as would be appropriate for a tooltip. On a link, this could be the title or a description of the target resource; on an image, it could be the image credit or a description of the image; on a paragraph, it could be a footnote or commentary on the text; on a citation, it could be further information about the source; on interactive content, it could be a label for, or instructions for, use of the element; and so forth. The value is text.

    Ergo, only text is allowed in the title attribute. For a custom HTML tooltip, you must use a custom solution, such as hiding the content by default and showing it on :hover with CSS, or using JavaScript to pull from a data-... attribute.

    0 讨论(0)
  • 2020-12-14 13:25

    Nothing is impossible. edited the solution by Andres Ilich to the question: 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>

    0 讨论(0)
  • 2020-12-14 13:26

    If what you want to do is display a text when the mouse hovers an image, you can do something using CSS :hover state.

    Such a solution is described in this blog post.

    0 讨论(0)
  • 2020-12-14 13:33

    You can do that by this way-

    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
    }
    <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>

    0 讨论(0)
  • 2020-12-14 13:40

    Title attributes in IMG tags cannot contain HTML, so you cannot do this.

    0 讨论(0)
提交回复
热议问题