I'm looking for a way to change the color of a specific div out of the 3 I have, but I don't want to use

<div></div>

<div> paragraph</div>

<div>paragraph1</div>

Looking for a solution to change the background color of an empty div using only CSS, without making any changes to the HTML. Can anyone provide assistance with this?

Answer №1

Utilize the following code snippet:

div:nth-child(2)

[HTML]

<div>
  text content
</div>

<div>
  additional content
</div>

[CSS]

div:nth-child(2) {
  background-color: pink;
}

Live Demo Here

Answer №2

Remember that the div element must remain completely empty with no spaces.

<div></div>

div:empty
{
    background-color:blue
}

Answer №3

Give this a shot:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("div:eq(0)").css("color", "blue");
            $("div:eq(1)").css("color", "green");
            $("div:eq(2)").css("color", "purple");
            });
    </script>

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

Tips for displaying backend error messages on the frontend

I am facing an issue with returning error messages from the backend to the frontend in my Angular project. The specific requirement is to display an error message when the value of msisdn is not eligible for renewal. Currently, the hardcoded error message ...

Is there a way to include a text file in HTML for referencing or posting on my website?

Managing a website where my colleagues frequently post updates can be time-consuming as I am the only one knowledgeable in html and css. I find myself constantly formatting their posts and creating new pages for them. I am exploring ways to simplify this ...

Include brand logo to the Bootstrap navigation bar

Following the method provided in Bootstrap documentation, I included the following code: <div class="container"> <a class="navbar-brand" href="#"> <img src="/img/hangers.jpg" alt= ...

Difficulty encountered when trying to obtain div height automatically

For a while now, I've been attempting to dynamically determine the height of an image as the browser window is adjusted. Despite using console.log() to verify my results, I keep getting a value of 0. What could be causing this discrepancy? $(functio ...

Record details to text file after the button is clicked on ASP.NET

My goal is to save an integer value (incremented each time a button is pressed) into a .txt file for each individual. However, I am struggling with how to store the value of each person's button click. I have a method in mind, but I need assistance wi ...

How to customize the active color of the navigation pills in Bootstrap 4

I want to customize the background color of each pill, but I also want a faded version of that custom color with an active color matching the full color of the corresponding pill. pill one > faded red pill two > faded blue pill three > faded bla ...

Arrange two divisions vertically in the footer

Despite being a common question, I am facing difficulties with Bootstrap 4, which may be due to some conflicts. I am trying to create two divs placed one above the other, fixed to the bottom, with text centered in both. Here is the code snippet: ...

`CSS Content Placeholder Issue When Used Alongside JavaScript`

Let me explain a bit, I have a master page named UserProfile.master which has a content placeholder linked to UserProfileWall.aspx. Now, I am trying to incorporate some additional JavaScript and a second CSS file in the userprofilewall page. However, whene ...

I am experiencing a situation where my content is overflowing but the scroll feature is

After making my website dynamic, I encountered an issue where the content overflow on the home page prevented me from scrolling to the bottom of the page. Here is a link to my website for reference: ...

Issues with style not loading properly within innerHTML in Angular2

Currently, I am in the process of developing a page using Angular2/4 that includes a left navigation bar. To achieve reusability, I have separated this left menu into its own component and nested it within the main component. The objective is to utilize th ...

The Jquery click event is not triggering when clicked from a hyperlink reference

I have a specific HTML href in my code snippet: <a id="m_MC_hl6_8" class="no_loaderbox button_link inline_block " href="somelink" target="_self">link</a> Upon clicking this link, a waiting box is displayed on the page. However, I don't ...

Images displaying correctly in Next.js development environment, but failing to load on Github Pages production site

I am currently using Next.js alongside Tailwind CSS and I'm encountering an issue while attempting to set a background image for a specific div. Oddly enough, the image appears to load correctly in development mode, but fails to display when deployed ...

Discovering the position of elements in relation to their (grand^N)parent

Among my possessions are elements A and B. B has the ability to exist as a direct child of A, or there may be anywhere from 0 to N elements separating them. It is imperative that I determine how B is situated in relation to A, specifically noting the dist ...

Is there a barrier I've reached with the amount of hashtags I'm using?

My goal is to limit the use of javascript and rely more on CSS to achieve desired effects on my website. One feature I have developed is a modal that opens using hashtags and targets, similar to this example: http://codepen.io/maccadb7/pen/nbHEg. While th ...

Align the inline text at the center as if it were an inline-block element

Currently working on a design that appears deceptively simple, yet I'm encountering challenges in centering it while keeping the text aligned to the left. https://i.sstatic.net/qhf6p.png In an attempt to have the background span only the actual text ...

Network plot search box

I have utilized the forceNetwork() function from the networkD3 package to construct a Network of protein mutations. It is then displayed in RStudio's "Viewer" pane. After creating this network, I am able to save it as an HTML file for sharing while r ...

Utilizing dynamic CSS classes within an ngFor loop

Struggling with my pure CSS star rating implementation, specifically with the hover effect. When hovering on one rating star, the behavior affects the first or all five stars of the previous question. https://i.sstatic.net/cpjvw.png In the provided image ...

Unable to apply a border to the button

I'm having trouble adding a border to my button on the website. I've tried setting the color, which works fine for the font color within the button, but the border properties are not working. Despite trying some highly rated solutions, none of th ...

I am faced with the puzzling situation where the value of my input type slider is displayed below the slider

I am currently working with HTML code that allows users to select a percentage using an input type range. Here is the code snippet I am using: <div class="container-fluid"> <div class="row"> <div class="c ...

Using HTML5 to implement a progress bar that updates on click

I currently have an HTML file that contains clickable content. <tr onclick="callfn('Afile.jsp?param1=<%= value1%>');" id="contenttr"> The function callfn is located in a JavaScript file that loads images. Because this function can t ...