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
Related
I am using two CSS classes for highlighting the icon color as white and changing the 'h6' tag background-color as #325868.
The icon color is by default black in color. Now, I have actually used a '.highlighted_fileName' class for highlighting the 'h6' element and my icon is placed in a 'span' tag just beside the 'h6' tag. Below is my code that I have tried, please refer to the same.
.highlighted_fileName{
color:white !important;
background-color: #325868 !important;
.closeTab{
color:#ffffff;
}
}
<h6 class="float-left fileName card elementSelectedId truncate" data-toggle="tooltip" title=` +noOfEditors[0].pathTitle+ ` id="featureFileName_1"
style="border-color: cadetblue; left: 0.5vw; top: 2vh; font-weight: bolder; border-width: thin;"
onclick="openEditorTab(this,noOfEditors[0].pathTitle,noOfEditors[0].fileName);"> `+ noOfEditors[0].fileName +`</h6><span><i id = "icon_1" class="fa fa-times closeTab" style="position: relative; top: 26px; right: 10px;display:block;" onclick="closeTab(this,noOfEditors[0].pathTitle,noOfEditors[0].fileName,document.getElementById('featureFileName_1'));" aria-hidden="true"></i></span>
I know this is not true as I have tried it and it didn't give me the result as my expectation.
So can anyone please tell me how can I achieve this. Also, please refer to the below picture for more information.
Add The Following
.highlighted_fileName + span i{
Color:#fff:
}
Please, do not use !important. Also for css kebab-case is better.
Check this, just rename it as you want, and put needed colors.
.label {
color: #000;
background-color: #fff;
.close-icon {
color:#000;
}
}
.label.active {
color: #fff;
background-color: #000;
.close-icon {
color:#fff;
}
}
Unless you are using a CSS preprocessor like SASS or LESS, nesting rulesets is not permitted. Thus, your closeTab ruleset is likely being ignored.
To address this, simply break out your closeTab class from within the highlighted_fileName class as follows:
.highlighted_fileName {
color:white !important;
background-color: #325868 !important;
}
.highlighted_fileName .closeTab {
color:#ffffff;
}
Depending on the specificity of the rulesets provided by Font Awesome, this may still not work. At minimum, you'll be providing the browser with CSS it can understand. Hope this helps!
P.S. As Vitaliy mentioned, using !important should be avoided unless absolutely necessary. I don't fully understand your use case, but I think you should be able to get away without it.
I have recently learnt how to create a html internal link; allowing my members to jump down the page to specific information.
However the coding I have used is set at a standard size and font. I would like to edit the font size and font style of the topic title.
a name="category-one">Under 6's</a>
Above is my current coding; how can I increase the text of the title "Under 6's"?
you can use inline style in a element this way
<a name="category-one" style="font-size:18px; color: green;" >Under 6's </a>
You should always use css for styling.
You can give each element a separate class and style those accordingly.
Under 6's
In css:
.title a{color: blue;}
There's much more efficient ways to refer to an object.
class is versatile in that you can apply it multiple times, this is the prefered way.
id is not as versatile because each id must be unique so your'e limited to just styling a single element.
a[name='category-one'] {
font-size: 16px;
font-family: "Palatino Linotype";
}
a.category-two {
font-size: 1em;
font-family: "Source Code Pro";
}
a#category-three {
font: 1rem;
font-family: "Verdana";
}
a:hover {
font-size: 20px;
font-family: "Arial";
}
<a name="category-one">Under 6's (refer by name attribute)</a>
<a class="category-two">Under 7's (refer by class attribute)</a>
<a id="category-three">Under 8's (refer by id attribute)</a>
There's three ways to apply styles:
The prefered way is using a separate file (e.g. style.css) and then pointing to it from your main page:
<link href="http://www.example.com/css/style.css" rel="stylesheet"/>
It may be more work to maintain a separate file, but it can be used by multiple pages on your site.
Another way to provide CSS rules is to use the <style> element and place that before the closing </head> tag. Although faster when loading, the code will become difficult to manage and it can only be used by one page (the page the <style> is on.)
....
<style>
.foo { height: 60px; }
....
</style>
</head>
Inline styles are discouraged and should be used sparingly if at all. They are limited to only the element they are on and harder to locate and debug. One advantage is that the rules will take priority over all other non-inline style rules (or should I say, most of the time, because there's always a bug or edge cases).
<a name="category-four" style="color: red; background: #000;">Under 9's</a>
I feel a bit silly asking this because it's so basic, however I appear to be stuck.
Using CSS I want to apply a style to all the IMG elements on the page where no other class has been applied.
So, for example let's say I want all my images on my page to have have a red border, unless a class is applied which says different. This is basic stuff, right? But I'm finding that my other classes are overridden by the base IMG class.
In the following simple example I would like a red border on the top image and a green border on all the others:
CSS:
<style>
IMG {
/* I want this to apply to all images except those where other style applied */
border: solid 12px red;
width:50%;
}
IMG:nicepic {
border:solid 12px green;
}
</style>
EDIT: Problem was a typo and nothing more. IMG:nicepic should have read IMG.nicepic ("." instead of ":"). This entire question is based on a simple typo in my CSS; I tried to delete the question but was denied permission to do that by stackoverflow.
The class selector in css is a dot (.):
img.nicepic { /*not img:nicepic*/
border:solid 12px green;
}
hey there is small change
CSS CODE
IMG {
/* I want this to apply to all images except those where other style applied */
border: solid 12px red;
width:50%;
}
IMG.nicepic {/*not (:) is (.)*/
border:solid 12px green;
}
See Demo http://jsfiddle.net/JentiDabhi/ygco1ahf/
Why dont you just create a separate class for the first image, so you won't have to use the general "img"?
So I want to have a horizontal rule <hr> with a padding of 50px using CSS. However, I already have custom CSS assigned to the <hr> tag which already has padding-bottom:25px; padding-top:100px; padding-left:50px; padding-right:50px; Which is being used to divide the footer from the main content. So how would I achieve having two different CSS styles for the same element?
P.S. I am using Twitter Bootstrap 3
I actually found the answer to what I wanted after all this time.
Basically i did the following:
hr.custom {
margin: 10px;
etc...
}
Then I used:
<hr class="custom">
There are plenty of different ways to add different CSS to different HTML elements. Mainly, we use IDs and classes, read more about IDs and classes on the W3Schools website, I use it all the time. Here is an example of using both the ID and class system to style CSS:
The HTML:
<p class="coolclass">This is the cool class</p>
<p class="redclass">This is the red class</p>
<p id="strangeid">This is the strange ID</p>
The CSS:
.coolclass{
color: blue;
}
.redclass{
color: red;
}
#strangeid{
background-color: green;
color: white;
}
Here is a live example: http://jsfiddle.net/Xanco/8sex7x3p/
New live example with HR stylings: http://jsfiddle.net/Xanco/8sex7x3p/2
Let me make it simple then ..
give it an id and style it accordingly
See it here
Just for this element, you can add a new class to your <hr> like this:
<hr class="actualClass newClass">
with this CSS:
.newClass {
padding: 50px !important;
}
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>