What is the reason that CSS selectors cannot use numbers as IDs?

I have come to realize that numbers cannot be used in CSS selectors.

<!DOCTYPE html>
<html>
<style type="text/css">
  div{width:350px;word-wrap:break-all; }
  #1{float:left;}
</style>
<div class="up">
  <p><img id="1" src="http://i.imgur.com/Vt9ni32.jpg?1" /> Web graphics are visual representations used on a Web site to enhance or enable the representation of an idea or feeling, in order to reach the Web site user. Graphics may entertain, educate, or
emotionally impact the user, and are crucial to strength of branding, clarity of illustration, and ease of use for interfaces. Examples of graphics include maps, photographs, designs and patterns, family trees, diagrams, architectural or engineering
blueprints, bar charts and pie charts, typography, schematics, line art, flowcharts, and many other image forms.
  </p>
</div>

</html>

When using numbers in CSS selectors, the text does not align properly, and a gap remains. Changing id=1 to id=2 or any other number, and changing #1{float:left;} to #2{float:left;}, does not eliminate the gap.
If I change the numbers to words, the gap disappears as shown below.

<!DOCTYPE html>
<html>
<style type="text/css">
  div{width:350px;word-wrap:break-all; }
#test{float:left;}
</style>
<div class="up">
  <p><img id="test" src="http://i.imgur.com/Vt9ni32.jpg?1" /> Web graphics are visual representations used on a Web site to enhance or enable the representation of an idea or feeling, in order to reach the Web site user. Graphics may entertain, educate,
or emotionally impact the user, and are crucial to strength of branding, clarity of illustration, and ease of use for interfaces. Examples of graphics include maps, photographs, designs and patterns, family trees, diagrams, architectural or engineering
blueprints, bar charts and pie charts, typography, schematics, line art, flowcharts, and many other image forms.
  </p>
</div>

</html>

Answer №1

When working with HTML 4, it is advised not to start IDs with a number as it may cause functionality issues. However, in HTML 5, it is acceptable to do so. Keep in mind that although HTML allows for this, CSS does not fully support it, so selectors may still fail. For more information on valid IDs and classes with numbers, you can check out (acceptable IDs). Additionally, you can explore the topic further by reading here.

Answer №2

In HTML 5, it is acceptable for IDs to begin with a number, but CSS does not support this: learn more by clicking here

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

I am unable to see the last row of the table when I apply a condition after the table name

I need to display all the data from my table. However, if I include ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, the last row does not appear. <?php include "db.php"; $tbl_name="report"; // Table name $sql="SELECT * FROM $tbl_ ...

Use jQuery to trigger a click event when an element is in focus, unless it was clicked to

I am currently developing a website using the MDL framework. One issue I encountered is that there is no default select form element provided. After some research, I found a solution by utilizing a menu component that displays when the input is clicked. Th ...

HTML Scripts: Enhancing Web Functionality

I have another question regarding executing a script (docs()) upon clicking on text. I attempted to use the following code: <p><span class="skill">Documentation can be found here.<script>docs()</script></span></p> Unfo ...

Google Maps and certain styles may require the browser to be refreshed multiple times

Something strange is happening with the Google Maps implementation on my website. Occasionally, when I first load the page, the map doesn't show up but appears after a refresh. Sometimes, I have to refresh multiple times before it displays properly. A ...

Specify the width of one element to always be the same as the width of another

One common challenge is maintaining the width of one element equal to another when the page loads (refer to link description here). However, it becomes tricky when the side width changes, such as resizing the browser window. Is there a way to dynamically ...

Maintain the menu item color even after clicking

Here is the menu code snippet: <div class="header"> <div id="logo"></div> <ul class="menu"> <li><a href="/index.aspx">Home</a></li> <li><a href="/about.aspx"& ...

Utilizing Vue CSS variables as inline styles

Incorporating CSS variables in my component has allowed me to dynamically set the width of a div based on computed data within the setup() setup(props) { const progressBar = PositionService.getProgressBar(props.position); const progressWidth = `${p ...

Bootstrap 5 does not support tab navigation functionality

I created a new HTML page, and I encountered an issue while trying to enable tabs using JavaScript. <html> <head> <title>Excel Viewer</title> <link hr ...

Prevent using HTML escape characters when coding in PHP

When attempting to make an API call using PHP, one of the parameters required is 'currency'. The API call looks like this: <?php $call=".....&currency=USD&......."; $response = hash_call("Pay", $call); ?> However, when I print ...

Tips on displaying a successful message and automatically refreshing the page simultaneously

I am working on a JSP Servlet code that utilizes AJAX to handle user data. In the success function of AJAX, I aim to display a success message to the user and clear all fields in the form by reloading the page. Unfortunately, when the page reloads, the a ...

How can I use jQuery to display a larger image in a specific div when a thumbnail is clicked?

My current setup involves a div that serves as a target: <div id="rightbox"></div> I have my thumbnail images neatly organized into groups: <img src="group1/thumb/01.png" width="160" height="97" class="imgtopleft" /> <img src="group ...

When CSS is modified by inserting an extra div, it causes the positioning of other elements to shift towards

I'm currently working on adapting a method to make elements "sticky" for older browsers as discussed in this particular article. The basic idea involves implementing some JavaScript code that listens for scroll events. Upon detection of such an event ...

Having trouble displaying a gray background/overlay in a Twitter Bootstrap modal design

Library version: Jquery 1.11.0 Framework version: Bootstrap 3.1.1 CodePen link: https://codepen.io/amitshahc/pen/r6ueupfj/1/ HTML snippet: <!-- Modal --> <div class="modal fade" id="az-progress" tabindex="-1" role="dialog" aria-labelledby= ...

Align the prices of products in a uniform position within each table cell

Apologies for the lengthy explanation. I am working with osCommerce and have a page that displays all our product specials in a 3 column table layout. My challenge is to align the price of each product so that they are aligned perfectly across the screen. ...

What is the best way to make JavaScript display the strings in an array as bullet points on different lines?

We were assigned a task in our computer science class to analyze and extend a piece of code provided to us. Here is the given code snippet: <!DOCTYPE html> <html> <head> <title>Example Website</title> </head> <body& ...

Exploration of jQuery Dropdown Menus

Encountering a problem with my dropdown menu. Check out the code here: http://jsfiddle.net/xY2p6/1/ It seems like a simple issue that I can't figure out, but as shown in the link, the functionality is not working properly. I need help linking the hid ...

Top method for achieving a smooth transition in a loading CSS3 animation

Having trouble implementing a fade in/fade out effect on a div with a CSS3 based login animation when a function is loaded. I have set up a jsfiddle but still can't get it to work. Any assistance would be greatly appreciated! http://jsfiddle.net/adam ...

Loading an animated SVG sprite file in real-time

Recently, I received an SVG sprite file from our designers to use in my app. The specified convention is to load the sprite at the top of the <body> element and then render icons using a specific code snippet: <svg class="u-icon-gear-dims"> ...

Where within Video.js can I modify the color of the large play button when the cursor hovers over the video?

After successfully changing the SCSS $primary-background-color to orange using the video.js default skin editor (CodePen), I encountered an issue. Whenever I hover my mouse cursor over the video, the big play button background reverts to its default grayis ...

What steps should I take to ensure that jQuery functions remain functional on loaded code?

I have implemented modals on all my pages except for the first one to save time. Here is the script that loads my modals: $(function () { $('#header').load('reusenavbar.php'); $('#loginModal').load('reuseloginmo ...