🌐
W3Schools
w3schools.com › css › css_selectors.asp
CSS Selectors
To select an element with a specific id, write a hash (#) character, followed by the id of the element. The CSS rule below will be applied to the HTML element with id="para1":
🌐
W3Schools
w3schools.com › cssref › sel_id.php
CSS #id Selector
The CSS #id selector selects the element with the specified id.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Reference › Selectors › ID_selectors
ID selectors - CSS | MDN
<!-- The next two paragraphs have id attributes that contain characters which must be escaped in CSS --> <p id="item?one">This paragraph has a pink background.</p> <p id="123item">This paragraph has a yellow background.</p>
🌐
Supfort
supfort.com › how-use-id-in-css
How to properly use HTML “id” inside your CSS files
To do this, you will need to call the "id" name inside your CSS file by using the hashtag "#" symbol. Now, this can sound complicated at first but let's take a look to a quick example.
🌐
HubSpot
blog.hubspot.com › home › website › everything you need to know about id in css
Everything You Need to Know about ID in CSS
April 20, 2022 - That means if an element is targeted by an ID selector and a class selector, the CSS style of the ID selector will be applied to the element over the style of the class selector. Let’s take a look at an example demonstrating this rule below.
🌐
University of Washington
washington.edu › accesscomputing › webd2 › student › unit3 › module5 › lesson1.html
Understanding ID and Class in CSS
However, since there's no other element - paragraph or otherwise - that has that same id, we could just as easily have used "#alert" as our selector, without the "p", like this: #alert { color: black; font-weight: bold; background: #FFFF66; /* yellow */ border: 2px solid black; padding: 1em; } It's generally a good practice though to include the element because it helps you to remember which elements had certain id's. Sometimes you'll want to know that just by looking at the CSS file, without having to refer back to the original HTML file.
🌐
W3docs
w3docs.com › learn-css › css-id-and-class.html
CSS ID and Class Selectors | W3Docs
An ID selector is a unique identifier ... must have a specific style. Both in Internal and External Style Sheets we use hash (#) for an id selector....
🌐
HTML Dog
htmldog.com › guides › css › intermediate › classid
Class and ID Selectors - CSS
In the CSS, a class selector is a name preceded by a full stop (“.”) and an ID selector is a name preceded by a hash character (“#”). ... New Examples Section! See all of this code stuff in action, and play around with it.
Find elsewhere
🌐
W3Schools
w3schools.com › html › html_id.asp
HTML - The id attribute
The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id. The syntax for id is: write a hash character (#), followed by an id name.
🌐
CSS-Tricks
css-tricks.com › almanac › selectors › i › id
ID | CSS-Tricks
November 1, 2024 - The #id selector allows you to target an element by referencing the id HTML attribute. Similar to how class attributes are denoted in CSS with a “period” (.) before the class name, ID attributes are prefixed with an “octothorpe” (#), ...
Top answer
1 of 4
4

you should use id #footer and class .page1, .page2, .page3 etc. - it is a better attempt because you still got the same footer (so ID should be the same) and you just want to change something (which can be done using different classes)

EDIT: and a quick tip from me: be carefull of setting width: 100% and border: 1px solid black because border isn't computed in item's width unless you set box-sizing: border-box property

what do I mean is that if you have a 1024px wide screen, your footer with css that you have presented will be 1026px wide with 2px cropped on the right side

2 of 4
1

When you refer to an id or class in css, you must use the full name of the class or id you are selecting. For example, when you want to refer to a div element that has id="someid" you must write #someid { in your stylesheet to reference this div by id.

Anyway, you're thinking about it right but your syntax is a bit off. Here is what you might be looking for:

Copy/* common footer code goes here */
.footer
{
    width: 100%;
    height: 100px;
    border: 1px solid black;
    text-align: center;
}

/* code specific for each page goes here */
#page1.footer
{
    background-color: red;
    font-color: white;
}

#page2.footer
{
    background-color: blue;
    font-color: white;
}

#page3.footer
{
    background-color: white;
    font-color: black;
}

Using two selectors in the same line is called selector chaining. In this case, you want to chain an id selector with a class selector.

Edit: Here is a jsfiddle.

Looking at your code, the obvious "bad habit" one could find is that the ids page1, page2, and page3 are all in the footer div of those pages, which is a bit confusing, as "page" doesn't exactly uniquely define a footer. Make sure you only use one id of the same name on any page, and if you do use an id, it should describe that element uniquely.

As the others have said, it should be noted that recently it has become good practice to avoid using ids (except for javascript functionality). Using only classes whenever possible is now the standard. It's good to know how to preform selector chaining and of course proper syntax is always important.

Top answer
1 of 3
1

It's perfectly fine to have inline CSS. Whether you should use inline CSS or simply set a unique ID for the page depends on the complexity and flexibility of the CMS you're using. Using inline CMS just means that you'll have to update the CSS from each individual page, rather than from a single source for all separate pages.

As for your second point, adding CSS to the head in a <style> tag is not bad practice. In fact, <style> is required to be a child of <head> in order to validate correctly. According to the HTML 5.2 specification, <style> can be a child of any element as long as it it scoped, though at the present date, Firefox is the only browser that can use the scoped attribute.

On top of this, using a <style> tag in the <body> could lead to a flash of unstyled content due to the way in which the page gets loaded. So if you use inline CSS, always do so in the head to both validate correctly, and improve user experience :)

Hope this helps!

2 of 3
0

In a word yes. For reasons of maintainability. You would be better using the cascading nature of css to target a style specific to a specific page than to have lots of inline styles.

For example, consider:

<body class="my-page">
<h1 id="myId">Title</h1>


<body>
<h1 id="myId">Title</h1>

h1#myId{ font-size:12px; }
body.my-page h1#myId{ font-size:14px; }

the latter style will have precedence. h1#myId would be global, yet on a specific page you can override this style. Is there no way to work out what template is in use, and adjust the body class?

🌐
BBC
bbc.co.uk › bitesize › guides › zggs2nb › revision › 6
Using element IDs with CSS - Implementation: CSS - National 5 Computing Science Revision - BBC Bitesize
March 13, 2023 - If a webpage had three different paragraphs, the code within the body could look like this: ... These are three different paragraphs, each containing some random text. It is possible to give a paragraph a unique ID. Here is the updated version of the HTML code. ... The first paragraph now has an id assigned, the id is "introduction". It would be possible to create a CSS rule that will apply to any HTML element called "introduction".
🌐
The Odin Project
theodinproject.com › lessons › foundations-intro-to-css
Intro to CSS | The Odin Project
Add styles to HTML with CSS. Understand how to use the class and ID attributes.