how to properly use CSS Class and ID [closed] - html

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Just after some beginner advice please on how to properly arrange some CSS.
I have a basic 3 page website. Each page will all display a footer at the bottom.
The footer will keep the main styles the same across each page (things like width, height etc) but I'd like to change other styles dependant on which page I'm on (background color, font color). I know this can be done, but I'm looking for some tips on the correct syntax to use so don't learn using bad habits, unless of course this is already correct?
What I have in the CSS is:
.footer
{
width: 100%;
height: 100px;
border: 1px solid black;
text-align: center;
}
#footer_page1
{
background-color: red;
font-color: white;
}
#footer_page2
{
background-color: blue;
font-color: white;
}
#footer_page3
{
background-color: white;
font-color: black;
}
...and to call it in the HTML I have:
<div class="footer" ID="footer_page1">
Some text here
</div>
Is this OK, or should this be done a better way?
Many thanks.

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

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:
/* 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.

It's inadvisable to use IDs in CSS at all although it can be useful sometimes. In fact I would advise against using anything except classes and pseudo-classes and occasionally attribute selectors (although I personally use ID and element selectors all the time mostly out of laziness). The reason for this is so that you only have to work with one level in the cascade which simplifies things quite a lot in your stylesheets, especially if they grow very large.
.footer { /* default styles */ }
.page1 { /* this is already after the .footer ruleset, so it overrides
the earlier rules automatically (by the nature of CSS */ }
.page2 { /* and so on */ }
<div class="footer page1">
Some text here
</div>
You could also add the class to the container of the entire page or something, which may make more sense. That way you can manipulate the header and footer rulesets simultaneously:
.footer {}
.header {}
.page1 .footer {}
.page1 .header {}

You need to select <div class="footer" ID="page1"> by .footer#page1.
I would recommend to use class for page#, because ID should be unique in one page, but ID="page1" might be used frequently.
Finally result can be
.footer
{
width: 100%;
height: 100px;
border: 1px solid black;
text-align: center;
}
.footer.page1
{
background-color: red;
font-color: white;
}
.footer.page2
{
background-color: blue;
font-color: white;
}
.footer.page3
{
background-color: white;
font-color: black;
}
with
<div class="footer page1">
Some text here
</div>

Related

CSS Styling approach

Which is the more preferred style of CSS styling in the industry?
Create classes for each attribute (like bootstrap), then attach the classes in HTML. For example:
HTML: <p class="text-white text-bold"> John Doe </p>
CSS:
.text-white {
color: white;
}
.text-bold{
font-weight: bold;
}
Add usually 1 class per html tag. For example:
HTML: <p class="description"> John Doe </p>
CSS:
.description {
color: white;
font-weight: bold;
}
Which is overall the better approach?
Based on my experience, I am usually doing the first approach. Reason for this is for code reusability, because you can still apply it to any elements that would be needing the same styling.
Always name your classes as generic as possible.
Example:
.hand { cursor:pointer; }
.padding-5 { padding:5px; }
.padding-left-5 { padding-left:5px; }
.border-1 { border:solid 1px #f00; }
.corner-1 { corner-radius: 3px; }
.margin-auto { margin: auto; }
.bgcolor-1 { background-color: #b2e8ff; } /* light blue */
.bgcolor-2 { background-color: #FFF3CD; } /* light yellow */
.table-hover tr:hover { background-color: #eaf6ff !important} /* overide bootstrap's table hover */
Don't do it like that. Having only one style in a class will get confusing it would more so than putting all the styles for a element in its tag as
<div style="border:solid 1px #333; color:#0f0; etc;
What I found is a better approach is to create a generic type group class as
.boxStyle1{
border:solid 1px #333;
color:#0f0;
}
.fontStyle1{
font-family: Arial, ...;
font-size:.9em;
}
And using them in different elements such like
<div class="boxStyle1 fontStyle1">
Using the group style approach lets you apply the same theme for different elements depending on what look you want. This way its less confusing when you want to change the look of your site.
I learned something similar to the second method. You specify style class/id names indicated in the HTML tag, <p class="sample">...</p>, then outline those details in a separate CSS file:
.sample {
font-family:Arial;
font-variant:small-caps;
color:#FF0;
text-align:center;
}
One of the primary benefits is efficiency. With all of the style details in one file, it's loaded once for your site visitor and the information is available to render for every page of your website that they visit. Thus reducing the amount of time it takes for your pages to load and improving the user experience. It is also more efficient for you while you are coding your pages. Once you decide how you want your website to look, you can create all of the element styles and reference it as often as necessary, greatly reducing the time to create your site. (Note: this does not mean that you can not or should not have more than one CSS file.)
There are occasions when you want to list more than one class/id style for an element: <div class="sample sample2"></div>. Sometimes it makes sense to do it, however, seperating each individual style into it's own class/id is superfluous. Learn to group everything for your class="sample" in your CSS .sample (class).
The W3C, Mozilla, and the online courses offered through edX all support this method as well. Which should clearly indicate that it is the "industry standard".
W3C Starting with HTML + CSS
Mozilla's Introduction to CSS Layout
edX's CSS Courses

Get attribute value in LESS?

In css we have something similair for :pseudo elements. According to this, it will come in the future. At the moment its not supported by any major browser http://caniuse.com/#feat=css3-attr
.test {
display: inline-block;
background-color: gray;
color: attr(data-color); /* Doens't work in css */
width: attr(data-width px);
}
.test:after {
content: attr(data-name);
}
<div class='test' data-name=" - CONTENT" data-color="#f00" data-width="200">test</div>
But what i want is, lets say ive the following div
<div data-color="#f00">...</div>
In LESS i want to be able to pick that color through the data attribute.
.example-class {
color: attr(data-color); /* Something like this */
}
Is this, or something similar, possible using LESS?
Considering that LESS compiles to CSS anyway, and therefore never knows about the HTML, that doesn't seem possible. That is the entire reason why the attr() function is in CSS, not LESS.

Why won't CSS class work in browser? [duplicate]

This question already has answers here:
Is there a reason why CSS doesn't support ids and classes, starting from numbers?
(8 answers)
Closed 8 years ago.
I have this html;
<div class="1"><br/>hi</div>
<div class="2"><br/>hi</div>
<div class="3"><br/>hi</div>
<div class="4"><br/>hi</div>
and then I added normal CSS formatting to the divs;
div{
height: 100px;
width: 100px;
border-radius: 100%;
margin: 20px;
display: inline-block;
text-align: center;
}
and then i wanted each div to be a different colour so I used the classes like this;
.1{
background-color: pink;
}
.2{
background-color: red;
}
.3{
background-color: orange;
}
.4{
background-color: yellow;
}
I am writing this in dreamweaver and when i click on the divs the little class thing tells me that they are coloured and the code is working, but when i preview in a browser the colours are not showing up and I just get the div part of the CSS.
it's probably very obvious but I can't think of why this is happening.
Thanks :)
Please avoid using classes with number at the beginning. It will fail for sure.
You can use for example cl1, cl2, cl3, etc.

Some questions about CSS rules priority

I have the following CSS problem: a website first import a CSS style file named bootstrap.css (The BootStrap framework CSS settings), then it is imported another CSS file named my-custom-style.css that override some of the bootstrap.css settings (so I can create some custom settings leaving unchanged the bootstrap.css file)
Now I have the following situation, in the bootstrap.css file I have this property that I want to override:
.img-thumbnail {
background-color: #FFFFFF;
border: 1px solid #DDDDDD;
border-radius: 4px;
display: inline-block;
height: auto;
line-height: 1.42857;
max-width: 100%;
padding: 4px;
transition: all 0.2s ease-in-out 0s;
}
Now I have to override it in my my-custom-style.css file in such a way that the .img-thumbnail object have no border. So I delcare a
.img-thumbnail {
}
and I want to say to CSS that the following field (setted in the **bootstrap.css) must not exist in the overrided file (so I have not the border)
background-color: #FFFFFF;
border: 1px solid #DDDDDD;
border-radius: 4px;
height: auto;
Can I do something like this or have I to override it with a specific value?
I tryied to override it with a specific value but I can override the background-color with a new color value (and it work) but when I try to change the border value to 0px it still use the bootstrap.css definition
Can you help me to solve this problem? I think that exist an elegant way to simply say: "don't use the overrided file settings without explicitly override it with new values
Tnx
Andrea
Basically, the CSS engine will decide which rule to use based in 3 things (listed here in order of importance):
!important clauses
More specific rule
Rule order
Now, check out this fiddle.
First, let's talk about the order. We have:
div { background:green; }
and
div { background:gray; }
So, which background CSS will use? green or gray? They are both rules with no !important clauses, and have the same specification level (both are applied for div) only remaining the order to decide. In this case gray comes last so it will be applied to all div elements.
Now, the "specificness" of the rule.
#div1 { background: red; }
This one is a much more specific rule than the other rules that apply only to div elements. So #div1 will have a red background even with a div{ background: gray; } coming later.
And last, but not least !important.
These rules are... important. They can only be overridden by another !important rule that comes later and have the same specific level.
Even if a !important rules is declared in a lower level of specification, it won't be overridden. Like in:
div { width:50px !important; }
#div2 { border:3px solid blue; width: 100px; }
Even coming later and being more specific, width: 100px; will not be applied to #div2.
Now that you know all of this, it's a matter of inspecting the element to see what's going on and then guess "how much power" you'll need to override that rule.
yeah, just override that class in your own css file and add !important at the end

Why is IE6 not rendering this border properly?

I am currently finishing a site that the client wants to work on all browsers. However, there seems to be a CSS issue which I can not get around. The border around this page seems to start from the middle of the page, as opposed to surrounding the entire page. It works on all other browsers though. I am guessing that it is a float problem, but the #contact-form underneath has basically the same CSS applied to it but the border still surrounds it, while the #info seems to have broken out of the border.
The webpage in question is http://lunaskymoda.co.uk/contact-us/
The only validation error is with an unregistered keyword "Nextgen", but i doubt that is the problem. I have spent an entire day tackling this and cannot seem to come up with a reasonable explanation as to why this is happening.
the CSS for the possible HTML elements producing the error are:
#main #main-content {
border: 1px solid white;
display: block;
margin: 12px 0;
background: black;
}
.contact #main-content .info {
margin: 10px;
width: 300px;
font-size: 14px;
color: white;
float: right;
display: block;
}
You're not the first one to have issues with ie6 :)
The problem is of course the "clear: both" of the clear class not being honoured, so you need to hack it.
Here's a possible approach:
http://damienhowley.wordpress.com/2009/04/01/ie6-hack-replacing-clearboth/
You may also try to replace the <div class="clear"></div> by <br clear="all">.