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

Using array.map() in React does not display elements side by side within a grid container

I'm currently working with React and material-ui in order to achieve my goal of displaying a grid container that is populated by an array from an external JavaScript file. The challenge I am facing is getting the grid to show 3 items per row, as it is ...

Transferring data between pages using HTML5 and jQuery

I'm currently developing Windows Phone 8 apps using HTML5 and jQuery. My project involves two HTML pages - the first page consists of input boxes and a button, while the second page contains controls to display the data entered on the first page. Upon ...

Utilize Angular to transform a JSON string into HTML text featuring organized bullet points

I am currently working on a project using Angular and I need to display some data from a JSON file on a webpage. The issue I am facing is that the message is quite lengthy, and I would like it to be presented in bulleted points. "messageToDisplay" ...

What is the best method to center a div on the screen?

Is there a way to have a div open in the center of the screen? ...

Utilizing v-data-iterator in Vuetify to display data fetched from an API in a table

After a long day of searching and experimenting, I've finally arrived here. Initially, I attempted to create a simple table with 2 columns using Row and iterate over the object to populate the left column with its keys. However, I hit a roadblock when ...

Visit the embedded AJAX feature that is failing to show any results

I've been working on this seemingly simple problem for days now, but I'm still stuck. I have the following Go code... package main import ( "fmt" "net/http" "html/template" "database/sql" _"github.com/mattn/go-sqlite3" ...

Steps to make a unique custom tooltip without using jQuery by utilizing the title attribute

I am looking to implement a tooltip similar to the one shown in the image. The value in the title tag is dynamic and fetched from the controller. https://i.sstatic.net/C2v3D.png Below is the code snippet of my table: <table border="1" cellpadding="10" ...

jQuery and CSS animation implemented on website performs flawlessly on all browsers, with the exception

I have recently started learning jQuery and I am facing an issue with the website linked below. It works perfectly in Firefox, Chrome, and Opera but not in Safari. <head> <meta charset="UTF-8"> <meta name="viewport" content="width=devic ...

Inserting multiple values into my PDO database

I am facing an issue with my code that I need help with. I am trying to include more data than just my :SteamID in my database. This function/PDO is currently executing every time someone visits my site. Working code: /*Check possible existing data*/ $odb ...

Tips for converting inline CSS to stand-alone attributes without any cost

I am attempting to modify an au generated HTML code, for example: <div class="intro editable" id="field_text">text <strong>text</strong> text <span style="text-decoration: underline;">text</span> <span style="color: #ff00 ...

I prefer boxes to be aligned next to each other without any gaps, and I won't be

In my quest to showcase 3 boxes seamlessly lined up in a row, I am faced with the challenge of eliminating spaces between them. My goal is to accomplish this feat without resorting to the font-size property. html,body { height:100%; width ...

Knockout text binding does not automatically update the width

Recently, I encountered a bug in an ongoing project where the user name was appearing within another element until hovered over with a cursor. Upon further investigation, I discovered that this issue stemmed from the project's usage of Knockout.js. I ...

How can I use HTML to replace specific text or blocks within the DraftJS Editor?

Incorporating Rich Text capabilities into my React-based web application using DraftJS Editor has been a focus of mine. One specific requirement I have is the ability for a user to trigger a drop-down menu by typing the "#" key next to the editor. From th ...

"Optimizing Background Images with IE 8 Stretch Cover in Bootstrap

Page Broken I've searched all over Stack Overflow for a solution to resolve this background image issue. I'm using Bootstrap for the boxes and removed any margins. The problem arises when setting the background as cover; in IE8, it doesn't ...

Having trouble sending correct true/false values for selected radio buttons on an Angular5 table, often requiring users to click them twice to submit the appropriate values

My goal is to assign true or false values to selected radio buttons in each row and then form an object for submission. To distinguish between the radio button values in each row, I have used {{k}}+{{sizeobj.rbSelected}} as the value which is causing issue ...

Restrict the maximum and minimum time that can be entered in an HTML input

I've implemented a basic code feature that allows users to input minutes and seconds on my React website. <div> <span> <input defaultValue="0" maxlength="2"/> </span> <span>m</span> <spa ...

Guide on making a JavaScript button with both "light and dark" modes

Currently, I am attempting to change the color scheme of the page by adjusting the :root variables using a function called changeBackgrondColor(). This function is then assigned to the fontAwesome moon "button". However, when I click on the moon, the pag ...

When using HTML5's checkValidity method, it may return a valid result even if

Consider the following scenario: <input pattern="[a-z]"/> If you run the command below without entering any input: document.querySelector('input').checkValidity() You will notice that it actually returns true. This seems counterintuiti ...

Deciding on pixel values, percentages, and sizing elements within CSS styles

For all you web development experts out there, I have a question regarding CSS styles. I often see precise pixel or percentage measurements used for properties like "padding" and "height." Experienced developers seem to effortlessly position their content ...

What exactly is the significance of localizing an Aria label?

Looking to add an aria label that needs to be localized. What exactly does this mean? Here is the code I have: <a href="link"><i class="button" aria-label="back button"> How do I modify this code in order to make ...