<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<style>
form:focus{
background:red;
}
</style>
<title>Home, sweet home</title>
</head>
<body>
<form>
<input type="text"/>
<input type="submit"/>
</form>
<form>
<input type="text"/>
<input type="submit"/>
</form>
<form>
<input type="text"/>
<input type="submit"/>
</form>
</body>
</html>
This obviously doesn’t work, as is why I’m asking the question. How can I get the form which has one if it’s inputs as the focus to highlight? That is, I want to be able to apply styles to the active FORM, not the active INPUT – is that doable without JS or something?
This code works as an exercise
but probably not a solution you should use. The version relying onlegendactually seems acceptable.There is no
form:focusselector so I thought instead the individualinput:focuscould create the desired effect using pseudo-elements. However, pseudo-elements can only be used on elements with content, like if I were to replaceinput[type=submit]withbuttonFiddled, but it instantly looked pretty crazy, so I’ve refactored the solution to rely onto a
legendelement. Enjoy 🙂