Expand the background of the selected option in a dynamic way

Issue at hand: The current problem lies in the fact that the selected option's background does not expand to fit the width of the option text, resulting in a situation where if the background is white, the remaining text is not visible. How can this be rectified? I am currently using Chrome, but I would like this solution to also apply to Firefox and IE.

Here is the HTML code snippet:

<select multiple="" class="h-scrollable form-control">
                <option ng-repeat="elem in list">
                    {{elem}}</option>
            </select>

This is the CSS associated with it:

.h-scrollable
{
    width: 350px; 
    height: 150px !important;
    overflow: auto;
    margin-right:20px;
    font-size: medium;
}
.h-scrollable:focus
{
    height: 150px !important;
    overflow: auto;
    margin-right:20px;
    font-size: medium;
    background-size: 100%;
    background: pink;
}
.h-scrollable option
{
    /*width: 350px; */
}
.h-scrollable option:checked
{
    /*width: 400px; THIS FIXES THE PROBLEM FOR A TEXT OF WIDTH = 350px; I want a general solution  that will work for any length of  text*/
}

Answer №1

To achieve the desired layout, place the select element inside a div. Remove any width settings on the select and option elements, then add a width and overflow: auto to the containing div. For a visual example, see this JSFiddle demo.

<div style="width: 240px; overflow: auto;">
    <select multiple="" class="h-scrollable form-control">
    <option>one option</option>
    <option>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</option>
    <option>another option</option>
    <option>last option is really very long one</option>
    </select>
</div>

You may need to make some adjustments for your specific preferences, but this should provide the basis for what you are trying to achieve.

** Update: I have removed the margin-right property to prevent a scrollbar from appearing if the content is not wider than 240px. Additionally, a min-width has been added to the .h-scrollable class. Does this meet your requirements?

Answer №2

To implement text truncation without the need for a scrollbar, utilize CSS and the text-overflow property.

Although ultimately at the discretion of the designer, it is advisable to reconsider the decision of requiring users to scroll within a select field.

For an example, refer to this updated version of your code sample: http://jsfiddle.net/7dkhgLrv/6/

Additionally, ensure to include overflow:auto; in the .h-scrollable class for optimal display.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the $scope controller variables based on the URL

My web app consists of a simple table with formatted JSON data. By clicking on a column, users can filter that column in ascending order. I'm looking to modify the URL when this action occurs, like so: www.website.com/column/asc, This way, users ca ...

Hey there, I'm curious about a couple of things related to CSS

Seeking assistance as a newcomer to website creation. I am struggling with linking all my HTML pages to a single CSS sheet. The desired border effect can be seen in the image here: https://i.sstatic.net/Ojwuh.jpg The code snippet shows attempts to achieve ...

Enhancing navbar with a dropdown menu in CSS

Is there a way to add a dropdown menu to my navigation bar and make it visible when hovered over? Any help or support would be greatly appreciated. Here is the code I have on JSFiddle: http://jsfiddle.net/nbh2e15y/2/ The CSS: #nav { background: none ...

What steps can I take to alter this situation?

I am working on creating an API Code. Here is the code I have written: When we run the code, it displays as follows: website <?php include('simple_html_dom.php'); $url = 'https://www.g2g.com/wow-us/gold-2299-19249?&server=30805&a ...

Initiate playing YouTube videos by clicking on an image using HTML and CSS

Currently, I am in the process of learning HTML, CSS, and CSS3. I am trying to figure out how to play YouTube videos by clicking on an image using only HTML and CSS or CSS3. If anyone has any suggestions or guidance on how to achieve this, please let me ...

Avoid replicating controllers while utilizing AngularJS

I am using AngularJS version 1.2.25 and have a route configuration set up as follows: $routeProvider.when( '/config-root', { templateUrl: configRootTemplate, controller: "ClientConfig", } ).when( '/config-ac ...

divs with sticky sidebars are a common design choice

I have a sidebar for each div and I want to make all the sidebars fixed when scrolling down. You can find the code snippet here: var tmpWindow = $(window), sidebar = $('.sidebar'), sidebarHeight = sidebar.height(), offsetBottom = $( ...

Utilizing Angular's File Upload feature with Glyphicon icons

Has anyone tried to open a local file using bootstrap glyphicon? I found this example at https://jsfiddle.net/JeJenny/ZG9re/, but it doesn't seem to work. Does anyone have any ideas on how to make this approach work with images like glyphicon? main.h ...

Issue with Bootstrap error class not functioning in conjunction with hide class

I need to create a form that slides down two input fields when an error occurs, and I want them to have the bootstrap error class. The 'error' class works fine without the 'hide' class being present, but when the 'hide' class ...

Tips for enlarging the box width using animation

How can I create an animation that increases the width of the right side of a box from 20px to 80px by 60px when hovered over? What would be the jQuery code for achieving this effect? ...

What could be causing the .text method to not retrieve text even when it is present?

Currently experimenting with web scraping using Selenium to extract the color of a particular dress. Despite finding the text content under the 'screen-reader-text' class when inspecting the website, my attempts at fetching it result in an empty ...

Error Encountered during AngularJS Broadcast Unit Testing

Just diving into AngularJs and unit testing, I encountered an issue with a test I created that resulted in the following error message. It seems like there might be some bracket errors, but here is a snippet of the code to get a better understanding of how ...

Show the table in a vertical orientation with HTML and CSS

I am currently tackling a project that involves generating a PDF file from HTML. However, I have encountered an issue where the table exceeds the page width. As a result, I am seeking a solution to display this table vertically. Is there a way to achieve ...

Exploring CSS Outlining in Visual Studio 2013

Back in the days of Visual Studio 2010, whenever you opened a CSS file, a window would automatically pop up displaying all the classes and IDs for easy navigation within the file. However, in Visual Studio 2013, I seem to be unable to locate this helpful ...

AngularJS interpolation allows for dynamic placeholders in ui-select2.Feel free to ask

Currently in my AngularJS application, I am utilizing ui-select2 and I am looking to implement dynamic placeholder text. However, when I attempted to do so with the following code snippet: <input id="listcategories" type="hidden" data-placeholder= &apo ...

Do not execute the script if the window's width is below a certain threshold

I have a unique website layout that adjusts to different screen sizes, and I've incorporated some jQuery functionality. Here is a snippet of the code: <script> $(document).ready(function(){ $("#D1000C36LPB3").click(function(){$("#D ...

When attempting to access the essential assets and dependencies, I encounter errors while executing the command $ rake bower:install

Here are the specifications of my environment: - Operating System: Mac - Rails Version: 5.0.5 - RVM Ruby Version: ruby-2.4.0 - Node Version: v6.11.2 - NPM Version: 5.3.0 To download the required assets, I execute the following comman ...

Please ensure that all default tags (checkboxes) have a data-id or id assigned to them

When attempting to update todo tags using patch, I encountered an issue where the id or data-id is missing from pages created by Onenote with default todo tags. How can I target the element with patch? For example, on this page, the data-tag="to-do" does ...

Vue2 data vanishing mysteriously

I created a basic webpage using vue2 to convert binary numbers to decimal. However, I encountered an issue where the data disappears when submitting the binary value in an HTML form. <template> <div class="bar"> <input id="str" t ...

How to efficiently align images in Rails using Bootstrap styling

I've been attempting to center and enlarge two images by creating a class and modifying the stylesheets/scss, but so far, I haven't had any success. Below is the HTML code: <div class="row" class="testimoinal_image"> <div class="col ...