Prevent input field inheritance in a specific div using CSS

I've been struggling with this issue for almost 48 hours now and I haven't been able to find the right assistance to solve my problem. Hopefully, you can provide some guidance :)

There is a main div with the ID "myDiv" and a stylesheet that has specific properties overriding the input elements. However, there are times when I want certain inputs to not adopt the styles specified in the stylesheet.

In the example below, input1 should follow the CSS specifications, while I would like input2 and input3 to have no styling applied. Ideally, they should each have their own unique styling.

Just bear in mind that this is a small excerpt from a larger stylesheet =)

Example:

<div id="myDiv">
   <input id="input1" />
   <div class="Picker">
       <input id="input2" />
       <div>
           <input id="input3" />
       </div> 
   </div>
   ... Other html markup
</div>

Stylesheet:

#myDiv input {
   width: 40%;
   border: 1px solid #c4c4c4;
}

I attempted to use the :not pseudo-class (referencing Mozilla documentation), but it didn't work as expected:

#myDiv div:not(.Picker) input {
   width: 40%;
   border: 1px solid #c4c4c4;
}

I also tried another approach with direct inheritance:

#myDiv div:not(.Picker) > input {
   width: 40%;
   border: 1px solid #c4c4c4;
}

Thank you all for your assistance. Apologies for any language errors in my message :)

Bobuche

Answer №1

If there is confusion about whether to target #input1 specifically or other inputs as well, consider the following:

To target only #input1, you can use the selector:

#myDiv input#input1 {
  ...
}

If you want to exclude #input1 and target all other inputs instead, utilize the :not() pseudo-class like so:

#myDiv input:not(#input1) {
   ...
}

Both of these selectors have a higher specificity than simply targeting #myDiv input, allowing you to override any previously defined properties.

Answer №2

Issues often arise with exclusions. In this case, you need to specify "any input that is a direct child of #myDiv or any input that is a descendant of a child of #myDiv, excluding .Picker".

#myDiv > input, #myDiv > :not(.Picker) input { ... }

#myDiv > input, #myDiv > :not(.Picker) input { 
  border: 2px solid red;
}
<div id="myDiv">
   <label for="input1">input1: </label><input id="input1" />
   <div class="Picker">
       <label for="input2">input2: </label><input id="input2" />
       <div>
           <label for="input3">input3: </label><input id="input3" />
       </div> 
   </div>
   <div class="Something else">
       <label for="input4">input4: </label><input id="input4" />
       <div>
           <label for="input5">input5: </label><input id="input5" />
       </div> 
   </div>
   ... Other html markup
</div>

Answer №3

To apply CSS properties to an input with the id of input1, you can simply use the selector #input1. The id attribute is unique for each tag, so it will not affect other HTML input tags.

#input1 {

}

If you want to apply CSS to inputs with the ids input1 and input2, you can use either of these methods:

#input1,#input2{

}

.Picker input{

}

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

Clip the text with ellipsis if it exceeds the limit and show the file

Currently working on an upload feature where I need to show the name of the uploaded file. File Name.txt The problem arises when the name is too lengthy, resulting in a display like this: File Name Is Too Long...txt I attempted to solve this by implement ...

unique design for custom scrollbar on dropdown menu

I'm looking to customize the scrollbar for a select box. I've experimented with the code below, but I want to avoid using the default scrollbar on the select box. Instead, I want to implement a custom scrollbar without using the size attribute on ...

Fixed position scrollable tabs with Material UI

I recently implemented material-ui scrollable tabs in my project, referencing the documentation at https://mui.com/material-ui/react-tabs/#scrollable-tabs. Here is a snippet of the code I used: <Tabs value={value} onChange={handleChange ...

Different ways to adjust the appearance of table borders in HTML

I find myself at a standstill, hence why I am reaching out with this question. This Table presents itself with various border sizes and colors, along with similar complexities in the background of its cells. Could anyone provide me with guidance on how t ...

Position <UL> at the center of <DIV>

I've attempted several solutions found on Stack Overflow to center my <UL> element, but nothing seems to work. Can someone please take a look at it? I am looking to center the text "Shop 943 01 ... not available". Thank you in advance. ...

Tips for altering the appearance of the material-ui slider thumb design when it is in a disabled state

Through the use of withStyles, I have successfully customized the style of the Slider: const CustomSlider = withStyles(theme => ({ disabled: { color: theme.palette.primary.main }, thumb: { height: 24, width: 24, }, }))(Slider); How ...

What is the method for positioning a logo image, phone number, and location above the navbar using bootstrap?

I just started learning Bootstrap and I am trying to add a logo image, location image with address, and phone number with text above the navigation bar. I want all these elements to be centered on the page. However, my current attempt is not achieving the ...

Overflow of nested item within flex container

I'm trying to make the content under "foo" fully scrollable, but I've been unsuccessful using flexbox. I attempted it with min-height, yet it didn't work out. Check out this JSFiddle for reference $('.ui.dropdown') .dropd ...

Tips for aligning my icon in the middle when I collapse my sidebar

Seeking assistance on centering the hamburger icon on my sidebar. Currently, when I adjust the width of the sidebar, the icon is not centered at all. Does anyone have a solution for achieving this? I attempted using justify-content: center in the main clas ...

What is the best method for displaying an empty message within a table cell (td) when the ng.for loop has fewer items than the table headers

I have created a code to display data in a table format. However, I am facing an issue where the table headers (#4 and #5) do not show any data (td). How can I implement an empty message for these table headers when there is no corresponding data available ...

What steps can I take to address the problem in iOS 17 where sound is coming from the earpiece instead of the speaker during camera activation?

I have a Progressive Web App where I use the getUserMedia() API to access the camera and HTML audio tags for handling media content. However, ever since updating to iOS 17, I've faced an issue where audio plays through the earpiece instead of the medi ...

Is it feasible for nested div to remain unaffected by bleeding when the container is positioned relatively and has an overflow hidden

Is it possible to bleed a nested div even when the container is positioned relative with hidden overflow? In this case, giving the nested div a fixed position is not an option. Please check out this unique example: http://jsfiddle.net/s7nhw/11/. If you ...

Two variations of identical descriptions within a single div container

Is there a way for me to use an item, specifically an img, within the same div class but with different definitions? For example: section .row img { margin: 0 0 30px; width: 100%; border: 4px solid #18a00e; } section .row img { margin: 0 ...

Remove newline character from HTML code using Python 3.2 and urllib module

After fetching HTML content as a string using urllib, I encountered difficulty in searching the string due to its HTML format. Is there any way to "unformat" the string by removing all the new lines without altering the HTML code itself? Here is the Pytho ...

What is the best way to eliminate the final border in a row that has been applied using the <

I have a table where the tr is styled with a border. I am looking to eliminate the border from the last td in the tr. Here is an example: <table> <thead> <tr> <th>a</th> <th>b</th> <th&g ...

Tips for placing a button on the bottom of a window where the content expands

Seeking a solution to ensure the submit button is always positioned at the bottom of the page. While this can be done using position: absolute, bottom, there's a requirement to address. If the content height is less than the wrapping div height, the ...

Tips for developing a custom function to retrieve information from a WCF Rest service and showcase it in a table

In my code, I have created a function called service.js to get data from a WCF Rest API in JSON format. Then, I implemented another function called entryCtrl.js to fetch the data and display it in HTML. service.js (function (app) { app.service("CRUD_An ...

Exploring the issue: Using Safari, setting a min-height of 100% does not function properly within a flex-grow child

When testing my code in Chrome, Edge, and FireFox, I noticed that the innermost div fills its parent correctly using min-height: 100%. However, Safari does not display the same behavior. The green div is not completely covered by its children as expected. ...

Concealing HTML content from search engines using an htaccess AJAX call

Recently, I created a basic website that utilizes ajax calls to HTML files for display purposes. The issue arises when search engines, such as Google, index the HTML files and users are directed to view them out of context from the main website. It seems l ...

Interactive layout for Tetris created using only HTML and CSS

Currently, I am working on creating a Tetris board using HTML. My goal is to illuminate the block each time it is hovered over. However, I have encountered an issue where the first div lights up both blocks, but the second one does not light up the first b ...