Languages
[Edit]
EN

HTML - get focus on div element

10 points
Created by:
user4838
728

In this short article we would like to show how to get focus on div element in HTML.

Quick solution:

add tabindex="0" attribute to the div element.

 

Practical example

In the below example we use :focus pseudo class only to show the div element is focused.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <style>

    div:focus {
        outline: 3px solid #b2d2f0;
    }

  </style>
  <div tabindex="0">Click me to get focus!</div>
  <div tabindex="0">Click me to get focus!</div>
  <div tabindex="0">Click me to get focus!</div>
</body>
</html>

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join