Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML 5 Autofocus messes up CSS loading

When I set autofocus="autofocus" on an input element, then in Firefox, when the page loads, it displays for a split second without the CSS applied. (E.g. content not centered, heading rendered in default font etc.)

If I remove the autofocus, the page loads fine and displays when it is ready.

Is there a way to get the page to load normally while using the autofocus feature in Firefox?

like image 279
Jimmay Avatar asked Sep 22 '13 11:09

Jimmay


People also ask

How do I turn off auto focus in CSS?

If you want to remove the focus around a button, you can use the CSS outline property. You need to set the “none” value of the outline property.

Does autofocus attribute always set focus on first input field in html5?

The first input or textarea in source order that has the autofocus attribute will be focused on page load. In browsers without autofocus support, no fields will be focused on page load, unless otherwise given focus with JavaScript.

What does the html5 autofocus attribute?

The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.


1 Answers

I have found that by adding some JavaScript in the <head>, the page waits for the style to load before the focus.

I'm not exactly sure why this works, but it does!

Example:

<script type="text/javascript">
    // Fix for Firefox autofocus CSS bug
    // See: http://stackoverflow.com/questions/18943276/html-5-autofocus-messes-up-css-loading/18945951#18945951
</script>
like image 184
Jimmay Avatar answered Oct 16 '22 13:10

Jimmay