Having trouble with restricting ::before to only one specific h2 element

This is a simplified version of my code, focusing on the essential information.

<div class="container">
<div class="newsletter">
    <div class="newsletter_title">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-5 col-lg-4 col-xl-3 col">
                <h2 class=".newsletter .newsletter_title">
                <span>Newsletter</span>
                </h2>
            </div>
        </div>  
        <div class="row">
            <div class=" col-xs-12 col-sm-7 col-md-7 col-lg-8 col-xl-9 col">
                <div id="newsletter_embed_signup_scroll">
                    <div class="col-xs-12 col-md-12 col-lg-6 col-xl-5">
                        <h2 id="newsletter_embed_signup">Subscribe to our mailing list</h2>
                        <h3 id="newsletter_embed_signup">(And get a 10% voucher)</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Looking closely at the HTML structure, you'll notice that both <h2> and <h3> elements are assigned unique classes and IDs. In the CSS file, the style rule

.newsletter .newsletter_title h2::before
impacts both elements because they share a common container. Is there something I might have overlooked in this setup?

Answer №1

AndrewL64's response is spot-on. I just wanted to mention that it's not a good practice to use the same ID multiple times as shown in the following example:

<h2 id="newsletter_embed_signup">Subscribe to our mailing list</h2>
<h3 id="newsletter_embed_signup">(And get a 10% voucher)</h3>

Otherwise, you'll encounter more problems down the line, so opt for using classes instead.

Answer №2

If you want to style the first h2, make sure to include the class name "newsletter" of the first h2 in your selector like this:

.newsletter .newsletter_title h2.newsletter::before {
  /* add your styles here */
}

To target the second h2, use the h2's id "newsletter_embed_signup" in your selector like this:

#newsletter_embed_signup::before {
  /* add your styles here */
}

You can also apply the same method for styling your h3 elements.


Remember, unlike classes, IDs should be unique and each ID name can only be used once.

Make sure to either give a common class name to a group of elements or assign unique IDs to each element to avoid any conflicts.

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

What's the best choice for ASP.NET: Using CSS or Themes?

What are the differences between using ASP.NET Themes and CSS for web design? When is it most beneficial to use each approach, and what are the advantages and disadvantages of one over the other? ...

What is the best way to reveal the menu options by tapping on the top left icon?

Seeking guidance on how to activate the menu by clicking the top left corner icon. A sample code setup is available in Codepen through this link: https://codepen.io/RSH87/pen/rmgYbo. I require assistance with incorporating the JavaScript file into my React ...

What is the best way to position a div vertically on the right side of another div?

I've been experimenting with different padding and margins to try and position the div below the dotted div. My goal is to have the div that's currently at the bottom appear vertically aligned to the right of the other div, similar to the layout ...

Error encountered in PHP due to a spacing issue in the GET

<?php $current_subject = $_GET['subject_id']; $current_content = $_GET['note_id']; echo "<form method=\"post\" action=mainpage.php?subject_id=".$current_subject."&note_id=".$current_content.">"; ?> <in ...

Is there a way to have the inline form appear on the same line as the buttons?

Currently, I am working on developing an app using Bootstrap 4. In my layout, I have 3 buttons and an inline form. However, the form is displaying below the buttons instead of being in the same line. Can anyone provide guidance on how I can ensure that a ...

Is it possible to alter the font size in Vuetify depending on the viewport size?

Looking to customize font sizes based on viewports? <v-card-text style="font-size:5em"> Some Heading Here </v-card-text> If you want to change the font size to 3em on XS view, without duplicating code, consider using CSS only. Avoid dupli ...

navigate to a different section on the page with a series of visuals preceding it

When I try to navigate to a specific dom element on another page, the page initially works fine but then jumps to somewhere before that element. I believe this issue occurs because the page calculates the position before images load, and once the images ar ...

Overlaying diagonal lines/textures on a gradient or image background

Is it possible to achieve a similar texture/fill effect using only CSS? I am looking to overlay diagonal lines on top of various containers with different background fills, and I would prefer to avoid creating patterns as images if possible. Do you have a ...

HTML - various incorrect horizontal alignments

I'm having trouble getting the site logo, site name, and site header to align horizontally on my website. They keep ending up in different places.https://i.stack.imgur.com/5yL5f.jpg I'd like it to look similar to this: https://i.stack.imgur.com/ ...

Steps to customize the default thumbnail image of an embedded video that is not from YouTube

I've added a video to an HTML page. Here is the code: <div> <video width="400" controls> <source src="videos/myvideo.mp4" type="video/mp4"> </video> </div> Is there a way to modify the default thumbnail image of ...

Container for grid template columns and responsive window in a single row

Imagine having around 250 divs with the class slider-item styled in a certain way. You have a responsive grid in CSS called A which arranges these divs as columns/items when the window resizes, with a minimum item width of 240px. Check out how it looks bel ...

Dynamic Design with Pure CSS

I am in the process of creating a flexible layout design. Everything appears to be functioning as intended on FF, Chrome, Safari and IE8 However, I have encountered an issue with IE7. It seems to be related to the floated containers. I have attempted a fe ...

Conceal the initial element featuring an image

Given that the post-body includes various elements, such as links and divs, I am interested in concealing only the first child element that contains an image. <div class="post-body"> <div class="separator"><img/></div> </div&g ...

Update the code-behind for the Joomla "submit article" page

After incorporating a new template on my Joomla website, I encountered an issue with the "submit an article" page. The fields in the publishing tab were altered to width: 0px;. Here is how the HTML for the category dropdown appears: <div id="jform_cati ...

How do I center an SVG vertically inside a button using Bootstrap 5?

Is there a way to center the SVG vertically within the button while using Bootstrap for styling? <a href="#home" class="navbar-home"> <button type="button" class="btn btn-outline-warning"> & ...

Two elements occupy the rest of the vertical space

In the center, there is a div with content inside. I want the top and bottom divs to occupy the remaining space. Similar to this example <div class="container"> <div class="top"> </div> <div class="middle"> C ...

Passing data from the <ion-content> element to the <ion-footer> element within a single HTML file using Ionic 2

In my Ionic 2 html page, I have a setup where ion-slide elements are generated using *ngFor. I am seeking a way to transfer the data from ngFor to the footer section within the same page. <ion-content> <ion-slides> <ion-slide *n ...

Collect the text shown in an alert message created with JavaScript

I'm currently unsure if this is achievable or not. In one of my scenarios, I aim to extract text that appears in alert boxes on external websites by including my JavaScript file. Is there a method to obtain the text content shown in an alert message ...

Meteor - Utilizing If Statements in HTML Documents

Currently, I am utilizing an FS Collection named "MyUploads" for storing user-uploaded files. Within the HTML structure, there exists a submit button that needs to be displayed conditionally. The question at hand is: how can I create an if statement so tha ...

The area available for clicking does not align correctly with the visible display of the div element

Is it possible to create clickable, animating sprites without relying on Flash? I want the sprites to show a bubble popup when clicked or touched, and then dismiss when the screen is clicked again. However, I'm facing an issue where the clickable area ...