Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have focused Input form with Bootstrap

I'm using Bootstrap 3.0.0 and I want to use the focused input form: See here, under Form States.. input focus

I have used <input class="form-control" id="focusedInput" type="text" value="This is focused..."> this to get it as focused, but I'm not getting that. Documentation said to use :focus to get focused but I don't know how.

some code in bootstrap.css

.form-control {
  display: block;
  width: 100%;
  height: 34px;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.428571429;
  color: #555555;
  vertical-align: middle;
  background-color: #ffffff;
  border: 1px solid #cccccc;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
          box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  -webkit-transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
          transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
}

.form-control :focus {
  border-color: #66afe9;
  outline: 0;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
          box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6);
}
like image 536
Lonely Avatar asked Aug 21 '13 05:08

Lonely


People also ask

How do I focus input in Bootstrap?

Answer: Use the jQuery . focus() method You can simply use the jQuery . focus() method to set the focus on the first input box or textarea inside the Bootstrap modal when it is loaded upon activation.

How do you make an input field focused?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do I change the input focus color in Bootstrap 5?

Answer: Use the CSS box-shadow Property By default, all the textual <input> elements, <textarea> , and <select> elements with the class . form-control highlighted in blue glow upon receiving the focus in Bootstrap.

How do you focus an input box in HTML?

Input Text focus() MethodThe focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.


2 Answers

You can achieve what you want by setting the autofocus attribute inside the input tag to autofocus. The keyboard input will be focused on the input field and it will glow.

<input class="form-control" id="focusedInput" type="text" autofocus="autofocus" value="This is focused...">
like image 192
omar.furrer Avatar answered Sep 21 '22 07:09

omar.furrer


They present there only a focus demo. The #focusedInput styles come from docs.css and it's used just to force the focused look. That isn't included in Bootstrap pack, but only in documentation pages, as you can see in the following screen shoot (see: docs.css:1072):

Then we have to create a CSS class that will have the focused styles:

.focusedInput {
    border-color: rgba(82,168,236,.8);
    outline: 0;
    outline: thin dotted \9;
    -moz-box-shadow: 0 0 8px rgba(82,168,236,.6);
    box-shadow: 0 0 8px rgba(82,168,236,.6) !important;
}

Now, only including focusedInput class in HTML input elements, you will be able to set focus styles for non-focused elements.

<input class="form-control focusedInput" type="text" value="This is focused...">

See the JSFIDDLE


:focus selector is used for setting the styles for focused state of input.

JSFIDDLE

For example:

#focusedInput:focus {
    background: red;
}

This will set red background when you focus the input.

like image 28
Ionică Bizău Avatar answered Sep 17 '22 07:09

Ionică Bizău