CSS3 Making :before content not highlightable - html

== FIXED BY CHROME IN VERSION 60.0.3112.90 ==
-CHROME BUG-
bug reported: https://bugs.chromium.org/p/chromium/issues/detail?id=703807&can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=
Okay, so I've been working on a project that has CSS line numbers. When users highlight/select text the numbers get highlighted/selected as well. Luckily the :before content doesn't get copied with it, but it still annoys me. Is there any way to fix this.
Notes: Pre is contenteditable='true'
What I have tried:
user-select: none;
AS WELL AS
pre code:before::selection {
background: transparent;
}
This is some example code:
<pre contenteditable='true'><code>bla</code><code>bla</code><code>bla</code></pre>
<style>
pre {
margin: 0px;
outline: none;
}
pre code {
display: block;
color: #a9b7c6;
counter-increment: line;
}
pre code:before {
content: '\00a0'counter(line)'\00a0';
margin-right: 2px;
border-right: solid #4b4b4b 1px;
}
</style>
What it looks like now:
What it should look like:

You should have included an MCVE in your question. It is now not clear what goes wrong. I cannot reproduce your problem, please see the below snippet.
Note that having the pre be contentEditable does not combine very well with having every line be a separate code element. I am assuming that you are handling this via JavaScript? Or am I misunderstanding what you do?
In any case, the below (that does not even use user-select: none) seems to work for me (in Firefox and Chrome, screenshot is taken in Firefox).
pre, code {
margin: 0;
padding: 0;
}
pre {
font-size: 0;
}
pre code {
counter-increment: line;
font-size: 1rem;
}
pre code:before {
content: counter(line)".";
margin-right: .5rem;
padding-right: .2rem;
border-right: 1px solid #ccc;
}
<pre contenteditable="true">
<code>a line 1</code>
<code>a line 2</code>
<code>a line 3</code>
<code>a line 4</code>
<code>a line 5</code>
<code>a line 6</code>
</pre>
Edit. I can actually reproduce the issue in Chrome. It looks like only the counter is highlighted by the selection, any other content is not. That is, in the below snippet, the dot is not highlighted. If I replace the content: counter(line)"." with, say, content: "1.", then the selection ignores the pseudo-elements as expected. The counter is always selected. It is not impacted by styling of ::selection either. This smells like a bug in Chrome to me - although nothing is guaranteed about text selection in the CSS spec of course (user-select and ::selection are both only in the draft for CSS4-UI)... But text in pseudo-elements should really not be selectable.
You could open a bug report. See what they say.
The only thing I can offer you is a workaround. Hope it still helps. It puts the line numbers in a completely separate element. The line numbers now need to be maintained using JavaScript. Since you state that you already have scripts in place to handle editing, I figured why not script this too?
* {
margin: 0;
padding: 0;
background: #fff;
color: #000;
}
.editor {
position: relative;
}
html {
font-size: 18px; /* define font size for rem */
}
pre {
padding-left: 2rem;
font-size: 0;
}
pre code {
font-size: 1rem;
line-height: 1.3rem;
}
lines {
position: absolute;
top: 0;
left: 0;
}
line {
display: block;
padding-right: .2rem;
border-right: 1px solid #ccc;
font-family: monospace;
font-size: 1rem;
line-height: 1.3rem;
-moz-user-select: none;
-ms-user-select: none;
-webkit-user-select: none;
user-select: none;
}
<div class="editor">
<pre contenteditable="true">
<code>a line 1</code>
<code>a line 2</code>
<code>a line 3</code>
<code>a line 4</code>
<code>a line 5</code>
<code>a line 6</code>
</pre>
<lines>
<line>1.</line>
<line>2.</line>
<line>3.</line>
<line>4.</line>
<line>5.</line>
<line>6.</line>
</lines>
</div>

It might not work because you forgot the prefixes:
pre code::before {
content: counter(line);
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
If that doesn't work:
pre code::before {
content: counter(line);
pointer-events: none;
}

Related

Content of <button> vertically centered, but content of <a> not, why? [duplicate]

Check out this code sample of a button and an anchor: http://jsbin.com/ecitex/2/edit
I'm trying to make them identical in all browsers. But differences remain, and different differences in every browser (tried Chrome, Safari, Firefox, IE8).
Which CSS normalizations am I missing?
Update:
Per suggested:
I added line-height: 50px (although my user agent's (Chrome's) default line-height for button elements is normal, and still it vertically centers text – how?!)
I added cursor: pointer to normalize mouse cursors.
http://jsbin.com/ecitex/11/edit
So, now check out the result in Firefox: notice the padding on the button?
Then check out the result in IE8: whoa, notice how the two are completely and utterly different?!
Update 2:
It seems that IE's problems are known and non-resolvable: http://www.quirksmode.org/css/tests/mozie_button2.html
I haven't found anything on Firefox's padding though. (The quirksmode article mentions an issue with Mozilla, but that's a different issue.)
Update 3:
Awesome, we fixed the Firefox issue: http://jsbin.com/ecitex/15/edit
Okay, so far every single answer has been providing part of the solution so there's not really one single best answer. I'll grant the best answer to the person that either:
Explains why we have to specify a line-height: 50px to vertically center text in an a, while a button has vertically centered text with a mere line-height: normal.
Provides a solution for the IE issue.
You can remove that extra padding in Firefox by using:
button::-moz-focus-inner {
border: 0;
padding: 0;
}
Here's a good explanation from Eric Meyer about line height which hopefully explains why you need to explicitly set it as 50px: http://meyerweb.com/eric/thoughts/2008/05/06/line-height-abnormal/.
Here's some new CSS that fixes the font size issue in IE:
button, a {
display: inline-block;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 10px 0;
padding: 0px;
height: 50px;
border-width: 0;
background-color: Red;
color: White;
font-family: sans-serif;
font-weight: normal;
font-size: inherit;
text-decoration: none;
line-height: 50px;
cursor: pointer;
font-size: 100%;
}
button {
#width:0px;
overflow: visible;
}
button::-moz-focus-inner {
border: 0;
padding: 0;
}
You need to use line-height property to bring your anchor tag text vertically centered
Demo
CSS
button, a {
display: inline-block;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 10px 0;
padding: 0;
height: 50px;
border-width: 0;
background-color: Red;
color: White;
font-family: sans-serif;
font-weight: normal;
font-size: inherit;
text-decoration: none;
line-height: 50px; <-------- Here
}
add the attribute cursor:pointer; in order to add a pointer when the mouse is hover (the input not always have it)
and at last use line-height:46px; for the vertical align
the full code is here -> http://jsbin.com/ecitex/10/edit

Simple html and css tooltip with newline/carriage return

I stumbled on a simple wonderful tooltip(http://cbracco.me/a-simple-css-tooltip/) but I'm unable to add a new line/ carriage return when I use it.
I have tried
&013;
\n
but no success so far. I have also tried using the html inbuilt tooltip but it looks ugly in firefox.
Tooltip on hover:
Sentence one here. Sentence
two here. Sentence three here.
What I'm trying to output on hover:
Sentence one here.
Sentence two here.
Sentence three here.
<style>
/* Some basic styles */
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
a:hover {
text-decoration: none;
}
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}
/* Position tooltip above the element */
[data-tooltip]:before {
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 7px;
width: 160px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
background-color: #000;
background-color: hsla(0, 0%, 20%, 0.9);
color: #fff;
content: attr(data-tooltip);
text-align: center;
font-size: 14px;
line-height: 1.2;
}
/* Triangle hack to make tooltip look like a speech bubble */
[data-tooltip]:after {
position: absolute;
bottom: 150%;
left: 50%;
margin-left: -5px;
width: 0;
border-top: 5px solid #000;
border-top: 5px solid hsla(0, 0%, 20%, 0.9);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
content: " ";
font-size: 0;
line-height: 0;
}
/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
</style>
<a href="#" data-tooltip ="Sentence one here.
Sentence two here.
Sentence three here.">Click for more details</a>
You can
EDIT: This corrects my previous answer that said you can't.
Because your CSS uses content: attr() to get the text inside a specified attribute, you can use an HTML entity code such as
to insert a newline.
Inserting a newline via an HTML entity code:
Note: You must set display: block and white-space: pre|pre-line|pre-wrap for the newline to display.
[data-tooltip] {
cursor: default;
font: normal 1em sans-serif;
}
[data-tooltip]:hover:after {
display: inline-block;
content: attr(data-tooltip);
white-space: pre-wrap;
color: Tomato;
background-color: OldLace;
padding: 1em;
}
<span data-tooltip="Sentence one here. Sentence
two here. Sentence three here.">See my CSS tooltip with HTML-entity &#xa; line break:</span>
This was also answered here: CSS data attribute new line character & pseudo-element content value
Try finishing the line with a full stop (period) and begin the next line with a space. I think you will find that works
UPDATE
There is still some debate about this working. Are the lines being pushed down because they are too long? The answer is NO they are not.
As you can see in the above image. The very long line (line 1) is split. However, the smaller lines (line 2 and 3) are still placed on new lines and they are not long enough to be wrapped.
FINAL UPDATE SOLVED
As you can see this now works properly
This is the code to write the tooltip properly
<a href="#" title ="Sentence
one here. Sentence two here.
Sentence
three
here.">Click for more details</a>
Each new line = a new line in the tooltip. use "title" and not data-tooltip.
I had trouble with the other solutions, so here's what I opted for on my site:
<a href="" title="Sentence one here. Sentence two here. Sentence three here.">
I was able to get the multi-line tooltip to work via the title attribute by using the
HTML entity:
<a href="" title="Sentence one here.
Sentence two here.
Sentence three here.">

Add a line next to a header with CSS

Is there a way to display a line next to a header using CSS? Here's an image of what I'm talking about:
I could do it with a static background image, but that'd require custom CSS for every heading. And I could do some hacky stuff using :after and background colors on the h1, but it wouldn't look right against a gradient background.
I'd like to do this with CSS, not JavaScript. If it doesn't work in older browsers, that's fine.
UPDATE:
In the past I've done something like this:
<h1><span>Example Text</span></h1>
h1 {background-image:url("line.png");}
h1 span {background-color:#FFF;dislpay:inline-block;padding-right:10px}
While that works, it's hacky, and it doesn't work well with gradient backgrounds, because the span has to have a solid background color.
What I'm really looking for is something like this:
<h1>Example Text</h1>
h1 {background-image:url("line.png");} /* but don't appear under the example text */
I misspoke about the :after thing in the original post, I was thinking of another issue I had in the past.
You could do something like the following:
HTML
<div class="border">
<h1>Hello</h1>
</div>
CSS
h1 {
position: relative;
bottom: -17px;
background: #fff;
padding-right: 10px;
margin: 0;
display: inline-block;
}
div.border {
border-bottom: 1px solid #000;
}
Here is the JsFiddle to the above code.
After doing some more research, I think I found the best solution:
h2 {
color: #F37A1F;
display: block;
font-family: "Montserrat", sans-serif;
font-size: 24px;
font-weight: bold;
line-height: 25px;
margin: 0;
text-transform: uppercase;
}
h2:after {
background: url("../images/h2.png") repeat-x center;
content: " ";
display: table-cell;
width: 100%;
}
h2 > span {
display: table-cell;
padding: 0 9px 0 0;
white-space: nowrap;
}
Modified from: How can I make a fieldset legend-style "background line" on heading text?
It still requires some extra markup, unfortunately, but it's the most minimal that I've found. I'll probably just write some jQuery to add the span automatically to the h2s.
Here is one way of doing it.
Start with the following HTML:
<h1>News<hr class="hline"></h1>
and apply the following CSS:
h1 {
background-color: tan;
display: table;
width: 100%;
}
.hline {
display: table-cell;
width: 100%;
padding-left: 20px;
padding-right: 20px;
border: none;
}
.hline:after {
content: '';
border-top: 1px solid blue;
width: 100%;
display: inline-block;
vertical-align: middle;
}
See demo at: http://jsfiddle.net/audetwebdesign/Dsa9R/
You can repurpose the hr element to add the line after the text.
The advantage here is that you don't have to wrap the text with some other element.
Note: You can rewrite the CSS selectors and avoid declaring a class name and save a bit of typing.

How to normalize a button and an anchor with CSS?

Check out this code sample of a button and an anchor: http://jsbin.com/ecitex/2/edit
I'm trying to make them identical in all browsers. But differences remain, and different differences in every browser (tried Chrome, Safari, Firefox, IE8).
Which CSS normalizations am I missing?
Update:
Per suggested:
I added line-height: 50px (although my user agent's (Chrome's) default line-height for button elements is normal, and still it vertically centers text – how?!)
I added cursor: pointer to normalize mouse cursors.
http://jsbin.com/ecitex/11/edit
So, now check out the result in Firefox: notice the padding on the button?
Then check out the result in IE8: whoa, notice how the two are completely and utterly different?!
Update 2:
It seems that IE's problems are known and non-resolvable: http://www.quirksmode.org/css/tests/mozie_button2.html
I haven't found anything on Firefox's padding though. (The quirksmode article mentions an issue with Mozilla, but that's a different issue.)
Update 3:
Awesome, we fixed the Firefox issue: http://jsbin.com/ecitex/15/edit
Okay, so far every single answer has been providing part of the solution so there's not really one single best answer. I'll grant the best answer to the person that either:
Explains why we have to specify a line-height: 50px to vertically center text in an a, while a button has vertically centered text with a mere line-height: normal.
Provides a solution for the IE issue.
You can remove that extra padding in Firefox by using:
button::-moz-focus-inner {
border: 0;
padding: 0;
}
Here's a good explanation from Eric Meyer about line height which hopefully explains why you need to explicitly set it as 50px: http://meyerweb.com/eric/thoughts/2008/05/06/line-height-abnormal/.
Here's some new CSS that fixes the font size issue in IE:
button, a {
display: inline-block;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 10px 0;
padding: 0px;
height: 50px;
border-width: 0;
background-color: Red;
color: White;
font-family: sans-serif;
font-weight: normal;
font-size: inherit;
text-decoration: none;
line-height: 50px;
cursor: pointer;
font-size: 100%;
}
button {
#width:0px;
overflow: visible;
}
button::-moz-focus-inner {
border: 0;
padding: 0;
}
You need to use line-height property to bring your anchor tag text vertically centered
Demo
CSS
button, a {
display: inline-block;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 10px 0;
padding: 0;
height: 50px;
border-width: 0;
background-color: Red;
color: White;
font-family: sans-serif;
font-weight: normal;
font-size: inherit;
text-decoration: none;
line-height: 50px; <-------- Here
}
add the attribute cursor:pointer; in order to add a pointer when the mouse is hover (the input not always have it)
and at last use line-height:46px; for the vertical align
the full code is here -> http://jsbin.com/ecitex/10/edit

any css after this code not working , what is the problem in this css code, only in firefox?

Footer css after form's css is not working , what is the problem in this css code?
/*------------------------------------*\
FORMS */
fieldset { padding: 10px; border: 1px solid #ccc; margin-bottom: 1.5em; }
label { display: block; cursor: pointer; }
label:after { content:\": "; }
label::after { content:\": "; }
input,textarea { font-family: inherit; font-size: 1em; line-height: 1.5; }
[placeholder] { cursor: pointer; }
[placeholder]:active,[placeholder]:focus { cursor: text; }
fieldset > :last-child { margin: 0; }
footer {background:red; height:200px; width:200px}
jSfiddle http://jsfiddle.net/Aw239/
in jsfiddle example footer should be red background.
At least firefox Doesnt seem to understand this label:after { content:\": "; } i have no idea whats going on here but all i know is that it is the culprit. I've never actually used content properly.
But, remove those lines and everything runs smoothly.. ( OR reformat those two lines as Kyle showed and it should do it. )
Seems like it doesnt like to have closing semicolons in the same row with everything else
http://jsfiddle.net/Aw239/3/
Or.. get rid of the \'s ?
I formatted your CSS a bit and it seems to work in my FF5. http://jsfiddle.net/Aw239/2/
It is red. Are you using IE8? Because you'll need to use the HTML5 shim if you want to style HTML5 elements.
Does this work for you?