Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a div's outline color when selected?

Tags:

I need to change the outline color when the div is selected. I have tried by using the same technique as hover (shown below), but I need help with a selector. Is it possible via JavaScript or is CSS enough?

Here is what I tried:

div {      background: #ccc;      margin: 20px;  }  div:hover {      outline: 1px solid blue;  }
<div>1</div>  <div>2</div>  <div>3</div>
like image 828
UI_Dev Avatar asked Oct 23 '14 15:10

UI_Dev


People also ask

Which property is used to apply color for an outline?

The outline-color CSS property sets the color of an element's outline.

What is outline-color?

An outline is a line that is drawn around elements, outside the borders, to make the element "stand out". The outline-color property specifies the color of an outline. Note: Always declare the outline-style property before the outline-color property. An element must have an outline before you change the color of it.


1 Answers

You can try :focus pseudo-class. Note you will need tabindex to make your divs focusable.

div {      background: #ccc;      margin: 20px;  }  div:focus {      outline: 1px solid blue;  }
<div tabindex="-1">1</div>  <div tabindex="-1">2</div>  <div tabindex="-1">3</div>
like image 138
Oriol Avatar answered Oct 07 '22 18:10

Oriol