CSS:前/后内容与标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22352618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 02:04:28  来源:igfitidea点击:

CSS: before / after content with title

csstitlepseudo-element

提问by Augustin Riedinger

I can do

我可以

div:after {
  content: "hello"
}

But can I have the hellotext with a title so that when I hover it with the mouse, the title is displayed?

但是我可以让hello文本带有标题,以便当我用鼠标悬停时显示标题吗?

Thanks

谢谢

采纳答案by Paulie_D

You don't need a pseudo-element for that:

您不需要伪元素:

p {
    background:lightblue;
    padding:15px;
    margin:15px;
}
<p class="hover-me" title="I Show up on Hover">Some Text</p>

However, if you need to use a pseudo element

但是,如果您需要使用伪元素

p {
    background:lightblue;
    padding:15px;
    margin:15px;
    position: relative;
}

p:hover:after {
    position: absolute;
    content:attr(data-title);
    left:0;
    top:0;
    width:200px;
    height:1.25rem;
    background-color: blue;
    color:white;
}
<p class="hover-me" data-title="I Show up on Hover">Some Text</p>

回答by Amr

A workaround for this, is to have two pseudo elements.

对此的解决方法是使用两个伪元素。

For example the ::afteris the main element and the ::beforewill be shown on hover and acts like a title.

例如::after是主要元素,而::before将在悬停时显示并充当标题。

Also, you can use javascript or jQuery code to detect mouse events over the pseudo element.

此外,您可以使用 javascript 或 jQuery 代码来检测伪元素上的鼠标事件。

Demo

演示

Bellow is the demo explanation:

波纹管是演示解释:

$('.alert').on('mousemove', function(e) {
    if (e.offsetX > (this.offsetWidth - 42)) { //42 is the ::after element outerWidth
        $(this).addClass('hover');
    } else {
        $(this).removeClass('hover');
    }
}).on('mouseleave', function(e) {
    $(this).removeClass('hover');
}).on('click', function(e) {
    if (e.offsetX > (this.offsetWidth - 42)) { //42 is the ::after element outerWidth
        $(this).remove();
    }
});
.alert {
    padding: 15px;
    margin: 50px 0 20px;
    border: 1px solid transparent;
    border-radius: 4px;
    position: relative;
    color: #a94442;
    background-color: #f2dede;
    border-color: #ebccd1;
    font-size: 0.85em;
    transition: all 1s;
}
.alert::after, .alert::before {
    content: 'X';
    position: absolute;
    right: 0;
    top: 0;
    width: 40px;
    height: 100%;
    font-size: 0.85em;
    padding: 0;
    line-height: 40px;
    text-align: center;
    font-weight: 900;
    font-family: cursive;
    cursor: pointer;
}
.alert::before {
    display: none;
    content: 'This is a Title';
    top: -105%;
    width: auto;
    border: inherit;
    border-radius: inherit;
    padding: 0 0.4em;
    background: rgba(0, 0, 0, 0.48);
    color: white;
}
.alert.hover::after {
    color: white;
    filter: sepia(10%);
    transform: scale(1.1);
    border-radius: inherit;
    border: inherit;
    background: inherit;
}
.alert.hover::before {
    display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="alert">
  <strong>Hover</strong> over X to display the title.
</div>

回答by nikitz

Use:

用:

div:hover:before{
  content: "Bye";
          }