Styling HTML Text Using Your Own Unique CSS Class Style

After developing some CSS classes to define specific hex colors for my web application, I ran into issues applying these styles to text in the HTML. Despite my best efforts, I am struggling to make the styles show up as intended.

CSS

.custom-orange-color {
  color: #F58B30;
}

.custom-pink-color {
  color: #ED178F;
}

HTML

<p>I want to use the custom <style class="custom-orange-color">orange</style> color and the custom <style class="custom-pink-color">pink</style> color in this text.</p>

When I tried implementing this code, the text within the style tags seemed to disappear completely. What could be causing this issue?

Answer №1

It appears that you may be misunderstanding the purpose of css classes. Instead of using <style> tags, consider utilizing <span> tags. Style tags are typically used for containing css styles, not for applying classes or other css rules.

The span tag is primarily used for grouping inline-elements within a document.

By itself, the span tag does not result in any visual changes to the content.

The span tag offers a means to apply a marker to a section of text or document.

In your scenario, it is advisable to assign the desired colors by associating the correct class with the respective span element. This can be implemented as shown below:

.custom-orange-color {
  color: #F58B30;
}

.custom-pink-color {
  color: #ED178F;
}
<p>Here is the custom <span class="custom-orange-color">orange</span> color and here we have the custom <span class="custom-pink-color">pink</span> color.</p>

Answer №2

The purpose of the <style> element is to contain a stylesheet, not actual content.

Your document's <body> should include text and HTML elements that accurately represent the meaning of that text.

The <style> element belongs in the <head> section and should only consist of CSS code.

Instead of using

<style class="custom-orange-color">
, choose a more descriptive element for applying an orange color effect, such as:

<em>

Then you can style it with CSS.

em {
  color: #F58B30;
  font-style: normal;
}

strong {
  color: #ED178F;
  font-weight: normal;
}
<p>This represents the <em>emphasised</em> text color and this shows the <strong>strongly emphasised</strong> text color.</p>

Add classes in your HTML only if necessary (CSS uses class selectors, among other features, but not classes).

Your use of a class selector was correct, though better names should be chosen based on meaning rather than appearance. The issue with incorrect HTML could have been detected by utilizing a validator.

Answer №3

Check out this code snippet that will help you implement custom colors in your HTML using CSS. Remember, the <style> tag is crucial for defining the style within your HTML document.

.custom-orange-color {
  color: #F58B30;
}

.custom-pink-color {
  color: #ED178F;
}
<p>This is the custom <span class="custom-orange-color">orange</span> color and this is the custom <span class="custom-pink-color">pink</span> color.</p>

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

Issue with conditional comment in IE 8 not functioning as expected

Struggling with changing the SRC attribute of my iFrame specifically for users on IE 8 or older. This is the code I have written: <script> var i_am_old_ie = false; <!--[if lte IE 8]> i_am_old_ie = true; <![endif]--> </script> ...

Is it possible to implement a code that will organize a dropdown list alphabetically?

Could someone assist me in sorting the location items alphabetically in this search form at: I would like the locations to be displayed in alphabetical order. Any help with updating the code below would be greatly appreciated. Here is the current code sn ...

The _rfs.scss width rule in Angular-Bootstrap-NgBootstrap is overriding the column width of the col-rules

The Dilemma I am currently working on an Angular project that incorporates Bootstrap and ng-bootstrap. My intention is to utilize the grid system within this project, but I encountered some bugs along the way. When trying to render the following HTML/TS ...

Vertical scrollbar in iframe unexpectedly appears immediately after the submit button is pressed

I have designed a contact form that is displayed in an iframe within an overlay div. I have carefully adjusted the dimensions of this div to ensure that there are no scrollbars visible when the form initially appears. However, after filling out the form an ...

Is there a way to establish a universal font for all apps within ant design?

I am currently working with NextJS version 14 and I have the following setup: In my globals.css file, I have imported a Google font like so: @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@100;200;300;400;500;600;700;800& ...

Angular 9 - Auto-adjust the height of the text box as the user types, returning to its original size when no longer in focus

I need a solution where an input field's height adjusts to display the user's entry, then returns to normal size when they click away. It should function similar to this example image: https://i.stack.imgur.com/yKcik.png Once the user enters th ...

Add a new element to the page with a smooth fade-in animation using jQuery

var content = "<div id='blah'>Hello stuff here</div>" $("#mycontent").append(content).fadeIn(999); Unfortunately, the desired effect is not achieved with this code. I am trying to create a sleek animation when adding new content. ...

After a slight delay, the animated element twitches into action

I am currently working on implementing a menu item behaviour in the header similar to the one found at: . On this webpage, there is a bar that slides back and forth when hovering over elements. However, I am unable to add another element to my header, so I ...

Adjust and modify class when resizing the window

I've encountered a challenge with my Bootstrap Modal when trying to set the width to 750px on large desktops. When resizing the window to a smaller size, the modal loses its responsiveness. To tackle this, I added a class to the modal to give it a fix ...

When a dialog box is displayed, a scrollbar will automatically appear

I've encountered a strange issue while working with Angular dialogs: A vertical scrollbar unexpectedly appears, even though I've set the dialog to have a fixed width. component.ts const dialog = this.dialog.open(DialogComponent, { width: ...

How to attach input to function invocation in Angular 2

Can we connect the @Input() property of a child component to a parent component's function call like this: <navigation [hasNextCategory]="hasNextCategory()" [hasPreviousCategory]="hasPreviousCategory()" (nextClicked)="next ...

access older siblings, accordion animation

I need assistance achieving an accordion effect on a DIV when hovering over it. The right side of the accordion is functioning properly, but I am facing issues with the left side. You can view my code on jsFiddle Could someone please provide guidance on ...

What is the best way to include a swf video in a list of hyperlinks?

My current code displays an image inside a box, and I am looking to replace the image with either a swf video or .AVI video. I have the object video code to play a swf video, but I need the video to be played within the box where the image is currently l ...

Manipulating binary data through the use of encodeURIComponent

Currently, I am reading a binary file by making a jQuery ajax get request. The file (a zip file in this instance) is returned as a string. After performing some actions on the file within the browser without modifying it, I need to send it back to a server ...

What is the best way to emphasize a certain row within XSLT?

I am seeking a solution to emphasize rows containing the "year" element with a value exceeding "2000" in XML and XSLT. Here is the code snippet: XML <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="harrypotterbooks. ...

Implementing jQuery form.js to dynamically update image src upon success

I am currently utilizing jquery.form.js to submit a form via AJAX and retrieve a response. My PHP page is configured to output the image URL in the format "images/thumbs/071112130957.jpg" Below is my jQuery code: $("#imageform").ajaxForm( { target: &ap ...

Develop diverse canvases with shapes such as ovals, rectangles, and circles

My task involves creating a canvas in an oval shape. Below is the code I am using in jQuery with the created object of canvas. $("#decalshap").change(function() { alert("decal"); var shap = $(this).val(); if(shap=="oval") ...

Setting a class for a specific date on a jQuery UI calendar using the MultipleDates plugin

I recently encountered an issue with the Multiple Dates picker for jQuery UI date picker. It seems that the developer who created it has not updated it in quite some time, resulting in pre-highlighting dates no longer functioning properly. To address this ...

The mysterious behavior of HTML 5 causing images to mysteriously add a 3px margin at the

Whenever I update my website to <!DOCTYPE HTML> Each img element enclosed within a DIV mysteriously acquires a 3px bottom margin, even though no CSS rule defines it. To clarify, there are no style attributes responsible for this 3px bottom margin. & ...

Locating Elements with Python 3 and Selenium: Unraveling the xpath Mystery

https://www.facebook.com/friends/requests/?fcref=jwl&outgoing=1 I am looking to click on the "See more request" option within the "friend request sent" section. <div id="outgoing_reqs_pager_57b87e5793eb04682598549" class="clearfix mtm uiMorePager ...