Description

This example shows you how align the input elements and the labels in the form. To change the horizontal alignment of the input element, set the align property to 'left', 'right' or 'center'. To change the horizontal alignment of the respective label, change the labelAlign property. The default alignment for text input and other HTML elements is left. The default labels alignment is also left.

jQuery Form: Form Input Alignment

jQuery Form Input Elements Alignment.

A live JavaScript/jQuery example of jqxForm, the jQWidgets Form component, with its complete source code below. It is one of 7 jqxForm demos for jQuery.

jqxForm API used in this demo

Source code

To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxForm shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxbuttons.js, jqxinput.js, jqxlistbox.js, jqxscrollbar.js, jqxpanel.js, jqxdropdownlist.js, jqxform.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).

script.js

$(document).ready(function () {

    var template = [
        {
            bind: 'item1',
            type: 'text',
            label: 'Input left alignment',
            align: 'left',
            labelWidth: '160px',
            width: '160px'
        },
        {
            bind: 'item2',
            type: 'text',
            label: 'Input center alignment',
            align: 'center',
            labelWidth: '160px',
            width: '160px'
        },
        {
            bind: 'item3',
            type: 'text',
            label: 'Input right alignment',
            align: 'right',
            labelWidth: '160px',
            width: '160px'
        },
        {
            bind: 'item4',
            type: 'text',
            label: 'Label left alignment',
            labelAlign: 'left',
            labelWidth: '160px',
            width: '160px'
        },
        {
            bind: 'item5',
            type: 'text',
            label: 'Label center alignment',
            labelAlign: 'center',
            labelWidth: '160px',
            width: '160px'
        },
        {
            bind: 'item6',
            type: 'text',
            label: 'Label right alignment',
            labelAlign: 'right',
            labelWidth: '160px',
            width: '160px'
        },

    ];


    var sampleValue = {
        item1: 'left aligned',
        item2: 'center aligned',
        item3: 'right aligned',
        item4: 'left aligned',
        item5: 'left aligned',
        item6: 'left aligned'
    };

    $('#sampleForm').jqxForm({
        template: template,
        value: sampleValue,
        padding: { left: 10, top: 10, right: 10, bottom: 10 }
    });
});

body.html

<div id="sampleForm" style="width: 500px; height: auto;"></div>

Learn more

More jQuery Form demos