Using CSS to target the first element of a type that is not a sibling

Here is my current code structure:

<div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 2</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 2</p>
</div>
</div>

I am attempting to use the first-of-type selector to hide the first h4 tag under each combination of selectors: .title.blue and .title.green.

My belief is that this method is not working due to the fact that the elements are not siblings. Is there a way to still utilize the first-of-type selector in this scenario?

You can find an example pen here

h4.title.blue:first-of-type {
  display: none;
}
<div>
  <div class="container">
    <div class="title-container">
      <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 1</p>
  </div>
  <div class="container">
    <div class="title-container">
      <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 2</p>
  </div>
  <div class="container">
    <div class="title-container">
      <h4 class="title green">green</h4>
    </div>
    <p>Something green no 1</p>
  </div>
  <div class="container">
    <div class="title-container">
      <h4 class="title green">green</h4>
    </div>
    <p>Something green no 2</p>
  </div>
</div>

Answer №1

To make the first blue and first green elements disappear, you will need to make changes to both your html and CSS. Refer to the example provided below:

h4.title:first-child {
  display: none;
}
<div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 2</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 2</p>
</div>
</div>

The example above shows that using the first-child selector makes each <h4> tag disappear because they are not on the same level. See the illustration below:

- container
 - title container
  - h4
 - </title container
- </container
- container
 - title container
  - h4
 - </title container
- </container>
- container
 - title container
  - h4
 - </title container
- </container>
- container
 - title container
  - h4
 - </title container
- </container>

As each <h4> tag is in a new container, applying CSS like this:

.container .title-container h4.title:first-child {
  display: none;
}

would hide the first <h4> tag in each container since every container only has one.

To solve this issue, try the following approach:

.container .title-container h4.title:first-child {
  display: none;
}
<div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue 1</h4>
        <p>Something blue no 1</p>
        <h4 class="title blue">blue 2</h4>
        <p>Something blue no 2</p>
    </div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green 1</h4>
        <p>Something green no 1</p>
        <h4 class="title green">green 2</h4>
        <p>Something green no 2</p>
</div>
</div>

I hope this explanation helps!

Answer №2

h4.title:first-of-type {
  display: none;
}
<div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 2</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 2</p>
</div>
</div>

You need to make changes in the CSS style

h4.title:first-of-type {
  display: none;
}

Answer №3

In an attempt to utilize the first-of-type selector, I am aiming to conceal the initial h4 tag within either the combined selectors .title.blue and .title.green.

You're on the right track, but not quite there yet.

To achieve this, you will need to apply :nth-of-type to an element that is higher up in the DOM hierarchy.

Here's a Functional Example:

div > div:nth-of-type(odd) > div h4 {
display: none;
}
<div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title blue">blue</h4>
    </div>
    <p>Something blue no 2</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 1</p>
</div>
<div class="container">
    <div class="title-container">
        <h4 class="title green">green</h4>
    </div>
    <p>Something green no 2</p>
</div>
</div>

Answer №4

In the end, I discovered a different approach to solve this. Despite not being able to alter any markup, I managed to rearrange CSS classes.

My selector was

.container > .class-to-hide ~ .class-to-hide
. This allowed me to target all elements except for the first one with the class .class-to-hide.

You can view a demonstration of this solution on this code pen

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

Website design with a central vertical alignment and a subtle shadow effect

Yesterday I inquired about a website design featuring a shadow, but I have since created a better example and would like to pose the question again. My requirements are as follows: A vertically centered website; A shadow on the content block which may h ...

Styling Images Inside a DIV Container Using CSS Float

Considering that I have 2 divs with images and text, I encountered some formatting issues when attempting to float the image to the left using float:left;. The strange formatting is ruining the layout. Below, I've included my code snippet along with a ...

Smooth scrolling feature for Wordpress websites

I am currently in the process of converting an HTML5 template into a Wordpress theme. One specific feature I'd like to add is a custom scroll for Wordpress using the code: <script src="jquery.nicescroll.js"></script> DEPENDENCIES This cod ...

I encountered an issue where the font awesome icons failed to load on my website

My website is experiencing an issue where all the Font Awesome icons are not displaying despite having added Font Awesome. Here is the link to my site: https://i.stack.imgur.com/RcNtv.png .login-top .lg-in::before { font-family: "Fontawesome"; ...

Creating a new grid row when changing the order of elements in the grid column arrangement

Dealing with HTML that is not under my control, I am attempting to rearrange the elements using CSS grid. Despite setting grid-template-rows: 1fr, the layout shows 2 rows with 3 columns each instead of a single row with 3 columns. .grid-container { b ...

Customizing Javascript for Mouse Exiting Browser Window

I have implemented a JavaScript function on my website to display a lightbox when the visitor's mouse goes beyond the browser window. You can check it out here: [http://mudchallenger.com/index-test2.html][1] However, there seems to be an issue where ...

CSS: The hiding should only apply to specific sections of my menu

I'm having trouble hiding a portion of my menu. Whenever I use the display:none property, the entire menu disappears. I have assigned IDs to different elements to distinguish them, so I'm not sure why this is happening. Here's the code snipp ...

Having trouble with overriding an @media query for material-ui react components?

I've been trying to modify a @media CSS rule on a Material UI component, similar to the discussions on How to over-ride an @media css for a material-ui react component and Override components like MuiTab that use media queries. However, I have not bee ...

Extract the entire div including all its elements and then transmit it using the PHP mail function

Currently, I am developing a feature that allows users to send an email. The email should include only one div from the page which contains elements added using the jQuery drag and clone function. I am unsure how to copy the entire div along with its child ...

Ways to transfer information from HTML form components to the Angular class

I am currently working with angular 9 and I have a requirement to connect data entered in HTML forms to the corresponding fields in my Angular class. Below is the structure of my Angular class: export interface MyData { field1: string, textArea1 ...

Dynamic graphic with integrated navigation menu

I'm looking to achieve a similar design concept like the one on this website: The navigation bar should remain fixed at the bottom of the window until you start scrolling. I am unsure if this can be done using just CSS, but I have made some progress ...

Retrieve base64 encoded data from several content attributes

Is there a way to optimize the use of base64 data in CSS by defining it once and referencing it in multiple content properties? div#my_id1 { content: url(data:image/png;base64,...); } div#my_id2 { content: url(data:image/png;base64,...); } Using ...

Unexpected behavior of Bootstrap columns within nested rows, occurring between the small and extra-small breakpoints

Currently, I am in the process of developing a small website using Bootstrap 5. My goal is to have 6 elements displayed on two lines for breakpoints greater than or equal to md and on a single line for smaller breakpoints. It all seems to be working fine f ...

Guide for creating a scroll-triggered rotation animation using only JavaScript

Looking to achieve a cool scroll effect with an image that rotates on the X-axis by a specific degree, such as 70deg. The goal is to have the image's rotateX value change to 0deg when it enters the viewport upon scrolling and revert back to 70deg whe ...

I'm looking to incorporate XML data into table cells using JQuery. How can I go about creating this table?

I am currently working on creating a 16x4 table in JQuery for a "Meet the Staff" page that will feature information on 16 employees. Each row of the table will contain details such as the employee's name, image, position in the company, and a brief de ...

What is the best way to ensure the meta value is updated to reflect exactly what is displayed on the

I attempted to change a meta value on the WordPress frontend using the code below: function code_form() { $content = ''; $content .= '<h2> code </h2>'; $content .='<form method="post">'; $content .= ...

Tips for generating a JSON string with nested structure

Can someone assist me in generating a JSON based on the HTML elements provided below? { "AppName": "ERP", "ModuleDesc": [ { "Name": "Payroll", "AcCESSRIGHTS": [ { "Create": "Y", "Retrive": "Y", "U ...

Building a sub-navigation menu within the main navigation menu

Can anyone help me create a submenu in my navigation menu? My CSS knowledge is limited and I'm encountering some issues. When I add a submenu to the nav menu, it displays as ul li directly. #cssmenu { background: #88BC18; width: auto; z-index ...

What is the best way to update a stylesheet dynamically within an Angular project?

I have been attempting to modify my bootstrap.css file in order to change the theme of my application: $scope.changeStyle = function (style) { $scope.myStyle = style + '/bootstrap.min.css'; $scope.$apply(); } The 'myStyle' ele ...

Tips for creating a puzzling effect with shadow as you move

When I work on my jigsaw puzzle game, I encounter an issue where the shadow of the parts moves with them when I try to scroll or move them. The problem looks like the image linked here: https://i.sstatic.net/3xhwY.jpg I attempted to use the code canvas.c ...