An H1 tag that has a class will take on the attributes of the h1 heading

During my CSS practice, I encountered some confusion regarding Class Selectors. Specifically, I had styled a H1 tag and then applied additional styles to a H1 tag with a class. To my surprise, the H1 tag with a class was inheriting properties from the original H1 tag. This behavior left me perplexed. How can a tagged element inherit properties from another tagged element with the same tag but without a class? Is there a way to prevent this inheritance of styles?

<html>
<head>
<style>
/*styling for h1 tag.*/
h1
{
text-align:center;
color:yellow;
}
h1.class1
{
color:blue;
font-size:30px;
}
h2.class1
{
color:purple;
font-size:25px;
}
h3.class1
{
color:red;
font-size:15px;
}
</style>
<body>
<h1>C.S.S. Class Selector with different tags.</h1>
<hr>
<p>In this example you will see different level of headings with different styles but with same class.</p>
<h1 class="class1">
I am a H1 heading and I have class1 as a class.
</h1>
<h2 class="class1">
I am a H2 heading and I also have class1 as a class.
</h2>
<h3 class="class1">
I am a H3 heading and I also have class1 as a class.
</h3>
</body>
</html>

Answer №1

Cascading Style Sheets (CSS) operates in a cascading manner, where the initial declaration takes precedence until a subsequent declaration supersedes it. For instance,

h1 {
   text-align: center;
   color: red;
}

In this case, all <h1> elements will be red in color and centrally aligned. However, if you assign a specific class to an <h1>, you can override the above styles as demonstrated below:

h1.class1 {
    text-align: right;
    color: blue;
}

This indicates that <h1> elements with the class class1 will not inherit the red color or central alignment; instead, they will be right-aligned and appear in blue.

If the text-align property is omitted in the class1 definition, <h1> elements with the class1 class will adopt the alignment specified for generic <h1> elements, resulting in central alignment. To enforce a different alignment, you must explicitly override the initial declaration.

Answer №2

Even though the h1 tag remains a h1-tag, whether it has a class or not.

If there is a class assigned to the h1, then it will have some slight differences compared to those without. In this scenario, you would write CSS like this for an h1 with a class:

h1.class1
{
color:blue;
font-size:30px;
text-align:left;
}

Alternatively, you could write:

h1.class1
{
color:blue;
font-size:30px;
text-align:inherit;
}

To ensure that the h1 with a class floats in the same way as other text.

Answer №3

To customize the styling of a specific h1 element, make sure to apply your CSS changes using the class "class1" instead of directly targeting all h1 elements with the .h1 selector.

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

Discover the absent style attributes within an HTML code in C# and incorporate them

Currently facing a challenging situation where I have a C# string with html tags: string strHTML = "<span style="font-size: 10px;">Hi This is just a section of html text.</span><span style="font-family: 'Verdana'; font-size: 10px; ...

In terms of professional design, which is the better choice - Bootstrap or Foundation?

Greetings! I am in the process of creating a website with a professional design. I'm trying to decide between using Bootstrap 3 or Foundation 5 for my CSS framework. Which one do you recommend for designing purposes? Is there another CSS framework t ...

Adjust the number of columns displayed when rendering with Datatables

I've utilized DataTables to create a dynamic datatable. $('table').DataTable( { dom: 'Bfrtip', buttons: [ 'copy', 'excel', 'print', ], responsive: true } The table I created c ...

You are unable to extract data from an HTML table

I have been encountering an issue while trying to extract data from an HTML table. Every time I attempt to do so, I am faced with an ERROR Message 13 "Type Mismatch". I suspect that the problem lies in my use of incorrect HTML tags. Despite spending severa ...

Tips for choosing an image using jQuery from an external HTML page

I'm attempting to embed an HTML page within a div and then individually select all the img tags within that page to display an overlay div on top of the images. Currently, I can insert the HTML into a div named "asd" and it seems like the jQuery is f ...

What is the best way to vertically align Bootstrap Columns to ensure they have consistent heights?

I'm currently in the process of building a website that features multiple images and content organized into columns using Bootstrap as the framework. I'm wondering if there's a way to automatically adjust all the columns to the height of th ...

"Fixing index.html with the power of Bootstrap 4: A step-by

*I am facing an issue where the default bootstrap navbar style appears before my custom styles get applied every time I reload the page. Any assistance would be greatly appreciated* - Whenever I reload the page, the default bootstrap navbar style appears ...

How to Use Absolute Positioning in Bootstrap 3.0 to Position Child Divs

I have a div block that needs to always be displayed at the bottom of the section, regardless of whether the section above is empty. The inner div should stay at the bottom of the section, as shown in the attached image. https://i.sstatic.net/cor6k.jpg T ...

Transparent gap separating div and border with curvature

I am attempting to achieve a specific look for my button: I have experimented with using outline, but unfortunately I am unable to apply border radius to the outline. I have also tried using a box shadow with a white border, but I need the border to be se ...

How can I implement a Mouse Over event listener for a FlexTable in GWT 1.7?

What is the best method to attach an event listener or handler to widgets in GWT 1.7? I have looked at similar questions on SO, but they appear to be outdated. How can I add a Hover listener to a FlexTable, disregarding the existing :hover in CSS? ...

Issue with displaying list items in Ajax Accordion with CSS

I have implemented an accordion control extender on my webpage, and it is functioning properly. I added a CSS file to display it as a list, which is working perfectly in all browsers except for IE compatibility view. In IE compatibility view, the list-styl ...

Bootstrap 5 - Dropdown menu - Links unresponsive to user interaction

Often, I want to incorporate a Bootstrap 5 dropdown menu into my project, but I encountered an unexpected issue. The menu opens correctly, but when I hover over the menu items, the hover effect is not triggered, and clicking on a link does not work. It ap ...

Retrieve the input value from a Bootstrap Form Input

When using a Bootstrap Form Input to allow the user to enter text, I am wondering how to save that text and use it as a parameter in a function. Below is the HTML snippet I have: <!--Directory Input Form--> <div class="row"> <div class= ...

Is it time to swap out those divs for tables in your email template?

I have set up my template like this and I am questioning whether I should replace every div with a table. Let's start with the first div in the example. Do I really need to create a new table element, followed by tr and td tags just for a banner? I u ...

Could jQuery and CSS be utilized for image enlargement?

Is it possible for jQuery (or CSS!) to detect an area around the mouse when hovering over an image, capture the image underneath, and then zoom in on that area? This concept differs from typical jQuery zoom effects where there are two separate images - sm ...

The height of the footer element is gradually dwindling

Have you ever wondered why a footer element mysteriously shrinks in height when the browser window is resized? I attempted to create a fiddle with my code, but unfortunately, it's not replicable on JSFiddle so I won't be sharing it. This is how ...

Each time I load the page, it displays differently depending on the browser. I'm struggling to understand the reason

Trying to figure out how to make the navigation bar close when a link is clicked and directed to a section instead of a new page. When on a large screen, I want the nav bar to remain open but automatically close when on a small screen (for example, when th ...

Unnecessary PHP code will run on its own in JavaScript

Attempting to initiate a session in PHP and assign a value to a session variable within a Javascript function on a PHP/HTML page is creating an issue. The problem arises when the PHP code inside the if statement runs automatically upon loading the page, ra ...

Align three out of seven items in the navbar to the right using Bootstrap 4.1

Bootstrap 4.1 offers a great way to create a Navbar with multiple menu items, but I'm struggling to right justify three specific menu items within the Navbar. I've tried different methods, including using align-self-end, justify-content-end, and ...

PHP and MySQLi for inserting records into database

Currently, I am attempting to retrieve user input values from a form and store them in a database using PHP. Below is the contents of my dbConfig file: <?php $mysqli = new mysqli("localhost", "root", "password", "testDB"); /* checking connecti ...