开发者

CSS - Focus login fields just like twitter with only CSS?

I already posted a similar question and got a jQuery solution that works. Now I want to do it with only CSS/HTML. I saved twitter's homepage locally and deleted all the js scripts and noticed that the effect I'm trying to achieve is with CSS/HTML (when you click on the username/pass the values "Username"/"Password" stay there until you enter text).

I'm a newbie at these kind of new CSS/HTML effects and have spent the last couple of hours trying to replicate it with no success.

Here's the html of twitter's login form:

<form action="#" class="signin" method="post">
      <fieldset class="textbox">
        <div class="holding username">
          <input type="text" id="username" value="" name="session[username_or_email]" title="Username or email" autocomplete="on">
          <span class="holder">Username</span>
        </div>
        <div class="holding password">
          <input type="password" id="password" value="" name="session[password]" title="Password">
          <span class="holder">Password</span>
        </div>
      </fieldset>
      <fieldset class="subchck">
  <label class="remember">
    <input type="checkbox" value="1" name="remember_me">
    <span>Remember me</span>
  </label>
  <button type="submit" class="submit button">Sign in</button>
&开发者_开发技巧lt;/fieldset>

I've looked over the site's CSS but it's 10,000 lines and very complicated. How should the CSS look like? Or could you point me out to a tutorial on how to achieve the same effect as this is driving me nuts?

Thank you very much, Cris


Set the HTML autofocus attribute:

<input type="text" placeholder="Type here ..." autofocus="autofocus" />


You can target elements that are focused or blured like so:

input:focus {color:red;}

You now need to nest the CSS to hide the span called holder inside the input.

span.holder input:focus {visibility:hidden;} 

I have not tried this, but it would be something like this.


To clarify, I have just pulled the JavaScript twitter use and the source for their home page and I can confirm that they are using the following JavaScript function for focus on the field

 inp.focus()

The JavaScript is quite lengthy but it looks like after a quick read that they are using jQuery that is setting focus based on the class being username.

I just looked at the autofocus property suggested by another poster and this method has worked for me in my web app currently under development.

The code for this is

<input type="text" id="username" value="" name="session[username_or_email]" title="Username or email" autocomplete="on" autofocus>

Note, per the documentation at the W3C website, the autofocus property can only be used once on the page. I have put it into a form that is hidden and shown in an inline element using Fancybox.

The grayed out text in the input field can be done with the place-holder element, something I'm already using, add the following into your input element

placeholder="Username" 

NOTE: Both placeholder and autofocus are HTML5 properties and may not be supported by all major browsers yet, this is why JavaScript is still being used by sites like twitter.

The styling is done based on CSS/CSS3 greatly, an excellent resource is W3Schools. I would recommend for what you're wanting to achieve start at the CSS3 section looking at borders. Another resource that is excellent but hasn't been updated for about a month and a half sadly is doctype.tv. Nick has some fantastic advise regarding styling your website along with some great insight into design.


Judging by the bolded text in your question (when you click on the username/pass the values "Username"/"Password" stay there until you enter text), I'm guessing what you want is the placeholder attribute, which @phihag has in his example.

<input type="text" placeholder="This text will disappear" />

The placeholder attribute works without Javascript in browsers that support it. For older browsers, you'll need some Javascript, and this is probably what Twitter is doing in their code.

See the Wufoo page on the Placeholder Attribute for more details, including how to do a javascript fallback and what browsers it is currently supported in.

See also this demo which shows how to style the ":placeholder" and ":active" states (at least for webkit and mozilla).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜