how to get select2 dropwdown to stay at constant width - css

I am using select2 for a dropdown in a form and I like using a table layout (although maybe this is part of the problem?) to keep the widths of all of the form-group elements the same.
The problem is that when I type text into the select2 dropdown, the width changes (and in weird ways). Is there a way I can force the initial width that the browser gives the select2 dropdown to remain fixed?
Here is my html:
<table>
<tr>
<td>
<div class="row">
<div class="form-group">
<label>Title</label>
<input name="title" class="form-control"/>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="row">
<div class="form-group">
<label>a label</label>
<input class="form-control"/>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="row">
<div class="form-group">
<label>a label</label>
<select id="tag_list" class='form-control' multiple="multiple">
<option value='1'>abba</option>
<option value='2'>zabba</option>
<option value='3'>doo</option>
</select>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="row">
<div class="form-group">
<label for="category_type">Category Type:</label>
<select name="category_type" id="category_type_list" class="form-control">
<option value="" disabled selected>If there is a matching type for the category, please select it</option>
<option value="1">A</option>
<option value="2">B</option>
</select>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="row action">
<div class="form-group">
<div class="inline">
<button class="btn btn-primary form-control" type="submit">submit</button>
</div>
<div class="inline">
<button class="btn btn-primary cancel">Cancel</button>
</div>
</div>
</div>
</td>
</tr>
</table>
and my css:
.form-group{
display: block;
}
table {
margin-left: 5%;
table-layout: fixed;
}
input {
height: 30px;
border: 1px solid #e3e3e3;
padding: 3px 10px;
width: 100%;
}
.inline{
float:left;
margin-right: 5px;
}
span.select2-container{
width: 100% !important;
tex-overflow: ellipsis;
}
Here is a fiddle example. You can try it out by typing something long enough into the select2 dropdown and pressing enter seeing that the width changes. How can I stop that from happening?

I Added a new css class that seems to work aimed at that specific select box.
The + is equivalent to the jquery next function. This should get you pointed in the right direction. Just to be safe, make sure this is set after you load the select2 css.
#tag_list + .select2-container .select2-selection--multiple {
max-width: 400px;
}
So if you want the search box to be the same size for all select2s (single or multi ) then try...
.select2-container .select2-dropdown,
.select2-container .select2-selection--multiple {
max-width: 300px;
}
I turned the last select box into a select2 and tried this.
What I am doing is using the DOM explorer built into IE to find the element that is giving me issues and what classes are being applied. Then I simple overwrite or append them.

by setting width=100% as below
<select id="tag_list" style="width:100%; display:block;" class='form-control' multiple="multiple">
<option value='1'>abba</option>
<option value='2'>zabba</option>
<option value='3'>doo</option>
</select>

.select2 dropdown to stay at constant width in table tag use below style in your code
<style>
.select2-container{
width: 100% !important;
}
</style>

Related

How to align properly?

I have to align the fields(right) as shown in below image.Here is my code built in Angularjs using label tag for all the legends in form and i styled using CSS. How to correct the alignment, so that i can submit this to higher officials?
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<head>
<title> SAP WORKBENCH </title>
<style>
body{
background-color: lightgray;
margin-left: 500px
}
.option{
width: 300px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button{
width: 300px;
}
</style>
</head>
<h2>Password Reset</h2>
<body>
<div ng-app="">
<b></b>
<form>
<label>System:</label>
<select class="option" ng-model="myVar">
<option></option>
<option value = "DR9">DR9</option>
<option value = "QR9">QR9</option>
<option value = "PR3">PR3</option>
</select>
<br><br>
<div ng-switch="myVar">
<label>Client:</label>
<select class="option" ng-switch-when="DR9">
<option>100</option>
<option>400</option>
<option>500</option>
</select>
<select class="option" ng-switch-when="QR9">
<option>500</option>
</select>
<select class="option" ng-switch-when="PR3">
<option>500</option>
</select>
<select class="option" ng-switch-default>
<option></option>
</select>
</div>
<br>
<label>User:</label>
<input class="option" type="text" placeholder="Enter User Id.."></input>
<br><br>
<label>New Password:</label>
<input class="option" type="password"></input>
<br><br>
<label>Re-Enter New Password:</label>
<input class="option" type="password"></input>
<br><br>
<input class="button" type="button" value="Reset">
</form>
</div>
</body>
</html>
Wrap each label + select into a separate div. Then give the parent container the properties display: flex, flex-direction: column, align-items: center.
Further tip: Try to avoid the <br > tags and work with margin-bottom. For this purpose also the new single <div> elements can be used.
It would be best if you use bootstrap to do this. It's much easier to get alignment done that way so please do use bootstrap. Also I have done a test code without bootstrap using display: flex;. Hope this helps your problem (I have not included any of your ngswitch statements here)
HTML
<h2>Password Reset</h2>
<body>
<div>
<b></b>
<form class="d-flex flex-row">
<div class="d-flex flex-column text-right mr-1">
<label class="margin-b">System:</label>
<label class="margin-b-7">Client:</label>
<label class="margin-b-7">User:</label>
<label class="margin-b-7">New Password:</label>
<label class="margin-b">Re-Enter New Password:</label>
</div>
<div class="d-flex flex-column">
<div class="margin-b">
<select class="option" ng-model="myVar">
<option></option>
<option value = "DR9">DR9</option>
<option value = "QR9">QR9</option>
<option value = "PR3">PR3</option>
</select>
</div>
<div class="margin-b">
<select class="option">
<option>100</option>
<option>400</option>
<option>500</option>
</select>
</div>
<div class="margin-b">
<input class="option" type="text" placeholder="Enter User Id.."></input>
</div>
<div class="margin-b">
<input class="option" type="password"></input>
</div>
<div class="margin-b">
<input class="option" type="password"></input>
</div>
<div class="margin-b">
<input class="button" type="button" value="Reset">
</div>
</div>
</div>
</form>
</div>
</body>
CSS
body {
background-color: lightgray;
}
.option {
width: 300px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button {
width: 300px;
}
.d-flex {
display: flex;
}
.flex-row {
flex-direction: row;
}
.flex-column {
flex-direction: column;
}
.text-right {
text-align: right;
}
.mr-1 {
margin-right: 1rem;
}
.margin-b {
margin-bottom: 5px;
}
.margin-b-7 {
margin-bottom: 7px;
}
JS Fiddle Link : https://jsfiddle.net/SJ_KIllshot/mhp6uksd/

Chrome CSS failing

Hi I am trying to change the css for a select element, and it works fine in firefox but it doesnt work in google chrome. When I tried to use the Chrome Developer tool, I see that under Styles/Matched CSS Rules the elements height is successfully changed to 30px but when I go to Computed Style the height shows up as 18px but when I click the arrow to get more information it shows that the height should be 30px
#new-job select - 30px
I am new with html/css and chromes developer tools, but it seems like it should be working to me. Here is my code
<div id="new-job">
<form accept-charset="UTF-8" action="/jobs" class="new_job" id="new_job" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="vfKmC+vnnIl/6icDIbDRfZghNR927qmEqTdB9I2OeMQ="></div>
<div class="field">
<label for="job_category">Category</label>
<select id="job_category" name="job[category]"><option value=""></option>
<option value="Bistro Card">Bistro Card</option>
<option value="Dongle">Dongle</option>
<option value="Return">Return</option>
<option value="Catering">Catering</option>
<option value="Other">Other</option></select><br><br>
</div>
<div class="field">
<label for="job_location">Location</label><br>
<input id="job_location" name="job[location]" size="30" type="text">
</div>
<div class="field">
<label for="job_description">Description</label><br>
<textarea cols="40" id="job_description" name="job[description]" rows="20"></textarea>
</div>
<div class="actions">
<input class="button small radius" id="submit-job" name="commit" type="submit" value="create job">
go back
<div id="new-job-errors"> </div>
</div>
</form>
</div>
And heres my CSS
#new-job {
select {height: 30px;}
}
the correct syntax should be
#new-job select {
height: 30px;
}
To select the select element you can use the assigned id, or the tag itself.
An example of both is here - http://jsfiddle.net/9HndG/
In your css, it would look like this:
by id
#job_category{
height:30px;
}
by tag
select{
width:200px;
}
by both
select#job_category{
height:30px;
}
add some style in your <select>
<select id="job_category" name="job[category]" style="height:30px;">...

Cannot get my div to move(position)

I'm doing my first piece of HTML & CSS today, and I'm having trouble trying to move a div. I read some tutorials on CSS and tried to replicate what I've seen. But for some reason I cannot get the div to move.
Can anybody set me straight as to what I've done wrong please?
CSS
#seax {
position:static;
top:400;
left:400;
}
HTML
<div id="seax">
<form autocomplete="off" method="post" action="/search.html">
<table>
<tbody>
<tr>
<td>Search:</td>
<td>
<input type="text" size="40" name="for" class="ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">
</td>
<td>
<input type="hidden" name="brand" value="0">
<input type="image" src="/user/templates/custom/search.gif" value="Go" alt="Go" style="padding: 0px">
</td>
</tr>
</tbody>
</table>
</form>
</div>
Change position:static; to position:relative;. Static position displays the div in it's default position, which is as it'd appear in the document flow you see.
Add "px" to your CSS, and use absolute
#seax {
position:absolute;
top:100px;
left:100px;
}
http://jsfiddle.net/djwave28/tnvQz/
It really depends on how you want to position the div.
position: static; is definitely your issue, as static position (as #Omega noted) displays the div in it's default position. You mean to write either position: absolute or position: relative. The difference between the two is best outlined here but I'll give you a tl;dr.
position: absolute positions the div relative to the whole page, whereas position: relative positions it relative to the parent.
Also, you are missing px at the end of your top and left property values (i.e top:10px; and left:10px;)
Give the div a position of absolute
#seax {
position: absolute;
top:400;
left:400;
}
Try changing
<div id="seax"></div>
to
<div class="seax"></div>
and
#seax {
position:static;
top:400;
left:400;
}
to
.seax {
position:absolute;
top:400px;
left:400px;
}
if you want to use seax for multiple elements.
You could even try:
<form autocomplete="off" method="post" action="/search.html">
<div class="seax">
<table>
<tbody>
<tr>
<td>Search:</td>
<td>
<input type="text" size="40" name="for" class="ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">
</td>
<td>
<input type="hidden" name="brand" value="0">
<input type="image" src="/user/templates/custom/search.gif" value="Go" alt="Go" style="padding: 0px">
</td>
</tr>
</tbody>
</table>
</div>
</form>
That should fix it. Otherwise your HTML document should look something like this:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<style>
.seax {
position:absolute;
top:400px;
left:400px;
}
</style>
<body>
<div class="seax">
<form autocomplete="off" method="post" action="/search.html">
<table>
<tbody>
<tr>
<td>Search:</td>
<td>
<input type="text" size="40" name="for" class="ui-
autocomplete-input" autocomplete="off" role="textbox" aria-
autocomplete="list" aria-haspopup="true">
</td>
<td>
<input type="hidden" name="brand" value="0">
<input type="image" src="/user/templates/custom/search.gif"
value="Go" alt="Go" style="padding: 0px">
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>

Trying to create tableless control with dynamic content

I am pulling some content from a webservice that creates select lists with a label as a form of title. Rules are that the select lists must line up and stack on top of each other, and take up the rest of the screen real estate that the label does not take up.
I can do this by creating them with tables, but am looking for a more pure CSS route if possible.
tried using a combination of divs with display: table, display: table-row, display: table-cell but I couldn't get the stacking effect to work properly.
this is the effect I am looking for:
.horizontalFill {
width: 100%;
}
.noWrap {
white-space: nowrap;
}
<table class="horizontalFill">
<tr>
<td class="noWrap">
<label>some dynamic text</label>
</td>
<td class="horizontalFill">
<select class="horizontalFill">
</select>
</td>
</tr>
<tr>
<td class="noWrap">
<label>
other dynamic text here
</label>
</td>
<td class="horizontalFill">
<select class="horizontalFill"></select>
</td>
</tr>
</table>
You can try something similar to this:
label {
display: block;
overflow: hidden;
padding-bottom: 5px;
}
label .label {
display: block;
float: left;
width: 150px;
}
label .input {
display: block;
margin-left: 160px;
}
label select {
width: 100%;
}
<label class="row">
<span class="label">Label name one</span>
<span class="input">
<select>
<option>Select item</option>
</select>
</span>
</label>
<label class="row">
<span class="label">Label name two</span>
<span class="input">
<select>
<option>Select item</option>
</select>
</span>
</label>
<label class="row">
<span class="label">Label name three</span>
<span class="input">
<select>
<option>Select item</option>
</select>
</span>
</label>
It involves the label column being set to a static width, though, so it may not work out for you if you need that column to stretch or shrink. The label text will wrap to the next line without any issue, though.

How do I line up input fields and their labels in a grid like manner with css?

I'm trying to do something that must be relatively easy, but I've spent hours mucking around with this and I'm no getting to the answer.
I need to layout some input fields and their layers on a grid (or like a table I guess) with
lable input label input
label input label input
Because the input fields are different widths (and would look pretty crappy if they were all the same width) the best I've managed to get is
label input label input
label logerinput label input
How do I line up the second set of labels and there inputs?
I've made two classes for the labels
#dpi_form label {
display: inline-block;
width: 150px;
margin-left: 20px;
}
#dpi_form .right-label {
display: inline-block;
width: 150px;
margin-left: 220px;
}
and the associated controls are
<label for="req_retailer_index_fld">Select the retailer*:</label><select id="req_retailer_index_fld" name="req_retailer_index_fld" class="required selectbox ui-widget-content"><option>item 1</option><option>item 2</option></select>
<label for="req_region_index_fld" class="right-label">Select the region*:</label><select id="req_region_index_fld" name="req_region_index_fld" class="required selectbox ui-widget-content"><option>item 1</option><option>item 2</option></select><br />
<label for="req_customer_type_index_fld">Select the customer type*:</label><select id="req_customer_type_index_fld" name="req_customer_type_index_fld" class="required selectbox ui-widget-content"><option>item 1</option><option>item 2</option></select>
<label for="req_meter_state_index_fldi" class="right-label">Select the meter state*:</label><select id="req_meter_state_index_fld" name="req_meter_state_index_fld" class="required selectbox ui-widget-content"><option>item 1</option><option>item 2</option></select><br />
within a div.
I've tried absolute positioning, relative positioning, padding, all manner of right and left margins but still can't get the result I'm after.
I can find heaps of stuff or vertical alignment of controls.. but nothing showing me how to do this one.
Any clues please?
Peter.
Despite my comment about using tables on your question, this is how I would do it.
CSS:
label,
input {
display: block;
}
label {
padding: 4px 0 0;
}
.labels1 {
float: left;
width: 80px;
}
.labels2 {
float: left;
width: 80px;
}
.inputs1 {
float: left;
width: 200px;
}
.inputs2 {
float: left;
width: 200px;
}
HTML:
<div class="labels1">
<label for="input1">Input 1: </label>
<label for="input2">Input 2: </label>
<label for="input3">Input 2: </label>
</div>
<div class="inputs1">
<input type="text" value="" name="input1" id="input1" />
<input type="text" value="" name="input2" id="input2" />
<input type="text" value="" name="input3" id="input3" />
</div>
<div class="labels2">
<label for="input4">Input 4: </label>
<label for="input5">Input 5: </label>
<label for="input6">Input 6: </label>
</div>
<div class="inputs2">
<input type="text" value="" name="input4" id="input4" />
<input type="text" value="" name="input5" id="input5" />
<input type="text" value="" name="input6" id="input6" />
</div>
Then you can change the labels and inputs classes to the width you want.
Although I still think tables are easier because then you don't have to worry about setting widths yourself; you also don't have to worry about vertical alignment with tables.
use following styles.
for parent container
display: table;
for row container
display: table-row;
for cell container
display: table-cell;
example
<div style="display: table;">
<div style="display: table-row;">
<div style="display: table-cell;">
lable
</div>
<div style="display: table-cell;">
input
</div>
<div style="display: table-cell;">
label input
</div>
</div>
<div>
<div style="display: table-cell;">
lable
</div>
<div style="display: table-cell;">
input
</div>
<div style="display: table-cell;">
label input
</div>
</div>
</div>
Use a table, that's what they are for.
I would suggest using a table or for a pure CSS solution maybe the 960 grid system 960.gs
I would use floats. Here's a jsfiddle showing how I would do it:
http://jsfiddle.net/pSsap/
I'll reproduce the code below.
With html like this:
<form class="grid">
<section>
<label for="wind">wind</label>
<span class="field"><input id="wind" name="wind" type="input" class="regular"></span>
<label for="earth">earth</label>
<span class="field"><input id="earth" name="earth" type="input" class="regular"></span>
</section>
<section>
<label for="fire">fire</label>
<span class="field"><input id="fire" name="fire" type="input" class="long"></span>
<label for="air">air</label>
<span class="field"><input id="air" name="air" type="input" class="regular"></span>
</section>
</form>
And css like this:
form.grid section {
clear: both;
}
form.grid section label, form.grid section span.field {
display: block;
float: left;
}
form.grid section label {
width: 50px;
}
form.grid section span.field {
width: 150px;
}
input.regular {
width: 100px;
}
input.long {
width: 140px;
}
Solutions:
Use a list: <ol> or <ul>
Set a width for that list: (in the example, 960px is the width of the <ul>)
Float the lists: <li> and set a width to limit its floating point: (in the example, 320px is the set width)
If you want to have a consistent alignment with the <label> and <select> pairs, set a width to the <label> (make sure you set it as a block-level element first: in the example, the <label> was set to 160px)
Make sure to clear (clear: left) any elements following this list (<ul>) used.
The Markup:
<ul>
<li>
<label for="req_retailer_index_fld">Select the retailer*:</label>
<select id="req_retailer_index_fld" name="req_retailer_index_fld" class="required selectbox ui-widget-content">
<option>item 1</option><option>item 2</option>
</select>
</li>
<li>
<label for="req_region_index_fld" class="right-label">Select the region*:</label>
<select id="req_region_index_fld" name="req_region_index_fld" class="required selectbox ui-widget-content">
<option>item 1</option><option>item 2</option>
</select>
</li>
<li>
<label for="req_customer_type_index_fld">Select the customer type*:</label>
<select id="req_customer_type_index_fld" name="req_customer_type_index_fld" class="required selectbox ui-widget-content">
<option>item 1</option><option>item 2</option>
</select>
</li>
<li>
<label for="req_meter_state_index_fldi" class="right-label">Select the meter state*:</label>
<select id="req_meter_state_index_fld" name="req_meter_state_index_fld" class="required selectbox ui-widget-content">
<option>item 1</option><option>item 2</option>
</select>
</li>
</ul>
The CSS
ul {
background: #EEE;
width: 960px;
}
li {
background: #FFC0CB;
float: left;
list-style: none;
width: 320px;
}
label {
display: inline-block;
width: 160px;
}
The result is that, the list will just drop when the <ul> can't contain it any longer (since you have set a width in it). On the other hand, the width of the <li>s will consistently make them align to each other, while being floated.

Resources