Utilize select2 to add custom styles to select elements

One issue I'm facing is that when I use select2 from the cdn with my select multiple input, the specific styles I apply to this select no longer show up. No matter if I specify these styles via id or class, they disappear once I add $('select').select2(); in my JavaScript code.

This is how I am implementing it:

$('select').select2();  

Here's my HTML:

<select id = "cars" name="cars[]" multiple = "multiple" required = "required">          

And here is the CSS styling for the select:

#cars{
   background-color:red !important;
}

Any suggestions on how to resolve this issue and make sure the specified styles are not overridden by select2?

Answer №2

Let's hide the select2 set select display and generate an element that looks like this:

<span class="select2 select2-container select2-container--default select2-container--below select2-container--focus" dir="rtl" style="width: 97px;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-c968-container"><span class="select2-selection__rendered" id="select2-c968-container" title="ظرفیت - بیشترین">ظرفیت - بیشترین</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>

To modify the style, you'll need to inspect the element as setting select { ... } won't work since the select element is hidden by select2.js.

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

Is it possible for webpack to combine css/scss/less files without creating duplicate entries for required files?

Consider this scenario where I have two components that need the same utility css file directly. Component one: import component1Css from '/path/to/component1.scss' import someOtherUtilityCss from './path/to/some-other-css.scss' ... e ...

Is it conceivable that Rails may be rendering HTML at a slower rate compared to static

When using a Rails layout, you can include HTML tags to render images like this: <%= image_tag("logo.png", :alt => "Sample App", :class => "round") %> This will generate the following HTML code: <img alt="Sample App" class="round" src="/i ...

Displaying CSS source on a webpage using Polymer 2.0 style modules for sharing styles

I have implemented a style module using Polymer 2.0 (https://www.polymer-project.org/2.0/docs/devguide/style-shadow-dom#style-modules). The styles are functioning properly in Firefox, but in Chrome, the CSS content is displaying as jumbled characters on t ...

What sets apart Material UI's gutterBottom from a paragraph?

Can you explain the distinction between these two options? While exploring the Typography Component API, I noticed that both the gutterBottom and paragraph props seem to serve the same purpose. If set to true, the margin bottom will be 0. You can find mor ...

Tips for ensuring the final row remains fixed at the bottom of the table while maintaining a minimal body height:

I'm currently working on designing an invoice table using HTML and CSS. I have dynamic rows that are added to the table, and I want the body of the table to have a minimum height to fit around 5-6 rows, after which it should extend based on the number ...

Create a versatile 1, 2, 3 column layout that seamlessly transitions from mobile to desktop using Flex

My vision is clear: https://i.sstatic.net/xi7KI.png Flex is the way to go for this, but my attempts are falling short: .content-wrapper, .tablet-top, .tablet-middle, .tablet-bottom { display: flex; justify-content: center; } /*tablet*/ @media ...

Issues with jQuery.ajax functionality have been reported across all versions of Internet Explorer

At my work computer, the code below doesn't seem to work in IE10 or earlier versions. However, everything runs smoothly on my home computer. I've tried debugging by setting breakpoints and checking the network tab, but it doesn't even appear ...

Challenges in designing a comment system using JavaScript

In the process of creating a comment system, I am developing a feature where a div fades in upon typing. This div will contain a text area and a submit box to enhance user experience. However, I have encountered some challenges that I need assistance from ...

Highcharts: Show tooltip above all elements

Is there a way to configure tooltip display above all elements? I want to define specific coordinates so that the tooltip remains open even if it is covering the chart, and only closes when interacting with the top block. Screenshot 1 Screenshot 2 For e ...

Issue with margin-top in combination with clear:both does not function as expected

<div style="float: left;">Left</div> <div style="float: right;">Right</div> <div style="clear: both; margin-top: 200px;">Main Data</div> What is causing the margin-top property to not work as expected for 'Main Dat ...

How about incorporating a randomized, dynamic background image that adjusts to different screen sizes and proportions

Can anyone help me with adding a responsive background image between the header and footer of my website? I want to make sure the background image changes randomly on each page load from a set of 5 different images. I've made some progress, but I&apo ...

Issues with foundation tab functionality in the section

I'm currently facing an issue with Foundation 4 section tabs. After copying and pasting the example code to my local environment for testing, the tabs aren't showing up as expected. In an attempt to resolve the issue, I made some modifications b ...

Using PHP to extract information from an HTML checkbox and select the relevant query

I am facing an issue with my HTML file that contains a checkbox element. The code snippet is as follows: <body> <div id="wb_Web_Reports" style="position:absolute;left:329px;top:143px;width:510px;height:244px;z-index:4;"> <form name="Web_Re ...

Is there a method to loop through elements that share the same ID?

I am looking to dynamically assign the same event listener to two buttons with identical IDs. I am facing an issue where, when duplicating the editable content div (the DOM element updated by the event listener) like below, only the first button works and ...

Stop webpage loading with -webkit-transform

I recently encountered a frustrating issue with the Background Image display problem on iPad. I received advice to add -webkit-transform: translateZ(0); to the id, which initially fixed the problem but then led to a new issue. While it resolved the backgro ...

Does anyone know if it's achievable to include a background position within the img /asp:image tag?

Currently, I am endeavoring to enhance the loading speed of my webpage. The initial approach I decided to pursue is base64 conversion. On my homepage, there are a total of 18 small images that need to be loaded. Since base64 encoding increases image size ...

Setting focus on an input box using programmatic methods triggered by the ItemInvoked event in a ListView in a Windows 8 Metro HTML5 environment

Note: If you have not worked on Windows 8 before, please refrain from answering this question. You are advised not to vote or read it unless you are familiar with Windows 8 METRO HTML5/js applications that run in the Windows Runtime environment. Query: I ...

Ways to adjust the size of v-btn

Objective: My current task involves enhancing the customization options for Vue components, specifically focusing on adjusting the size of a button component based on user input. Our project utilizes Vuetify. Previous Code: <v-btn @click.prevent=&quo ...

Tips for creating a responsive Material UI TextField that adjusts to zooming in and out

While working on my initial Material UI project, I encountered a responsiveness issue with TextField and SelectField. During development, the application looked fine at a screen width of 1920px. However, when I attempted to decrease the screen width, I no ...

completing data tables and presenting them to clients

I am currently working on a website for a Nutrition specialist who requires a monthly diet regimen table with five meals daily. The doctor will be responsible for completing these tables, which will then be presented to clients. It's important that th ...