Newbies struggle with styling HTML elements within nested IDs


I am new to learning HTML and struggling with working with IDs and classes within IDs and classes.

Based on my understanding, an ID is denoted by a # symbol. So if I wanted to style an ID within another ID, would it be written like:

#ID1 #ID2 {
    ...
}

This would mean that the styling will only impact ID2 that is nested inside ID1, right? However, when I applied this concept in my basic code as a beginner, it didn't work. Here's the snippet of my code:

<!DOCTYPE html>
<html>

<head>
    <title>1 May 2016</title>
    <style>
       #name {
           color: blue;
           font-family: Tahoma;
       }

       #parafont #1 {
           font-family: Arial;
       }

       #parafont #2 {
           font-family: Times;
       }

       #parafont #3 {
           font-family: Courier;
       }

       parafont #4 {
           font-family: Lucida Grande;
       }

       #parafont #5 {
           font-family: Helvetica;
       }

       #test1 #6 {
           color: blue;
       }
    </style>
</head>

<body>
    <h3 id="name">Bob Bobbington</h3>
    <p>1 2 3 4 5 6 7 8 9 10</p>
    <p>When was this website created? Check the <b>title</b>.</p>
    <h3>All animals are quite interesting...</h3>
    <p id="parafont">
        <span id="1">This is a test paragraph.</span>
        <span id="2">Each sentence should have a different font.</span>
        <span id="3">This paragraph is going to use some styling.</span>
        <span id="4">Styling will change the font of each sentence.</span>
        <span id="5">Let's see whether it works!</span>
    </p>
    <div id="test1">
        <p id="6">Test</p>
    </div>
</body>

</html>

I would greatly appreciate any assistance on this matter!

Answer №1

Revise the identification code 1 to start with letters instead.

ID and NAME attributes should initiate with a letter ([A-Za-z]) and can include any combination of letters, numbers ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Answer №2

It is important to note that IDs cannot begin with numbers. Additionally, because IDs are intended to be unique identifiers, using nesting to target elements is unnecessary. Selecting #id #id2 would yield the same result as simply selecting #id2, provided that the more specific option #id #id2 was utilized.

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

Challenge with adjusting opacity of background when hovering over a box

I've encountered an issue with the background transparency in the following demo. For some reason, it's not working properly. .figure .caption-mask:hover { background: rgba(0, 0, 0, 0.0); } I'm attempting to remove the opacity f ...

Issues with a responsive website not displaying correctly in the Firefox browser

I have created a carousel that is functioning correctly. However, I am encountering an issue with making it fully responsive. It appears to work properly in webkit browsers, but there are some issues in Firefox. Despite the images resizing properly, the ...

CSS with a "true" space for a fixed element (ensuring the previous element does not overlap with the current fixed element)

I am facing an issue with the layout of my webpage. I have two horizontal columns - the left one is fixed, along with a bottom footer that is below the second content column. When I add a border to the content column, it extends all the way down to the fo ...

Unable to change background-image as intended

Here is an example of my HTML code with an initial background image: <div id="ffff" style="width: 200px; height: 200px; background-image: url('/uploads/backroundDefault.jpg')">sddsadsdsa<br>dffdsdfs</div> Everything seems to b ...

Organizing my website directories using external tools

I am dealing with a website that follows this specific structure: \ |--css | \ | |--vendors | | \ | | |--normalize.css | | |--... | | | |--styles.css | |--media-queries.css | |--js | \ | |--vendors | | \ | | |- ...

Angular 11 - Binding Data to an Array Element

Currently, I am attempting to apply data-binding to specific properties of an element categorized as Ripe: export interface Ripe{ version: string; data_call_status: string; cached: boolean; data: { is_less_specific: boolean, ...

Display different data in an HTML table for each individual click on the "Read more" event

I am currently extracting data from a SQL table to exhibit it in an HTML table. The issue I am facing is that I would like to implement a "Read More" feature to prevent clutter in the table. As I am retrieving the data row by row, I am utilizing a class n ...

What could be causing the footer to not show up at the bottom of the page?

I've encountered an issue with my code where the footer appears to be stuck at the bottom of the h2_c div instead of the bottom of the .content div where it should be. I have tried using positioning but it hasn't resolved the problem. Thank You. ...

What is the best way to apply "display: none;" on a span element nested within a title attribute?

I am utilizing the social-share-button gem to share blog posts on social media. The website has been internationalized, making it bilingual (English and German). Everything is functioning correctly, but I encounter an issue with the social share buttons wh ...

Styling a dynamically-injected div using an AJAX request (xhrGet)

Hello everyone, currently I am using the code below to fetch updated content after receiving a "push" event from a server. The new content is then used to replace the existing div/content. However, I'm facing an issue where none of my styles from the ...

Enhancing Accessibility of the 'Return to Top' Link

Currently, I am designing a web page that requires users to scroll extensively. To enhance the user experience, I have included a back-to-top link at the bottom of the page for easy navigation back to the top. This is the HTML markup I have implemented: ...

Guide on receiving a date input in a datetime format upon clicking a submit button

Currently, I am developing a daily report feature for my system. One of the requirements is to have an input field where users can select a date using a datepicker. When the user clicks the submit button, the system needs to retrieve the date from the data ...

Displaying the Indian Rupee symbol in PHP

I'm attempting to show the Indian rupee symbol using HTML code ₹ The HTML code is being echoed in PHP and everything seems to be working fine, except that the rupee symbol is not showing up. I've tested using other currency symbols and the ...

Automatically updating the JavaScript content on a webpage without having to refresh the entire HTML page, based on the changing

Is there a way to adjust the opacity change rate of my nav bar automatically without the need to refresh the page? The current opacity change rate is based on the width of the window, but once the page is loaded, adjusting the width does not affect the rat ...

Is it possible to run both the frontend and backend on the same port when using vanilla JavaScript for the frontend and Node.js for the backend?

Is it possible to run frontend and backend on the same port in Rest APIs if using vanilla JS for the frontend and Node.js for the backend? I've come across information on how to do this with React, but nothing specific to vanilla JS. Any insights on t ...

Is it possible to enhance the GamepadAPI's functionality?

I've been working on enhancing the built-in GamepadAPI by adding custom controller code. With TypeScript, I created a basic function to trigger a "gamepadconnected" event. // emulate gamepadconnected event function dispatchGamepadConnectedEv ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

Creating responsive lines with CSS on top of an image

I would like to add lines on an image using CSS3 and HTML5 Canvas. I came across a tutorial and demo that utilizes an html div: However, when attempting this effect on an image, the line appears outside of the image. How can I ensure that the line is dra ...

Angular: Generating components dynamically in HTML is unsuccessful

I possess the capability to dynamically generate components: import { Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, OnInit, ViewChild, ViewContainerRef } from '@angular/core'; import { FilterComponent } from '../filter ...

Instructions for inserting an anchor tag into the middle of a <p> element utilizing document.createElement("p")

When generating elements dynamically with JavaScript using document.createElement("p"), I am looking to create a paragraph element <p></p> that includes an anchor tag in the middle, creating a clickable link within the text. I aim to use JavaS ...