I can do
div:after {
content: "hello"
}
But can I have the hello
text with a title so that when I hover it with the mouse, the title is displayed?
Thanks
Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.
Special welcome offer: get $100 of free credit. CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.
To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.
::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.
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>
A workaround for this, is to have two pseudo elements.
For example the ::after is the main element and the ::before will be shown on hover and acts like a title.
Also, you can use javascript or jQuery code to detect mouse events over the pseudo element.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With