Change width of select element for each active option - html

I have a select element that lays directly up against an input. It looks great for the most part, but there's too much whitespace on some of the options. It seems like unless I set some static width myself, select expands to the width of its largest option.
I want to make it so that the select option only takes up as much width as the currently displayed option. I'd ask if it's possible, but I know it is somehow because I've seen it on Amazon's homepage on their search bar. The behavior of the select that controls their search filter is essentially what I'm trying to recreate on my website, but I can't figure out exactly what they did. Here's the code that reproduces the basic idea of what I've got currently.
HTML:
<select>
<option>All</option>
<option>Consoles</option>
<option>Games</option>
<option>Equipment</option>
</select>
<input type="text" />
CSS:
select {
-moz-appearance: none;
-webkit-appearance: none;
background-color: #f8f8f8;
border: 1px solid #ccc;
border-radius: 4px 0 0 4px;
border-right: 0;
box-sizing: border-box;
height: 44px;
padding: 10px;
text-align: center;
float: left;
}
input[type='text'] {
height: 44px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 0 4px 4px 0;
margin: 0;
padding: 0 5px;
font-size: 14px;
min-width: 300px;
}
I have a fiddle with everything here as well.
I know I could use JavaScript to make it work, but that would be an absolute last resort in my mind. I want to do it in pure HTML and CSS if I can. Any help is greatly appreciated.

Hope this is the thing that you want exactly. But since the <select> width need to be calculated or determined with another value, it is required some JavaScripts. I used pure jQuery and did some changes to your HTML and CSS.
The answer to your problem is, you need to append your selected option to another select option to get the current option width and keep it until the next change of the select.
HTML:
<div id="main-div">
<select id="resized">
<option value="All">All</option>
<option value="Consoles">Consoles</option>
<option value="Games">Games</option>
<option value="Equipment">Equipment</option>
</select>
<input type="text" />
</div>
<!-- hidden select to calculate the selected option width -->
<select id="hidden_select">
<option id="hidden_select_option"></option>
</select>
CSS:
select {
-moz-appearance: none;
-webkit-appearance: none;
background-color: #f8f8f8;
border: 1px solid #ccc;
border-radius: 4px 0 0 4px;
border-right: 0;
box-sizing: border-box;
height: 44px;
padding: 10px;
text-align: center;
float: left;
}
input {
height: 44px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 0 4px 4px 0;
margin: 0;
padding: 0 5px;
font-size: 14px;
}
#main-div{
display: none;
width: 375px;
}
#hidden_select {
display: none;
}
jQuery:
$(document).ready(function() {
$('#resized').change();
$("#main-div").css("display", "block");
});
$('#resized').on("change", function(){
$("#hidden_select_option").html($("#resized option:selected").text());
$(this).width($("#hidden_select").width());
//fix the input filed width
var x = $("#main-div").width();
var y = $(this).width() + 21;
$("input").width((x-y) - 12);
});
Note: values 21 and 12 are your padding values and border values of select and input.
Working Code:
https://jsfiddle.net/y82x60fa/10/

Here is the solution with simple jquery. On Amazon's home page is also done with js or other scripting language.
$('#resizingSelectTag').change(function(){
$("#widthTempOption").html($('#resizingSelectTag option:selected').text());
$(this).width($("#selectTagWidth").width());
});
#resizingSelectTag {
width: 50px;
}
#selectTagWidth{
display : none;
}
select {
-moz-appearance: none;
-webkit-appearance: none;
background-color: #f8f8f8;
border: 1px solid #ccc;
border-radius: 4px 0 0 4px;
border-right: 0;
box-sizing: border-box;
height: 44px;
padding: 10px;
text-align: center;
float: left;
position:absolute;
border-right: 1px solid #ccc;
}
input[type='text'] {
height: 44px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 0 4px 4px 0;
margin: 0;
padding: 0 5px;
font-size: 14px;
min-width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="resizingSelectTag">
<option>All</option>
<option>Lorem Ipsum</option>
<option>Lorem Ipsum is</option>
</select>
<select id="selectTagWidth">
<option id="widthTempOption"></option>
</select>
<input type="text" />

Related

How to make datalist the same width as input html

Hi I was wondering how to give a datalist and the corresponding input the same width. I tried the following :
input {
width: 100%;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
datalist,option{
width: 100%;
}
<input list="myList"/>
<datalist id="myList">
<option> This is a great option</option>
</datalist>
The datalist is not the input width when I use the code above. Does anyone know how to make a datalist with the same length as the input he belongs to. Thanks in advance.
You could try something like this. Assign a class to each and style with CSS.
CSS
.input {
width: 100%;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
.Option {
width: 100%;
}
HTML
<input list="myList"/>
<datalist id="myList">
<option class="Option"> This is a great option</option>
</datalist>
It might give you the desired outcome.

How to fix disappearing hoverable Box with HTML-Select-Input (in Firefox)

I'm trying to fix an issue with a hoverable span in html. Inside the span element is a html-select option. When I try to select something, the hoverable span disappears.
This issue only occurs in Firefox. In Chrome I don't have this issue.
HTML Part:
<span class="over">
<i class="fa fa-cogs edit"></i>
<div>
<form method="post" accept-charset="utf-8" action="/edit">
<div class="input select">
<label for="select-id">Select</label>
<select name="select_id" id="select-id">
<option value="">-- select --</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
</div>
</form>
</div>
</span>
SCSS Part:
.over{
margin-right: 0.5em;
&>i{
color: #bbbbbb;
padding: 2px;
}
&>a{
padding: 2px;
display: none;
&:hover{
color: #000;
}
}
&>div{
display: none;
position: absolute;
border: 1px solid #ccc;
margin: 6px 0 0 -20px;
padding: 2px;
background: white;
border-radius: 4px;
min-width: 140px;
z-index: 100;
text-align: left;
input, select{
padding: 0 5px;
line-height: 1.5em;
width: auto;
display: inline-block;
}
label{
margin: -4px 4px;
display: inline-block;
}
}
&:hover{
border: 1px solid;
border-radius: 4px;
&>i{
color: #000;
}
&>a{
display: inherit;
}
&>div{
display: inline-block;
}
}
}
}
In Chrome I can hover over the span item and select a option from the select-element. Even if I leave the hoverable area.
In Firefox I can also hover over the div, but as soon as I leave the hoverable area, the box disappears and I cannot select an Item.
Problem seems to be fixed in Firefox now.

How override default styles for a select element multiple

For a select element multiple I want to remove the background color of the selected option as I'm indicating the selection with the box before each option.
The intention is give a checkbox style to a select multiple element.
Why using a select multiple?
I'm working in a Angular App and should be useful style a select multiple as a collection of checkboxes. Doing that I will be able to use angular validations, ng-options, etc. I can build the same interface with inputs type checkbox but that imply a lot more of code for the same piece of functionality.
.form-group{
margin: 10px 0 0 20px;
}
select[multiple]{
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: none;
overflow: hidden;
box-shadow: none;
}
select[multiple].form-control{
padding: 6px 0 6px 6px;
}
select[multiple] option{
padding: 5px 0 7px 0;
}
select[multiple]:focus{
box-shadow: none;
}
select[multiple]:focus option:checked{
background-color: white;
}
select[multiple] option:before,
select[multiple] option:after{
content: "";
display: inline-block;
position: absolute;
width: 17px;
height: 17px;
left: 0;
margin-left: 12px;
border: 1px solid #ccc;
border-radius: 3px;
background-color: #fff;
}
select[multiple]:focus option:checked:before{
background: white;
}
select[multiple] option:checked:before{
background-color: #319DB5;
border-color: #2c8ca1;
}
select[multiple] option:checked:after{
background-color: gray;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="container">
<div class="form-group">
<label for="someOptions">Select multiple</label>
<select multiple="multiple" class="form-control" size="10" name="options" required="required" id="someOptions">
<option label="Option 1" value="number:1">Option 1</option>
<option label="Option 2" value="number:2">Option 2</option>
</select>
</div>
</div>
</body>
I think you are looking for this
select[multiple] option:checked { background: none; }

footer at the bottom with horizontal line

I am trying to put footer at the bottom with a horizontal line just above the footer. But I am not even able to get footer at the bottom. Tried many posts and blogs but I am missing out on something. I am using the base of some blog to create the signup page.
Fiddle
html
<div id="header">
</div>
<div id="main">
<div id="container">
<form action="index.html" method="post">
<p id="form_title" style='color:#808080'>Create an Account</p>
<fieldset>
<legend style="color:#585858">Get started with Your Profile</legend>
<label for="name" style='color:#808080;font-size:14px'>CUSTOM NAME</label>
<input type="text" id="name" name="user_name" style="color:#404040">
<label for="type" style='color:#808080;font-size:14px'>TYPE</label>
<select id="sel-type" name="type">
<option value="frontend_developer">Front-End Developer</option>
<option value="php_developor">PHP Developer</option>
<option value="python_developer">Python Developer</option>
<option value="rails_developer"> Rails Developer</option>
<option value="web_designer">Web Designer</option>
<option value="WordPress_developer">WordPress Developer</option>
</select>
<label for="type" style='color:#808080;font-size:14px'>REGION</label>
<select id="sel-region" name="region">
<option value="frontend_developer">Front-End Developer</option>
<option value="php_developor">PHP Developer</option>
<option value="python_developer">Python Developer</option>
<option value="rails_developer"> Rails Developer</option>
<option value="web_designer">Web Designer</option>
<option value="WordPress_developer">WordPress Developer</option>
</select>
</fieldset>
<button type="submit">Create Profile</button>
</form>
</div>
</div>
<div id="footer">
About
Instructions
Encountered an issue?
</div>
css
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: 'Lato';
background-color: #E8E8E8;
}
#header {
width:100%;
background-color: #27272D;
height: 50px ;
border:1px solid;
position:relative;
}
#main{
border:1px solid;
width:100%;
height:100%;
}
#container{
margin-top: 100px;
border:1px;
}
form {
max-width: 300px;
margin: 10px auto;
padding: 10px 20px;
border-radius: 3px;
background-color: white;
border:1px;
}
#form_title {
margin: 10px 0 30px 15px;
font-size:20px;
}
input[type="text"],
select {
background: rgba(255,255,255,0.1);
border: none;
font-size: 16px;
height: auto;
margin: 0;
outline: 0;
padding: 15px;
width: 100%;
background-color: #e8eeef;
color: #8a97a0;
box-shadow: 0 1px 0 rgba(0,0,0,0.03) inset;
margin-bottom: 20px;
}
input[type="text"]{
border-radius: 6px;
-moz-border-radius: 6px;
-khtml-border-radius: 6px;
-webkit-border-radius: 6px;
height:44px;
font-size: 14px;
}
select {
padding: 6px;
height: 44px;
border-radius: 2px;
}
button {
color: #FFF;
background-color: #13ABAF;
font-size: 14px;
text-align: center;
font-style: normal;
border-radius: 5px;
width: 96%;
height:44px;
border: 1px solid;
border-width: 1px 1px 3px;
margin-bottom: 10px;
margin-left: 10px;
}
fieldset {
border: none;
}
legend {
font-size: 17px;
margin-bottom: 24px;
}
label {
display: block;
margin-bottom: 8px;
}
label.light {
font-weight: 300;
display: inline;
}
#horizontal-line{
display: block;
margin-top:100px;
margin-bottom: 60px;
width:96%;
margin-left: auto;
margin-right: auto;
border-style: inset;
border-width: 1px;
border-color: #F0F0F0;
}
#footer {
position : absolute;
bottom : 0;
height:60px;
margin-top : 40px;
text-align: center ;
font-size: 10px ;
font-family: 'Lato' ;
}
#media screen and (min-width: 480px) {
form {
max-width: 480px;
}
}
For your css, try
#footer {
position: relative;
...
}
Also for the horizontal line just use
<hr>
html tag above footer
(sorry about that, my editing looks awkward because stackoverflow prints out a horizontal line whenever I use that tag)
Also its more simple and if you want minimal changes to css.
http://jsfiddle.net/750h2crz/7/
I would simply use separate tag for footer sec and use Horizontal Rule tag
<div id="footer">
<hr>
<footer>
About
Instructions
Encountered an issue?
<footer>
</div>
Thx
Remove the position: absolute in your style:
#footer {
position: absolute;
/* ... */
}
FIDDLE: https://jsfiddle.net/lmgonzalves/750h2crz/2/
put this in your css, and use a border instead of a horizontal line.
footer {
position: absolute;
bottom: 0;
height: 60px;
border-top-width: 5px;
border-top-style: solid;
border-top-color: #FFF;
}
updated your css with
#footer {
position:fixed;
width:100%; /* fill the whole width of the screen */
border-top:1px solid #aaa; /* you can change to whatever color you want */
padding-top:10px; /* add some spacing between line and links/text */
background:#fff; /* this is important otherwise your background will be transparent, change the color based on your needs */
/* ... your other properties */
}
Also updated you body styling with some padding at the bottom to offset the footer
body {
/* ... your other properties */
padding-bottom:65px;
}

Having issues placing html buttons side by side in a form

this is my first question here.
I'm having an issue getting my html form buttons side by side.. can somebody take a look and tell me whats wrong? it'd seem like they should by default be placed inline, but I guess that isnt the case.
Here is my html code.
<input type="submit" name="1" formtarget="" value="1">
<input type="submit" name="2" formtarget="" value="2">
<input type="submit" name="3" formtarget="" value="3">
<input type="submit" name="4" formtarget="" value="4">
and here is the CSS for the form input and individual name
#form input {
position: relative;
display: inline;
margin: 0;
padding: 0;
border: none;
outline: none;
-webkit-box-shadow: none;
box-shadow: none;
-webkit-border-radius: 4px;
border-radius: 4px;
text-shadow: none;
line-height: 44px;
-webkit-appearance: none;
}
and this is the same for each button besides the color changes.
#form input[name="1"] {
margin-top: 8px;
height: 44px;
width: 50%;
background: #A901DB;
border-bottom: 1px solid #B404AE;
color: #FFF;
font-size: 18px;
text-align: center;
}
#form input[name="2"] {
margin-top: 8px;
height: 44px;
width: 50%;
background: #A901DB;
border-bottom: 1px solid #B404AE;
color: #FFF;
font-size: 18px;
text-align: center;
}
Can someone help me set this up so that they are inline, side by side?
EDIT: This is what it shows.
http://jsfiddle.net/g01juc2z/2/
you have 4 elements set to width:50% which equals 200% width. Change them to width: 24% (inline-block elements have a natural spacing of 1 or 2px) or less and they will be aligned:
#form input[name="1"] {
margin-top: 8px;
height: 44px;
width: 24%; <---------------
background: #A901DB;
border-bottom: 1px solid #B404AE;
color: #FFF;
font-size: 18px;
text-align: center;
}
FIDDLE
in your css make these change where you write
[name="1"]
replace it with
[type="submit"]
and do not repeat it like a name
and another change is
width:24%;