What is the method for adjusting the border color of an ng-select component to red?

I am having an issue with the select component in my Angular HTML template. It is currently displaying in grey color, but I would like it to have a red border instead. I have tried using custom CSS, but I haven't been able to achieve the desired result.

My project is built on Angular 5

<ng-select class="custom" [searchable]="true" formControlName="contactList" [items]="contactLists" 
     bindLabel="name" 
     placeholder="{{ 'PLACEHOLDERS.CONTACT_LIST' | translate }}" [compareWith]="compareResource">
</ng-select>

The custom CSS that I have defined for this component is as follows:

.ng-select.custom {
    min-height: 0px;
    max-width: 350px;
}
.ng-select.dropdown {
    border: 1px solid black;
} 

Answer №1

If you're searching for a solution, you may want to consider using the selector ::ng-deep. For more information, please refer to the official documentation. Please note that this feature is currently deprecated.

::ng-deep .ng-select.custom {
        min-height: 0px;
        max-width: 350px;
}

::ng-deep .ng-select.dropdown {
    border: 1px solid black;
} 

Answer №2

To modify the SCSS variable, you have the option to override it in your styles.scss file

$ng-select-highlight: #6200ee !default;
@import "~@ng-select/ng-select/scss/material.theme";

Answer №3

Include these CSS styles in the stylesheet of your component. Learn more

/* 
    Change to red when touched
*/

/* Border Color */
.ng-select.ng-invalid.ng-touched .ng-select-container{
    border-color: red;
}

/* Arrow Color */
.ng-select.ng-invalid.ng-touched .ng-arrow-wrapper .ng-arrow{
    border-color: red transparent transparent;
}
/* Placeholder Color */
.ng-select.ng-invalid.ng-touched .ng-select-container .ng-placeholder{
    color: red;
}

Answer №4

It seems like the issue could be related to Angular view encapsulation and CSS specificity in the browser. Additionally, I noticed that you have used border: 1px solid black; instead of red as mentioned in your question.

For more information on CSS specificity, you can visit: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

To learn about view encapsulation in Angular, check out:

Answer №5

Have you experimented with selecting ".ng-select" in your css? Maybe try targeting it as an element instead of a class?

 ng-select.customize {
     min-height: 0;
     max-width: 350px;
 }

 ng-select.drop {
     border: 1px solid black;
 } 

Answer №6

<ng-multiselect-dropdown #multiSelect [placeholder]="'Select Service'" [settings]="dropdownSettings" class="custom-dropdown"
  [data]="dropdownList">
</ng-multiselect-dropdown>

Specify a unique class like custom-dropdown and then in your styles.scss modify it as follows:

.custom-dropdown .multiselect-dropdown {
    position: relative;
    font-size: 0.9375rem !important;
    font-weight: 600 !important;
    color: #757575 !important;
    font-family: "Gilroy-SemiBold", "Arial", serif !important;
    .dropdown-btn {
        border: 1px solid #red !important;
    }
}

Answer №7

To customize the border color of the ng-select component, simply adjust the border color within the ng-container element -

.ng-select .ng-select-container {
    border: 1px solid #2aa0d0;
}

Answer №8

If you desire validation to change the color to red, follow these steps. Otherwise, simply copy the CSS style provided below:

Here is the outcome:


<div class="row mb-3">
              <div class="col-md-12">
                <label for="companyId" class="form-label">Company</label>
                <ng-select placeholder="select a company" appNgSelectWorkaroundValidator
                  formControlName="companyId" [items]="companies" bindValue="id" bindLabel="name"
                  required>
                </ng-select>
              </div>
            </div>

A new appNgSelectWorkaroundValidator directive was added to the ng select, automatically applying the isvalid CSS state.

This meets our requirements.

Let's create a directive as the first step.

import { Directive, HostBinding, Optional, Self } from '@angular/core';
import { NgControl } from '@angular/forms';

@Directive({
  selector: '[appNgSelectWorkaroundValidator]',
  standalone: true,
})
export class NgSelectWorkaroundValidator {
  constructor(@Optional() @Self() public ngControl: NgControl) {}

  @HostBinding('class.is-invalid') get isInvalid(): boolean {
    // The control is invalid and has been touched.
    return this.ngControl
      ? this.ngControl.invalid &&
          (this.ngControl.dirty || this.ngControl.touched)
      : false;
  }
}

Don't forget to set the CSS globally somewhere:

.is-invalid {
  border: 1px solid red;
  border-radius: 4px; /* Optional, for rounded corners */
}

And make sure to import the directive in your component:

@Component({
  selector: 'app-some-component',
  standalone: true,
  imports: [ NgSelectWorkaroundValidator],

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

How to successfully pass a Bootstrap dropdown value back to Flask

I need assistance with extracting a selected value (player) from a dropdown menu. The goal is to send this 'player' value to Flask upon selection, and then render the corresponding player's image on the website. HTML: <form method="P ...

Using ngTemplateOutlet to pass ng-template to a child component in Angular 5

I am looking to develop a versatile component that can utilize custom templates for data rendering, while consolidating the business logic to prevent redundancy. Imagine this use case: a paginated list. The pagination logic should be housed within the com ...

Tooltip remains visible even after formatting in highcharts

I have successfully hidden the datalabels with 0 values by formatting them. However, after formatting the tooltips for 0 valued data in a pie chart, there is an issue where hovering over the 0 valued portion shows a white box as shown in the picture. I hav ...

How can I float a square to the right in Bootstrap with a col-md-4 size but with a height restricted to only 200 px

Looking to add a small square on the page for users to search and purchase courses directly. The square should be 4 columns wide, follow Bootstrap4 grid template, and be around 200-300 px in length. It needs to remain visible as the user scrolls down the p ...

Icon displayed within the input field

Is there a simple method to add an input text element with a clear icon on the right side, similar to the layout of the Google search box? I've searched, but I could only find instructions for placing an icon as the background of the input element. I ...

Text being enveloped in a span tag

Currently, I have a table displaying a list of users with their corresponding roles. However, the roles column is sometimes wrapping before reaching the end of the line. The user list data looks like this: $scope.userList = [{ "id": 1, "name":"AB ...

Angular encountered an error: spawn UNKNOWN was not handled

Within my Angular-13 project, everything had been running smoothly. However, out of nowhere, I encountered an error when trying to run ng serve: An unhandled exception occurred: spawn UNKNOWN See "C:\Users\JOSHU~1.IBE\AppData\Local ...

Issue with initial navigation in MVC app causing Angular app routing to fail

Encountering a problem with navigating within an Angular app hosted on an MVC page. While most of the functionality is working smoothly, there is one scenario where the URL changes but the new page is not displayed. Let's simplify the setup (this iss ...

Exploring Appsetting Configuration in AppModule of Angular 8

I'm looking to update my configuration in the appsettings file by replacing a hardcoded string with a reference to the appsetting. Currently, I have this hardcoded value in appmodule.ts: AgmCoreModule.forRoot({ apiKey: 'testtesttest', li ...

How can I arrange individual events in agendaWeek view on FullCalendar.io and then merge them all into one line?

I am using Fullcalendar.io version 2 When I switch to the agendaWeek mode, all my events are displayed on one line in each day square. As a result, the more events I have, the thinner the event blocks become. Is there a way for me to display only one eve ...

What prevents me from displaying the image in the Bootstrap tooltip?

I am currently utilizing the Bootstrap framework v3.3.0 for my website. I'm trying to implement an image as a tool-tip when the user hovers their mouse pointer over an icon. Here is the HTML code I have: <div class="col-sm-5"> <div class= ...

Angular 5, utilizing rxjs to map to JSON, encountered an issue where 'json' is not recognized on the type 'object'

Having trouble following an online video tutorial on Angular, none of the other solutions seem to be working for me. https://i.sstatic.net/KMNT4.png import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/co ...

Using Jquery to toggle the visibility of a DIV element and adding a blinking effect when it becomes visible

I have a hidden div that is displayed when a link is clicked, and I want it to blink as well. Here is my current setup: The link to display the hidden div: <a class="buttons" href="#" onclick="show(this)">join us</a> The hidden div to be di ...

Utilizing HTML and JavaScript to dynamically switch stylesheets based on the width of

When it comes to using stylesheets for mobile and non-mobile devices, there is a need for different approaches. The idea of comparing browser height and width in JavaScript seems like a good one, but the implementation may not always work as expected. ...

Using a loop variable within a callback function in JavaScript/TypeScript: Tips and tricks

I have a method in my TypeScript file that looks like this: getInitialBatches() { var i = 0; for (var dto of this.transferDTO.stockMovesDTOs) { i++; this.queryResourceService .getBatchIdUsingGET(this.batchParams) ...

How to Deactivate Horizontal Scrolling Using CSS in WordPress

Hey there, I'm currently working on a WordPress blog and facing an issue. Here's the link to my problem: When I resize the browser (X-Axis) to a minimum, like when using a mobile device, I noticed that I can scroll to the right in the Content se ...

What is the best way to assign unique IDs to automatically generated buttons in Angular?

Displayed below is a snippet of source code from an IONIC page dedicated to shapes and their information. Each shape on the page has two buttons associated with it: shape-properties-button and material-information-button. Is it possible to assign different ...

Show information from mysql in a dual-column format

I am pulling data from a single table in mysql, currently displaying it in one column. I want the data to be displayed in two columns next to each other. You can check out the results at www.urimsopa.com Here is my current code: $results = $mysqli->qu ...

Is there a way to create a soft light blue backdrop for text using HTML and CSS?

Is there a way to create a light blue background effect behind text using HTML and CSS? You can view the image reference here ...

Obtain the outcome of HTML5 FileReader by utilizing promises within an asynchronous function

I am encountering a challenge in my Angular 4 application where I am working with an image. I am trying to pass the base64 string to another variable, but due to the asynchronous nature of this process, the image.src ends up being empty. As a result, the ...