Is it feasible to select the last child of an input without having to specify the submit

Here is a portion of the HTML code I am currently testing:

<form action="#" method="post>
    <input name="username" type="text" placeholder="Username" /><br />
    <input name="password" type="password" placeholder="Password" /><br />
    <input name="emailAddress" type="email" placeholder="Email Address" /><br />
    <input type="submit" value="Submit" />
</form>

And here is an excerpt from the CSS code I am using for this test:

form {
    width:300px;
    overflow:auto;
    margin:20px 0 0;
    padding:0;
}
/*-Inputs-*/
form input[type=text]:focus, form input[type=password]:focus {outline:0;}

form input {
    width:calc(100% - 22px);
    margin:0;
    padding:10px;
    background:#FFF;
    border:none;
    border-left:solid 1px #CCC;
    border-right:solid 1px #CCC;
}
form input:first-child {
    border-top-left-radius:5px;
    border-top-right-radius:5px;
    border-top:solid 1px #CCC;
}
form input:last-child {
    border-bottom-left-radius:5px;
    border-bottom-right-radius:5px;
    border-top:none;
    border-bottom:solid 1px #CCC;
}

Can the form input:last-child selector be modified to exclude the [type=submit] element? This way, the CSS would recognize [type=email] as the last child instead?

Note: While the layout may vary, there will always be a submit button in my forms that I want the :last-child selector to ignore.

Answer №1

One way to solve this issue is by using the CSS selector not, although it may not be fully supported in some browsers.

Check out this link for more information on CSS :not selector

form input:not([type="submit"]):last-child

Alternatively, you can apply classes to your inputs which is a more cross-browser friendly solution.

Here is an example of how you can implement this:

<form action="#" method="post">
    <input class="first-input" name="username" type="text" placeholder="Username" /><br />
    <input name="password" type="password" placeholder="Password" /><br />
    <input class="last-input" name="emailAddress" type="email" placeholder="Email Address" /><br />
    <input type="submit" value="Submit" />
</form>

And here is how you can style these inputs using CSS:

form .first-input{ }

form .last-input{ }

Answer №2

In my opinion, using `form input:nth-last-child(2) {...} is the best approach for this situation. It has excellent cross-browser support: http://caniuse.com/css-sel3. Additionally, there are resources available like polyfills to assist with older browsers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with radio button not updating when selected

I am encountering a strange issue with a form I am currently developing. The code snippet below showcases the problem: <?php $show = ' checked '; $hide = ' '; /* Some logic to swap the checked here */ ?> <label cl ...

The input box within the form is insufficiently sized

My task is to generate textareas with custom parameters like "cols" and "rows" and display them in forms. However, these parameters seem not to be functioning as expected. Whether I set cols=1000 or cols=1, there is no difference in the output. After revie ...

Collapsible Span with Maximum Width in Unique Twitter Bootstrap

UPDATED If I assign a max-width value to the .container element, it disrupts the alignment of my spans. <div class="container" style="max-width:940px"> <div class="row"> <div class="span4" style="background-color:#F00">1</div& ...

Adjusting canvas context position after resizing

I've been experimenting with using a canvas as a texture in three.js. Since three.js requires textures to have dimensions that are powers of two, I initially set the canvas width and height to [512, 512]. However, I want the final canvas to have non-p ...

Determining the exact pixel height of a table row

Is there a way to use JavaScript to accurately determine the height of a table row in pixels? Specifically, I am looking for the measurement from the top of one green border to the top of the next green border. I have tried using jQuery's .height(), ...

Another method to verify an input using HTML5 regex and JavaScript

When working with vanilla JavaScript to check an HTML input against a regex pattern, things can get tricky. I find myself going back to the parent element to validate the input, and while it works, it's not as elegant as I would like it to be. Here i ...

Load the iframe element only when the specified class becomes visible within the container div, without relying on jQuery

I am facing a unique challenge where the performance of my page is significantly impacted by loading multiple iframes. These iframes are contained within popup modals, and I would like to delay their loading until a user clicks on the modal. When the mod ...

How can I show/hide a div based on checkbox click on my website? It works in jsFiddle, but not on my actual site. Any suggestions?

Is there a way to show or hide a div based on a checkbox click? I've got it working in jsFiddle, but for some reason, it's not functioning properly on my website. Any thoughts on how to fix this? My goal is to offer multiple payment methods (cre ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

Issue with Caching during Javascript Minification

I Have ASP.Net MVC 3 App. Utilizing YUICompressor.Net for compressing Javascript and CSS files post build with MSBuild. The minimized javascript file is named JSMin.js and the CSS file is CssMin.css. In my master page, I reference these files as shown bel ...

Populate the row with an image while maintaining its size

I have encountered an issue with containing images within a Bootstrap row. When I insert an image, it does not stay contained within the row and instead overflows causing scrollbars to appear. My goal is to have the image fit seamlessly into the container ...

Guide to building a list of clickable links to all the forms created using django-forms-builder

Today, I discovered a new tool called django-forms-builder and decided to give it a try in one of my projects. I successfully installed it and managed to create forms within the admin area, which I found to be quite convenient. However, I am now facing a ...

What might be causing Flex not to function properly on my personalized landing page?

I attempted to create a basic landing page featuring an image background with a logo, button, and a row of 4 images displayed side by side using Flex. However, for some reason, the Flex is not functioning properly for the row of images. Here is the code s ...

establishing irregular edges for the div container

Looking to create a class that can transform an element into a specific shape using CSS. Not entirely sure if this is achievable with pure CSS alone, but applying a class to the image element will morph it into the desired shape as shown below. <style ...

Sending information through a form via a POST request after clicking on a hyperlink

I am attempting to submit a form using POST by clicking on a link and passing some hidden values. This is the code I'm currently using: $( document ).ready(function() { $('a#campoA').click(function() { $.post('testForm2.php', { ...

What is the best method for adding a line break in the body of an email when we include the recipient, subject, and message?

Do you have a requirement where information needs to be sent to an end user via Gmail (preferred) and they need to click on a button in the email body to respond? The button response is set up with predefined to, subject, and body fields, but there are dif ...

The unusual behavior of a page refresh in jQuery when replacing content

My website features a flash player. This code references the flash player stored in player_codes.php: <script language="javascript" type="text/javascript" src="songs_related/flashmp3player/swfobject.js" ></script> <!-- Div that contains pl ...

Completing the remainder of the page with CSS styling

Currently, I have three Divs positioned absolutely on the page - leftDiv, middleDiv, and rightDiv. The middleDiv has a width of 900px, which is fine. However, I need the leftDiv and rightDiv to fill the remaining space on the left and right sides, regardle ...

Error message: Failure to retrieve / on the Express application

Embarking on a new project using express, I've set up the foundation with an index.js file in my project folder: var express = require('express'); //App setup var app = express(); var server = app.listen(4000, function(){ console.log(&a ...

Is it feasible to utilize VAST for delivering HLS streams? Can m3u8 files be incorporated into VAST tags for delivery?

We are integrating a video player that supports VAST pre-roll, mid-roll, and post-roll video ads. I'm wondering if it's feasible to include m3u8 files in VAST tags? I've reviewed the vast specification and examples, but haven't come ac ...