Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4099534
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:26:47+00:00 2026-05-20T20:26:47+00:00

Is there a way to make an <li> have an active state when an

  • 0

Is there a way to make an <li> have an active state when an input field is focussed using CSS only? If not, any suggestions on the best way to go about achieving this?

See live demo: http://jsfiddle.net/W5LzP/

In the example, I want the background colour of the LI to stay yellow when the user clicks in the input field.

Example HTML:

<ol>
    <li>
    <label for="first_name">Your first name</label>
    <input name="first_name" placeholder="Type Your First Name" /></li>

    <li>
    <label for="lastt_name">Your last name</label>
    <input name="last_name" placeholder="Type Your Last Name" />
    </li>

    <li>
    <label for="email_address">Your first name</label>
    <input name="email_address" placeholder="Type Your Email Address" />
    </li>

    <li>
    <label for="country">Your country</label>
    <input name="country" placeholder="Select your country from the drop down list" />         
    </li>
</ol>

Example CSS:

ol li label{
    display:block;
}

ol li{
    margin-bottom:8px;
    background-color:#CCC;
}

ol li:hover{
    background-color:#090;
}

ol li:active{
    background-color:#FF9;
}

ol li input:focus{
    border:1px solid #99C;
    background-color:#99F;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:26:48+00:00Added an answer on May 20, 2026 at 8:26 pm

    CSS can only select descendants, it can’t select ancestors (perhaps it should for the reason you have outlined).

    You would have to use JavaScript, look at parentNode property.

    var inputs = document.getElementsByTagName('input');
    
    for (var i = 0, inputsLength = inputs.length; i < inputsLength; i++) {
    
        inputs[i].onfocus = function() {
            this.parentNode.className += 'active';
        }
    
        inputs[i].onblur = function() {
            this.parentNode.className = this.parentNode.className.replace(/\bactive\b/, '');
        }
    
    }
    

    jsFiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set cursorline in my vimrc. Is there any way to make vim
Is there a way to make a keyboard disappear without resignFirstResponder ? I have
I am wondering if there is some way to make an box have the
Is there a way to have \n make a line break in a TextBlock
Is there a way to make the SVG version of an image not being
When a push notification is received, is there any way to make the sound
Is there a way to make this prettier; if ($(#Input).text === A) { sOutput
Is there a particular way to make a <ul> a height? I have this
I know it doesn't necessarily make any sense, but is there a way to
Is there a way to make a DataGridView have no cell selected? I notice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.