2

I know I can set an element to be focused on document load using JS/jQuery:

$('#myInput').focus();

My question is if it's possible to do it using CSS only, no script.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • 1
    perhaps the HTML5 input `autofocus` attribute? – Hvordan har du det Jan 15 '13 at 15:01
  • @RoxDorentus +1, thanks. the only exception with this attribute is IE, obviously. – Yair Nevet Jan 15 '13 at 15:06
  • possible duplicate of [Setting input control focus without javascript](http://stackoverflow.com/questions/983013/setting-input-control-focus-without-javascript) – j08691 Jan 15 '13 at 16:04
  • @j08691, they're different questions. This one's asking about CSS, the other one's asking about not using JavaScript. – zzzzBov Jan 15 '13 at 17:08
  • @zzzzBov - yes however if you read the answers and comments you'll see that the answer is that it's can't be done without JavaScript unless you use HTML5. In other words, it can't be done in CSS. – j08691 Jan 15 '13 at 17:10
  • @j08691, different questions can have the same answer. That doesn't make the question any less different. – zzzzBov Jan 15 '13 at 17:12

1 Answers1

6

It is not possible to set focus in CSS. It’s not the kind of things CSS is designed for, as it is a behavior issue.

As noted in a comment, in HTML you can set initial focus using the autofocus attribute.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390