Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using :focus to style outer div?

When I begin writing text in the textarea, I want the outer div, with a class box, to have its border turned solid instead of dashed, but somehow the :focus doesn't apply in this case. If it works with :active, how come it doesn't work with :focus?

Any ideas why?

(Note. I want the DIV's border to turn solid, NOT the textareas)

div.box {     width: 300px;     height: 300px;     border: thin dashed black; }  div.box:focus{     border: thin solid black; }  <div class="box">     <textarea rows="10" cols="25"></textarea> </div> 
like image 324
Weblurk Avatar asked Oct 24 '11 13:10

Weblurk


People also ask

Does focus work on a div?

Yes - this is possible.

Can I focus on a div CSS?

The <div> does not accept input, so it cannot have :focus . Furthermore, CSS does not allow you to set styles on an element based on targeting its descendants. So you can't really do this unless you are willing to use JavaScript. Note that a <div> can get focus if you set the contenteditable="true" attribute.

Can I use CSS focus within?

The :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus .


2 Answers

Other posters have already explained why the :focus pseudo class is insufficient, but finally there is a CSS-based standard solution.

CSS Selectors Level 4 defines a new pseudo class:

:focus-within

From MDN:

The :focus-within CSS pseudo-class matches any element that the :focus pseudo-class matches or that has a descendant that the :focus pseudo-class matches. (This includes descendants in shadow trees.)

So now with the :focus-within pseudo class - styling the outer div when the textarea gets clicked becomes trivial.

.box:focus-within {     border: thin solid black; } 

.box {      width: 300px;      height: 300px;      border: 5px dashed red;  }    .box:focus-within {      border: 5px solid green;  }
<p>The outer box border changes when the textarea gets focus.</p>  <div class="box">      <textarea rows="10" cols="25"></textarea>  </div>

Codepen demo

NB: Browser Support : Chrome (60+), Firefox and Safari

like image 104
Danield Avatar answered Oct 06 '22 01:10

Danield


DIV elements can get focus if set the tabindex attribute. Here is the working example.

#focus-example > .extra {    display: none;  }  #focus-example:focus > .extra {    display: block;  }
<div id="focus-example" tabindex="0">    <div>Focus me!</div>    <div class="extra">Hooray!</div>  </div>

For more information about focus and blur, you can check out this article.

Update: And here is another example using focus to create a menu.

#toggleMenu:focus {    outline: none;  }  button:focus + .menu {    display: block;  }  .menu {    display: none;  }  .menu:focus {    display: none;  }
<div id="toggleMenu" tabindex="0">    <button type="button">Menu</button>    <ul class="menu" tabindex="1">      <li>Home</li>      <li>About Me</li>      <li>Contacts</li>    </ul>  </div>
like image 40
Karlen Kishmiryan Avatar answered Oct 06 '22 00:10

Karlen Kishmiryan