222

I want to change border color of TEXTAREA on focus. But my code doesn't seem to working properly.

The code is on fiddle.

<form name = "myform" method = "post" action="insert.php"  onsubmit="return validateform()" style="width:40%">
    <input type="text" placeholder="Enter Name." name="name" maxlength="300" class="input">
    <input type="email" placeholder="Enter E-mail." name="address" maxlength="300" class="input">
    <textarea placeholder="Enter Message." name="descrip" class="input" ></textarea>    
    <br>
    <input class="button secondary" type=submit name="submit" value="Submit" >
</form>

Here is the CSS

.input {
    border:0; 
    padding:10px; 
    font-size:1.3em; 
    font-family:"Ubuntu Light","Ubuntu","Ubuntu Mono","Segoe Print","Segoe UI";
    color:#ccc; 
    border:solid 1px #ccc; 
    margin:0 0 20px; 
    width:300px;
    -moz-box-shadow: inset 0 0 4px rgba(0,0,0,0.2); 
    -webkit-box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.2); 
    box-shadow: inner 0 0 4px rgba(0, 0, 0, 0.2);
    -webkit-border-radius: 3px; 
    -moz-border-radius: 3px; 
    border-radius: 3px;    
  }

input:focus { 
    outline: none !important;
    border-color: #719ECE;
    box-shadow: 0 0 10px #719ECE;
 }
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Mercurial
  • 3,615
  • 5
  • 27
  • 52

9 Answers9

449
.input:focus {
    outline: none !important;
    border:1px solid red;
    box-shadow: 0 0 10px #719ECE;
  }
Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
Adrift
  • 58,167
  • 12
  • 92
  • 90
  • 4
    You could also change the input:focus to .input:focus as you are already using that class on the textarea. You get rid of the extra selector that way. – Hoshts Apr 22 '13 at 21:10
  • 1
    Just curious, buy why do textareas need `outline: none` but normal textbox inputs do not? – Justin Skiles Jul 11 '14 at 01:15
  • I think better is using outline and shadow together "outline: solid 2px #8dc63f; box-shadow: 0 0 21px #8dc63f;" :-) – Bruno Jun 08 '17 at 21:04
  • worked for me without use of !important as well. – Suki Jun 30 '22 at 12:54
  • 1
    Don't use `outline: none`, use `outline: transparent` as it's more friendly to people with assistive technologies if I'm not mistaken. – Vince Jul 11 '23 at 20:31
54

There is an input:focus as there is a textarea:focus

    input:focus { 
        outline: none !important;
        border-color: #719ECE;
        box-shadow: 0 0 10px #719ECE;
    }
    textarea:focus { 
        outline: none !important;
        border-color: #719ECE;
        box-shadow: 0 0 10px #719ECE;
    }
Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
  • 17
    Both of the rules can be combined as follows - `input:focus, textarea:focus { outline: none !important; border-color: #719ECE; box-shadow: 0 0 10px #719ECE; }` If you want same effect on both type of elements. – Hitesh May 03 '16 at 15:07
  • Its clean but how its this still good for Accessibility? – klewis Jun 23 '22 at 15:38
  • for me `textarea:focus{ background-color: lightyellow; }` is not working – S. W. G. Mar 27 '23 at 17:14
26

Probably a more appropriate way of changing outline color is using the outline-color CSS rule.

textarea {
  outline-color: #719ECE;
}

or for input

input {
  outline-color: #719ECE;
}

box-shadow isn't quite the same thing and it may look different than the outline, especially if you apply custom styling to your element.

Mitchell Simoens
  • 2,516
  • 2
  • 20
  • 29
Grgur
  • 7,092
  • 2
  • 19
  • 34
  • 1
    Yes, but isn't the outline a rectangle? Not good if you are styling the buttons/input with border-radius. – Joseph Kreifels II Aug 01 '19 at 12:13
  • 3
    The outline is what browsers use for highlighting focused fields for accessibility. I would recommend sticking to it whenever possible. Obviously, you're free to create workarounds if your design is very specific :) – Grgur Aug 03 '19 at 12:28
  • For anyone wondering, outline-color works fine with border-radius. – wihlke Jun 12 '22 at 19:37
9

so simple :

 outline-color : green!important;

the whole CSS for my react-boostrap button is:

 .custom-btn {
     font-size:1.9em;
     background: #2f5bff;
     border: 2px solid #78e4ff;
     border-radius: 3px;
     padding: 50px 70px;
     outline-color : blue !important;
     text-transform: uppercase;
     user-select: auto;
     -moz-box-shadow: inset 0 0 4px rgba(0,0,0,0.2);
     -webkit-box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.2);
     -webkit-border-radius: 3px;
     -moz-border-radius: 3px;
 }
Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
Farbod Aprin
  • 990
  • 1
  • 10
  • 20
7

This will work fine.

input:focus{
    outline: none;
    border:1px solid red;
}
Tawhid Monowar
  • 126
  • 1
  • 6
5

On my personal code I removed the border property because it changed component size.

.input:focus {
  outline: none;
  box-shadow: 0 0 0 0.125rem red;
}
Italo Brenner
  • 128
  • 1
  • 7
3

Try out this probably it will work

input{
outline-color: #fff //your color
outline-style: none // it depend on you 
}
vicky raj
  • 59
  • 1
1

you need just in scss varible

$input-btn-focus-width:       .05rem !default;
Vahid Alvandi
  • 588
  • 9
  • 17
1

You can use CSS's pseudo-class to do that. A pseudo-class is used to define a special state of an element.

there is a ::focus pseudo-class that is used to select the element that has focus. So you can hook it in your CSS like this

Using class

.my-input::focus {
  outline-color: green;
}

Using Id

#my-input::focus {
  outline-color: red;
}

Directly selecting element

input::focus {
  outline-color: blue;
}

Using attribute selector

input[type="text"]::focus {
  outline-color: orange;
}
Hadi Mir
  • 4,497
  • 2
  • 29
  • 31