"Place label and input fields side by side for a streamlined mobile display experience

My goal is to have the label and input field appear on the same line when there is enough width, but on separate lines when the width is too small.

I have achieved this functionality, you can see it in action on this jsFiddle.

Here is the HTML Code:

<div style="border-bottom-width:0;" data-role="fieldcontain">
    <label for="ti_gebdat">Geburtsdatum</label>
    <input name="ti_gebdat" id="ti_gebdat" style="text-align: right" type="date"  data-theme="d">
</div>
<div style="border-bottom-width:0;" data-role="fieldcontain">   
    <label for="oeffDienst">Öffentlicher Dienst</label>
    <select name ="oeffDienst" id="oeffDienst" data-role="slider">
        <option value="0">Nein</option>
        <option value="1">Ja</option>
    </select>
</div>

However, in the jsFiddle demonstration, the controls move to the next line too early. I am looking for a solution to keep them on the same line until they would actually overlap.

I am utilizing jQuery-Mobile and testing on android devices where the controls always display on separate lines.

Answer №1

Check out the updated fiddle for an alternative solution without using media queries: http://jsfiddle.net/ezanker/qCmxZ/3/

In this new approach, I have opted to place the label and control in separate divs set to display inline instead of block, avoiding the "fieldcontain" role from jQM. To prevent overlapping, I am clearing floats between each line.

    <div class="dispInlineLabel">
        <label for="ti_gebdat">Date of Birth</label>
    </div>
    <div class="dispInline">
        <input name="ti_gebdat" id="ti_gebdat" style="text-align: right" type="date"  data-theme="d" />
    </div>
    <!-- Clear floats for each new line -->
    <div class="clearFloats"></div>

    <div class="dispInlineLabel">  
        <label for="oeffDienst">Public Service</label>
    </div>
    <div class="dispInline">
        <select name ="oeffDienst" id="oeffDienst" data-role="slider">
            <option value="0">No</option>
            <option value="1">Yes</option>
        </select>
    </div>

The CSS sets a min-width for labels and controls for proper sizing. As the page resizes, controls will wrap only when the total width exceeds the defined min-width. Here is the CSS:

.dispInline, .dispInlineLabel{
    display: inline-block;
    border-bottom-width:0;
}
.dispInlineLabel{
    min-width: 150px;
}
.dispInline{
    min-width: 200px;
}
.clearFloats{
    clear:both;
}

To avoid overlap, use min-width in CSS to manage the layout accordingly.

Answer №2

It was pointed out by Human that media queries play a crucial role in determining the order of elements on a webpage. Within your jQuery mobile CSS file, 450px serves as the threshold where elements stop being placed next to each other.

If desired, one option is to override the default stylesheet with a customized rule that applies specific styles for widths exceeding 450px regardless of screen size. This can be achieved by directly altering the selectors specified (http://jsfiddle.net/qCmxZ/1/):

@media all {        
    .ui-field-contain input.ui-input-text,
    .ui-field-contain textarea.ui-input-text,
    .ui-field-contain .ui-input-search {
        width: 78%;
        display: inline-block;
    }

    .ui-field-contain label.ui-slider,
    .ui-field-contain label.ui-input-text {
        vertical-align: top;
        display: inline-block;
        width: 20%;
        margin: 0 2% 0 0;
    }
}

Nevertheless, it's important to exercise caution when making such modifications, as inconsistent changes could disrupt the intended behavior of jQuery mobile's input fields on smaller screens.

For a more comprehensive solution, exploring the principles of responsive Web Design would be advisable.

Answer №3

Encountering a similar issue, I found a solution inspired by mritz_p's response. To workaround this "problem," I included a customized CSS file on the page. The CSS code was extracted from the jqm CSS file and modified slightly to adjust the min-width label width and .ui-controlgroup-controls.

If you need it as a point of reference, here is the CSS code:

@media (min-width: 28em) {
    .ui-field-contain,
    .ui-mobile fieldset.ui-field-contain {
        padding: 0;
        margin: 1em 0;
        border-bottom-width: 0;
    }
    .ui-field-contain:before,
    .ui-field-contain:after {
        content: "";
        display: table;
    }
    .ui-field-contain:after {
        clear: both;
    }
    .ui-field-contain > label,
    .ui-field-contain .ui-controlgroup-label,
    .ui-field-contain > .ui-rangeslider > label {
        float: left;
        width: 20%;
        margin: .5em 2% 0 0;
    }
    .ui-popup .ui-field-contain > label,
    .ui-popup .ui-field-contain .ui-controlgroup-label,
    .ui-popup .ui-field-contain > .ui-rangeslider > label {
        float: none;
        width: auto;
        margin: 0 0 .4em;
    }
    .ui-field-contain > label ~ [class*="ui-"],
    .ui-field-contain .ui-controlgroup-controls {
        float: left;
        width: 78%;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }
    /* ui-hide-label deprecated in 1.4. TODO: Remove in 1.5 */
    .ui-hide-label > label ~ [class*="ui-"],
    .ui-hide-label .ui-controlgroup-controls,
    .ui-popup .ui-field-contain > label ~ [class*="ui-"],
    .ui-popup .ui-field-contain .ui-controlgroup-controls {
        float: none;
        width: 100%;
    }
    .ui-field-contain > label ~ .ui-btn-inline {
        width: auto;
        margin-right: .625em;
    }
    .ui-field-contain > label ~ .ui-btn-inline.ui-btn-icon-notext {
        width: 1.75em;
    }
}

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 displaying Django Bootstrapv5 component in carousel item

I am struggling to display at least 5 cards in each carousel-item, but currently it only shows one card per item. Can you help me figure out what needs to be done? {% for knowledge in knowledges %} <div class="carous ...

I'm curious if there is a method to validate HTML elements within AngularJS. You can check out my app on Plunker [here](https://jsfiddle.net/4jttdczt/)

I'm a newcomer to angularjs and I am attempting to test HTML elements, but my tests are failing. Specifically, I would like to test the value or text contained within an HTML element. Can anyone provide guidance on how I can achieve this? Below is my ...

jquery code to show/hide all elements

My webpage contains multiple content tabs with expand/collapse icons in each panel. To simplify the process of closing all expanded sections, I have included buttons for expanding and collapsing all panels at once. While this feature is functioning correc ...

Utilizing the jQuery .on event handler alongside ES2015 arrow functions

Currently, I am in the process of transitioning my project to utilize ES2015. However, an issue has arisen related to jQuery's .on event handler and the usage of the 'this' keyword. Let me provide an example of the code: $(".actionBtns") ...

The CSS of the Sendgrid template is being overlooked

I have utilized Sendgrid to create the template mentioned below: https://i.sstatic.net/TFQfl.png In order to send this template using my Node server, I have developed the module provided in the code snippet: /* * Created by root on 6/6/16. */ var path ...

Need help creating perfect round buttons with bootstrap 4?

After incorporating this fiddle into my code, the goal was to design a circular button. View Fiddle Here The code provided is for bootstrap 3. However, when using it with bootstrap 4, the button ends up being square instead of circular. See example here: ...

Previewing multiple images in multiple divs before uploading using jQuery

Currently, I am working on a Bootstrap Modal that allows users to upload multiple images. The functionality is such that when the user clicks the 'Add-Product-Image' button, it generates a new image upload input form. Once the user selects an ima ...

`Modifying CSS in Bootstrap 4 for a personalized website design`

I am currently building a Bootstrap website for my sister, but I am facing issues with changing the CSS. Most of the time, the changes do not seem to take effect. I created a new file called "style.css" to override the Bootstrap styles and placed it below ...

Tips on incorporating additional spacing between columns in Bootstrap 4

Is there a way to increase the distance between these two columns? <div class="row "> <div class="col-md-6 shadow"> ... </div> <div class="col-md-6 shadow "> ... </div> </div> ...

What is the best way to display the JQuery ThickBox plugin content on a webpage?

After setting up the ThickBox Plugin and making an AJAX request to retrieve content, I noticed that when I click the print link, it prints the entire page visible through the Thickbox. How can I modify this to only print the specific content being displaye ...

Exploring data extraction with scrapy and xpath

I've been attempting to extract data through scraping but keep receiving empty values or None. I tried utilizing the next sibling method and failed (most likely due to incorrect implementation). Any assistance or guidance would be highly appreciated. ...

When a selection element has a CSS box-shadow animation applied to it, it reverts to its default settings when expanded or clicked

A user notified me of an issue with a select element that has an animated box-shadow on it. I confirmed the problem on my work Windows computer, but unfortunately, I don't have access to a Windows setup at home for debugging. The select element in qu ...

Using jQuery to dynamically change button icons when clicked

My form has a unique structure: <form name="armdisarmform" action="/cameras" method="POST"> <input type='hidden' name='armdisarmvalue' value="ENABLED"/> <button class="armdisarm" name="armdisarmbutton" onClick=&a ...

Parsing all HTML elements using Nokogiri

I've been searching everywhere and all I could find was how to use CSS selection with Nokogiri. What I really need is to completely remove all HTML tags. For instance, this: <html> <head><title>My webpage</title></head& ...

What is the best way to ensure that the red overlay only covers the image without bleeding over to the right side as shown in the image?

As shown in the image, the red spill on the right side extends beyond the image Below is the HTML code I am using <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv=" ...

Failure of jQuery Code Upon Successful Execution

I'm having an issue with my jQuery ajax code. It's functioning perfectly, but I can't seem to get it to change the class of the button that triggers the event when it is successful. The code under success: does not seem to be working as inte ...

What is the best method to inform JavaScript about PHP server-side login and logout functionalities?

Looking for a way to notify users conveniently, without compromising security: I have JavaScript set up to poll for new information while a user is logged in. I need it to stop polling when the user logs out. What's the best approach to inform JavaSc ...

What is the process for converting HTML assets into a SCORM package?

**css styles fonts images media menu1_images menu2_images menu3_images menu4_images** index.html index_custom.js index_actions.js menu1.html menu1_custom.js menu1_actions.js menu2.html menu2_custom.js menu2_actions.js menu3.html menu3_custom.js menu3_actio ...

Developing a dynamic, live updating ordering platform using Firebase

Recently, I stumbled upon Firebase and it seems like the perfect fit for my project. My goal is to create a real-time HTML dashboard for ordering, but as a beginner in JavaScript, I could really use some assistance. I envision a real-time dashboard that s ...

Creating space between DIVs in a Bootstrap 4 modal: Easy steps to follow

What is the best way to add spacing between DIVs in a Bootstrap 4 modal? https://i.sstatic.net/1XXgN.jpg HTML <div class="form-group clearfix row"> <div class="col-lg-3 pd-30 card bg-info border-radius-5 box-shadow text-center justify-conte ...