HTML 5 Autofocus messes up CSS loading
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>
Autofocus doesn't work on Firefox and doesn't work when coming from another page
You can try with:
$("#username").focus();
after the page has been loaded.
How to avoid automatic focus on first input field when popping a HTML form as a JQuery dialog?
A solution is to set tabindex="-1"
on ALL input fields to keep HTML placeholders visible.
Why does calling focus() break my CSS transition?
As @user1585345 correctly stated a browser immediately scrolls to the focused element in order to bring it inside the current view, and not a px further!
What happens here is the following: you change the relative position of your element, but immediately you change focus, since the focused elements when the event fires is not visible the content also scrolls (ending the animation abruptly).
You should explicitly fire your focus only when the animation is complete, so that the browser recognise that no scrolling is necessary.
One solution is (suggested by @Mark)
there are two event listeners 'ontransitionend' and 'onanimationend'
you can add an event for and then fire your focus() function
Related Topics
How to Keep Text Opacity 100 When Its Parent Container Is Having Opacity of 50
Onmouseout Event Not Triggered When Moving Mouse Fast (Gwt - All Browsers)
@Media Query to Target Hi-Res Windows Phone 8+
How to Make The Scrollbar on a Div Only Visible When Necessary
Touchmove Pointer-Events: None CSS Doesn't Work on Chrome for Android 4.4/Chromeview
Resource Interpreted as Stylesheet But Transferred with Mime Type Text/HTML
How to Stop a CSS Keyframe Animation
How Exactly Does Link Rel="Preload" Work
Why Is My CSS3 Animation Not Working in Chrome or Safari
How to Change -Webkit-Scrollbar Width When Hover on It
Vertical-Align: Middle with Bootstrap 2
How to Replace @Media (Max-Width) Using Stylish or Greasemonkey
Simple Two Column HTML Layout Without Using Tables
Why Is There Excessive Whitespace with Facebook Mobile Comments Plugin
CSS - Placement of a Div in The Lower Left-Hand Corner
Difference Size Font Rendering on Windows Vs Macos. How to Solve