【问题标题】:CSS: before / after content with titleCSS:带有标题的内容之前/之后
【发布时间】:2014-04-16 15:39:20
【问题描述】:

我可以的

div:after {
  content: "hello"
}

但是我可以让hello 带有标题的文本,这样当我用鼠标悬停它时,标题就会显示出来吗?

谢谢

【问题讨论】:

  • 不太清楚:你能举个例子吗?
  • 你不能用 CSS 做,用 jquery mouse enter
  • Title 属性应该是 css 属性。
  • 接受的答案很酷,但不能回答您的问题。你解决了吗?

标签: css title pseudo-element


【解决方案1】:

你不需要一个伪元素:

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

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

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>

【讨论】:

  • 你不能给一个伪元素一个标题属性。另外,如果您尝试这样做,它建议内容应该在您的标记中开始。
  • 我一般同意,但这取决于额外的“内容”是什么。
  • 这不是对伪元素的内容与标题(工具提示)不同的问题的答案
【解决方案2】:

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

例如 ::after 是主要元素,而 ::before 将在悬停时显示并起到标题的作用。

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

Demo

下面是演示说明:

$('.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>

【讨论】:

  • 问题是 CSS,所以没有 JS 的解决方案。此外,您的“这是一个标题”在 CSS 中,而关键是将它放在 HTML 本身中(这样它也可以完成 title 的工作)。
  • 伪元素不能有标题。所以我的回答只是给那些还想用伪元素的人提个建议!
【解决方案3】:

用途:

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

【讨论】:

  • 相当肯定:hover 是一个伪选择器,而不是一个伪元素;您需要使用 :before:after 之类的东西才能使用 content
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 2011-06-02
  • 2021-02-13
相关资源
最近更新 更多