&:focus on input wont change element

Viewed 53

I am trying make a button appear after typing in my input tag. Currently I set my button to display: none and when I focus on the input field, it should change my display: inline-block which should make it reappear. What am I doing wrong?

html

            <h1 className='title-caption'>Get started today!</h1>
                <div className='email-newsletter-container'>
                    <p className='sign-up-caption'>
                        Sign up for our newletter for more information
                    </p>
                    <input
                        className='email-input'
                        placeholder='Enter email address'
                    ></input>
                    <br />
                    <button className='submit-button' id='submit-button'>
                        Submit
                    </button>
                </div>

css

    .email-input {
        margin-top: 20px;
        height: 40px;
        width: 400px;
        text-align: center;
        border-radius: 5px;
        border: none;
        outline: none;
        position: relative;

        &:focus + .submit-button {
            display: inline-block;
        }
    }

    .email-input:focus + .submit-button {
        display: inline-block;
    }

    .submit-button {
        position: relative;
        border-radius: 20px;
        width: 90px;
        margin-top: 15px;
        padding: 10px 10px 10px 10px;
        font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande',
            'Lucida Sans', Arial, sans-serif;
        background-color: transparent;
        border: 2px solid white;
        color: white;
        display: none;
    }
3 Answers

button {
  display: none;
}

input:focus ~ button {
  display: inline-block;
}
<input type="text">
<div></div>
<button>Button</button>

Use the ~ selector instead of the + selector. Here's a little demonstration.

The + selector will only select the sibling immidiately placed after, while the ~ selector will select any sibling placed after, using the class selector we can then filter those.

What if you try something like this Demo

CSS

.email-input {
    margin-top: 20px;
    height: 40px;
    width: 400px;
    text-align: center;
    border-radius: 5px;
    border: none;
    outline: none;
    position: relative;

    &:focus + .submit-button {
        display: inline-block;
    }
}

.email-input:focus + .submit-button {
    display: inline-block;
}

.submit-button {
    position: relative;
    border-radius: 20px;
    width: 90px;
    margin-top: 15px;
    padding: 10px 10px 10px 10px;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande',
        'Lucida Sans', Arial, sans-serif;
    background-color: transparent;
    border: 2px solid white;
    color: white;
    display: none;
}

HTML

 <script>
 function myFunction() {
 var x = document.getElementById("myDIV");
 if (x.style.display === "block") {
 x.style.display = "none";
 } else {
 x.style.display = "block";
 }
 }
</script>
<h1 className='title-caption'>Get started today!</h1>
            <div className='email-newsletter-container'>
                <p className='sign-up-caption'>
                    Sign up for our newletter for more information
                </p>
                <input
                    className='email-input'
                    placeholder='Enter email address'
                    onfocus="myFunction()"
                    
                ></input>
                <br />
                <button style="display:none" className='submit' id="myDIV">
                    Submit
                </button>
            </div>
            
  • <input /> tag is self closed. It is incorrect in your syntax.

  • &:focus + .submit-button { display: inline-block; } - it means that after input follows .submit-buton but in your html there is <br/> tag after input, so this will not work

Related