Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to layout labels and values in Bootstrap 4?

Tags:

bootstrap-4

I'm looking for a way to layout labels and data throughout my application. I want a solution that will work for my example below, but also other data.

enter image description here

I want to lay out labels and values so that they are left aligned in a grid format. I don't want to use the Bootstrap 4 col-md-N classes because I don't want to hard code column sizes. Instead I want the label and data widths to define the widths of the columns. I don't want to always have to change the column sizes when adding new labels that might have longer text. I also want to be able to left or right align a column easily.

Also Bootstraps <dl> styling does not seem correct for this.

Right now this is being done with a table.

Also, to be clear, this is not a form, so the Bootstrap form elements don't seem to be the correct option either. (There is nothing to post).

Here's my code so far:

    <table>
        <tbody>
            <tr>
                <td class="text-right"><label class="mr-2">Short:</label></td>
                <td>Short data</td>
            </tr>
            <tr>
                <td class="text-right"><label class="mr-2">A very long label:</label></td>
                <td>1/2/2018</td>
            </tr>
            <tr>
                <td class="text-right"><label class="mr-2">An even longer label:</label></td>
                <td>and longer data...</td>
            </tr>
        </tbody>
    </table>
    <hr />
    <table>
        <tbody>
            <tr>
                <td><label>Price:</label></td>
                <td class="text-right">$100.00</td>
            </tr>
            <tr>
                <td><label>Rebate:</label></td>
                <td class="text-right">$10.00</td>
            </tr>
            <tr>
                <td><label>Total:</label></td>
                <td class="text-right">$90.00</td>
            </tr>
        </tbody>
    </table>

PS: In the past I have tried some other options, but I want to use something easy from Bootstrap 4.

like image 808
Jess Avatar asked Jan 23 '18 18:01

Jess


People also ask

Is label supported in Bootstrap 4?

No 'label' can be found in the Components section. No 'tag' can be found in the Components section. You'll find Badges in Components again. So, at the moment 'Label' and 'Tag' components have still been replaced with 'badges'.

How do I resize a label in Bootstrap 4?

just add the style="font-size:XXpx;", ej. Richard Yeber V.

How do I center align a label in Bootstrap?

Add text-align: center in your CSS or text-center class to your parent element (probably to a row or container ).

How do I create a horizontal form in Bootstrap 4?

To make a form horizontal, add class=”form-horizontal” in the <form> element. If you're using the <label> element then you must use class=”control-label”. Also, remember that you can use Bootstrap's predefined grid classes to align labels and groups of form controls in a horizontal layout.

What is a Bootstrap layout?

The layout of the bootstrap consists of containers, an effective grid system, responsive utility classes, and a media object. Types of bootstrap layouts depend upon which type of container is used. While creating a responsive layout, you have an option to choose from one of two containers. One can create a responsive website with both containers.

How to use labels in Bootstrap 4?

The label has “for” attribute that is used to bind an input type like textbox by its ID. As you click on the label, the input that is bound to the textbox gets focused. The example of inline labels By using the Bootstrap 4 grid classes, you may create labels at the same level (inline) with the form fields rather than above the field.

What is Bootstrap 4 forms controls?

Bootstrap 4 forms controls are easily style and change the appearance with various layout options. You can create a wide variety of forms with the pre-defined classes of Bootstrap. The forms can be easily customized to display on different screen sizes.

What are the different types of bootstrap forms?

There are two types of form layout in bootstrap4 which are vertical layout (Stacked form) and inline layout. The label and bootstrap form attribute set in vertical order known as vertical bootstrap form layout or stacked form. The form label and their bootstrap attribute set in horizontal order known as horizontal form layout.


2 Answers

Using a dl is actually appropriate and will work fine if you are ok with truncated labels.

Here is the doc: https://getbootstrap.com/docs/4.0/content/typography/#description-list-alignment

And here is an example: enter image description here

<dl class="row">
  <dt class="col-sm-3">Description lists</dt>
  <dd class="col-sm-9">A description list is perfect for defining terms.</dd>

  <dt class="col-sm-3">Euismod</dt>
  <dd class="col-sm-9">
    <p>Vestibulum id ligula porta felis euismod semper eget lacinia odio sem nec elit.</p>
    <p>Donec id elit non mi porta gravida at eget metus.</p>
  </dd>
</dl>
like image 68
Jess Avatar answered Oct 20 '22 23:10

Jess


EDIT: In that case, tables are well suitable, yes.

Use the following structure:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<table>
    <tr>
        <th class="small text-muted pr-2" scope="row">Label 1</th>
        <td>Value 1</td>
    </tr>
    <tr>
        <th class="small text-muted pr-2" scope="row">Label 2</th>
        <td>Value 2</td>
    </tr>
    <tr>
        <th class="small text-muted pr-2" scope="row">Label 3</th>
        <td>Value 3</td>
    </tr>
</table>

You could also apply a bit of custom css but I think those native Bootstrap 4 classes do pretty much exactly what you want.

The pr-2 class applies "padding-right 2 units" to the labels.

like image 29
WebDevBooster Avatar answered Oct 21 '22 00:10

WebDevBooster