Is there a way to apply multiple colors to a h1 tag in HTML?

I need help with adding multicolor to a heading on my website using CSS. I am trying to define colors using the span element with different classes for each text section. However, the colors are not showing up as expected and there seems to be spacing issues between the span tags when rendered.

View current HTML and output here

I attempted to remove the styling for the h1 tag, thinking it might be causing the problem, but that did not resolve the issue.

Oops, forgot to include the actual code:

Here is the HTML snippet:

<!DOCTYPE html>

<head>
    <title>
        Why are you here?
    </title>
    <link rel="stylesheet" href="Akari.css">
</head>

<html>
    <body>
        <div>
            <h1>
            <span class="colorRed">
                Ak
            </span>
            <span color="colorDarkRed">
                 a
            </span>
            <span class="colorDarkOrange">
                ri -
            </span>
            <span class="colorRed">
                Play
            </span>
            <span class="colorDarkRed">
                st
            </span>
            <span class="colorGreen">
                ation
            </span>
            <span class="colorDarkOrange">
                3 V
            </span>
            <span class="colorOrange">
                SH
            </span>
            <span class="colorOrange">
                Me
            </span>
            <span class="colorYellow">
                nu
            </span>
            </h1>>
            
        </div>
        <div class="main">
            <p>
                test
            </p>
        </div>
    </body>


    <div class="navbar">
        <a href="../index.html">
           Back to home
        </a>
    </div>
</html>

And the corresponding CSS:

body
        {
            margin: 0;
            background-color: rgb(0, 0, 0);
            background-image: url(../images/channels4_banner.jpg);
            background-repeat: no-repeat;
            background-size: cover;
            color: white;
        }
        a:link
        {
            color: rgb(126, 70, 156);
            text-decoration: none
 
        }
     a:visited
        {
            color: rgb(126, 70, 156);
        }
     a:hover
        {
            color: rgb(255, 255, 255);
        }
     a:active
        {
            color: purple;
        }

    .navbar
        {
            background-color: rgb(52, 51, 51);
            overflow: hidden;
            position: fixed;
            bottom: 0;
            width: 100%;
            border-radius: 10px;
            opacity: 90%;
        }
    .navbar a 
        {
            float: left;
            display: block;
            color: rgb(126, 70, 156);
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 17px;
        }
    .navbar a:hover 
        {
            background-color: grey;
            color: rgb(55, 37, 58);
            transition: background-color 1s,
                color 1s
        }
    .navbar a.active
        {
            color: white;
        }

h1 
            {
              background-color:  rgb(126, 70, 156);
              opacity: 40%;
            }
colorRed
            {
                color:red;
            }
colorDarkRed
            {
                color:rgb(169, 4, 4);
            }
colorGreen
            {
                color:green;
            }
colorDarkOrange
            {
                color:rgb(162, 107, 4);
            }
colorOrange
            {
                color:orange;
            }
colorYellow
            {
                color:yellow;
            }


main
            {
                
            }

The styling within the main class in the CSS file has been intentionally left blank until we resolve the heading color issue.

I managed to fix the spacing problem, but the coloring is still not working correctly. See image description here

Answer №1

Spacing Error

The <span> tags are causing a spacing issue due to being on separate lines, resulting in spaces between the letters in your <h1>.

Instead of:

<span class="colorRed">
  Ak
</span>
<span color="colorDarkRed">
  a
</span>
<span class="colorDarkOrange">
  ri -
</span>
<span class="colorRed">
  Play
</span>
<span class="colorDarkRed">
  st
</span>
<span class="colorGreen">
  ation
</span>

Try this instead:


<span class="colorRed">Ak</span><span color="colorDarkRed">a</span><span class="colorDarkOrange">ri -</span><span class="colorRed">Play</span><span class="colorDarkRed">st</span><span class="colorGreen">ation</span>

Color Formatting

To apply colors, use the following format:

.colorRed {
  color: red;
}
.colorDarkRed {
  color:darkred 
}
/* and so on.. */

Edit: Ensure you add a dot before class names. Instead of:

colorDarkRed { /* styles */ }

Use:

.colorDarkRed { /* styles */ }

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

Is it possible to maintain HTML, JS, and CSS files as separate entities when developing Vue.js components, similar to how it is

Is it possible to maintain separate HTML, JS, and CSS files while creating Vue.js components? I recently read the "Why Vue.js doesn't support templateURL" article which discusses this topic. "Proper modularization is a necessity if you want to bu ...

What is the rationale behind browsers on OSX not supporting the styling of HTML option elements?

As an OSX user, I primarily use Chrome and recently discovered that styling the HTML option element is not applied in any browser (Chrome, Firefox, Safari) on OSX. // HTML <select> <option class="option">Value 1</option> < ...

Mistake while defining a global variable within an HTML file and utilizing it

In my HTML document, I have set a global variable like this: <script type="text/javascript"> var total; </script> However, when I try to use this variable, I encounter an error stating that it cannot be used in this context. To resolve this ...

Loading indicator for buttons

Issue with submit button onclick function (onClick="mandatoryNotes()"). It is taking a while to load, so a preloader script was added. The preloader is now working, but the function is not being called. Seeking assistance. Thank you. function mandatoryN ...

Conceal the <div> element if the <span>

Is there a way to hide the content within this div if the prop-value is empty? I specifically want to hide the text "First class" when Prop-value does not have any value. Any solutions? <div> <span class='prop-name'>F ...

Can you explain the mechanics behind Angular Component CSS encapsulation?

Is it possible to avoid CSS conflicts when using multiple style sheets? Consider Style 1: .heading { color: green; } And Style 2: .heading { color: blue; } If these two styles are applied in different views and rendered on a layout as a Partial Vi ...

What is the best method for implementing border-radius on select2 elements?

I previously had a default bootstrap user select box with a border-radius style applied: <div class="container"> <select class="form-control" style="border-radius: 1rem;"> <option value="1">Us ...

Customize the keyboard on your iPod by replacing the default one with a

Looking to customize the iPOD keyboard to only display numbers, similar to a telephone keypad: https://i.stack.imgur.com/X0L3y.png The current iPOD default keyboard looks like this: https://i.stack.imgur.com/VykjU.png ...

The pre tag does not have any effect when added after the onload event

I have been experimenting with a jQuery plugin for drawing arrows, detailed in this article. When using the plugin, this code is transformed: <pre class="arrows-and-boxes"> (Src) > (Target) </pre> into this format: Src --> Target The is ...

Using Javascript to extract formatted text from a webpage and save it to the clipboard

I've developed a straightforward tool for employees to customize their company email signature. The tool generates text with specific styling, including bold fonts and a touch of color - nothing too extravagant. When I manually copy and paste the styl ...

How can I optimize webkit-mask-box-image with a high-resolution Retina image?

Hey there, wondering if it's possible to achieve high-quality Retina-level CSS masking with the -webkit-mask-box-image property. Specifically, I'm trying to round the corners of an element without using border-radius due to performance issues: . ...

AngularJS: Modify Z-Index on Click Event

My current project involves a navigation bar with four items, each accompanied by an icon. These icons are positioned absolutely one behind the other and are loaded into the view from different templates within the same controller. I am attempting to dyna ...

How to target and style multiple descendants of an element using CSS

Can you target multiple elements with a common ancestor in CSS using class, id, etc? For example: table.exams caption, tbody, tfoot, thead, tr, th, td If not, is there a method to select all nested elements within that particular element? ...

Leverage the greater than operator within an AngularJS controller

This is the data stored in my controller: $scope.data = [{ "F": "1,26,000" }]; $scope.data2 = [{ "F": "26,000" }]; Now I want to implement an if-else condition using this data: if (Number($scope.d ...

What are the recommended margins for various alphabets?

When displaying text, some alphabets take up more space than others. So how can I adjust the white space between elements '#re1' and '#re2' automatically in the scenario described below? In this code snippet, what is the best way to ad ...

rectifying file extension hyperlinks

While designing a webpage on my MacBook's local server, I unintentionally omitted the ".css" file extension in the href attribute of a link to my locally stored stylesheet. The mistake went unnoticed until I transferred my files to an externally hoste ...

Iconic slim template for data disabling

Having an issue while working on a Rails app with slim. I am facing a problem passing HTML to slim through a button using the data-disable-with attribute. My aim is to display an icon on the button when it's clicked. = f.submit t("basket.next_step") ...

Unable to assign a default value to an Angular input field

My web page utilizes an Angular form to display user data fetched from a MongoDB database. The information includes the user's name, phone number, email, etc. I want the default value of the input field to be set as the placeholder text, allowing user ...

React- hiding div with hover effect not functioning as expected

I'm having trouble using the :hover feature in CSS to control the display of one div when hovering over another, it's not functioning as expected. I've successfully implemented this on other elements, but can't seem to get it right in t ...

Trying to decide between using a Javascript or HTML5 PDF Editor?

I am in need of a solution that enables users to edit PDF files directly on an ASP.NET web page. This functionality is intended for creating templates and adding blocks/form fields to existing PDFs, complete with rulers and other necessary features. Desp ...