Why would you use "div class" instead of "div id"
Usually when I name a div I use "id" to give a name to an element and with set parameters. I really only use "class" to give it special characteristics. For my example .
However, I have a professor who will "class" to identify all his divs. For example prof example .
Can someone explain the benefit of something like "div class" instead of "div id".
<div class="some_class">
<p>This can be used any number of times on one HTML page</p>
</div>
<div id="some_id">
<p>This CAN be used multiple times with the same ID,
but it is invalid code, as a specific ID should
only be used ONCE per html page</p>
</div>
Here's an older yet still good explanation.
ID are unique for the page, so it's better for identification.
Class aren't unique and are supposed to be to group similar style things together.
Said otherwise, if you need to apply something to THAT SPECIFIC div, you should call it by the id, because it will take priority over other CSS styles that may affect it.
Classes will allow you to set some common ground in your style so you can use similar fonts or sizes in different kind of elements.
If you are using Javascript there is a big advantage of using classnames to identify elements instead of id's.
Giving an id to an element also creates a global javascript variable with the same name. See Do DOM tree elements with ids become global variables? for more about that behaviour.
Having such implicitly created variables is at best confusing, and at worst leading to hard to find errors.
Related
So, I've read recently some posts about HTML5 best practices and all of them have the following practice:
"Use id attribute instead of name attribute"
Is that right? I mean, how am I going to handle forms in PHP, for example, if not by inputs' name attribute?
Look here for reference:
Difference between id and name attributes in HTML
https://teamtreehouse.com/community/what-is-the-difference-between-id-and-name-attributes-in-form-elements
Names can be used for more elements, while ids are unique. This helps with styling multiple elements without repeating your css code.
Long story short, id is not meant for naming form elements.
You use id to make it easier for CSS or JavaScript to handle that one particular element with the unique id.
Whether you're using CSS or JavaScript/jQuery with your forms, an ID is handy for most inputs/elements because you can reference them like easily. Remember, ID's are unique:
<div id="example1"></div>
If you have an element like I mentioned above, you can always call it later by doing this:
var x = document.getElementById("example1").id;
along with many other ways.
Names can sometimes be tied to a variety of elements on the page, whereas there will always only by one item with that particular ID.
This is almost going to sound like a joke, but I promise you this is real life. There is a site on the internet, one which you have all used, that does not believe in css classes. Everything is defined directly in the style tag on an element. It's horrifying.
My problem though is that it also makes the html extraordinarily difficult to parse. The structure that I've got to go on looks something like this:
<td>
<a name="<random_string>"></a>
<div style="generic-style, used by other elements">
<div style="similarly generic style">{some_stuff}</div>
</div>
<a name="<random_string>"></a>
...
</td>
Basically, I've got these a tags that are forming the boundaries of the reviews, whos only defining information is the random string that is their name. I don't actually care about the anchor tags, but I would like to grab the reviews between them using xpath.
I've looked into sibling queries, but they don't seem to be well suited for alternating boundaries. I also looked into the Kayessian method of xpath queries, which (aside from having an awesome name) only seems well suited to grab a particular div, rather than all divs between the anchor tags.
Any thoughts on how I could grab the divs here?
If //td/div[../a[#name]] works for you, then the following should also work :
//td[a/#name]/div
This way you don't need to go back and forth -or rather down and up-. For a more specific selector, you may want to try the following :
//td/div[preceding-sibling::*[1][self::a/#name]][following-sibling::*[1][self::a/#name]]
The XPath selects div element having all the following properties :
td/div : is child of <td> element
[preceding-sibling::*[1][self::a/#name]] : preceded directly by <a> element having attribute name
[following-sibling::*[1][self::a/#name]] : followed directly by <a> element having attribute name
I figured it out! It turns out that xpath will allow for relative attribute assertions. I am not sure if this behavior is desired, but it happens to work in this case! Here's the xpath:
//td/div[../a[#name]]
Nice and clean, the ../a[#name] basically just says:
Go up a level, and make sure on that level of the hierarchy there's an a element with a name attribute
i'm new to html/css and completed the css/html tutorial on codeacademy. I've reread over everything i've done and have a good knowledge around why everything works the way it does except the class/id tag. If i understand correctly the class tag is useful for when a bunch of elements should all recieve the same styling and id's are useful when you have exactly one element that should receive it's own styling. The thing i don't get is then what is the point of the id tag if i can get the same result using the class tag. For e.g. i have boxes 1,2 and 3 and i want them all to be the color black aka all recieve the same styling i would use the class tag. But i change my mind and now i want box2 to be white so in theory i should change box2 to an id tag so it can receive it's own styling but the thing is i can still use the class tag and get the same result by typing .box2 color:white;
My question is what is the point of the class and id tag if i can do the same thing for both using just the class tag.
Sorry if this is a difficult question to understand. I tried to word it as best as i could.
Ids are unique, you use id only for one element
<div id="me"></div>
On the other hand classes can be used to target more than one element
<div class="book">The Alchemist</div>
<div class="book">Harry Potter</div>
id tag is actually utilized the best in JavaScript, it is used to identify a tag uniquely among a bunch of tags. You would realise the importance of id tag when you start working with JavaScripts.
Suppose you have around 50 <p> tags in your HTML code. But you want to get value of one particular <p> tag, then the obvious way to do this is making you use of id.
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Date();
</script>
The above code checks for the tag whose id is "demo" and then assigns the output returned by Date( ) to that corresponding tag.
id is for single element.
class is for group.
if you want to change color of box2. you can alse give inline css on them because inline css priority is higher then another type
Usually when i create some HTML template, i tend to prefix and class (or id) everything, This is my way of keeping the markup more readable, and also it makes styling a lot easier.
For example if create a template called MyTemplate, then i prefix all elements like this
<form id="mt-form-blue" class="mt-form">
<input class="mt-input-large" type="text" />
<input class="mt-input-small" type="text" />
</form>`
I've seen lots of HTML templates, where the creators make very little use of classes and ids.
My question is why? Does using many classes and ids, have an impact on the browser performance? Does it have any dangerous side effects?
CSS selector do impact performance, but how much it impact will depend on its complexity and what operators were used. Usually you won't notice such tnings, but there are plenty of researches for this matter on the web.
Regarding the use of selectors, listing only the basic three, the use of them depends on what is your intention, for example:
tag name selector - When you want apply style to every element of this type
class selector - when you want to reuse your style in more than one element
ID selector - when you want to apply your style to only one element per document
Of course, their use is not limited for the list above but I believe it contains the most common use cases.
Trying to learn html/css. I've been looking at the html & css files of a couple different websites that have something along the line of:
<span class="mytest anothertest">some text goes here</span>
I understand the "mytest" part but what does "anothertest" do? There's no reference to that anywhere in their css or html files.
anothertest is just another class like mytest. You can apply more than one to an element.
There are several possible reasons for the presence of a class name in a class attribute value. Using the class in page stylesheets is probably most widely known, but not the only one:
The class name can be used in JavaScript in order to process a set of elements conveniently. (Using document.getElementsByClass is one way to achieve this; another way is to use jQuery; and you could even hand-code it rather simply.)
Designated class names are used in some metadata systems, such as microformats. Some search engines recognize such names and use them to provide semantic searching (though this approach probably loses to microdata, which uses different attributes).
A class name can be used in a user style sheet, e.g. by a developer who wishes to do some testing. This could well be the case if the class name is literally “anothertest.”
The name might be there to allow future development, e.g. so that elements of a class will be or may be styled in some future version. The designers might have ideas on styling but they haven’t decided on it—they just want it to be easy when they are ready.
It could be just a holdover. It was a class that had some use, but things changed. There was really no particular reason to remove it.
This is a very good question. It involved the difference between id and class.
ID
An ID placed on an element, is a unique identifier for that element. An element may only have one ID, and only one of the same ID may exist on a page. So for instance, the following examples are not possible.
<a id="someid anotherid">Multiple IDs - Wrong</a>
<a id="someid"><span id="someid">Same ID twice - Wrong</span></a>
Class
A class name however, is the exact opposite. An element may have several class names, and the same class name may appear multiple times on a page. Like so:
<a class="someclass anotherclass">Multiple Classes - Correct</a>
<a class="someclass"><span class="someclass">Same Class twice - correct</span></a>
In short, the syntax displayed in the question is simply having 2 class names on one element, which is perfectly acceptable.
Class name are also used to easily select elements in the page with JavaScript. You can use the getElementsByClassName method to access them or using your favorite CSS selector library (ex.: Sizzle) if you need compatibility for older browser.