Applying CSS exclusively to direct descendants within a specific class:

I have a div and I would like to create another div inside it with a different class, ensuring that the inner div is completely independent from the CSS settings of the outer div.

Here is an example:

<div class="outer">
    <div><h1> h1 </h1><div>
    <div class="inner">
        <h2> h2 </h2>
    </div>
    <h2> h2 </h2>
</div>
.outer h1{ color:blue; }
.outer h2{ color:red; }

.inner { height: 111px; }

My goal is to remove the red color specifically from the h2 within the "inner" div.

It may appear simple to just override the color to black, but what if there are numerous other CSS properties or if they are dynamic.

Edit: It seems there is some confusion, but what I actually need is somewhat contrary to the responses received so far. I have a main container div with extensive CSS settings. Now, I wish to add a div inside the main container without inheriting any of its CSS properties.

Answer №1

.container > h2 { color:red; }

By applying this style, only the immediate child of the container div will have the specified color, which should resolve the issue.

Answer №2

.inner-container * { color: #000; }

Applies the color black to all elements inside the inner container.

Check out the demo here

Answer №3

It seems like the solution you're looking for is

.inner h2 {color: inherit}

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

Create a copy of a div element once the value of a select element has

After modifying a select option, I'm attempting to replicate a div similar to the example shown in this link: http://jsfiddle.net/ranell/mN6nm/5/ However, instead of my expected lists, I am seeing [object]. Any suggestions on how to resolve this issue ...

"Oops! Your session has timed out because of inactivity. Just give it a little refresh and try logging in again." - error message displayed on respons

Everything seems to be going smoothly with my project, however, when I run it locally on an emulator to test mobile responsiveness and attempt to log in, I encounter the following error message: The page has expired due to inactivity. Please refresh and ...

What is the best way to control and manipulate this parallax effect?

Having trouble getting the icon dog to appear correctly in this parallax effect. I want the full body of the dog icon to be displayed over the section, but I'm having no luck so far. Please see my code below and let me know if there are any correction ...

The Github page is continuously loading without displaying any content

Recently, I created a web-map application using the OpenLayers library and published it on Github Pages. You can access it through this link: . However, when I try to load the page, the content doesn't show up and it just keeps loading indefinitely. ...

Challenges with JavaScript calculations on dynamic HTML forms

Currently, I am immersed in a project focused on creating invoices. My progress so far involves a dynamic form where users can add and remove rows (as shown in the snippet below). I'm encountering an issue with my JavaScript and I could use some ass ...

Getting image source and description from HTML data with Jsoup: A step-by-step guide

In an effort to extract image and description information from atom feeds using the ROME API, I encountered a challenge. The atom feed provides a content attribute that includes an image and article description. To access the desired information, you can u ...

Triggering a delayed onkeypress event in JavaScript

In my HTML form, I have an optional input field for a phone number, as well as two radio buttons (Leave Message, Don't Leave Message): <input type="text" class="input-medium phoneNum2" name="secondaryPhone" id="se ...

Tips on customizing the size of tooltips within ui-select-choices

While working on a small website for a homework assignment, I have encountered an issue with styling the size of a tooltip. The tooltip is part of an attribute in a span tag under the ui-select-choices. I only want to customize the style of the tooltip w ...

Automate web tasks with C# WebBrowser

Currently, I am in the initial phases of developing a system to automate the process of data extraction and collection from a website. I have a massive CSV file containing 16,000 lines of data. My goal is to input data from each line into a textarea on a w ...

Expand the background of columns to cover grid gutters

Imagine this... You're working on a layout with three columns, all within a fixed-sized container that is centered using margin:0 auto. The design requires the first column to have a background color that reaches the left edge of the browser window. ...

Alert appearing on screen with an option to dismiss it by clicking a button

I would like to implement an "X" button in the top right corner of the message so that users can easily close it. I could really use some assistance with this as my JavaScript skills are a bit lacking. Below is the HTML code: <div class="warning">T ...

CSS for the footer at the bottom of the page

Check out this fiddle where I am trying to create a footer that remains at the bottom of the page, similar to the screenshot provided: However, I'm facing an issue where when the browser window is resized so that the viewport is smaller than the cont ...

increasing the size of an element while keeping its overflow hidden

Is it possible to expand an element with "overflow: hidden" when clicked without affecting other elements? I'm looking for a solution that allows the expanded element to overlap and hide the element below it, rather than pushing it down. I've tr ...

Display a full-size image as the background of the modal

I am working with Materialize CSS and I encountered an issue with setting the background of a modal to display just one image without repetition. The goal is to create a large image with a blurred webpage background effect when the modal opens. However, cu ...

Incorporating user input into a div element

So, I'm in the process of building my own Task Tracker using JavaScript to enhance my skills, but I've hit a roadblock. I successfully implemented adding a new div with user-inputted tasks, however, there's no styling involved. <div cla ...

Adjusting the thickness of the CSS cursor line upon entering an input field

Having an image as the background for an input field has been causing issues for me. Although I can adjust the line-height and font-size easily, the cursor line appears outside of the background image when clicking inside the input field. Is there a speci ...

Open the overflow div within a table data cell

My goal is to create a drag-and-drop schedule tool similar to Fullcalendar. I have decided to use a table layout with a rectangular div inside each TD cell to represent tasks. <table onDragEnd={dragend}> <tr> <th>0</th> ...

Is there a way to make my flex containers wrap to a new line when they overflow?

I'm facing an issue on my website where the search results are displayed in card format. However, when there are multiple results, instead of overflowing to the next line, the cards stay on the same line and shrink in size, making the content inside t ...

Display HTML content using AJAX within a div element

Within my HTML code, I have the following: <li class="register"><a href="">Registreer</a></li> as well as <div id="content"> </div> I attempted to load an HTML file into the div using the code below in the header se ...

particular shade for button border and background

I'm looking to create a button using Material UI that has a specific design. Right now, I've only been able to achieve this: https://i.stack.imgur.com/xvv7s.png Here is the code I am currently using. If anyone can help me identify what I'm ...