How to change the style of the title attribute inside an anchor tag?

前端 未结 10 1316
故里飘歌
故里飘歌 2020-11-21 22:27

Example:

 Link 

How do I change the presentation of the \"title\" attribute in th

相关标签:
10条回答
  • 2020-11-21 22:57

    It seems that there is in fact a pure CSS solution, requiring only the css attr expression, generated content and attribute selectors (which suggests that it works as far back as IE8):

    a[title]:hover:after {
      content: attr(title);
      position: absolute;
    }
    

    Source: https://jsfiddle.net/z42r2vv0/2/

    update w/ input from @ViROscar: please note that it's not necessary to use any specific attribute, although I've used the "title" attribute in the example above; actually my recommendation would be to use the "alt" attribute, as there is some chance that the content will be accessible to users unable to benefit from CSS.

    update again I'm not changing the code because the "title" attribute has basically come to mean the "tooltip" attribute, and it's probably not a good idea to hide important text inside a field only accessible on hover, but if you're interested in making this text accessible the "aria-label" attribute seems like the best place for it: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-label_attribute

    0 讨论(0)
  • 2020-11-21 22:58

    Here is an example of how to do it:

    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;
        background: url(../images/status-info.png) #f0f0f0 no-repeat 100% 5%;
        left: 0px;
        margin: 10px;
        width: 250px;
        position: absolute;
        top: 10px;
        text-decoration: none
    }
    <a href="#" class="tip">Link<span>This is the CSS tooltip showing up when you mouse over the link</span></a>

    0 讨论(0)
  • 2020-11-21 22:59

    CSS can't change the tooltip appearance. It is browser/OS-dependent. If you want something different you'll have to use Javascript to generate markup when you hover over the element instead of the default tooltip.

    0 讨论(0)
  • 2020-11-21 22:59

    I thought i'd post my 20 lines JavaScript solution here. It is not perfect, but may be useful for some depending on what you need from your tooltips.

    When to use it

    • Automatically styles the tooltip for all HTML elements with a TITLE attribute defined (this includes elements dynamically added to the document in the future)
    • No Javascript/HTML changes or hacks required for every tooltip (just the TITLE attribute, semantically clear)
    • Very light (adds about 300 bytes gzipped and minified)
    • You want only a very basic styleable tooltip

    When NOT to use

    • Requires jQuery, so do not use if you don't use jQuery
    • Bad support for nested elements that both have tooltips
    • You need more than one tooltip on the screen at the same time
    • You need the tooltip to disappear after some time

    The code

    // Use a closure to keep vars out of global scope
    (function () {
        var ID = "tooltip", CLS_ON = "tooltip_ON", FOLLOW = true,
        DATA = "_tooltip", OFFSET_X = 20, OFFSET_Y = 10,
        showAt = function (e) {
            var ntop = e.pageY + OFFSET_Y, nleft = e.pageX + OFFSET_X;
            $("#" + ID).html($(e.target).data(DATA)).css({
                position: "absolute", top: ntop, left: nleft
            }).show();
        };
        $(document).on("mouseenter", "*[title]", function (e) {
            $(this).data(DATA, $(this).attr("title"));
            $(this).removeAttr("title").addClass(CLS_ON);
            $("<div id='" + ID + "' />").appendTo("body");
            showAt(e);
        });
        $(document).on("mouseleave", "." + CLS_ON, function (e) {
            $(this).attr("title", $(this).data(DATA)).removeClass(CLS_ON);
            $("#" + ID).remove();
        });
        if (FOLLOW) { $(document).on("mousemove", "." + CLS_ON, showAt); }
    }());
    

    Paste it anywhere, it should work even when you run this code before the DOM is ready (it just won't show your tooltips until DOM is ready).

    Customize

    You can change the var declarations on the second line to customize it a bit.

    var ID = "tooltip"; // The ID of the styleable tooltip
    var CLS_ON = "tooltip_ON"; // Does not matter, make it somewhat unique
    var FOLLOW = true; // TRUE to enable mouse following, FALSE to have static tooltips
    var DATA = "_tooltip"; // Does not matter, make it somewhat unique
    var OFFSET_X = 20, OFFSET_Y = 10; // Tooltip's distance to the cursor
    

    Style

    You can now style your tooltips using the following CSS:

    #tooltip {
        background: #fff;
        border: 1px solid red;
        padding: 3px 10px;
    }
    
    0 讨论(0)
  • 2020-11-21 23:02

    Native tooltip cannot be styled.

    That being said, you can use some library that would show styles floating layers when element is being hovered (instead of the native tooltips, and suppress them) requiring little or no code modifications...

    0 讨论(0)
  • 2020-11-21 23:04

    I have found the answer here: http://www.webdesignerdepot.com/2012/11/how-to-create-a-simple-css3-tooltip/

    my own code goes like this, I have changed the attribute name, if you maintain the title name for the attribute you end up having two popups for the same text, another change is that my text on hovering displays underneath the exposed text.

    .tags {
      display: inline;
      position: relative;
    }
    
    .tags:hover:after {
      background: #333;
      background: rgba(0, 0, 0, .8);
      border-radius: 5px;
      bottom: -34px;
      color: #fff;
      content: attr(gloss);
      left: 20%;
      padding: 5px 15px;
      position: absolute;
      z-index: 98;
      width: 350px;
    }
    
    .tags:hover:before {
      border: solid;
      border-color: #333 transparent;
      border-width: 0 6px 6px 6px;
      bottom: -4px;
      content: "";
      left: 50%;
      position: absolute;
      z-index: 99;
    }
    <a class="tags" gloss="Text shown on hovering">Exposed text</a>

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