5
<!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?

Bilal Akil
  • 4,716
  • 5
  • 32
  • 52
  • Do you want to get input box background color set to RED if particular Inputbox gets focus? – AvkashChauhan Jun 23 '12 at 02:44
  • I want the parent form to highlight red if any of its child inputs are the focus – Bilal Akil Jun 23 '12 at 02:45
  • That's is the big one. If you have multiple forms in the page, without JS, the renderer will not be able to link the stylesheet and the form. The best way to do it is to have the form name/ID and have JavaScript to apply the stylesheet when form get focus. – AvkashChauhan Jun 23 '12 at 02:49
  • 1
    I don't know of any method to do it *without* JavaScript, but it's not too hard with JavaScript/jQuery: http://jsfiddle.net/3mCwg/. Is there a particular reason why you want to use CSS only? – Zhihao Jun 23 '12 at 02:52
  • Damn I was hoping that wouldn't be the case :( It's weird though because form:hover and form:active work (I get why hover would, but active?) – Bilal Akil Jun 23 '12 at 02:52
  • I dunno I just wanna try to make the most of CSS – Bilal Akil Jun 23 '12 at 02:54

6 Answers6

10

This is an older question, but as of now, some of the more popular browsers are supporting a css pseudo selector called :focus-within, which can style a form (without javascript) that has a focused input.

Here is the current support for the selector: http://caniuse.com/#search=focus-within

If you're using a supported browser, here is an example: https://codepen.io/jumprope-design/pen/LjxORX

Joe Allen
  • 123
  • 1
  • 6
3

There is no parent selector in CSS so javascript is required. CSS 4 is planned to get this feature, however.

matt3141
  • 4,303
  • 1
  • 19
  • 24
  • Didn't know that it's planned for CSS4. Looked around a bit and found [this](http://davidwalsh.name/css4-preview). Thanks for the tidbit! – Zhihao Jun 23 '12 at 02:56
3

This code works as an exercise but probably not a solution you should use. The version relying on legend actually seems acceptable.

There is no form:focus selector so I thought instead the individual input:focus could create the desired effect using pseudo-elements. However, pseudo-elements can only be used on elements with content, like if I were to replace input[type=submit] with button

form {
    position:relative;
}
/*style the pseudo-element before a button that is a general sibling
  of any element that currently has focus within a form*/
form *:focus~button:before{
    content:"";display:block;background:red;
    /*take up the entire space of the form*/
    position:absolute;top:0;right:0;bottom:0;left:0;
    /*but render behind its children*/
    z-index:-1;
}

Fiddled, but it instantly looked pretty crazy, so I've refactored the solution to rely onto a legend element. Enjoy :)

Community
  • 1
  • 1
Oleg
  • 24,465
  • 8
  • 61
  • 91
  • That's very fascinating, it's got a few issues but in essence answers the question with pure CSS (and a legend)! Very innovative, thanks a lot for this – Bilal Akil Jun 23 '12 at 15:35
  • 1
    With the general sibling selector work in browsers that don't offer all CSS3 capabilities? – Will D. White Aug 17 '12 at 17:58
  • @WillD.White: the sibling selector has a better support matrix (IE7+) than the pseudo-elements do – Oleg Aug 18 '12 at 00:35
  • 1
    My kneejerk was that child selectors tend to behave funky in IE and honestly I had never seen the sibling selector before. Had to google it. You're right though the pseudo-elements might be more of a problem than the ~. Thanks for the tip! – Will D. White Aug 18 '12 at 23:00
2

I've been looking for the same styling technique for a while. From a UI/UX standpoint - simplifying search forms to a single element makes a lot of sense in certain situations.

Consider the example below:

Search Form Example

When you approach it from a development standpoint the knee-jerk is to decide to style the form itself instead of the input elements. A transparent input[type=text] to the left, and a transparent .PNG submit button to the right and you've got a sharp looking search field.

As you've discovered though, you give up the CSS style capabilities associated with :focus because the input field isn't the one controlling the background / color etc - the form element is instead.

The form:focus selector would be a perfect way to handle that. Unfortunately, we've got to wait to CSS4 for that (thanks to matt3141 for the tid-bit).

In the meantime, you have a few options available


Option 1 - Forgo the Clickable Submit Button

I usually try and avoid this if possible, but you have the option to forgo the submit button altogether. Style the text field as you intended, and use a background image with the position limited to the left or of the field right. When the user types in their query, and presses enter, you can still fire a GET action. The example image above uses this technique.

Example: http://designdisease.com/

Pros: Easiest to set up.

Drawbacks: Users who still click search buttons might be confused.


Option 2 - Use an Alternate Element to Style the Background

Your next option is to take advantage of the sibling selector and content tags as o.v. has so generously explained in his/her previous answer. This in effects adds a new element and styles it to act as a new background for a specified area when the :focus effect is applied to an input field.

Example: http://jsfiddle.net/ovfiddle/PEK7h/2/

Pros: Extendable to larger forms with multiple fields more easily.

Drawbacks: The intensive selectors may not degrade as gracefully as we'd like.


Option 3 - Use Absolute Positioning to Stack the Elements

In situations where the text field will encompass the full width of the form, you can use a the position:absolute; attribute to simply load the submit button over top of the input element, and then a few css tweaks on the button to remove the background / border - giving the same effect as our example image above, but with added benefit of making it clickable.

Step One: Give the form a position - relative/absolute/fixed.

Step Two: Give the text field a width of 100%.

Step Three: Give the button an absolute position, and right position of 0.

I've updated o.v.'s fiddle to incorporate the new technique:

Example: http://jsfiddle.net/PEK7h/17/

Pro's: Degrades gracefully, gives us what we want in most single input field cases.

Drawbacks: Not as easily extendable to large forms like o.v.'s fix is.

--

As you can see, each option has its own drawbacks - but if you know about them ahead of you can usually lessen their impact. Hope this helps!

Will D. White
  • 1,024
  • 5
  • 12
  • 22
  • Thanks for the research. I really liked your Option 3 solution. It's still usable with more flexible sizes, I'd just have to put a containing div for the buttons position to absolute to (instead of the entire form) - which isn't ideal, but still pretty good while waiting for CSS4. – Bilal Akil Aug 18 '12 at 01:17
  • I'm glad it helped! I've been using it just for search fields, but you're exactly right in that you could extend it to larger forms with a little creative positioning. – Will D. White Aug 18 '12 at 22:55
0

If you have multiple forms in the page, without JS, the renderer will not be able to link the stylesheet and the form. The best way to do it is to have the form name/ID and have JavaScript to apply the stylesheet when form get focus.

AvkashChauhan
  • 20,495
  • 3
  • 34
  • 65
  • Sorry mate that's changing the background colour of the input fields themselves, I want it to change it for the parent form. – Bilal Akil Jun 23 '12 at 02:59
  • The jsFiddle someone posted in the question comments nails it pretty well if you'd like to check it out, but of course thanks for the thought :) – Bilal Akil Jun 23 '12 at 03:01
0

You cannot "focus" on a form. You can only "focus" on the form elements inside the form (that are editable and enabled) using CSS. Hope this helps.

Geocrafter
  • 145
  • 10