I have this almost good piece of code. Almost because IE 11 breaks it:
.textarea {
text-align: left;
background: white;
border: 1px solid grey;
border-style: inset;
height: 150px;
overflow-y: auto;
word-wrap: break-word;
width: 400px;
}
<div class='textarea' contenteditable='true' id='sec1'>blabla</div>
Why does the text (first line) move down after I hit return? I want either the text to appear at first where it will drop to, or just stay there. This will happen even if the 'first line' is entered manually, with no initial content.
This happens because IE adds p when you press enter. Using Shift + Enter will enter a br like the rest of the browsers.
Alternatively you could style the p elements in there to not have top margin
.textarea {
text-align: left;
background: white;
border: 1px solid grey;
border-style: inset;
height: 150px;
overflow-y: auto;
word-wrap: break-word;
width: 400px;
}
.textarea p{margin-top:0;}
<div class='textarea' contenteditable='true' id='sec1'>blabla</div>
But the basic problem is that IE creates different HTML.
Also have a look at avoid ie contentEditable element to create paragraphs on Enter key which comes with its own issues (explained in the answer & comments there)
EDIT from op
First thanks for the great answer. Second, this is how I fixed this using JS:
this.iebreak = function (event) {
if (event.which == 13 && !event.shiftKey) {
event.preventDefault();
this.field.appendChild(document.createElement("br"));
}
}
and of course add this if your' on i.e.:
this.field.addEventListener('keydown',function(event){thisObject.iebreak(event);});
Related
I made an auto growing input field.
For this I used the „Solution with span:” solution from this CodePen: https://codepen.io/chriscoyier/pen/XWbqpzP
Basically a span tag with role="textbox"
My problem is when I copy text from websites it copies everything and when I paste it into the input field it contains all the html characters with styling.
So text has html elements such as div, p, etc... and styling like background-color, color, border...
Facebook uses the same solution with role="presentation" for comment writing input field for the posts. But it works fine there, it only shows text and numbers.
Is there any solution to get rid of the additional characters?
Here is a „case study” :)
I copied the CodePen's title from the top left corner, and paste it into the span filed. This picture is illustrates well:
https://i.stack.imgur.com/WFchM.jpg
And here is my code as well, if it helps to find something:
<span class="input__textarea input__textarea__msg" role="textbox" contenteditable></span>
And the css:
&__textarea{
border-radius: 19px;
background-color: $medium;
padding: 6px 30px;
font-size: 18px;
outline: none;
#include shadow();
display: block;
width: 100%;
overflow: hidden;
min-height: 34px;
max-height: 300px;
overflow-y: scroll;
span{
background-color: transparent !important;
color: $dark !important;
line-height: 1;
//user-select: text;
}
&__msg[contenteditable]:empty::before{
content: "Send message";
color: #757575;
}
And this is what I have in the console after I paste some content (this is not from CodePen):
https://i.stack.imgur.com/5hxDZ.jpg
I have a div inside my editable container and when I press enter the div duplicate and I end up have two divs one on the top of the other.
the container have contenteditable as true, this might be causing this issue.
but is there anyway i can prevent this
<div class="singlediv"></div>
with the css
.singlediv {
border-color: rgb(155, 196, 243);
border: dotted 1px;
width: 100%;
padding: 10px;
min-height: 75px;
}
code here
You can have a function like following in your js :-
const handleEnter=(e)=>{
if(e.keyCode===13)
{
e.preventDefault();
}
}
And your html will be-
<div contenteditable="true" onkeypress="handleEnter(event)">
<div class="singlediv"></div>
</div>
The function handleEnter ensures to prevent the the default behaviour when pressing enter button on a div with .contentEditable attribute. The key code for Enter key is 13.
This is more of a hack. If you can prevent having the inner div, then do so.
That's really simple, if you make contenteditable with a div withing then you press enter an another div will be created. This code run corretly :
.singlediv {
border-color: rgb(155, 196, 243);
border: dotted 1px;
width: 100%;
padding: 10px;
min-height: 75px;
}
<div contenteditable="true" class="singlediv"></div>
I have placed two elements within a div, one is a textarea tag and the other a time tag. The time tag placed on the div. When the textarea has few words, the space between the textarea tag and the time is fine. But when the textarea contains many characters it covers the time tag as shown in the picture below
My challenge is that how can I maintain the distance dynamically between the textarea and the time tag despite the number of characters in the time tag.
This is the CSS code to show my attempt
.messages textarea[readonly] {
font-size: 15px;
font-family: "Helvetica Neue";
margin: 0 0 0.2rem 0;
color: #000;
word-wrap: break-word;
resize: none;
overflow: hidden;
min-height: 5px;
height: 1px;
min-height: inherit;
background: #c2dfff;
margin-bottom: 0px;
z-index: 10;
}
.messages time {
font-size: 1.0rem;
color: #696969;
float: right;
position: absolute;
bottom: 10px;
right: 0;
z-index: 40;
padding-right: 5px;
}
This is the HTML view
<div class="message">
<textarea readonly elastic>{{ msg.Content }}</textarea>
<time datetime="2009-11-13T20:00">{{ humanize(msg.Time) }}</time>
</div>
If you don't mind having sometimes the date below the text, this could be a solution:
https://jsfiddle.net/91czko52/1/
Basically, we're creating a phantom :after element inside the paraghaph (the phantom elem is the black one > should be transparent) of the same MAX date size (or maybe a little more). So the text will never touch the date.
NOTE: this also implies the use of a 'classic' paragraph element instead of textarea: I hope and guess you probably don't really need textarea.
A possible solution, by duplicating the date and using pseudo element. https://jsfiddle.net/jLo9rnfz/1/
Similar to above but not using the max-width, here you always have the correct width. Whichever you prefer :)
/* Using a trick by duplicating the date you can keep the space to ensure no wrapping */
.container {
background: red;
position: relative;
line-height: 1.4;
}
.item {
/* This ensure you always have the correct spave available and never wrap over the visible date */
background: orange;
color: transparent;
word-break: keep-all;
word-wrap: normal;
white-space: nowrap;
}
.item::before {
/* Here you have a duplicate date but this one is visible and correctly positioned
Adding the date to css can be done with js or php, google search will help you out there */
content: '5 days ago';
position: absolute;
color: black;
right: 0;
}
<div class="container">
paragraph here to test wrappingparagraph here to test wrappingparagraph here to test wrappingparagraph here to test wrapping
<span class="item">
5 days ago
</span>
</div>
Also consider checking out how it is done (inspect element) in existing apps that show similar behaviour, such as WhatsApp web.
Whenever I click on the checkbox, the browser window (firefox) will scroll on the top of the screen.
How can I prevent this behavior so when I click on the checkbox the browser window will not scroll on top?
Here is the code found from here http://jsfiddle.net/zAFND/6/
Thank you.
<html>
<head>
<title>Test</title>
<style>
div label input {
margin-right: 100px;
}
body {
font-family:sans-serif;
}
#ck-button {
margin: 4px;
background-color: #EFEFEF;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
float: left;
}
#ck-button {
margin: 4px;
background-color: #EFEFEF;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
float: left;
}
#ck-button:hover {
margin: 4px;
background-color: #EFEFEF;
border-radius: 4px;
border: 1px solid red;
overflow: auto;
float: left;
color: red;
}
#ck-button label {
float: left;
width: 4.0em;
}
#ck-button label span {
text-align: center;
padding: 3px 0px;
display: block;
}
#ck-button label input {
position: absolute;
top: -20px;
}
#ck-button input:checked + span {
background-color: #911;
color: #fff;
}
</style>
</head>
<body>
<br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="ck-button">
<label>
<input type="checkbox" value="1"><span>red</span>
</label>
</div>
</body>
The problem is this rule:
#ck-button label input {
position:absolute;
top:-20px;
}
When you click on a label the browser tries to focus the related input. In your case the checkbox element is lying at the top of the page, even outside the viewport – so Firefox tries to scroll there.
You can solve it like this by adding:
#ck-button label {
display: block;
position: relative;
overflow: hidden;
}
Demo
Try before buy
Alternative
Heisenberg points out a problem in his answer which can occur when using extreme values. Unfortunately the proposed idea has the same quirk as the one shown above.
So an alternative solution is simply to hide the input. The functionality is not affected.
CSS
#ck-button label input {
display: none;
}
Demo
Try before buy
The answer accepted is not entirely true. Works, but not in all cases.
If you use the common css to hide elements (probably -999em or similar) at the "top" attribute, in this case position:relative has nothing to do because always -999em will be much higher than the viewport.
The answer accepted works fine because the "top" is only -20px . Try to set it a more higher number and you´ll see the problem.
So, the solution is not to set a relative position.
I think the correct way is only to set a negative value at left position (not top).
Try it. :)
you could hide your checkbox input like this:
#ck-button label input {
position:absolute;
top:+20px;
visibility: hidden;
}
I want to create something like quicknote and at first I thought it will be easy task.
HTML
<div class="quicknote">
<textarea></textarea>
</div>
CSS
.quicknote
{
width: 308px;
height: 400px;
background: url('../images/note-bg.gif') 0 0;
outline: none;
padding: 10px;
}
.quicknote textarea
{
border: 0;
width: 100%;
height: 100%;
resize: none;
background: transparent;
outline: none;
font: 12px/22px Arial, sans-serif;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px dashed #aeaeae;
text-align: baseline;
}
Here is div which contains textarea. Textarea lineheight is set to 22px, when enter is pressed cursor is positioned on right place. (I know text is on the middle of the line).
But when scroll happen it seems text is not scrolled for 22px. Check out screen cast video to see what happend.
http://screencast.com/t/RYsPD5DH
I probably wouldn't see this without those lines in background. Does anyone know what is wrong here?
SOLUTION
I think that I have solution but don't ask me why this works. With little help of jQuery:
$textarea.on('scroll', function() {
$textarea.scrollTop($textarea.prop("scrollHeight"));
})
and experimenting with textarea height, on example 332px, 354px, it seems it works well. Because 332 isn't divided with 22, remineder is 2, when I changed above jQuery with:
$textarea.on('scroll', function() {
$textarea.scrollTop($textarea.prop("scrollHeight")+2);
})
all start to work as expected as you can see here http://screencast.com/t/pfhNJoUrSQS.
It appears to be working correctly with a 22px line height; however, you may want to remove the overflow:hidden property so that you can view scrolling.