Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unselectable content/text within an HTML table

Tags:

html

css

I have the following html:

<table class="code-table hljs">
  <tbody>
    <tr class="code-row">
      <td class="line-number unselectable">1</td>
      <td class="code-col">one</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">2</td>
      <td class="code-col">two</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">3</td>
      <td class="code-col">three</td>
    </tr>
  </tbody>
</table>

The relevant css:

.unselectable {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

In both Firefox and Chrome if I select all (ctrl-a), what I expect to happen, only "one", "two", and "three" are selected and not the line numbers, happens. However, when I paste what's in the clipboard I get differing results:

Chrome output:

one
2 two
3 three

Firefox output:

one

two

three

So Chrome is copying every unselectable line except the first, and firefox is putting in an extra line where there shouldn't be one.

Current Chrome version is Version 54.0.2840.71 m and current firefox version is 49.0.2 (Both are able to use user-select: none; according to http://caniuse.com/#feat=user-select-none

Is a css solution to this currently possible?

Edit Please note that the table I'm receiving is being rendered by another library and I can really only manipulate the classes.

like image 433
Nate Avatar asked Nov 21 '16 16:11

Nate


People also ask

How do you make text unselectable?

In our HTML file, we will assign some text to the “<p>” tag and mention it inside the <body> tag. In the CSS file, simply utilize the “user-select” property with the value “none”. This will make the text unselectable respectively.

How do you make an element Unhighlightable in HTML?

For images not to be highlighted make them as background. You can also place a transparent div on top of the area where you don't want the selection to occur. Position the div so that they will appear on top of the element with a higher stacking order. For a greater stacking order you can use the z-index property.

How do I color a specific cell in a table in HTML?

The HTML <td> bgcolor attribute is used to specify the background color of a table cell. It is not supported by HTML 5. Attribute Values: color_name: It sets the text color by using the color name.


2 Answers

Is a css solution to this currently possible?

An HTML solution to this is possible:

<ol>
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
like image 56
Rounin - Glory to UKRAINE Avatar answered Oct 19 '22 22:10

Rounin - Glory to UKRAINE


@Rounin almost had it. Here's a modified version of what he made. It will show highlighting the 1-3 but it wont copy.

.unselectable {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  visibility: hidden;
  display: block;
 }


 .code-table {
 counter-reset: code-table;
 }

 .code-table tr td:nth-of-type(2)::before {
 counter-increment: code-table;
 content: counter(code-table) '  \00a0\00a0';
 }
<table class="code-table hljs">
  <tbody>
    <tr class="code-row">
      <td class="line-number unselectable">1</td>
      <td class="code-col">one</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">2</td>
      <td class="code-col">two</td>
    </tr>
    <tr class="code-row">
      <td class="line-number unselectable">3</td>
      <td class="code-col">three</td>
    </tr>
  </tbody>
</table>
like image 29
Branden Ham Avatar answered Oct 19 '22 22:10

Branden Ham