Applying CSS to color the letters of a text, without needing to alter the HTML code

I'm just beginning to learn CSS and today I received a school assignment:

Below is the HTML code provided:

<head>
    <meta charset="UTF-8">
    <title>ABC</title>
    <style>
        /* CSS section */
    </style>
</head>
<body>
    <p>ABC</p>
</body>

The task is to change the color of letter "A" to #ec407a, "B" to #ffb300, and "C" to #26a69a without altering the existing HTML code.

Any suggestions on how I can achieve this? Thank you!

Answer №1

Impressive coding skills demonstrated here

<style>
p {
  color:#ffb300;
  position: relative;
}
  p:first-letter {
    color: #ec407a;
  }
  p::after {
    bottom: 0;
    color: #26a69a;
    content: 'C';
    position: absolute;
    transform: translate(-100%, 0);
  }
</style>
<body>
    <p>ABC</p>
</body>

https://jsfiddle.net/fqu9ppL4/1/

Answer №2

One approach to achieving this effect is using the CSS property mix-blend-mode, which unfortunately is not yet widely supported by most browsers.

For a possible alternative in the future, consider the following:

p {
  margin:1em;
  display:table;
  position:relative;
  background:white;/*
  or add a huge white inset shadow to create a cut-off effect 
   box-shadow:inset 0 0 0 100px white; */
  }
  p+p {
  font-size:3em;
  font-family:'Courier New', Courier, monospace;
  
}
p:before {
  content:'';
  background:linear-gradient(to left, #ec407a ,33.33%, #ffb300 33.33%,#ffb300 66.66%, #26a69a 66.66%);
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  mix-blend-mode:screen;
}
<p>What about adding a few more letters?</p>
<p>WIW</p>

https://i.sstatic.net/LTdgs.jpg


Another technique you can explore is:

-webkit-background-clip: text;
-webkit-text-fill-color: transparent;

p {
  font-size:3em; /* for demonstration purposes, adjust as needed */
  display: table;
  background: linear-gradient(to left, #ec407a, 33.33%, #ffb300 33.33%, #ffb300 66.66%, #26a69a 66.66%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
<p>ABC</p>

Answer №3

This solution is a unique approach that may seem unconventional and specifically catered to the characters and font used.

p { 
  color: #ec407a;
  display: inline-block;
  font-size: 6em;
}
p:after {
  background-color: white;
  content: 'B';
  color: #ffb300;
  display: inline-block;
  float: right;
  position: relative;
  left: -1.33em;
}
p:before {
  content: 'C';
  color: #26a69a;
  display: inline-block;
  float: right;
  position: relative;
  left: -1.335em;
}
<p>ABC</p>

Answer №4

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8>
    <title>XYZ</title>
    <style>
        h1::first-letter {
            color: #f48fb1;
         }
    </style>

   <script type="text/javascript">
        var text = "XYZ";
        var colors = new Array("#f48fb1", "#ffc107", "#4db6ac");
        for (var j = 0; j < text.length; j++)
    document.write("<span style=\"color:" + colors[(j % colors.length)] + ";\">" + text[j] + "</span>");
    </script>

</head>

<body>
    <h1>XYZ</h1>
</body>

</html>

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

Allowing the primary content to span the entire width of the mobile screen

I have scoured various forums in search of answers, but unfortunately, I haven't found a solution that fits my specific query. I am looking to ensure that the .main-content (article text and images) occupies the full width of the mobile screen without ...

Creating a full width and height slider with a header menu that is responsive to all device screen sizes

Looking to create a full-width slider with a header that adjusts for all screen sizes, especially larger devices. I've been searching online but haven't found a satisfactory solution yet. If anyone has any ideas or tips on how to achieve this, pl ...

What is the best way to have my sliding panel automatically close when I click outside of it?

I have created a sleek sliding navigation panel for my website that appears when the screen width is reduced. Although I am satisfied with how it functions currently, I would like the panel to close when the user clicks/taps outside of it. What adjustments ...

Determining the precise margin within a nested div structure

Hopefully, my description of a "nested div" was accurate. My current situation involves an image within a div structured like this: content -> content-inner -> column_left I'm struggling to start the image at 0 px (the extreme left side of th ...

Navigate through the seamless elements with scroll snapping

I need to find a way to make horizontal scrolling on my really wide graph snap to specific points along the x-axis. I initially tried using scroll-snap-points-x, but unfortunately it doesn't seem to be supported. My attempt to add invisible elements ...

Arranging two stretchable div elements side by side

I'm currently working on a web page layout where I want two divs to be flexible and adjust their size as the browser window is resized. However, I've encountered an issue where instead of shrinking, the line breaks between the two divs when the w ...

CSS - Layering new DIVs over existing DIVs

My goal is to implement "vertical-align: bottom;" in order for the DIVs to align from the bottom of the wrapper/parent DIV to the top. However, I am facing an issue where I want the first DIVs to be displayed at the bottom, rather than at the default top p ...

Is it necessary for me to include body, html at the beginning of my CSS code?

For all of my pages, I have been including the following code at the top. body { color: #333333; font-family: Arial,sans-serif; font-size: 0.95em; line-height: 1.15; } However, a colleague of mine has informed me that I only need to targ ...

The Parcel build was unsuccessful due to an error from @parcel/resolver-default: The file '../../../../../css/main.css' could not be loaded within the './src' directory

I utilize [email protected]. Whenever I attempt to load an external css or javascript file in my index.html, Parcel consistently fails to build with the following error: Build failed. @parcel/core: Failed to resolve './css/main.css' from & ...

Adding Information to a Material Design Card

Currently, I am delving into frontend development and honing my CSS skills. In my latest project, I have incorporated a mat card which can be viewed in the linked image https://i.sstatic.net/TEDcg.png. Below is an excerpt of my code: <div class=&quo ...

Dim the brightness of an image on Internet Explorer

I discovered this code on another site and it functions flawlessly in Chrome and FF, however, IE (version 11.0.9) doesn't seem to like it. -webkit-filter: grayscale(0%); -moz-filter: grayscale(0%); -o-filter: grayscale(0%); filter: grayscale(0%); fil ...

Having trouble toggling the dropdown submenu feature in a Vuejs application?

.dropdown-submenu { position: relative; } .dropdown-submenu .dropdown-menu { top: 0; left: 100%; margin-top: -1px; } <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorial ...

Guide on customizing a dropdown button in a class-based Angular library with version 4 or higher

My dilemma revolves around utilizing the Angular Material library for a drop-down navigation bar. The issue at hand is my desire to hover through the list, yet I am unable to tweak the style within HTML. Fortunately, I can easily make alterations in Chrome ...

What's the process for browsers to render the 'span' element?

<p> <span>cancel</span><span>confirm</span> </p> <hr> <p> <span>cancel</span> <span>confirm</span> </p> Both should display the same result. However, in the ...

Table-styled div containing a horizontal scrolling div

I am currently in the process of developing a webpage with dual columns. The two columns are segregated into div containers, one labeled right and the other labeled left. These columns reside within a parent div called row, which is nested within a main di ...

What is the best way to horizontally align a div within a parent div?

I'm having trouble centering the red div containing three child elements (h1, h3, and button) within a green container div using CSS. I initially attempted to center the red div vertically and horizontally on its own, but after some research, I learne ...

using `clear: both` does not stop text from wrapping

Currently, I am facing an issue with positioning 3 divs in my layout. Two of the divs are floating to the left and right respectively, while the third one should be placed under the two others. I tried using clear: both but it doesn't seem to work as ...

Does SCSS have a specific 'self' identifier?

I am looking to incorporate SCSS styles on the body by default, and then reapply them specifically to the P and LI tags (since I do not have complete control over the site and want to prevent plugins from interfering with my p's and li's). To ac ...

How can you vertically center an icon button in Material UI?

Looking for help with aligning elements in this code snippet: <TextField id="outlined-basic" label="22Keyword" defaultValue={"test123"} variant="outlined" /> <IconButton aria-label="delete&q ...