Preventing hover on pseudo element - but pointer-events: none; doesn't do it - css

I have a few links on one line next to each other, and I would like to have dividing dashes between them. I chose to make this happen with the use of the ::before pseudo element, and it works nicely.
However, hovering over the dividers also triggers the hover over the element I have the ::before on.
This is a fiddle showing the issue. If you hover over the dashes, the underline appears under the a.
In my search as to how to prevent this from happening, I ran into this stackoverflow question. Together with the documentation on developer.mozilla.org and the caniusethis page on the pointer-events property I was sure this would fix it. But it didn't.
What am I missing here?

You need to make changes in css
.wrap a::before {
content: '----';
padding: 0 15px;
position: absolute;
left: -50px;
pointer-events: none;
}
.wrap a {
text-decoration: none;
position: relative;
margin-left: 50px;
display: inline-block;
}

You will need to use position:absolute for the ---- to make it out of the <a> flow and also position:relative to the parent <a> element.
Stack Snippet
.wrap a {
text-decoration: none;
margin: 0 30px;
position: relative;
}
.wrap p {
margin: 0;
}
.wrap {
font-family: sans-serif;
border: 1px solid green;
padding: 5px;
margin-bottom: 20px;
}
.wrap a:nth-of-type(1) {
margin-left: 50px;
}
.wrap a::before {
content: '----';
position: absolute;
pointer-events: none;
left: -30px;
transform: translateX(-50%);
}
.wrap a:hover {
text-decoration: underline;
}
<div class="wrap">
<p>These links do not have the pointer-events: none; property</p>
<br>
link text one
link text two
link text three
</div>
<div class="wrap">
<p>These do have pointer-events: none; yet their behaviour is the same as with the block above.</p>
<br>
link text one
link text two
link text three
</div>

Related

How to add multiple CSS attributes to a tag simultaneously?

I'm trying to modify my WordPress theme the way I want through the Additional CSS part in Appearance>Customize.
I want all my h1 tags in entry-content class to be like this:
so I used this code:
.entry-content h1 {
background-color: #cfbc00;
display: inline;
background-color:#000000;
}
I want the whole block to be colored #cfbc00 and the background of the text itself to be black. But the browser does not apply these simultaneously to my tag and it applies only one of the attributes. What should I do?
If you don't have access to the HTML code, then here is a CSS workaround:
.entry-content
{
position: relative;
}
.entry-content h1::before
{
content: "";
display: block;
position: absolute;
width: 100%;
background-color: #cfbc00;
height: 40px;
z-index: -1;
}
.entry-content h1 {
display: inline;
background-color: #000000ad; /* Sets transparency according to sample image */
top: 0;
/* Line height to maintain the height of the :before element */
line-height: 40px;
color: #fff;
font-size: 35px;
padding: 5px;
}
<div class="entry-content">
<h1>Test title</h1>
</div>

Css pointer-events hover issue

I have to following code:
<div class="playlist-item">
<a class="playlist-non-selected" href="#">
<span class="playlist-title">AudioAgent - Japanese Intro</span>
</a>
</div>
https://jsfiddle.net/4uyb7rh9/10/
The problem is when you rollover the text, in firefox and ie overPlaylistItem & outPlaylistItem are constantly called and cursor just keeps flickering. This works properly in chrome. Is there a way to make this work in all browsers?
This happens because when you set the class having pointer-events: none it triggers a mouse leave event, hence it flashes.
First of all, may I suggest you use :hover, second, whether you use :hover or script, you need to target the specific element that shouldn't be clickable, for example the span
.playlist-non-selected:hover span {
pointer-events: none;
}
Stack snippet
.playlist-item {
position: relative;
top: 0px;
left: 0px;
height: 40px;
margin-bottom: 5px;
overflow: hidden;
line-height: 40px;
}
.playlist-title {
display: inline-block;
position: relative;
top: 0px;
margin-left: 20px;
overflow: hidden;
font-size: 22px;
font-family: 'Gnuolane Free';
margin-bottom: 0px;
}
.playlist-non-selected {
color: #bbb;
}
.playlist-non-selected:hover{
color: red;
}
.playlist-non-selected:hover span{
pointer-events: none;
}
<div class="playlist-item">
<a class="playlist-non-selected" href="#">
<span class="playlist-title">AudioAgent - Japanese Intro</span>
</a>
</div>
And here is an updated fiddle using your script
Update based on comment about not working in Edge
Appears to be some kind of bug in Edge when the span has display: block so changing it to display: inline-block and it works.
For it to work in IE11, the span need display: inline (or just remove the display:...) so it use its default.
Update 2 based on comment about not working in Edge
If you need the span to display as block, changing it to a div and it works in both Edge and IE11.
An updated fiddle using your script
Why haven't you used :hover ? This can be done with CSS easily and will not pose any difficulty for browsers compatability like
.playlist-item {
position: relative;
top: 0px;
left: 0px;
height: 40px;
margin-bottom: 5px;
overflow: hidden;
line-height: 40px;
}
.playlist-title {
display: block;
position: relative;
top: 0px;
margin-left: 20px;
overflow: hidden;
font-size: 22px;
font-family: 'Gnuolane Free';
margin-bottom: 0px;
backface-visibility:hidden
}
.playlist-non-selected:hover{
color: red;
pointer-events: none;
backface-visibility:hidden
}
.playlist-non-selected {
color: #bbb;
}
<div class="playlist-item">
<a class="playlist-non-selected" href="#">
<span class="playlist-title">AudioAgent - Japanese Intro</span>
</a>
</div>

Styling text within a div similar to links

I have the following breadcrumb code.
#breadcrumb{
margin-bottom: 10px;
margin-top: 10px;
display: block;}
#breadcrumb a{background:#FFFFFF;
padding:4px;
margin-right:10px;}
<div id="breadcrumb">
HomeParent CatagoryChild Catagory
</div>
I have styled the 'Home' and 'Parent Catagory' links to have solid colour backgrounds. I would like to style the 'Child Catagory' text with a slightly different colour solid background from the two links. All three elements of the breadcrumb should have gaps between them and should not be touching.
The 'Child Catagory' text is not surrounded by <span> so I am unsure how to achieve this.
If I add styling to the surrounding 'breadcrumb' div, the space between the links is affected.
I need to style just the text in the 'breadcrumb' div, not the div itself and not the links.
This is a terrible hacky approach and should not be used. I strongly recommend wrapping the last bit of the breadcrumb in a span and styling the span.
#breadcrumb {
background:red;
display:inline-block;
}
#breadcrumb a {
background:green;
display:inline-block;
position:relative;
}
#breadcrumb a:after {
content: "";
background: white;
height: 1.5em;
position: absolute;
top: 0;
right: -5px;
width: 5px;
}
<div id="breadcrumb">
Home
Parent Catagory
Child Catagory
</div>
Edit
If you know the width of the last element, you can do the following:
#breadcrumb:after {
background: red;
content: "";
height: 1.5em;
width: 99px;
display: inline-block;
position: absolute;
top: 0;
right: 0;
z-index: -1;
}
#breadcrumb {
overflow: hidden;
display: inline-block;
position: relative;
margin-bottom: 10px;
margin-top: 10px;
}
#breadcrumb a{
background:#ccc;
padding:4px;
margin-right:10px;
}
<div id="breadcrumb">
HomeParent CatagoryChild Catagory
</div>
Option 1 - Edit the HTML
The question is: Can you edit the HTML?
The best thing to do would be to edit the HTML and add a surrounding div to the "Child Category" item, then style it.
<div id="breadcrumb">
Home
Parent Catagory
<div class="child">Child Category</div>
</div>
SCSS
#breadcrumb{
a,
.child{
background: #f5f5f5;
padding: 4px 7px;
margin: 0 5px;
&:first-child {
margin-left: 0;
}
}
.child{
display: inline-block;
background: #222;
color: #fff;
}
}
CSS Output
#breadcrumb a, #breadcrumb .child {
background: #f5f5f5;
padding: 4px 7px;
margin: 0 5px;
}
#breadcrumb a:first-child, #breadcrumb .child:first-child {
margin-left: 0;
}
#breadcrumb .child {
display: inline-block;
background: #222;
color: #fff;
}
Example:
http://codepen.io/anon/pen/vEaPqx
Option 2 -- Javascript workaround
Of course there are other ways using javascript like getting the value from another div and appending it to the breadcrumb div. The issue is that this is could eventually break if the HTML changes. It also depends on the content already on the page.
<div id="existing-child-category-text">Child Category</div>
<div id="breadcrumb">
Home
Parent Catagory
</div>
Javascript:
// Create the child element
var child_cat = document.createElement('div');
// Add a class name to the child element
child_cat.className = 'child';
// Add the text from the existing child category to the child div
child_cat.innerHTML = document.getElementById('existing-child-category-text').innerHTML;
// Add the new div to the breadcrumbs
document.getElementById('breadcrumb').appendChild(child_cat);
Example:
http://codepen.io/anon/pen/vEaPqx
Ultimately I would assume that if you are able to edit javascript then you could also change the HTML which would be a way more robust solution.
Option 3 -- Use Pseudo Elements
Try to avoid this but you could have a similar approach using css pseudo elements. but you would need to set the value via javascript or hardcode the value on css which is not the best solution.

Trouble creating headers with horizontal line going across

I want to create title that have an horizontal line going through them with the Title text appearing above the line, even though I believe I have correctly used the z-index rule it still does not work, this is the css I am using;
.heading {
border-bottom: 2px solid #222222!important;
text-align: center;
z-index: -1;
}
#sidebar .widget h3, #sidebar .widget .heading h3 {
color: #333333;
text-align: center;
z-index: 10;
margin-bottom: -8px;
position: relative;
}
The url is: http://crossfitblackboard.com/
z-index
works only if you are using a position
so you need also set the .heading to position: relative
Create a duplicate of the heading and line-through it.
HTML:
<h1 class="shadow">Your Awesome Heading</h1>
<h1>Your Awesome Heading</h1>
CSS:
h1{
position: absolute;
}
.shadow{
color: lightgrey;
text-decoration: line-through;
}
Here's the Fiddle: http://jsfiddle.net/39rwmjt6/1/
Let's keep the markup as simple as possible; We can do this with one element for your heading and the pseudo element :before with z-index: -1;.
Have an example!
HTML
<h1>Heading</h1>
CSS
h1 {
position: relative;
text-align: center;
font-size: 1em;
}
h1:before {
position: absolute;
content: '';
height: 2px;
background: #F00;
display: block;
width: 100%;
top: 0.6em;
z-index: -1;
}

block level clickable area not working correctly

I'm trying to make a <aside> element clickable. There are elements inside and I don't want the links to be applied individually, I would like it to be a clickable element.
<aside class="projectindicator">
<a href="#projectAnchor">
<img src="_img/showcase/downArrowProjects.png" alt="down cursor"/>
<h1>PROJECT</h1>
<img src="_img/showcase/downArrowProjects.png" alt="down cursor"/>
</a>
</aside>
/*Project display*/
.projectindicator{
width: 277px;
height: 35px;
position: relative;
top: 530px;
left: 360px;
text-transform: uppercase;
}
.projectindicator img{
float: left;
}
.projectindicator h1{
float: left;
padding: 0 10px 0 10px;
}
.projectindicator a{
display: block;
font-family: book;
font-size: 30px;
float: left;
}
.projectindicator a:link, .projectindicator a:visited{
text-decoration: none;
color: #2b3a42;
}
.projectindicator a:hover{
text-decoration: none;
color: #3f5765;
}
.projectindicator a:active{
text-decoration: none;
color: #2b3a42;
}
The problem is that I'm getting the clickable area below the element and the clickable area is smaller than the aside element. This gives the user a hard time to click the link.
Simple but I cannot find the solution. Could somebody help me?
In .projectindicator a, you added float: left, but this will cause the link to shrink to the size of its contents. I would remove that.
.projectindicator itself contains a height, while the link doesn't have a height. I'd either add the height to the link itself, or give the link height: 100%.
Last but not least: make sure .projectindicator itself doesn't have any padding and the link inside it doesn't have any margin.

Resources