Choose only the initial element, regardless of its category

Is there a way to apply a specific style exclusively to the very first element within a container, only if it is an h1, h2, or h3? However, if there are multiple headings of different levels on the same page, only the initial one should receive the designated styling.

For example:

<div class="container">
   <h2>I want to modify this</h2>

   <p>....</p>
   <h3>But not this!</h3>
   <h2>and not this!</h2>
</div>

I attempted using the following code:

.container h1:first-of-type, .container h2:first-of-type, .container h3:first-of-type {
    padding-top: 0 !important;
}

The issue arises when both the first H2 and H3 are impacted by this code.

I also tried the following, but unfortunately, it did not work:

.container > h1:first-of-type, .container > h2:first-of-type, .container > h3:first-of-type {
    padding-top: 0 !important;
}

Any suggestions on how I can accomplish this?

Answer №1

Give it another shot, I believe this is what you've been searching for

<!DOCTYPE html>
<html>
<head>
<style>
.container > h1:first-child{
    background-color: yellow;
}
.container > h2:first-child  {
    background-color: orange;
}
.container > h3:first-child {
    background-color: red;
}
</style>
</head>
<body>
<div class="container">
   <h2>I want to modify here</h2>

   <p>....</p>
   <h3>Leave this as it is!</h3>
   <h2>and also keep this one unchanged!</h2>
</div>

</body>
</html>

Answer №2

It may not be achievable in the way you are imagining, but there is a workaround to revert the changes by utilizing the sibling combinator here with the code symbol ~ in various combinations.

The CSS code provided is quite lengthy. To simplify, consider using less code. Take a look at this codepen example.

.container > h1,
.container > h2,
.container > h3 {
  background: red;
}
.container > h1 ~ h1,
.container > h2 ~ h1,
.container > h3 ~ h1,
.container > h1 ~ h2,
.container > h2 ~ h2,
.container > h3 ~ h2,
.container > h1 ~ h3,
.container > h2 ~ h3,
.container > h3 ~ h3 {
  background: transparent;
}
<div class="container">
   <h2>I want to change this</h2>

   <p>...</p>
   <h3>But not this!</h3>
   <h2>and not this!</h2>
</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

The SVG path is not aligned properly with the container's viewbox dimensions

Recently, I came across an icon provided by a plugin I am using called calendar. However, I wanted to enhance my icons set with a new addition - the twitter icon. svg { display: inline-block; height: 16px; width: 16px; margin: 2px 0; ...

Creating mobile-friendly websites for a variety of devices, including smartphones and tablets

Creating a prototype webpage for my friend, one crucial aspect is ensuring it looks good on different devices. Utilizing Bootstrap, I've implemented vertical collapsing for certain elements, but I'm struggling with how to adjust other components ...

Stopping horizontal scrolling in Material-UI Autocomplete/Popper: A troubleshooting guide

I am currently working on incorporating the Material-UI Autocomplete component, and my goal is to have each option label show an icon along with some text. The challenge I'm facing is that I only want the popper element to be the width of the text inp ...

Tips for creating a mobile-friendly Bootstrap carousel that is both scalable and zoomable while maintaining the default slide functionality

I have a website using Bootstrap 4.x carousel. The layout is user-scalable with the following viewport meta tag: <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2, shrink-to-fit=no">. Currently, users can pinch to z ...

Why won't my sticky element function properly with the position attribute set to sticky?

Hey there! I've been diving into learning all about CSS positions and have nailed down most of them. However, for some reason, the sticky position just won't cooperate and is stubbornly acting like a relative one instead. Check out my HTML snipp ...

Creating a custom design for ng-bootstrap accordion using CSS styling

I've encountered an issue with my Angular 2 component that utilizes an accordion from ng-bootstrap. While the functionality works perfectly, I'm facing a problem with applying custom styles using the .card, .card-header, and .card-block classes o ...

The HTML footer designed with bootstrap is not behaving as expected. It is not staying fixed at the bottom of the page and is not displaying at full width despite my coding efforts

Working on a small Sinatra app with bootstrap for layout, I'm encountering some issues. The layout is not sticking to the bottom and not displaying at 100% width as intended. Additionally, the footer text is not centering even after applying the text ...

Tips for ensuring the Search button always remains alongside the input bar during resizing with percentages

I've been trying to figure out why the search button is still showing at the bottom left and creating an "l" shape even though I'm using style="overflow: hidden; padding-right: .5em;". Does anyone have any ideas as to what might be causing this i ...

Can someone please help me figure out why the "setInterval" function in my script isn't functioning as expected?

I've been experimenting with controlling the refresh rate of drawn objects in a canvas using JavaScript. Even after going through the tutorials and examples on w3.school, I'm still unsure why the "setInterval" function is not executing the "gener ...

Tips for Placing a Div in a Precise Location

I'm currently working with Bootstrap and I'm attempting to replicate a layout similar to the one used by Twitter, as shown in the screenshot below. The specific part I'm struggling with is where the profile picture is located. I want to add ...

Is there a way to lock an image in place within a fancybox popup as I scroll within the popup?

I am working on a gallery that uses fancybox popup functionality. My goal is to display an image on the left side and accompanying text on the right side within the Fancybox Popup window. If the text happens to be too lengthy, I would like users to be able ...

What is the best way to decrease the font size of every element in the DOM?

Is there a way to decrease the size of all DOM elements by a specific amount? Currently, I am using Bootstrap styles where h5 is set at 14px and I need it to be 12px, and h1 is at 36px when I want it to be 34px, and so forth. I have considered two option ...

Creating a unique look for unordered list items in a dropdown navigation

I'm currently working on creating a drop-down menu using nested unordered lists. The functionality of the menu is all set, but I'm running into some styling issues. The main link that triggers the drop-down should have a blue background with whit ...

The el-dialog is functioning properly, however, I am looking to add CSS that will animate it to open

I've set up a dialog to open on button click, and it's functioning correctly. My goal is to have the dialog open from the bottom with a height of 300px, show the contents inside it, and then hide when I click outside or inside any element. Here ...

What steps can I take to avoid scaffold.css from taking precedence over my custom CSS?

I'm having trouble with this. It seems like a small issue, but I'm still very new to learning about rails. Any help you can provide would be greatly appreciated! Solution: To resolve the problem, I accessed the file responsible for adding head ...

Creating a Design That Works Well on Both Desktop and Mobile Devices

I have created this design using CSS and Bootstrap. However, I am encountering an issue with responsiveness. Can someone assist me in making this design responsive? HTML: .ordering .spanone { position: absolute; width: 100px; left: 43%; right: ...

Centered text in <td> with CSS aligned to the right

https://i.sstatic.net/UrB5f.jpg Is there a way to align the 3 input fields so that their right edges are all vertical? I still want the text+Input to be centered within the column but aligned vertically... Using Bootstrap 3.3.7 HTML: <link href="h ...

What is the reason that this particular JQuery code is malfunctioning in the IE browser once integrated into my website?

Currently, I am utilizing the DDCharts jQuery plugin from DDCharts JQuery to incorporate some charts into my website. After downloading the plugin and testing it in various browsers, I encountered an issue specifically with Internet Explorer 8+. Strangely, ...

I am having trouble viewing elements located below a div that I created using CSS

Currently, I am working on creating a portfolio page where the initial page opens with a height of '100vh' and width of '100%', which seems to be functioning correctly. However, I am facing an issue where I am unable to scroll down to v ...

trouble with react-table's default height and flexible design

I am facing an issue with my React table configuration. I have set up scrollable columns and header following a similar example here: https://codesandbox.io/s/kowzlm5jp7?file=/index.js:281-310 In the table styles, I have specified a height like this: ...