Adjusting label widths in Fieldcontain with JQuery Mobile

I've done a lot of research online, and it seems like a common issue. In the past, I have tackled this problem using a <table>, but I am now looking to resolve it using CSS instead of relying on JQM's data-grid.

The simple problem I am facing involves having n fieldsets, each with a <label> and an <input>, and I want to align all the labels and inputs together in a neat format. For example:

First:         [ input ]
Second:        [ input ]
Third:         [ input ]
...
Loooong-one:   [ input ]

Here is an illustration of the issue: Fiddle

HTML Code

<div data-role="content">
    <div data-role="fieldcontain">
        <label for="first">First:</label>
        <input type="text" name="first" value="something" />
    </div>
    <div data-role="fieldcontain">
        <label for="second">Second:</label>
        <input type="text" name="second" value="something" />
    </div>
</div>

CSS Code

div[data-role='fieldcontain'] label{
    background: blue;
    width: 200px !important; /* this is not working :( */
}

My goal is to apply a consistent fixed width of 200px to all the <label> elements. The blue background was only added for testing purposes to verify the correctness of the CSS selector being used.

Answer №1

To ensure proper association, make sure to add an id to the inputs:

    <div data-role="fieldcontain">
        <label for="first">First:</label>
        <input type="text" id="first" name="first" value="something" />
    </div>
    <div data-role="fieldcontain">
        <label for="second">Second:</label>
        <input type="text" id="second" name="second" value="something" />
    </div>

By adding IDs to your inputs, the labels will automatically align to the same width. However, keep in mind that the fieldcontain element will cause the label to take up 28% of the page width and the input to take 72% of the page width when the page is larger than 448px. When the page size is smaller than 448px, the label will stack above the input.

If the 28%/78% split does not suit your needs, consider abandoning the fieldcontain element and creating your custom divs displayed inline with floats to achieve the desired layout.

Answer №2

After experimenting with jqm 1.4.5, I stumbled upon a simple method to achieve this by adding the following CSS after the jqm CSS is loaded. Customize the "#myForm" and "10em" values to fit your requirements. Interestingly, it seems to function properly even without using ID attributes.

Custom CSS

@media (min-width:28em) {
    #myForm .ui-field-contain {
        min-height:2.5em;
    }
    #myForm .ui-field-contain > label {
        position:absolute;
        width:10em;
     }
    #myForm .ui-field-contain > label ~ [class*=ui-] {
        position:absolute;
        right:0;
        left:10em;
        width:auto;
    }
}

Just a heads up, I'm probably not the best source for determining if this is a best practice, so proceed with caution.

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

jQuery $.ajax problem encountered

When working with AngularJS, we have the ability to catch errors using the $http() service as shown below: return $http(defaultConfig).then(sendResponseData)**.catch(errorCallBack)**; On the other hand, when attempting to do something similar in jQuery: ...

What is the best way to incorporate CSS into an Angular 4 project?

I'm struggling to figure out how to import CSS into my app component. All the information I find on Google seems to lead me in different directions... Within my component, I have defined: @Component({ styleUrls: ['../css/bootstrap.min.css&ap ...

Ways to have a list component smoothly descend when a dropdown menu is activated

I have a situation where I have a list with two buttons, and each button triggers the same dropdown content in a sidebar component. The issue is that when I click on one button to open the dropdown, the second button does not move down to make space for it ...

Leveraging jQuery for Sending Form Data to an ASP.NET ASMX Web Service

I am working on creating a straightforward login form where the submit button will post form data to a method in an asmx file. Unfortunately, I encounter an error whenever I try to post from inside a form tag. Posting outside of the form tags functions ...

Using the CSS property 'clear' creates a significant gap in the layout

Using a div like this to clear space after floats: .clear { clear:both; } However, the formatting is getting messed up with extra space. Any ideas on how to fix it? Appreciate your help! ...

Applying flexbox to create a visually appealing design

Is there a way to achieve this layout using flexbox without relying on grid due to limited support in IE11? I want to be able to easily add or remove small divs without adding more containers. To see the desired layout, check out this image: Desired Layou ...

Issue with Jquery-UI tabs: Default tab loading twice

Utilizing jqueryui-tabs to create a tabbed user interface, my markup in the MasterPage appears as follows: <div id="channel-tabs" class="ui-tabs"> <ul class="ui-tabs-nav"> <li><%=Html.ActionLink("Blogs", "Index", "Blog", n ...

Tips on dynamically changing the position of a div: Utilize absolute positioning within a for loop

Is there a way to extract the position x value of my div and store it in a variable? How do we then iterate over it within a for loop? <div id="object"></div> <style> #object{ position:absolute; width:10px; height:10px; ...

Once the input is disabled, what is the best way to delete the previously inserted text?

Here's the scenario: I have two options that display different questions each. If a user decides to switch their option at the last minute, any inputs within option 1 will hide and show option 2. However, all input texts need to be removed and disabl ...

Click on the jq toggle button to increase productivity and complete additional tasks

Is it possible to use a toggle function to create a drop-down menu while also rotating the mobile menu icon image when clicked? Can this be accomplished within the same function, or would I need to add a new event listener to the image? jQuery('#mobi ...

A guide on integrating variables into a MySQL table using PHP

I am currently working on a project where users should be able to select their schedule. Here is the code that I have written so far: <?php //if form has been submitted process it if(isset($_POST['submit'])){ $servername = "localhost"; ...

Exploring the world of Django: Using model formsets with the power

I am struggling to use Ajax for submitting my zipped formsets. The code functions flawlessly without Ajax, but as soon as I try to incorporate Ajax, I encounter a ValidationError: [u'ManagementForm data is missing or has been tampered with'] Thi ...

The two CSS classes are styled with different border-color values, but only one is being applied correctly

My goal is to modify the border color of a textarea using jQuery. Previously, I achieved this by using .css("border-color","rgb(250,0,0)"), and it was working perfectly. However, I have now been advised against using CSS directly in JavaScript and told to ...

How can I create spacing between squares in an HTML div element?

Currently, I am working on my project using Laravel 5. I retrieve some integer values from a database and use them to create square divs in HTML. Below is the current output of my work. https://i.stack.imgur.com/sy2ru.png You may notice that there is spa ...

retrieve the value of a specific key from an array

How can I extract key-value pairs from an array in JavaScript where the data is structured like this? jsonData = [ {"dimensions":[5.9,3.9,4.4,3.1,4.8],"icon":0,"curves": [false,false,false,false,false],"id":"p1","color":"0x000000"}, {"dimensio ...

Dynamically insert a new row into an HTML table using AJAX and refresh the table with .load method

I am facing an issue with my HTML table that loads data dynamically through a PHP snippet containing SQL queries. There is a Select option and a button on the table to add a new row, which triggers an AJAX procedure to send the data to PHP for insertion in ...

Trouble with card alignment in Bootstrap

I've been experimenting with creating a grid layout using cards and utilizing ng-repeat for generating the cards. Unfortunately, I'm running into an issue where there is no spacing between each card, as depicted in the image. https://i.stack.img ...

PHP - Utilizing a while loop to dynamically wrap content in a

I am facing an issue with wrapping the date and all events in one day within a div called "obal_date". The current implementation is not working as expected. The number of events can vary, but I need to group them by date. Can someone help me achieve this? ...

Calculate the sum of a column in a table that is dynamically populated

I'm using jQuery to load a table with data: function getSales(customerId, fromDate, toDate){ $.ajax({ type: 'POST', url: 'ajax/sale_pl.php', data:{customer_id:customerId, from_date:fromD ...

Tips for creating a form-flip, similar to a card-flip effect, using web technologies

Looking to create a card flip effect similar to the one shown here. Once the sign up process is finished, the card will smoothly flip over to reveal the other side with a transitioning effect. ...