Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to confine HTML selection range to text itself

This question has long bothered me from time to time.

When we crate a paragraph/list/span within a div, we expect users to be able to select only the text, instead of the entire horizontal block on the webpage.

But when I create a webpage, what I get when selecting text is below:

enter image description here

Is there anyway to limit users' range of selection through html or CSS? Or is it the way I structure the divs that causes the problem?

The ideal situation:

enter image description here

like image 748
carl-pch Avatar asked Dec 24 '12 06:12

carl-pch


People also ask

How do you make text stay together in HTML?

The secret code To force a web browser to treat 2 separate words as if they were 1, use the code   instead of a space, like so: These two words are like one.

How do I force text to stay inside a div?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

How do I force text to stay on one line in CSS?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.

How do I stop text from overflowing outside Div box?

If overflow is set to hidden then any overflow will not be visible. This content is outside of the box.


1 Answers

It looks like only Chrome creates a full-page-width selection like on a pic. 1. Firefox, Opera, and IE 10 deal with it like on a pic. 2. (Check yourself.)

There is a non-standard CSS property which may give the expected result: user-select: element (supported by Webkit, Firefox, and IE 10 with respected prefixes); it restricts selection to the bounds of the selected element. However there is no use of it: Webkit doesn't support element value, and other browsers already work like you want.


Update

Looks like float: left or position: relative "fix" text selection in Chrome. (See fiddle.)

like image 143
Pavlo Avatar answered Oct 14 '22 04:10

Pavlo