Avoid textarea content overlapping with overlay box at the bottom - html

I'm trying to add an overlay box at the bottom of a textarea. Positioning the overlay box was easy, but now I want the textarea content to never overlap the overlay box.
My first approach was adding padding-bottom so that the text never reaches the bottom of the textarea, where the overlay box is placed. However, as I type, the text will go under it. Also, scrolling up will cause the same undesired behavior.
Edit:
In response to some of the answers that partially solve my issue. I'm trying to make the textarea look as native as possible, so border color changing on focus would be necessary as well.
.container {
position: relative;
width: 110px;
}
textarea {
width: 100%;
height: 50px;
resize: none;
}
texarea.with-padding {
padding-bottom: 1em;
}
span {
position: absolute;
bottom: 5px;
width: 100%;
height: 1em;
background: rgba(255,0,0,0.5);
}
<div class="container">
<textarea name="" id="">I want this to never go under the red box.</textarea>
<span></span>
</div>
<div class="container">
<textarea class="with-padding" name="" id="">I tried with padding-bottom, but it doesn't work either.</textarea>
<span></span>
</div>

You can use a <div> container (which holds your textarea and overlay) as a fake border and remove the border of textarea. Just as shown in the snippet below:
$('textarea').on('focus', function() {
$('.textarea-holder').css('border-color', 'rgba(255, 0, 0, 0.5)');
});
$('textarea').on('blur', function() {
$('.textarea-holder').css('border-color', '#333');
});
.textarea-holder {
border: 1px solid #333;
display: inline-block;
}
.textarea-holder textarea {
display: block;
resize: none;
border: none;
}
textarea:focus {
outline: none;
}
.textarea-holder .overlay {
width: 100%;
height: 20px;
background: rgba(255, 0, 0, 0.5);
}
body {
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="textarea-holder">
<textarea rows="6"></textarea>
<div class="overlay"></div>
</div>
Hope this helps!

You can simply add a bottom-border: 1em to the textarea to imitate the span element.
Here is a working example: http://codepen.io/anon/pen/woKyvy#anon-login
.container {
position: relative;
width: 200px;
}
textarea {
width: 100%;
height: 50px;
border-bottom: 1em solid rgba(255,0,0,0.5);
}
<div class="container">
<textarea>Try typing. The cursor will never end up under the red line.</textarea>
</div>

So I went ahead and wrote it down:
Removed the border and reset some styles of textarea
Added the fake border to the container and removed the positioning of the span and made it a block element.
See code below:
.container {
position: relative;
width: 110px;
border: 1px solid;
}
textarea {
width: 100%;
height: 50px;
resize: none;
border:none;
outline:none;
padding: 0;
}
.container span {
display:block;
width: 100%;
height: 1em;
background: rgba(255, 0, 0, 0.5);
}
<div class="container">
<textarea name="" id="">I want this to never go under the red box.</textarea>
<span></span>
</div>

I finally found a solution to this riddle thanks to Saurav Rastogi's and eyetea's answers. Both were almost perfect, but failed to make the textarea have its border highlighted on focus. I've managed to keep this behavior using outline.
I think both approaches are useful as they allow for two different border highlight on focus. One leaving the overlay outside, using a div wrapper strategy, and the one leaving it inside, using a very thick border-bottom.
/* Inner border on focus solution */
.textarea-wrapper {
border: 1px solid gray;
display: inline-block;
}
.textarea-wrapper textarea {
display: block;
border: none;
}
.textarea-wrapper textarea:focus {
outline: 1px solid green;
outline-offset: 0;
}
.textarea-wrapper .overlay {
width: 100%;
height: 20px;
background: rgba(255, 0, 0, 0.5);
}
/* Outer border on focus solution */
textarea.bottom-padded {
border-bottom: 21px solid rgba(255,0,0,0.5);
outline: 1px solid gray;
outline-offset: -1px;
}
textarea.bottom-padded:focus {
outline-color: green !important;
}
<div class="textarea-wrapper">
<textarea rows="3">Inner border on focus</textarea>
<div class="overlay"></div>
</div>
<textarea rows="3" class="bottom-padded">Outer border on focus</textarea>

Related

How to show the fieldset border through a section of the label?

I have a form with fieldsets, and would like to keep the border, but would like the border to be shown between some text legends. I can't get the legend to have a transparent background to let the border through (to be blocked by some text elements).
legend {
display:flex;
justify-content:space-between;
width: 100%;
background-color: transparent;
}
legend div {
background-color: white;
margin-left:0.5em;
margin-right:0.5em;
}
<form>
<fieldset>
<legend><div>Form Item</div><div>(extra 1)</div></legend>
<label>Input:</label><input></input>
</fieldset>
</form>
Extra div hack. If there is a way to do this without the extra div, that would be great.
I guess if I force the fieldset border (chrome's default border is 2px groove threedface), it works ok.
fieldset {
border: 1px solid black;
}
legend {
display:flex;
justify-content:space-between;
width: 100%;
position: relative;
}
legend div {
background-color: white;
margin-left:0.5em;
margin-right:0.5em;
}
legend div.line {
flex: 1 1 auto;
background-color: transparent;
}
legend div.line:before {
position: absolute;
z-index: -1;
content: '';
left: 0px;
right: 0px;
top: 50%;
border-top: 1px solid black;
}
<form>
<fieldset>
<legend><div>Form Item</div><div class="line"></div><div>(extra 1)</div></legend>
<label>Input:</label><input></input>
</fieldset>
</form>
background can approximate this without an extra div. You simply need to find the correct color:
fieldset {
border: 1px solid black;
}
legend {
display: flex;
justify-content: space-between;
width: 100%;
background: linear-gradient(black 0 0)center/100% 1px no-repeat;
}
legend div {
background-color: white;
padding:0 0.5em;
}
<form>
<fieldset>
<legend>
<div>Form Item</div>
<div>(extra 1)</div>
</legend>
<label>Input:</label><input>
</fieldset>
</form>

Stop form input overflow scrolling div contents

I have an input field within a absolute positioned div. The div has a specific width and overflow: hidden; applied with the input field being partially outside the confines of the container.
The problem arises in when typing in, the browser Firefox (and Chrome as well) will scroll the contents of this container div to make sure the input focus is within the viewport.
.overlay {
position: absolute;
border: 1px fuchsia solid;
padding: 20px;
max-width: 200px;
overflow: hidden;
label {
display: block;
color: #000;
}
input {
width: 400px;
}
}
How can I stop it doing this? The above example is on:
https://codepen.io/meep3d/pen/jdLdqV
You could get this done using JavaScript by setting scrollLeft to 0 on the scrollevent:
document.getElementById('overlay').onscroll = function(){
document.getElementById('overlay').scrollLeft = 0;
};
.overlay {
position: absolute;
border: 1px fuchsia solid;
padding: 20px;
max-width: 200px;
overflow: hidden;
}
label {
display: block;
color: #000;
}
input {
width: 400px;
}
Outside
<div class="overlay" id="overlay">
<label>Test Area</label>
<input type="text" />
</div>

Make text wrap around and not increase width

I have a div, and inside is a textarea and a div:
<div class="innotate">
<div class="innotate-form">
<div class="">
<textarea cols="30" rows="3" name="body"></textarea>
</div>
<div class="instructions">Some Text that is quite long but should wrap instead of lengthening the div</div>
</div>
</div>
I want my div.instructions to wrap around and not increase the width of my div.innotate.
I'm quite stumped on this. I know display: inline isn't what I need, and setting the width manually is not an option.
I want the textarea to decide the width. That means that the width of my parent div should be at most the width of my textarea.
http://jsfiddle.net/8fo6by8d/1/
I was searching for an answer for this question and found that another way to do this with out setting any min/max width to the container div is to add
max-width: fit-content
to the div around the text. And add
display: inline-block
to the container around the text and the textarea
<style type="text/css">
.innotate-form {
display: inline-block;
}
.instructions {
max-width: fit-content;
}
</style>
<div class="innotate">
<div class="innotate-form">
<textarea cols="30" rows="3" name="body"></textarea>
<div class="instructions">Some Text that is quite long but should wrap instead of lengthening the div</div>
</div>
</div>
Used flex box for this one, had to throw width into .innotate-form for good measure.
.innotate-form {
display: flex
flex-wrap: wrap;
width: 14em;
margin-top: 3px;
background-color: rgba(249,249,249,0.9);
border: 1px solid #eee;
padding: 12.5px;
z-index: 9999;
}
The fiddle
er. wait. You are physically setting the width of the text area at "cols" = 30. One good turn deserves another. When you set the width of the text area, set the width of .instructions to match. In fact, lets do it one better, all without JS.
see http://jsfiddle.net/8fo6by8d/5/
Lose the col= setting
<div class="innotate">
<div class="innotate-form">
<div class="">
<textarea rows="3" name="body"></textarea>
</div>
<div class="instructions">Some Text that is quite long but should wrap instead of lengthening the div</div>
</div>
</div>
CSS:
.innotate-form {
margin-top: 3px;
background-color: rgba(249,249,249,0.9);
border: 1px solid #eee;
padding: 12.5px;
position: absolute;
z-index: 9999;
}
.innotate-form .instructions {
font-size: 14px;
color: #f20;
width: 250px;
padding: 5px;
}
textarea {
width: 250px;
height: 120px;
border: 3px solid #cccccc;
padding: 5px;
}
And if you're upset at having to type in a width twice there is always
textarea, .innotate-form .instructions{
width: 250px;
padding: 5px;
}
use this CSS
.innotate-form .instructions {
font-size: 14px;
color: #f20;
min-width: 250px;
max-width: 250px;
padding: 5px;
word-break: break-all;
}
Also with flex, but German's didn't seem to work. This one uses a container instead. The text doesn't wrap, though. It just sizes with the container.
.container {
display: flex;
flex-wrap: wrap;
}
.innotate-form {
flex: 1;
margin-top: 3px;
background-color: rgba(249, 249, 249, 0.9);
border: 1px solid #eee;
padding: 12.5px;
}
.instructions {
font-size: 14px;
color: #f20;
}
textarea {
width: 100%;
}
JSFiddle
EDIT: I researched more and found CSS3's resize property. This will make any div behave like a text area, so it will squish the text. I used flex to make the text area occupy the entire area of the div, then disabled resizing on the actual textarea.
Can I Use?
JSFiddle
* {
box-sizing: border-box;
}
.container {
display: flex;
-webkit-resize: both;
-moz-resize: both;
resize: both;
overflow: auto;
width: 400px;
height:300px;
background: darkorange;
padding: 1em;
}
.innotate {
display: flex;
flex-direction: column;
flex: 1;
width: 100%;
padding: 1em;
background-color: rgba(255, 255, 255, 0.5);
border: 1px solid #FFF;
}
.instructions {
padding: 1em;
font-size: 16px;
color: red;
}
textarea {
flex: 1;
width: 100%;
resize: none;
background-color: rgba(255, 255, 255, 0.5);
border: 1px solid #FFF;
}

How to constrain CSS to when a contained input has focus?

Assume I have a form input structured as follows:
<div class="wrapper">
<span class="icon icon-search"></span>
<input type="text"/>
</div>
is there a way, with CSS3, to apply a red border around the .wrapper div on a focus state on the input element?
.wrapper input:focus {
border solid thin red;
}
puts the border on the input field but I want it on the containing div.
You're looking for a css parent selector. Unfortunately that isn't currently available. The exact functionality you're looking for would need JavaScript or alternative HTML.
It looks like you want the border to surround the icon and the field but currently it is only surrounding the field? My suggestion would be to use the sibling selector like so: (From my example below i've moved the icon after the input)
* {
box-sizing: border-box; /* Helps with sizing calculations */
}
.wrapper {
position: relative;
}
.icon {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
left: 200px;
top: 0;
border: solid 1px transparent;
border-left: none;
}
input {
width: 200px;
height: 20px;
border: solid 1px blue;
border-right: none;
}
input:focus {
border-color: red;
outline: 0;
}
input:focus + .icon {
border-color: red;
}
<div class="wrapper">
<input type="text"/>
<span class="icon icon-search"></span>
</div>

How do I change the border color of 2 divs at once using the hover effect

How can I change the border-color of my outer div #refdocs_main while also changing the bottom border color of my div #refdocs_container? Right now, only the outer container's border is colored on hover; how can I get both effects using CSS simultaneously?
Here is a fiddle: http://jsfiddle.net/Nb8cC/
And what I've tried so far:
HTML
<body>
<div id="refdocs_main">
<div id="refdocs_container"><input type="text" id="refdocs">
</div>
<div id="refdocs_wrapper">
<div id="refdocs_list">
<ul></ul>
</div>
</div>
</div>
</body>
CSS
#refdocs {
border: 0;
width: 100%;
height: auto;
padding-left: 2px;
}
#refdocs_main {
border: 1px solid rgb(170,170,170);
width: 179px;
overflow: hidden;
margin-top: 2px;
}
#refdocs_main:hover {
border-color: rgb(128,128,128);
}
#refdocs_container {
border-bottom: 1px solid rgb(170,170,170);
height: 20px;
}
#refdocs_wrapper{
height: 100px;
overflow-y: scroll;
overflow-x: hidden;
}
#refdocs_list {
width: 100%;
}
#refdocs_list ul {
margin: 0;
padding: 0px;
list-style-type: none;
}
#refdocs_list li {
cursor: default;
padding: 2px;
}
Use:
#refdocs_main:hover, #refdocs_main:hover #refdocs_container {
border-color: rgb(128, 128, 128);
}
By adding #refdocs_main:hover #refdocs_container you enable the border on #refdocs_container to change only when #refdocs_main is being hovered.
jsFiddle example
Add:
#refdocs_main:hover #refdocs_container {
border-bottom: 1px solid red;
}
to your css stylesheet.
This reads when #refdocs_main is hovered over then for the #refdocs_container element apply the following changes.