Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to "cheat" the design of a select box to act as an inline tab/widget.

After done some research, I know it's hard to modify by CSS. But I think it's probably manageable.

So, based on the codepen below, when I click the option, the text will turn white. Only once I click some white space, the text become black. Why is that and how to fix it?

Secondly, how to remove the right side arrow bar thing? I run this CSS on the staging site, chrome doesn't show the arrow bar, but mozilla got.

An Anonymous Pen on CodePen[^]

What I have tried:

- I thought maybe it's because of selection thing, so I tried css below but nothing happened
::selection {color:#000 !important;}

- I also tried select option:active, select option:focus but still the same.
Posted
Updated 14-Sep-18 5:48am

1 solution

I'm sorry to say, that what you are looking to do is not achievable. Color and background color may not be set on a select option. However, you may set the background-image. The code below demonstrates setting the background image through the use of JQuery (and resetting the background of the un-selected option). It also hides the right side scroll bar through CSS overflow:hidden

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function () {
			$('#mySelectList').change(function (){
			var $option = $(this).find('option:selected');			
			$option.css('color','#00000');  // <-- this is not permitted, can't set color or background on an option
			$option.css('background-image','linear-gradient(blue, gray)');
			// reset all			
			var $unoption = $(this).find('option:not(:selected)');			
			$unoption.css('background-image','none');
		});
	});
</script>
<style>
select {
	width: 100%;
	padding: 0;
	border: none;
  background-image: none;
	font-size: 16px;
	color: #868686;
	overflow: hidden;
}

select option {
		display: inline;
		float: left;
		margin-right: 128px;
}

select option:hover {
		cursor: pointer;
}

select option:checked {
		font-weight: 600;
}

</style>
<html>
<body>
 
<select id="mySelectList" multiple="multiple">
  <option value="">Latest</option>
  <option value="52">New</option>
  <option value="53">Pre-Owned</option>
  <option value="115">Unworn</option>
</select>
<div id="results">
</div>

</body>
</html> 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900