Why can I select the text under a positioned pseudo element? - css

I have an element on which I use a pseudo-element (::after) to style an overlay. For technical reasons I cannot/don't want to use another element (e.g. div) to add the overlay. The pseudo-element is absolutely positioned and appears in front off the actual element. I was surprised to see that the text inside the div is still selectable, "through" the pseudo-element. I've played around with z-index and pointer-events, without success. See this fiddle for an (external) example.
Why is this happening? Why can the text still be selected with the mouse? Is there any other solution apart for user-select?
div {
position: relative;
}
div::after {
position: absolute;
content: '';
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0.4;
background-color: black;
}
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sollicitudin dui nec neque rutrum, eu auctor nulla accumsan. Quisque non eleifend nibh. Fusce aliquet imperdiet odio vitae pretium. Nam tincidunt mattis ante, nec consectetur diam maximus
vel. Fusce at lectus porttitor, feugiat purus sed, porta felis. Morbi tempus ante a orci finibus rhoncus. Nullam a porta enim. Sed id eros convallis, consectetur turpis a, gravida nunc. Nullam sed dui interdum diam placerat suscipit sit amet nec mi.
Maecenas ultricies metus massa, id vestibulum nisi posuere facilisis. Aliquam erat volutpat. Quisque blandit condimentum augue. Nullam pulvinar turpis libero, id luctus dolor ultricies quis.</p>
</div>

As for your question:
Why is this happening?
It's seems to due to the fact that generated content itself is not selectable. see How can I make generated content selectable?)
Try selecting the text in the following snippet:
div:before {
content: 'generated content - before... ';
}
div:after {
content: '...generated content - after';
}
<div>Only content of div is selectable</div>
So apparently, selection is about selecting only elements which appear in the DOM and according to the CSS 2.1 spec:
Neither pseudo-elements nor pseudo-classes appear in the document
source or document tree.
So when selecting on the overlay - the overlay is ignored, and instead the text in the div is selected
That being said, the spec here seems to say that generated content should be selectable
Generated content should be searchable, selectable, and available to
assistive technologies
...but to my knowledge that bit of the spec has not been implemented in any browser.

If you don't want to use user-select: none an alternative can be to set as transparent the ::selection.
p::selection {
background-color: transparent;
}
This doesn't just hide the selection but also make it impossible to highlight. It is compatible with most of the browser except mobile ones and Firefox (you have to use ::-moz-selection).
div::after {
position: absolute;
content: '';
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0.4;
background-color: black;
}
p::selection {
background-color: transparent;
}
p::-moz-selection {
background-color: transparent;
}
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sollicitudin dui nec neque rutrum, eu auctor nulla accumsan. Quisque non eleifend nibh. Fusce aliquet imperdiet odio vitae pretium. Nam tincidunt mattis ante, nec consectetur diam maximus
vel. Fusce at lectus porttitor, feugiat purus sed, porta felis. Morbi tempus ante a orci finibus rhoncus. Nullam a porta enim. Sed id eros convallis, consectetur turpis a, gravida nunc. Nullam sed dui interdum diam placerat suscipit sit amet nec mi.
Maecenas ultricies metus massa, id vestibulum nisi posuere facilisis. Aliquam erat volutpat. Quisque blandit condimentum augue. Nullam pulvinar turpis libero, id luctus dolor ultricies quis.</p>
</div>

Related

Is there a way to make the ::before pseudo element appear at the beginning of the line instead of at the end of the previous line?

I'm trying to add a little triangle indicator for highlighted text:
I added it with the ::before pseudo-element:
.highlight {
position: relative;
background-color: yellow;
}
.highlight::before {
content: "";
border-left: 5px solid transparent;
border-bottom: 5px solid #47484a;
border-right: 5px solid transparent;
position: absolute;
top: -0.5px;
left: -3px;
transform: rotate(-45deg);
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacinia ligula eget tempus mollis. Maecenas <span class="highlight">maximus</span> interdum libero vel varius. Pellentesque quis varius augue. Phasellus facilisis, turpis et commodo posuere, leo enim egestas magna, id luctus sapien neque eget ligula. Pellentesque a imperdiet libero. In nec leo tempor, egestas massa quis, iaculis leo. Donec mattis mauris placerat congue egestas. Aliquam vehicula dictum scelerisque.
</p>
But if the highlight happens to be on the beginning of a line (which happens if you change the screen size), the indicator appears at the end of the previous line instead of the beginning of the current one.
Is there a way to tell the browser to not do this? It doesn't happen in Firefox so I'm assuming it's an issue with Chrome
Use a gradient to create that shape
.highlight {
background: linear-gradient(135deg,#47484a 5px,yellow 0);
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacinia ligula eget tempus mollis. Maecenas <span class="highlight">maximus</span> interdum libero vel varius. Pellentesque quis varius augue. Phasellus facilisis, turpis et commodo posuere, leo enim egestas magna, id luctus sapien neque eget ligula. Pellentesque a <span class="highlight">imperdiet libero. In nec leo tempor, egestas massa quis, iaculis leo.</span> Donec mattis mauris placerat congue egestas. Aliquam vehicula dictum scelerisque.
</p>
Make the highlight class an inline-block appears to work. But that'll break some minor details such as word-breaking.
.highlight {
display: inline-block;
position: relative;
background-color: yellow;
}
.highlight::before {
content: "";
border-left: 5px solid transparent;
border-bottom: 5px solid #47484a;
border-right: 5px solid transparent;
position: absolute;
top: -0.5px;
left: -3px;
transform: rotate(-45deg);
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacinia ligula eget tempus mollis. Maecenas <span class="highlight">maximus</span> interdum libero vel varius. Pellentesque quis varius augue. Phasellus facilisis, turpis et commodo posuere, leo enim egestas magna, id luctus sapien neque eget ligula. Pellentesque a imperdiet libero. In nec leo tempor, egestas massa quis, iaculis leo. Donec mattis mauris placerat congue egestas. Aliquam vehicula dictum scelerisque.
</p>

Crossbrowser CSS when adjusting a WordPress theme

My CSS:
.n-drop-cap::first-letter {
box-sizing: content-box;
font-size: 4rem;
font-weight: bold;
color: var(--color-1);
float: left;
height: auto;
width: auto;
margin-right: 0.8rem;
margin-top: -1rem;
margin-bottom: -2rem;
display: block;
}
HTML:
<p class="n-drop-cap">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
This is a case of adjusting a ready made WordPress theme for my needs. So, I have not resetted CSS styles. I decided that maybe it is not necessary in this case.
Anyway, my styles has not proved to be cross browser. In Chrome it looks satisfactorily (uppermost screen shot). In Firefox it falls apart.
Web page: https://galina.xyz/krasota/strizhki-na-korotkie-volosy/
Could you help me understand how to make everything crossbrowser?
The problem is that :first-letter does not supports display property.
All supported CSS rules subset can be found in it's documentation
You can always wrap your first letter manually or via script and then apply style for it:
.first-letter {
padding: 0 25px;
font-size: 24px;
display: block;
float: left;
color: #D93588;
}
<span class="first-letter">3</span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eu lectus ac tortor commodo molestie at sit amet nunc. Vestibulum quis risus maximus, elementum urna nec, egestas lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Quisque porta accumsan sem, a lobortis tortor faucibus finibus. Maecenas ac risus sed ante dictum sagittis. Nullam magna mi, consequat vitae sodales ac, dapibus et velit. Etiam vel lectus sed metus aliquet aliquet. Etiam mattis risus quis urna scelerisque tempor. Aenean maximus, diam vitae efficitur vulputate, mauris sem cursus velit, sed aliquet diam elit ac leo. Curabitur ut volutpat justo. Fusce maximus semper mi ac feugiat. Aliquam laoreet enim purus, vitae tempor elit viverra consequat. Nunc mollis sodales enim sed dictum.

make element visible only if scrolled down or page higher than video, CSS only

I want absolute element appear only when page is scrolled down, or at least when page height is higher than window height.
Is this possible with pure CSS? How?
example code:
<body>
content...
<button id="return">return to top</button>
</body>
want to show #return only if 1 page is higher than window OR 2 scrollbar is visible OR 3 even better scrollbar is visible and scrolled down
Here is one idea where the element will appear after a small scroll and will sticky to the bottom:
p {
font-size: 35px;
padding: 10px;
}
body {
margin:0;
position: relative; /* relative to the body */
}
.fixed {
position: absolute;
top: 110vh; /* defined when the element will appear */
bottom: 0;
left: 0;
right: 0;
/* needed to push the sticky element down */
display:flex;
flex-direction:column;
justify-content:flex-end;
/* to hide element if content is short*/
clip-path:polygon(0 0,100% 0,100% 100%,0 100%);
}
.fixed>div {
/* stick to the bottom */
position: sticky;
bottom: 0;
background: red;
padding: 15px;
color: #fff;
text-align: center;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ac risus ac est pulvinar finibus cursus non augue. Proin turpis tortor, dapibus vel metus eget, fermentum aliquam diam. Quisque sodales est sed diam mollis dictum. Aliquam ut volutpat nisi.
Aenean luctus turpis in malesuada malesuada. Praesent non ligula ut ligula feugiat convallis porttitor nec leo. Integer vel risus eget metus viverra ultrices vel vel enim. Sed vitae laoreet ex. Fusce tristique feugiat odio non consequat. Sed bibendum
tempor est, sed vestibulum ex aliquet quis. Nam libero quam, laoreet at elementum in, scelerisque vel ex. Vivamus ac purus eget orci vehicula gravida. Aliquam eleifend felis eget porttitor sollicitudin. Donec ut sodales eros. Sed a eros urna.
tempor est, sed vestibulum ex aliquet quis. Nam libero quam, laoreet at elementum in, scelerisque vel ex. Vivamus ac purus eget orci vehicula gravida. Aliquam eleifend felis eget porttitor sollicitudin. Donec ut sodales eros. Sed a eros urna.
</p>
<div class="fixed">
<div class="banner">some content here</div>
</div>
With some fading effect:
p {
font-size: 35px;
padding: 10px;
}
body {
margin:0;
position: relative; /* relative to the body */
}
.fixed {
position: absolute;
top: 110vh; /* defined when the element will appear */
bottom: 0;
left: 0;
right: 0;
/* needed to push the sticky element down */
display:flex;
flex-direction:column;
justify-content:flex-end;
/*fading*/
-webkit-mask:linear-gradient(to bottom,transparent,#fff 200px);
mask:linear-gradient(to bottom,transparent,#fff 200px);
}
.fixed>div {
/* stick to the bottom */
position: sticky;
bottom: 0;
background: red;
padding: 15px;
color: #fff;
text-align: center;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ac risus ac est pulvinar finibus cursus non augue. Proin turpis tortor, dapibus vel metus eget, fermentum aliquam diam. Quisque sodales est sed diam mollis dictum. Aliquam ut volutpat nisi.
Aenean luctus turpis in malesuada malesuada. Praesent non ligula ut ligula feugiat convallis porttitor nec leo. Integer vel risus eget metus viverra ultrices vel vel enim. Sed vitae laoreet ex. Fusce tristique feugiat odio non consequat. Sed bibendum
tempor est, sed vestibulum ex aliquet quis. Nam libero quam, laoreet at elementum in, scelerisque vel ex. Vivamus ac purus eget orci vehicula gravida. Aliquam eleifend felis eget porttitor sollicitudin. Donec ut sodales eros. Sed a eros urna.
tempor est, sed vestibulum ex aliquet quis. Nam libero quam, laoreet at elementum in, scelerisque vel ex. Vivamus ac purus eget orci vehicula gravida. Aliquam eleifend felis eget porttitor sollicitudin. Donec ut sodales eros. Sed a eros urna.
</p>
<div class="fixed">
<div class="banner">some content here</div>
</div>
To avoid the extra scroll when the content is short here is a trick using min() (it won't work on Firefox for now)
p {
font-size: 35px;
padding: 10px;
}
body {
margin:0;
position: relative; /* relative to the body */
}
.fixed {
position: absolute;
top: min(100%,110vh); /* defined when the element will appear */
bottom: 0;
left: 0;
right: 0;
/* needed to push the sticky element down */
display:flex;
flex-direction:column;
justify-content:flex-end;
/*fading*/
-webkit-mask:linear-gradient(to bottom,transparent,#fff 200px);
mask:linear-gradient(to bottom,transparent,#fff 200px);
}
.fixed>div {
/* stick to the bottom */
position: sticky;
bottom: 0;
background: red;
padding: 15px;
color: #fff;
text-align: center;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ac risus ac est pulvinar finibus cursus non augue. Proin turpis tortor, dapibus vel metus eget, fermentum aliquam diam. Quisque sodales est sed diam mollis dictum. Aliquam ut volutpat nisi.
Aenean luctus turpis in malesuada malesuada. Praesent non ligula ut ligula feugiat convallis porttitor nec leo. Integer vel risus eget metus viverra ultrices vel vel enim. Sed vitae laoreet ex. Fusce tristique feugiat odio non consequat. Sed bibendum
tempor est, sed vestibulum ex aliquet quis. Nam libero quam, laoreet at elementum in, scelerisque vel ex. Vivamus ac purus eget orci vehicula gravida. Aliquam eleifend felis eget porttitor sollicitudin. Donec ut sodales eros. Sed a eros urna.
tempor est, sed vestibulum ex aliquet quis. Nam libero quam, laoreet at elementum in, scelerisque vel ex. Vivamus ac purus eget orci vehicula gravida. Aliquam eleifend felis eget porttitor sollicitudin. Donec ut sodales eros. Sed a eros urna.
</p>
<div class="fixed">
<div class="banner">some content here</div>
</div>

Can't get a sticky footer working for the life of me

I've tried every CSS-only implementation of sticky footers that exist on the internet it seems, and for the life of me I cannot figure out why it's not working for me.
The problem code is here: https://jsfiddle.net/7ck2xk2p/1/
So the problem is footer is still just sitting under the content, and is not stuck to the bottom of the page.
As you might be able to see, my most recent attempt was the technique detailed here by Ryan Fait
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important; /* This line and the next line are not necessary unless you need IE6 support */
height: 100%;
margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 155px; /* .push must be the same height as .footer */
}
I am very new, so if things are messy in that fiddle please excuse me. The relevant details should still be distinguishable though.
What am I doing wrong?
you can use this approach for sticky footer (CSS only)
html,
body {
height: 100%;
margin: 0;
}
div {
min-height: 100%;
/* equal to footer height */
margin-bottom: -70px
}
div:after {
content: "";
display: block
}
footer,
div:after {
height: 70px
}
footer {
background: green
}
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dictum vel dolor vel commodo. Nam id nisi eros. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi commodo leo ac enim molestie, vitae sodales
dolor consequat. Donec imperdiet orci at luctus lacinia. Donec bibendum velit sed risus eleifend ultricies. Sed nisl massa, ornare quis augue et, faucibus feugiat sapien. Vestibulum pharetra tempor quam eu congue. Proin posuere lorem quis nisl efficitur
aliquam. Curabitur elit ex, convallis sed fringilla a, varius quis dui. Nullam eget est sed orci imperdiet imperdiet sit amet eget dui. Integer egestas nisi a sagittis rutrum. Quisque id convallis nisl, at blandit nunc. Curabitur elementum viverra tristique.
In auctor pretium mattis. Fusce vulputate porta lacus tincidunt rhoncus. Aenean dapibus tortor non faucibus laoreet. Morbi fringilla leo nisl, imperdiet hendrerit elit semper at. Donec suscipit orci in nulla viverra ultrices. Donec aliquet risus non
libero viverra, sed aliquam massa congue. Aliquam suscipit ullamcorper erat sed vehicula. Donec elementum tincidunt dolor, non scelerisque dolor pretium ut. Praesent vitae porttitor turpis, et pharetra libero. Sed imperdiet tempor facilisis. Cras eget
vehicula dolor.
</div>
<footer>
Sticky Footer.
</footer>

Align text on slanted line

Is it posible to make text left aligned on a slanted line? it's alignement should follow the slanted slanted image with required support for IE9+?
My example code :
img {
display: block;
float: left;
transform: rotate(-5deg);
margin: 0 15px;
}
<div>
<img src="http://placehold.it/150x250&text=img" alt="image" />
<p>Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu,luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in, paragraph.</p>
</div>
Using LESS
You guys made me think a bit more outside of the box, so I came out with my own ugly solution.
My idea is to add a bunch of extra square elements and calculate its size:
.loop(#i) when (#i > 0){
.loop((#i - 1));
.space#{i}{
width: floor(#i*#hSize/(1/tan(5deg)));
}
}
#hSize: 15px;
.space {
float: left;
clear: left;
width: #hSize;
height: #hSize;
}
HTML:
<p>
<span class="space space1"></span>
<span class="space space2"></span>
<!-- (...) -->
<span class="space space11"></span>
Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu, luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in, paragraph.
</p>
Proof of concept: http://codepen.io/Tymek/pen/jEypOX?editors=110
#chipChocolate.py, it was just a matter of principle for me NOT to use JavaScript for this. If anyone wants to write JS/jQuery code based on my solution, you're welcome. Please share it here afterwards.
WARNING: The shape-outside property should not be used in live projects1. This answer is here just to show how the desired output can
be achieved with this property.
Here is an example using the shape-outside property (modern webkit browsers only) :
DEMO
img {
display: block;
float: left;
transform: rotate(-5deg);
margin: 0 20px;
-webkit-shape-outside: polygon(0 3%, 85% -3%, 100% 97%, 15% 103%);
shape-outside: polygon(0 3%, 85% -3%, 100% 97%, 15% 103%);
}
<div>
<img src="http://placehold.it/150x250&text=img" alt="image" />
<p>Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu,
luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in,
paragraph.</p>
</div>
1The CSS Shapes Module Level 1 actually (mai 2016) has the status of "Candidate Recommendation". As this means it is a work in progress, it may change at any moment and therefore should not be used other than for testing.
The same layout could be achieved with the shape-inside property and specify a containing box for the text but no browser I know of supports this property today.
For a cross browser approach please see Tymek's answer.
img {
display: block;
float: left;
transform: rotate(-5deg);
margin: 0 15px;
}
p {
transform: skew(6deg);
font-style: italic;
}
<div>
<img src="http://placehold.it/150x250&text=img" alt="image" />
<p>Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu,
luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in,
paragraph.</p>
</div>
I can't give you a code example, this is more complicated than a skew transform.
You must parse the text and the related DOM contained in it and look for each new lines of text (not br or \n but each first character of every rendered line).
With this information you can add a padding-left calculated from the images position and dimension.

Resources