Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS :not(:last-child) is not working

ig21

Coder
Hello!
I've got a problem with this simple code. Margin-bottom does not appear
Thanks!

CSS:
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.container div{
    position: relative;
}
.container{
    width: 300px;
    text-align: center;
    margin:20px auto;
    padding: 10px;
}
form:not(:last-child){
    margin-bottom: 20px;
}

HTML:
<body>
    <div class="container">
        <h2>Please sign up</h2>
        <form name='form'>
            <div class='fname'><input type="text"  name='fname' placeholder='First name'></div>
            <div class='sname'><input type="text" name='sname' placeholder='Second name'> </div>
            <div class='email'  ><input type="email" name='email' placeholder='Email adress'> </div>
            <div class='phone' ><input type="number" name='phone' placeholder='Phone number'> </div>
            <div class='pass'  ><input type="password" name='pass' placeholder='Password'> </div>
            <button type="button"> Sign Up</button>
        </form>
    </div>

    <script src="script.js"></script>
</body>
 
Solution
I'm not sure it's possible to do it this way, but you can give each element that is not the last a class name like this:
HTML:
<body>
    <div class="container">
        <h2>Please sign up</h2>
        <form name='form'>
            <div class='fname notLast'><input type="text"  name='fname' placeholder='First name'></div> <!-- I used notLast as the class name -->
            <div class='sname notLast'><input type="text" name='sname' placeholder='Second name'></div>
            <div class='email notLast'><input type="email" name='email' placeholder='Email adress'></div>
            <div class='phone notLast'><input type="number" name='phone' placeholder='Phone number'></div>
            <div class='pass'><input class='notLast'...
I'm not sure it's possible to do it this way, but you can give each element that is not the last a class name like this:
HTML:
<body>
    <div class="container">
        <h2>Please sign up</h2>
        <form name='form'>
            <div class='fname notLast'><input type="text"  name='fname' placeholder='First name'></div> <!-- I used notLast as the class name -->
            <div class='sname notLast'><input type="text" name='sname' placeholder='Second name'></div>
            <div class='email notLast'><input type="email" name='email' placeholder='Email adress'></div>
            <div class='phone notLast'><input type="number" name='phone' placeholder='Phone number'></div>
            <div class='pass'><input class='notLast' type="password" name='pass' placeholder='Password'></div> <!-- You can also put the class name in the input element like I did here -->
            <button type="button"> Sign Up</button>
        </form>
    </div>

    <script src="script.js"></script>
</body>

And in the CSS, change the form:not(:last-child) to your class name:
CSS:
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.container div{
    position: relative;
}
.container{
    width: 300px;
    text-align: center;
    margin:20px auto;
    padding: 10px;
}
.notLast{
    margin-bottom: 20px;
}
 
Solution
I'm not sure it's possible to do it this way, but you can give each element that is not the last a class name like this:
HTML:
<body>
    <div class="container">
        <h2>Please sign up</h2>
        <form name='form'>
            <div class='fname notLast'><input type="text"  name='fname' placeholder='First name'></div> <!-- I used notLast as the class name -->
            <div class='sname notLast'><input type="text" name='sname' placeholder='Second name'></div>
            <div class='email notLast'><input type="email" name='email' placeholder='Email adress'></div>
            <div class='phone notLast'><input type="number" name='phone' placeholder='Phone number'></div>
            <div class='pass'><input class='notLast' type="password" name='pass' placeholder='Password'></div> <!-- You can also put the class name in the input element like I did here -->
            <button type="button"> Sign Up</button>
        </form>
    </div>

    <script src="script.js"></script>
</body>

And in the CSS, change the form:not(:last-child) to your class name:
CSS:
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.container div{
    position: relative;
}
.container{
    width: 300px;
    text-align: center;
    margin:20px auto;
    padding: 10px;
}
.notLast{
    margin-bottom: 20px;
}
Thank You!
 
Last edited by a moderator:

Buy us a coffee!

Back
Top Bottom