Does applying a CSS class to a <b> element adhere to proper HTML/CSS guidelines?

Is it considered valid HTML/CSS to apply a CSS class to a <b> tag?

For instance, is the following code acceptable:

<b class="myclass"> Foo Bar </b>

Is this appropriate in terms of HTML/CSS standards?
I am looking to assign a class to a b tag purely as an identifier to use with jQuery/JavaScript. It will not have any CSS styles associated with it.

Answer №1

Absolutely, the b tag is capable of accepting all global attributes, including the class attribute. Here is a comprehensive list of attributes that you can include within the b element:

  • accesskey
  • class
  • contenteditable
  • contextmenu
  • dir
  • draggable
  • dropzone
  • hidden
  • id
  • inert
  • itemid
  • itemprop
  • itemref
  • itemscope
  • itemtype
  • lang
  • spellcheck
  • style
  • tabindex
  • title
  • translate

Furthermore, feel free to utilize any custom data attributes you wish.

Lastly, don't forget that you can also incorporate the ARIA role attribute.

Answer №2

Sure thing! That's perfectly fine.

But it's typically not recommended to solely rely on the class attribute for element identification. You might want to consider using a different attribute like data-reference for better accuracy and efficiency. This way, you can avoid forcing the browser to keep track of a class that isn't actually being used as a class.

Answer №3

Indeed, this situation is completely acceptable. There are no issues with it whatsoever.

Answer №4

There is no definitive issue with it, but it is worth noting that the <b> bold tag is no longer recommended, with the <strong> tag being the preferred choice.

I typically do not assign a class to the strong (or b) tag because I reserve it for instances where I want the text to be strong and eye-catching, much like how the <em> tag is used for emphasizing words within a sentence.

When I need to make text bold or emphasize it for another reason, I opt for a div or span with a specific class — for instance, when italicizing the title of a book or article, I do not apply <em> to the title; instead, I use

<span class="title">This Is That Title</span>
to accurately denote what the title is, and then utilize a stylesheet to designate it as italicized.

Answer №5

When it comes to CSS, it's important to note that there are no actual "CSS classes". Instead, CSS utilizes class selectors which rely on the existence of a class attribute within a markup language. Therefore, the question of CSS classes is irrelevant in the context of CSS itself.

However, in HTML, the class attribute is technically valid for b elements starting from HTML 4.0. While the practicality of using the class attribute in this context may vary, there are no formal constraints on its usage. While class attributes are commonly used for styling purposes, they can also serve other functions, particularly in scripting.

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

How can I create an input field that only reveals something when a specific code is entered?

I am currently developing a website using HTML, and I would like to create an admin section that requires a password input in order to access. After consulting resources on w3schools, I attempted the following code: <button onclick="checkPassword()" i ...

Is it possible to modify CSS properties by clicking on buttons?

Hello, I really need help with this task. I want to change the style of a button (which is actually a link) by clicking on it. These buttons are housed within a dynamic child div that changes each time, but the name of the dynamic child div remains con ...

How can I achieve transparency for an element while it is nested within multiple parent

Is there a clever method to achieve transparency for an element that allows the background of its parent element to shine through and display the body background, for example? Consider this scenario: <body style="background: url(space.jpg)"> <di ...

Enhance the appearance of the standard black rectangle displaying the messages "No video source" or "No video permissions"

Is there a way to change the styling of the video element that appears as a black rectangle with a strikethrough play button when the user is prompted for permission on Safari? Does it have a specific ID, class, or tag that can be targeted? I'm curre ...

Determine the gravitational force of a planet on falling objects and display the result in an input field

I have developed a form code that includes several dropdown menus with different options, as well as an alert popup message to prevent users from selecting the same planet more than once. Additionally, I have disabled one input in a textbox because only on ...

Basic website layout

As someone who is new to web programming, I kindly ask for your patience :) I have a goal to design a page similar to the one linked below https://i.sstatic.net/HbJF3.png There are two key functionalities I am aiming for: 1) Clicking the button on the r ...

Why is it important to incorporate the CSS property background-repeat: repeat; into our code?

It is believed that if you don't expressly mention the background-repeat in CSS styling, browsers will automatically set it as background-repeat: repeat. With this default behavior in place, one might wonder why there is even a need for a separate ba ...

Animate a div once the parent section becomes visible while scrolling

I previously had this function working, but it seems that I have somehow messed it up in the process. My current setup includes a scroll hijack feature that navigates users to a new section or card each time they scroll. The script adds a visible class w ...

Check if the user input corresponds to the content of an anchor tag by using JavaScript's built-in functionality

Looking to compare a form field input value with anchor tag values using JavaScript. Here is my form field with text input and a button: <form action="#"> <input type="text" id="desc" value=""> <input name="submit" tabindex="1" value= ...

Styling the center menu

Is there a way to horizontally center this top menu using CSS? https://i.sstatic.net/9yvJf.png This is the HTML structure I am working with: <div class="wrapper_menu_hoz"> <div class="container_24 "> <div class="gr ...

Utilizing numbered lists through HTML, CSS, and/or JavaScript

I am looking to accomplish something similar to this... My guess is that the images should be stored in an array in Javascript with the image links. However, I am uncertain if this can be done using only HTML and CSS. I found a solution on this thread, w ...

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 ...

Achieve a perfectly centered and responsive image placement on a webpage accompanied by a countdown timer

I've been trying to design an HTML page with a countdown feature, and I want to place an image above it that is centered and responsive. Despite my efforts, I haven't been able to achieve the desired responsiveness. I envision it looking like thi ...

Extracting content from HTML-formatted email

When I receive inbound emails with HTML formatting that has been copied and pasted from office applications like Outlook, it often causes formatting issues when displayed on my HTML enabled UI. To address this problem, I usually copy the HTML content to an ...

The striped color override feature in Bootstrap appears to be malfunctioning in Internet Explorer 8

I have come across several discussions regarding this topic with various suggestions. My current objective is to make the alternating rows in Bootstrap (2.3.2) appear darker in Internet Explorer 8. To achieve this, I attempted to override the style by impl ...

Alert: Font preload was not utilized within a short timeframe after the window's load event

I've been working on optimizing the loading speed of fonts on my website, so I added the following: <link rel="preload" href="{{ '/css/fonts/bebasneue-webfont.woff' | prepend: site.baseurl | prepend: site.url }}" as="font" type="fo ...

Ensure that the left div spans 100% of the width of the container, while the right div remains on the right side and maintains a

Is there a way to create an effect with a div where the left side is 100% and the right side stays fixed? <!DOCTYPE html> <html> <style> .left { width: 100%; float: left; border: #ccc solid 1px; } .right { width: 50px; float:left; border ...

AngularJS directive does not trigger when switching tabs or scrolling pages

I came across a solution to display a placeholder image before the real image fully loads while browsing online here. I implemented the code provided in the accepted answer on my page, where I have two tabs using `ion-slide-box` for tab selection. Each tab ...

The color of the last clicked DIV is supposed to stay permanent, but for some unknown reason

I'm attempting to replicate this design in my project: http://jsfiddle.net/AYRh6/26/ However, I am facing issues with it and cannot pinpoint the exact problem in the code. I am using code for a toggle effect. Any assistance would be greatly appreciat ...

Utilizing PHP and AJAX to Extract Information from a MySQL Database

My goal is to fetch data from a MySQL database hosted on a webserver and display it in an HTML table. I've been following an example on W3Schools, but I'm facing issues retrieving the data successfully. Here is the source code: (HTML) <html& ...