An issue arises when trying to simultaneously utilize and avoid using the `:not()` selector in CSS

Below is the html code I am working with:

<div class='input-group has-danger'>
    <input class='form-control' placeholder='first name'> // first name
</div>
<div>
    <input class='form-control' placeholder='last name'> // last name
</div>

In addition, here is the CSS code I have:

.input-group:not(.has-danger):not(.has-success) .form-control:focus{
    border-color: #000;
}

I want to make a change using only CSS without altering the HTML. The issue lies in selecting the last name input field. Both inputs are enclosed within div elements, but only the div containing the first name input has the class .input-group. My goal is to change the border color of the inputs to #000 upon focusing. However, the current CSS code does not affect the last name input.

I attempted to modify the CSS code as follows:

.input-group:not(.has-danger):not(.has-success) .form-control:focus,
.form-control:focus{
    border-color: #000;
}

This modification successfully affects both inputs, but it also changes the border color of inputs with the classes .has-danger and .has-success, which is undesired. Despite trying to exclude these classes using :not(), the first name input still gets affected.

If anyone knows a better solution using CSS only, please advise. Thank you.

Answer №1

If you're facing a similar situation, simply substitute .input-group with *:

*:not(.has-danger):not(.has-success)>.form-control:focus {
  border: solid red 4px;
}
<div class='input-group has-danger'>
  <input class='form-control' placeholder='first name'>
</div>
<div>
  <input class='form-control' placeholder='last name'>
</div>
<div class='input-group'>
  <input class='form-control' placeholder='a name'>
</div>

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

Modifying the TR class appears to be ineffective in this instance

I am attempting to update the TR className using JavaScript. However, for some reason it does not appear to be working properly (please note that it must also work in IE8). <html> <head> <style> tr.test {background-color:#000000; ...

adjusting the font size based on the size of the screen

Hi everyone, I've encountered an issue with adjusting font sizes based on screen size. My framework is Bootstrap 3 and I'm using LESS. Here is the initial part of my LESS code: @font-size-base: 16px; @font-size-large: ceil((@fo ...

Show button once modal has been closed

I have a 'start' button that triggers a modal window with an embedded video. Once the user closes the modal, I want to show a 'redeem' button after a delay of 6 seconds. HTML: <a id="video-start" onclick="openVideoModal()"><i ...

Issue with Chrome related to svg getScreenCTM function

Check out the jsFiddle demo I am attempting to utilize the getScreenCTM function to retrieve the mouse position based on SVG image coordinates. It seems to work in IE and Firefox, but not in Chrome. When I define the attributes width and height in the SV ...

One method for stacking posts vertically without overlapping

I'm looking to display posts one after the other. Initially, I applied this css code, but all the posts are ending up in the same location. How can I adjust this code to show them sequentially (similar to a Facebook wall)? .post00{ top:150px; ...

Left-floating div moves to the bottom

My CSS is set up to float my divs next to each other, creating 3 rows and 3 columns as needed. However, I'm facing an issue where the first div in the second row is being pushed under the second column instead of the first one. It's quite puzzlin ...

Scrollbar styling functions flawlessly on Chrome and Safari, but encounters issues on Mozilla Firefox

Currently, the scrollbar is functioning properly in Chrome and Safari, but in Mozilla, it is not behaving as expected. Below, you can find my code. Although using jquery or JavaScript would be a quick solution, I am exploring CSS options first. ::-webki ...

Can a partially see-through front color be placed on top of an image?

My goal is to add a foreground color with opacity over some images within a div, allowing the image to still be visible. I've come across solutions on S.O., like this one, but they don't quite achieve what I'm looking for. Below is my HTML ...

Bootstrap is having trouble displaying the collapsed menu due to the abundance of content on the page

While the menu itself opens without any problems, I am facing an issue where the content within the menu is not visible due to other elements on the page obstructing the view. Ideally, I was expecting the collapsed drop-down menu to appear over the existin ...

Having trouble understanding how to create a continuous loop for a CSS animation within the KreatureMedia Layerslider

Currently, I am using KreatureMedias Layerslider and trying to make a fullwidth wave bob up and down. This is what I have at the moment... <img class="ls-layer" data-ls="durationin:1500;scalein:1.1;offsetxin:-50;loopcount:-1;loopoffsetx:0sw;loopoffset ...

Concealing a div element while ensuring that it does not obscure any of its contents

There is a challenge I'm facing that I need help solving. I am using a shop script that only allows me to edit the CSS file. Within this setup, there is a div with a background image and inside there is a normal image: <style type="text/css"> ...

Stepper wizard experiences a variation in CSS styling when minified

Two plunkers showcasing material design wizard selectors are causing different behavior upon stepper clicks. One uses a minified version of CSS while the other is expanded. The issue arises with the transition effects in the .step-wizard .progressbar class ...

Apply a patterned background with CSS styling

I have tried to implement a custom CSS design using the background image bg.jpg with a pattern of dots.png that repeats both horizontally and vertically. *{ margin: 0; padding: 0; } body { margin: 0px; padding: 0px; color: #666; fo ...

I am experiencing some unwanted movement of divs when I hide one element and show another. Is there a way to prevent this from happening

My webpage features a dynamic div that transforms into another div upon clicking a button. Although both divs share similar properties, clicking the button causes some elements to shift unexpectedly. Strangely enough, when the height of the replacing div i ...

Using javascript to quickly change the background to a transparent color

I am in the process of designing a header for my website and I would like to make the background transparent automatically when the page loads using JavaScript. So far, I have attempted several methods but none seem to be working as desired. The CSS styles ...

Creating a scoreboard layout with HTML and CSS

I am attempting to create a scoreboard in the following format: 0 - 0 Home - Away The issue I am facing is that if the length of the Home team's name is longer, it pushes the dash (-) further. I want the dash to be a ...

Revamp the current webpage to display attribute values in textual format

As I peruse a webpage, I notice that there is room for improvement in terms of user-friendliness. The page is filled with a list of movie titles, each accompanied by a link to IMDb. However, the IMDB user rating is only visible when hovering over the titl ...

Aligning HTML headers in a horizontal manner

Can anyone help me out here? I have a div containing 4 elements that are currently stacked on top of each other. I want them to be aligned horizontally instead. The div's ID is #ninesixty. Thank you in advance! HTML <header> <div id="ni ...

Get rid of the paper border in Material-ui

Is there a way to remove the top border in material-ui Paper component? I've attempted the code below, but it doesn't seem to be effective. <Paper sx={{ border: 0, borderTop: 0, borderRadius: 0, ...

Assist the floats to "ascend" and occupy the available space

The screenshot showcases boxes that have been styled with "float: left". Is there a way to make these boxes float to the first available space in a column? For example, can we move the Music Nation box to the location indicated by the red arrow in the ima ...