I am trying to style a select element that is created in php. The select code is very simple and I can style the text color of the drop down. But this doesn't style the select element when a option is selected. Also this code doesn't work on Safari at all.
Code:
<div class="theme">
<select class="theme" name="select_theme">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
CSS:
.theme {
width: 200px;
}
.theme select {
width: 200px;
background-color: rgba(200,200,200,0.8);
}
.theme select option[value="red"] {
color: red;
}
.theme select option[value="green"] {
color: green;
}
.theme select option[value="blue"] {
color: blue;
}
How do I style the default select so that it matches the option style rule?
How to get this to work on Safari? Possibly IE I haven't checked it yet no Windows system ATM.
Is there a way to get these rules to display an image as well? I've tried a few things but seems it requires Javascript which I try to avoid when I can.
I made a JSFiddle to demonstrate this.
There's no way you can accomplish this without JavaScript:
$('select.theme').change(function () {
$(this).css('color', $(this).find('option:selected').css('color'));
}).change();
Here's your fiddle: http://jsfiddle.net/uCmSR/2/
Related
I have been asked to style the background colour of a dropdown on a hover event. I tried applying a class class="page-limit-option" to the option element, but no luck. Do I need to create a new style component to use instead of the option?
<select class="page-limit"
id="per-page"
(change)="onLimitChange($event.target.value)">
<option class="page-limit-option"
*ngFor="let option of pageLimitOptions"
[value]="option.value"
[selected]="option.value == currentPageLimit"
>
{{ option.value }}
</option>
</select>
In the css file :
.page-limit {
background-color: blue;
}
.page-limit-option {
background-color: red;
}
.page-limit-option:hover {
background-color: orange;
}
stackblitz here:
https://stackblitz.com/edit/angular-style-a-select?file=app/app.component.ts
You can not style option as its browser specific feature. So better make a custom select dropdown using div. there you can give colors.
So I am using chosen - jquery plugin for selects.
Say I have four selects on the webpage, and I want the last one to open upward. I know how to change style to open select upward, but I can't figure out how to do this for only one select. It is probably trivial but I couldn't find the answer anywhere
To install it I downloaded files chosen.jquery.min.js as well as chosen.css and I put following code into my js file that handles all selects:
$(document).ready(function() {
$('select').chosen({ width: '100%', disable_search_threshold: 9 });
});
I should probably add classes to selects, add style that I want to override to chosen2.css, right? But then how to connect those particular selects with new css file?
Here is a fiddle of what I am trying to accomplish:
fiddle
EDITED:
I modified your fiddle, just put your last select inside a div and apply your css just to selects inside that div:
Drop Up
<div class="drop-up">
<div class="box" style="margin-left: 50px; margin-top: 15px;">
<select class="chosen-select">
<option selected="selected">10</option>
<option>20</option>
<option>50</option>
<option>100</option>
</select>
</div>
</div>
CSS
.drop-up .chosen-container .chosen-drop {
border-bottom: 0;
border-top: 1px solid #aaa;
top: auto;
bottom: 40px;
}
Let me know if this is what you are looking for :)
I think you can use classes or Ids on your select tag
for example :
$(document).ready(function() {
$('select.last').chosen({ width: '100%', disable_search_threshold: 9 });
});
or
$(document).ready(function() {
$('#last').chosen({ width: '100%', disable_search_threshold: 9 });
});
Good afternoon, do you know if there is any way to hide the 'Dropdown' that appears with the options of a select when clicking on it? When click add a class to an element to show a hidden div but for something less than 1 sec you see the dropdown.
If you can help me, I would be grateful
A greeting.
$('.select').on('click', function(e) {
e.preventDefault();
$(this).attr("disabled", true);
$('.options_select').addClass('show');
});
No, is a replaced element, that is rendered by the operating system. There is no CSS option to hide the drop-down.
There's some notes on https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#Styling_with_CSS
I think you can get what you're looking for like this: https://codepen.io/gerard-payne/pen/NVjppd
select {
appearance: textfield;
-webkit-appearance: textfield;
}
option {
display: none;
}
but also setting disabled and adding a font color.
<select disabled="disabled">
<option>2000</option>
<option>2001</option>
<option>2002</option>
</select>
select[disabled="disabled"] {
color: black;
}
don't add the options to the list or add something like:
CSS:
.select > option {
display: none;
}
$(".select").on('click',function(){
$('.submenu').toggle();
})
.select{ border:solid 1px #000; display:inline-block; padding:20px; cursor:pointer;}
.submenu{ background:#345; display:none;}
.submenu a{color:#fff;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="select">Select</div>
<div class="submenu">
option
option
option
option
</div>
i think you want this? if not let me know
Is there a way to right-align the dropdown menu with dropdownAutoWidth: true in a select2, instead of the standard left-align? I want the select to stay the same, but the dropdown to move to the left so it aligns with the select on the right side. See snippet below with the wrong alignment...
Note: I want different sizes of the select and the dropdown just as shown.
Edit // Updated with suggestion to add direction: rtl; text-align: right;. This only right-aligns the text. I want the whole dropdown to right-align with the selected option above it. E.g. the dropdown should stick out to the left of the selected option, not to the right.
Edit 2 // Updated with .select2-dropdown { left: -69px !important; } This makes it look the way I want, but is there a way to not have to set a fixed value of -69px?
$(".form-control").select2({
width: '100px',
dropdownAutoWidth : true,
});
.select2-container--default .select2-results > .select2-results__options { direction: rtl; text-align: right; }
.select2-dropdown {
left:-69px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
I want it to look like this, but not with a fixed value to achieve it.
As you want to have different sizes of the select and the dropdown, here is my solution using select2 built-in options and some CSS, but maybe not perfect one.
Making rtl is dead easy, just use dir, but we need to pass some other options as shown below:
$('select').select2({
dir: "rtl",
dropdownAutoWidth: true,
dropdownParent: $('#parent')
});
The key here is dropdownParent, also you need to edit your HTML as follow:
<div id='parent'>
<select>
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
</div>
And finally you need some CSS:
#parent {
/* can be any value */
width: 300px;
text-align: right;
direction: rtl;
position: relative;
}
#parent .select2-container--open + .select2-container--open {
left: auto;
right: 0;
width: 100%;
}
You can see it in action here on codepen
If you have more that one select, you need some JS code to handle dropdownParent option. btw your life will be easier if you remove dropdownAutoWidth option ;)
I managed to make it work slightly different. I needed to expand the dropdown to the left while keeping the options texts aligned left. I thought it's worth sharing. Here is the setup:
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js"></script>
<div id="select2-container">
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option>purple</option>
</select>
</div>
CSS
.select2-container.select2-container--default.select2-container--open {
right: auto;
left: unset !important;
}
.select2-container.select2-container--default.select2-container--open .select2-dropdown.select2-dropdown--below {
width: fit-content !important;
left: unset;
right: 0;
}
JavaScript
$('.form-control').select2({ dropdownParent: $('#select2-container') });
Result looks like:
If still looking for an alternative method. I recently needed to achieve this as well and the RTL answer was not a viable solution for my project. If you are using the select2.js(4.0.6) and do not mind modifying the script you can replace the following code and achieve right positioning with adding a class to the target element.
Look for lines 4381 to 4384
var css = {
left: offset.left,
top: container.bottom
};
Replace with the following
var containerWidth = this.$container.outerWidth()
var right = ($("body").outerWidth() - (offset.left + containerWidth));
if (this.$element.hasClass("right-align")) {
var css = {
right: right,
top: container.bottom
};
} else {
var css = {
left: offset.left,
top: container.bottom
};
}
Add the class right-align to your target select input.
<select id="client_select" class="form-control right-align">
<option value="01">Example One</option>
<option value="02">Example Two</option>
<option value="03">Example Three</option>
</select>
That should be all you need. If the target element has the class then the code will position the dropdown using right instead of left positioning. Should work the same if container is set to a parent. However, this is not thoroughly tested. It may not be an elegant solution, but it does not require overriding anything with CSS. Should do the trick until an update is made to add an option for handling right aligned dropdowns. Hope it helps.
init select with dir: "rtl" such as:
$('select').select2({
dir: "rtl,
...
...
...
...
})
option tag of select cannot be modified by css it need to be done jquery plugin
http://selectric.js.org/demo.html
It should help you my friend
I think it will help you to get what exactly you need.if u wont get it ping me back
select {
text-align-last: right;
}
option {
direction: rtl;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
I am trying to program my own WYSIWYG editor as a summer project. I am trying to implement the line height function that controls(single spacing, double spacing, etc). I have created the dropdown that will allow users to select between the types of spacing. However, I cannot seem to get the right Javascript, because the selected text does not change in line spacing at all no matter which option I choose. Can someone please take a look at my Javascript and tell me where I went wrong? If possible, can you give me the correct code for the Javascript so I can refer off of it? Thank you!
HTML(working):
<select id="lineHeight" onchange="spacing(this)">
<option value="20px">20px</option>
<option value="80px">80px</option>
<option value="100px">100px</option>
<option value="200px">200px</option>
</select>
Javascript(not working)
function spacing(sel) {
var text = editor.document.getSelection();
text.style.lineHeight = sel.value;
}
If I understand what you're trying to do, perhaps this will work for you:
function changeStyle( property, value ) {
if ( window.getSelection().rangeCount ) {
var range = window.getSelection().getRangeAt( 0 ),
contents = range.extractContents(),
span = document.createElement( 'span' );
span.style[ property ] = value;
span.appendChild( contents );
range.insertNode( span );
window.getSelection().removeAllRanges()
}
}
#editor {
width: 350px;
max-height: 100px;
padding: 20px;
overflow-y: auto;
background-color: #efefef;
border: 1px solid #ddd
}
<p>
<label for="lineHeight">Line Height: </label>
<select id="lineHeight" onchange="changeStyle('line-height', this.value)">
<option value="20px">20px</option>
<option value="80px">80px</option>
<option value="100px">100px</option>
<option value="200px">200px</option>
</select>
<button onclick="changeStyle('font-weight', 'bold')">Bold</button>
<button onclick="changeStyle('font-style', 'italic')">Italic</button>
<button onclick="changeStyle('color', 'red')">Color</button>
<button onclick="changeStyle('background-color', 'yellow')">Background</button>
</p>
<div id="editor" contenteditable>Change the line height of the selected text with Javascript:<br>Please note that this example should be completed.</div>