CSS class naming - context in name or not? [closed] - html

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've got (hopefully) quite interesting questions, regarding code semantics. Should a class name be specific in it's role even in a bigger context? I'll just give an example:
I need a class for description container. But on the page, description exists in various places: .page-slider, .offers, .articles etc.
So there are 2 options: either name class .description, and style each one individually in contex of it's parent (for example .page-slider .description). Another way is to make it self-explainable, like .offer-description, .slide-description etc.
The pros of first option are short names and imposing keeping the code inherit depenend (the question is if it's stil the right way, SASS kinda encouraged me to limit the selectors inheritance)
The pros of self-explainable names could be their movability, better explained, if called directly throught jQuery, and minimizing the css nesting. The con is possibly long names in the future (bloat + additional parsing time for browser).
Thanks in advance!

The main factors to choose which method I would use would depend on answering these two questions:
Do I understand what the selector selects?
In your example ".slide-description" and ".page-slider .description" both explain what the selectors select. I personally am in favor of using ".page-slider .description" because it would say to me "I am a description of my parent item page-slider". Using ".slide-description" I would not understand that it is about a description of ".page-slider" without having to read the html (Maybe I would if you called it ".page-slider-description", but it still won't tell me it is a description of its parent-item).
Will my selector allow me to make changes easily in the future?
At some point you might decide to change some things on your website. Having to change every description will get boring fast. Instead you would be better off using ".description" to change some general styles of your ".description" divs. Since they all have the same function on the site they probably share a lot of properties. You can always override the ones you want using ".page-slider .description". Once again I seem to be in favor of the ".page-slider .description" -method.

mmmm I would consider the visual design (if you have one) to see if the description class had common styles throughout its use in .slider, .offer and .articles If it did I would use .description and apply all the common styles. Then add additional styles based on the parent.
You could literally call your description container class .description-cont or .description-container.
If I was writing it in SASS I wouldnt make it self-explainable. I would simply have:
.slider{
//styles
.description{
//styles
}
}
Thanks

Related

When a specific css style is very often used, should it be a own class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
When I need very often a specific style like float: left is it better to make a own class or put this style in every class where it is needed?
Here is a example page for what I mean.
JS-Fiddle Example
Is it good how the class left is used? Or would it be better when I put every float: left style into the other classes?
The things to consider are readability and repeating yourself.
Readability:
Having a left class with the only rule as float: left will help to make your HTML more readable. Because whenever someone see that class on an element, they know it will be floated left. So in that way it improves readability.
DRY:
With CSS the old adage of "don't repeat yourself" is almost impossible to adhere to, but I think it should still be considered. In this case you should compare.
How many times will I add float: left in my CSS? versus How many times will I add class='left' in my HTML?
I would note that most CSS frameworks use utility classes like .left{float:left;}
It's simply a matter of opinion but I'd argue that no, you shouldn't.
HTML and CSS exist separately because they address separate concerns. HTML represents the information and CSS represents how that data should be displayed.
Creating classes containing only one rule starts to blur that distinction and starts to introduce style-specific information into your HTML.
Say you want to change all your stuff that was floated left to be floated right. You could either change your CSS rule to something like
.left {
float: right;
}
which is obviously horrendous or else you'd have to go into your HTML and change the class in every situation you wanted to change the value of the float - not ideal either. In a perfect world, you want to be able to make styling changes ONLY by editing the CSS. That's what it's there for. Obviously sometimes this just isn't possible but a lot of the time it is if you marked up your HTML in a semantically meaningful way.
There isn't anything wrong with doing that.
I create css helper classes for myself all the time.
When you're not using a framework like bootstrap it really helps to be able to add class="border" your html to quickly see what's going on in the box model
There are many opinions on top of this,
depends on your context...
In Sass there are placeholder selectors, you should have a look on how they work
Other way is creating helper classes, an example could be the pull-left implemented in bootstrap...
A simple class that does just one thing in order to be reusable everywhere in your code...
The first solution increases the output css file, the second, instead, increases the html file...
Maybe the secondone could be better that the first.

How to locate element in selenium which has no unique identifier? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I faced following question in interview: How to identify a web element that has no unique identifier and even it has no div or select or anything. I was unable to understand the question itself. Can anyone help me here?
Interview questions are always a challenge, and typically are trying to illicit a response (positive or negative) from the interviewee, that typically has little to do with the subject of the question so that they can gain a sense of your level of understanding or personality.
Depending on the interviewer, they might have been looking to see if you were well rounded in programming. I don't know what position you were applying. Assuming Selenium doesn't have any 'magic' method to find said element, perhaps the interviewer wanted to know if you could write, or understand parsing web-code programmatically.
Perhaps they were looking for you to quantify the element programmatically as to find it based on a parametric search.
Both of these concepts would show understanding of programming fundamentals to the interviewer without ever talking about specific code.
Well, there are so many different techniques to locate elements in the HTML. It's too broad to answer exactly, so, if I were you, I would just list the possible techniques with multiple examples. XPath expressions and CSS selectors are there to the rescue.
Tag names, id, name, class or any other data-related attributes are usually a good and reliable choice to locate elements. If none of these is present, it would depend on where the element is located, what parents, siblings, ancestors etc does it have and how unique the element text, corresponding label (if any), parents are - too many variables in action.
For instance, imagine you have the following HTML:
<span>
<label>Category:</label>
<b>Desired text</b>
</span>
The desired b tag here itself does not have id or name, but it's easy to see that we can probably rely on its preceding sibling and use this XPath expression:
//label[. = "Category:"]/following-sibling::*
Sometimes we know that the desired element is at a specific position in an element. For example:
<tr>
<td>text1</td>
<td>Desired text</td>
<td>text3</td>
</tr>
In this case, we can simply get the second td from the tr:
//tr/td[2]
Sometimes, there is something in the "text" of an element:
<div>The quick brown fox jumps over the lazy dog</div>
Let's say we know that "fox" is there:
//div[contains(., "fox")]
And so on.

OO CSS/CSS components [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am wondering, which way is real objective oriented CSS where you can reuse components really quickly in a clean way.
Scenario 1:
HTML:
<button class="button button-submit">Submit</button>
CSS:
.button {
/* basic styles */
}
.button-submit {
/* special styles for submit button */
}
Scenario 2:
HTML
<button class="button submit">Submit</button>
CSS
.button {
/* basic styles */
}
.button.submit {
/* special styles */
}
I only see two negative aspects, one in each scenario.
In scenario 1 you might end up having really long class names.
In scenario 2, if you define .submit as it's own element/component, you end up with the old problem where you cannot reuse code fragments to stay the same in any place.
Scenario 1 would probably be the more OO one but like all technologies and methodologies I would use the one that suits your needs best and you are going to enjoy writing.
Currently I am rebuilding an ecommerce platform from the ground up and when researching wanted to adopt similar methods. I ended up ignoring ways similar to your first example due to the long class names like you mentioned.
The idea behind all of it is to make writing code easier, quicker and more reusable. As soon as one of those factors is impacted too much while trying to satisfy the other, the whole idea behind trying them in the first place is voided.
Just to note this is a purely subjective answer and everyone is different.
Some good articles on the various methodologies and practices that I used to help make a decision:
http://www.smashingmagazine.com/2012/04/16/a-new-front-end-methodology-bem/
http://webuild.envato.com/blog/how-to-scale-and-maintain-legacy-css-with-sass-and-smacss/
https://smacss.com/book/
http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/
Hope this helps!
CSS is not OOP, you can however, use frameworks which allow to you code CSS in a DRY manner.
SASS
LESS

What is better css selector practice: Using multiple element or using one id/class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is better using Alt A header nav ul li a {color: red} or Alt B .nav-link {color:red}?
The pros for Alt a is that i don't need to introduce any more css id/classes, but it is more prone to specificity war than alt B.
Check out jsfiddle: http://jsfiddle.net/43znf/1/
This really is subjective. When I first learned I did everything using Alt A, but now I do a mix of A and B. Alt A will apply the style to every <header> <nav> <ul> <li> <a> nested element, meaning that if you have multiple sections that match this nest pattern they will be styled in the designated way. When you use Alt B, you have to apply the class / id to a certain element, meaning that you can pick and choose which nested <a> tag will receive the style.
Bottom line, it really is not practical to just code in Alt A or Alt B. I would recommend using a little of each.
EDIT: If you plan on getting a job where HTML / CSS editing is required, your boss may have a certain way he or she wants it done. Just some heads up.
EDIT 2: It's also a good idea to know when it's the appropriate time to use an ID and when a Class should be used. ID's should only be used once in a document, classes can be used multiple times.
It depends on how versatile you want your CSS to be. If you have a single element that you want styled or just a handful, use the id or class. However, using Alt A will allow you to add new elements without necessarily having to assign the id/class.
You could use both and both have valid use cases in real life.
When you have an element that is unique and sure that its styles need not be used anywhere else you could make use of id for simplicity.
eg: Header section of your website "template" or "layout" which remains same and probably you would not reuse the styles.
But when you have to style an element say a form button, you have to use a css class, as the form button will be used many a times as you would see.
Using classes can be elegant in that if you stick to the principles. Do not overstyle an element using a single class. Split the rules in an intelligent manner so that each class can be used somewhere else. Try to avoid writing context specific rules in a class which will block you from inheriting the class.

multiple classes on single element html [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Is it a good practice to use many classes on one single HTML element? For example:
<div class="nav nav-centered nav-reversed navbar navigation-block"></div>
I don't mean that two or three classes on one element is bad, but how about four, five or even six?
Short Answer
Yes.
Explanation
It is a good practice since an element can be a part of different groups, and you may want specific elements to be a part of more than one group. The element can hold an infinite number of classes in HTML5, while in HTML4 you are limited by a specific length.
The following example will show you the use of multiple classes.
The first class makes the text color red.
The second class makes the background-color blue.
See how the DOM Element with multiple classes will behave, it will wear both CSS statements at the same time.
Result: multiple CSS statements in different classes will stack up.
You can read more about CSS Specificity.
CSS
.class1 {
color:red;
}
.class2 {
background-color:blue;
}
HTML
<div class="class1">text 1</div>
<div class="class2">text 2</div>
<div class="class1 class2">text 3</div>
Live demo
It's a good practice if you need them. It's also a good practice is they make sense, so future coders can understand what you're doing.
But generally, no it's not a good practice to attach 10 class names to an object because most likely whatever you're using them for, you could accomplish the same thing with far fewer classes. Probably just 1 or 2.
To qualify that statement, javascript plugins and scripts may append far more classnames to do whatever it is they're going to do. Modernizr for example appends anywhere from 5 - 25 classes to your body tag, and there's a very good reason for it. jQuery UI appends lots of classnames when you use one of the widgets in that library.