Reputation: 1125
I am designing a list view using jquery mobile like below
I am trying to align the label and value as 30% an 70% respectively.
List view coding as below
<ul data-role="listview">
<li data-role="list-divider">Account Information</li>
<li>
<span class="field_label">Label:</span>
<span class="field_value">value</span>
</li>
<li>
<span class="field_label">Long Label:</span>
<span class="field_value">value</span>
</li>
</ul>
CSS styles used
.field_label{
width:30%;
}
.field_value{
width:70%;
}
Still the label and value are not aligned. What is the problem? How do I align?
Upvotes: 0
Views: 120
Reputation: 31732
You can achieve this using either solution:
Using jQuery Mobile Grids and modifying ui-block-a
and ui-block-b
width followed by !important
to force override.
<li class="ui-grid-a">
<span class="ui-block-a">Label:</span>
<span class="ui-block-b">Value</span>
</li>
CSS
li > .ui-block-a {
width: 15% !important;
}
li > .ui-block-b {
width: 80% !important;
}
Loop through labels and check widest one, then modify width of all label according to the widest one.
var width = 0;
var length = $(".field_label").length - 1;
var labels = $(".field_label");
var count = 0;
$.each(labels, function () {
var init_width = parseInt($(this).outerWidth());
if (init_width > width) {
width = init_width;
}
if (count >= length) {
$(".field_label").each(function () {
$(this).width(width + "px");
});
}
count++
});
CSS
.field_label {
display: inline-block;
}
Upvotes: 0
Reputation: 71
is an inline element and will not conform to the width you provided unless you specify display: inline-block
Upvotes: 1