Is there a way to control the width of wtf.form_field input fields without affecting the width of the label?

I am currently creating multiple Flask fields (SelectFields, InputFields) using the following jinja2 template :

<div>{{ wtf.form_field(form.blockingProbability) }}</div>

      

The result is the following format:

Current rendering

I would like to control the width of the dropdown (a narrower width would look more natural, metrics), but (unsurprisingly) when I try to do this by controlling the width div

, the entire box including the label is constrained and the label text wraps around.

Is there a way to control the width of the dropdown box (and other input fields) while keeping the label width unchanged?

+3


source to share


3 answers


This worked for me
jinja2 template:

<div style="width: 50%">
    {{ form1.language.label }}
</div>
<div style="width: 25%">
    {{ form1.language }}
</div>

      

enter image description here



This is the form1 class:

class myForm(Form):
   language = SelectField(u'Programming Language', choices=[('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')])

      

+4


source


This should work too, and also maintain visual consistency with other margin widths:

<div>{{ wtf.form_field(form.blockingProbability, horizontal_columns=('lg', 2, 4)) }}</div>

      



The last value - 4

- in horizontal_columns

sets the width of the input field.

+2


source


When the Jinja2 code is rendered to HTML, it will populate a set of attributes on each form field. For example, if you are viewing the page after rendering, it should look something like this:

<div>
    <label for='blockingProbability'>
        Blocking Probability
    </label>
    <input id='blockingProbability' name='blockingProbability' ... >
</div>

      

This way you can manage the fields and labels as a group or individually by adding some CSS to the header of the Jinja2 template:

<head>
    <style type="text/css">
        input {
            width: 20px;
        }
    </style>
</head>

      

0


source







All Articles