The difference between ids an classes in HTML [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 6 years ago.
Improve this question
I am learning HTML and CSS from a book and I've faced a problem with connecting between the ids and classes, can any one answer me when to use them?
<p id=”footer”>Please steal this page, it isn’t copyrighted in any way</p>
<p class="guarantee">
The book said : Giving an element an id is similar to adding an element to a class. The only differences are that the attribute is called “id”, not “class”, an element can’t have multiple ids, and you can’t have more than one element on a page with the same id.
but still not understand when to use them :P
thanks guys

classes are generally used to group elements together while ids are used to identify specific elements.
<div class="buttonContainer">
<button id="home">home</button>
<button id="about">about</button>
<button id="contact">contact</button>
</div>
<div class="buttonContainer">
<button id="email">email</button>
<button id="query">query</button>
</div>
another example
<div id="header">
<p class="title">Header Title Here</p>
</id>
<div id="footer">
<p class="title">Footer Title Here</p>
</div>

The quick answer is that an id should be used to identify a specific element on the page and all ids should be unique. A class, on the other hand can be used to identify multiple elements with the same or similar meaning.

You've already mentioned the main differences. But when it comes to the CSS #id selector, there are other things to consider:
See, for example:
<div id="mydiv" class="mydiv"><p>Hello there.</p></div>
CSS:
#mydiv {
color:red;
}
.mydiv {
color:blue;
}
This will yield red colored text because IDs are more specific and thus more relevant to CSS, they are a priority with respect to classes.

ID's are unique
Each element can have only one ID.
Each page can have only one element with that ID.
Classes are NOT unique
You can use the same class on multiple elements.
You can use multiple classes on the same element.

The 'id' attribute should be unique within the same webpage although it is possible to declare more than one element with the same id. There can't be more than one element with a particular 'id'.
There can be multiple elements on the page with the same 'class'. This is useful when you want, for example, apply same CSS style to several elements.
The 'id' is used, for example, in Javascript, for getting an element with a specified id, or when you want to apply a style to only that element.

Related

Are these HTML tags semantically correct? [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 last year.
Improve this question
I'm starting at web development and want to divide the content of one page in 2 columns, but I'm not sure if the following HTML semantics are correct.
<body>
<div class="left">
<section>
<h2>One</h2>
<img ...>
</section>
<section>
<h2>Two</h2>
<img...>
</section>
</div>
<div class="right">
<section>
<h2>Image related to those of the other div</h2>
<img...>
</section>
</div>
</body>
In any case your page is missing an h1 tag, which is essential for semantics, SEO and accessibility!
The rest, for the described purpose of presenting some images, seems okay. You wouldn't necessarily have to use section tags, though – the h2 tags are enough to organize them as images with related headers. But if those parts of the page are really thematically different, sections are a good way to emphasize that.
Welcome to the development community! Accomplishing something like this would require some CSS to display the columns like you're envisioning. Try checking out CSS flexbox. It allows you to create any kind of columns and rows you like based on the device viewing your website.
With the HTML portion, you're on the right track! We just need to wrap the image and it's description(or caption) inside a figure element for it to be syntactically correct. This method will also improve accessibility to people using screen readers.
We use a <figure> element with the <img> and a <figcaption> element inside of it. The <figcaption> element is literally a capture for the figure you're displaying. Here's a quick example:
<figure>
<img src="/macaque.jpg" alt="Macaque in the trees">
<figcaption>A cheeky macaque, Lower Kintaganban River, Borneo. Original by Richard Clark</figcaption>
</figure>

Html h1 tag inside span tag [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 1 year ago.
Improve this question
I have a task where I need to add some h1 tags on several pages inside a web application. The texts already exists but wrapped inside other tags which I need to convert to h1 tags. While I do this I need to keep the same design on the page, so design wise nothing should change. We need h1 tags for google bot.
Let me show you below an example. Currently I have on a page this:
<span class="container">
<span class="icon"></span>
<span class="page-title">
<span class="title">Title</span>
</span>
</span>
And I need to insert h1 there. I've read on the internet that it is not a good idea to add a h1 inside a span because my first idea was to replace the span.title with h1 and style that h1 in order to look like that span.
The whole block is basically an angular component which is used just for this heading section (you can see it also contains an icon before the text).
For this particular situation we have to deal with spans, but in other cases there are divs or other elements.
What is the best way to handle this situation ?
<span>s can't contain <h1>s;
A <span> is an inline element
A <h1> is a block element
An inline element cannot contain a block element
Since the <span> is an inline element, it may contain another <span>.
However, to create a clean DOM, using the <h1> for the Google Bot, you should consider changing the DOM to:
<div class="container">
<span class="icon"></span>
<div class="page-title">
<h1 class="title">Title</h1>
</div>
</div>
I'm not sure why all the tags are span tags, and would strongly encourage refactoring this code so that it contains semantic tags. While Omiod is correct that the site won't break, it also won't be very accessible. Using semantic tags creates a clear information hierarchy.
At the very least I would (as #gilbert-v suggests) put a h1 tag in place of the title span. Taking it a step further I would suggest the following:
<div class="container">
<span class="icon"></span>
<div class="page-title">
<h1 class="title">Title</h1>
</div>
</div>
Use CSS to style it the way you need it to look.
All those elements aside from the icon should just be divs. I only use spans for wrapping text content, generally speaking.
<div class="container">
<span class="icon"></span>
<div class="page-title">
<h1 class="title">Title</h1>
</div>
</div>

What tag should you use to make a custom heading instead of <h1>-<h6>? [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 3 years ago.
Improve this question
What tag should you use to make a custom heading? Because according to these sites...
https://www.w3schools.com/html/html_headings.asp
W3Schools - Note: Use HTML headings for headings only. Don't use headings to make text BIG or bold.
https://learn.freecodecamp.org/responsive-web-design/applied-accessibility/use-headings-to-show-hierarchical-relationships-of-content
FreeCodeCamp - One final point, each page should always have one (and only one) h1 element, which is the main subject of your content.
Or for example in a case I used up all of the h1 - h6 tags?
Would using p tag with css id be appropriate?
p Paragraph tag wouldn't make sense
h# Heading tags, if I change a certain heading tag number with css it would change any tag with the same tag number
Customising text formatting wouldn't make sense as it's tag name strictly suggests what it's meant to be used for
You seem to have a slight misconception about <h1> through <h6> (<hN>). The numbers N do not represent order but hierarchy. A <h1> heading is more important and higher up the hierarchy than a <h2>, which itself is higher than <h3>, etc. See is like chapters in a textbook.
<h1>My Great book</h1>
<h2>Chapter 1</h2>
<h3>1.1</h3>
<h3>1.2</h3>
<h2>Chapter 2</h2>
<h3>2.1</h3>
<h3>2.2</h3>
<h3>2.3</h3>
<h3>2.4</h3>
<h2>Chapter 3</h2>
<h3>3.1</h3>
<h3>3.2</h3>
<h4>3.2.1</h4>
<h4>3.2.2</h4>
<h3>3.3</h3>
That means you can have as many headings as you wish with one exception (primarily for SEO reasons). You should only have a single <h1> per page.
You can use heading tags and then use css to style
HTML:
<h2>Look at this text</h2>
<h2 class="bigFont">Now look at this text</h2>
CSS:
.bigFont {
font-size:50px;
} // You can change the size
And as mentioned above use only one<h1> tag use <h2> tags like this
I think one reason is that web crawlers that rank the website use such headings. Which is why it is imperative that your website must have at least 1 <h1> tag. Also, it should be noted that the headings from <h1> - <h6> must be used in order for better SEO(Search Engine Optimization).

HTML/CSS: To use the <br> tag or to repeat styling [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 5 years ago.
Improve this question
When you have to specify the same .css class for multiple headers <h3>, is it better to use the <br> tag or is it better to repeat the header while specifying the same style class over and over again? In this specific scenario, I can't put styling on the surrounding <div>
Which one is best practice and why?
Code example while using <br> tags:
<div class="more_room_left | more_room_top">
<h5 class="no_margin_top_bottom">
*location?.streeth* *location?.number*
<br> *location?.postcode* *location?.city*
<br> *location?.country*
<br>
</h5>
</div>
Code example using different headers:
<div class="more_room_left | more_room_top">
<h5 class="no_margin_top_bottom">*location?.streeth* *location?.number*</h5>
<h5 class="no_margin_top_bottom">*location?.postcode* *location?.city*</h5>
<h5 class="no_margin_top_bottom">*location?.country*</h5>
</div>
Visual example:
EDIT:
After reading more about when to use the <br> tag, I've concluded that in my case the <br> tag has no value and is considered a hack. I reworked the way I'm styling the headers <h3>, which are now paragraphs <p>, to fit the styling I need without using the <br> tag. This solution was handed to me through an already removed answer, so I can't give credit.
HTML
<div class="address_container | more_room_left | more_room_top">
<p>*location?.streeth* *location?.number*</p>
<p>*location?.postcode* *location?.city*</p>
<p>*location?.country*</p>
</div>
CSS
.address_container > p {
margin-top: 0;
margin-bottom: 0;
}
There are various factors that would define which one should be used.
For example, if you wish to retrieve this header text, how you want value. You want entire text or individual values. If individual values are expected, I'd suggest using multiple headers.
On the contrary, if you want to receive entire value together, use single header with brs. But using <br/> is not considered good practice and you should refer: Is it sometimes bad to use <BR />?
Another such case would be for adaptive design. On smaller screen how should they wrap? If you are fine with *location?.streeth* *location?. on one line and number* on next, you can use single header. But if you want *location?.number to wrap in next line, using multiple elements would suit better.
If values are not going to be retrieved and its just for view, then either one would do. But in such cases, I'd go for <p> (as suggested by apokryfos) and add custom styling for view.
It is worth noting that HTML 5 has put a great deal of effort into making web pages semantic by adding a number of tags which are visually identical but differ in name and therefore purpose. For that reason, you should use a header tag (like e.g. `) to contain a 5 level header, e.g., when you need to specify a 5 level subsection to a document. This means that you should not use a header at all for the elements in question because the sole purpose of the header being used is for styling rather than offering semantic significance.
Based on your code, I would personally use different header's or paragraph's to achieve the spacing as well as allow for styling. If in the future you wish to style one of these parts, it will be much easier to do so at the block level.
<div class="more_room_left | more_room_top">
<p class="no_margin_top_bottom">*location?.streeth* *location?.number*</p>
<p class="no_margin_top_bottom">*location?.postcode* *location?.city*</p>
<p class="no_margin_top_bottom">*location?.country*</p>
</div>

Semantic HTML for a business card [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 6 years ago.
Improve this question
I am trying to write semantic HTML for a business card.
I want to show the name, title, email and phone number on the markup.
<div id="bussinesscardcontainer">
<section class="Details">
<span> Name :ABC</span>
<span> Title:XYZ </span>
</section>
<footer class="contact">
<span class="email"> Email:abc#abc.com</span>
<span class="phonenumber">Mobile:123-123-123</span>
</footer>
</div>
I just want to understand is my markup sematically right or not.
This markup would end up looking as
Difficulty in Designing Business card using CSS
Your use of the section and footer element is not correct. These two lines shouldn’t form a section, and the footer would belong to the parent section (which might be the whole document), instead of the business card only.
If you need a sectioning content element (it depends on the context), it should wrap the whole business card. It seems that you don’t need a footer (or header) at all here.
For the actual content, you could use a dl element, as your card consists of name-value pairs.
The email address and the telephone number could become hyperlinks, with the a element.
So that gives you:
<dl class="business-card">
<dt>Name</dt>
<dd>ABC</dd>
<dt>Title</dt>
<dd>XYZ</dd>
<dt>Email</dt>
<dd>abc#abc.com</dd>
<dt>Mobile</dt>
<dd>123-123-123</dd>
</dl>
Your markup is technically correct but could subjectively be improved.
The HTML5 spec added many, more descriptive HTML properties like <footer> that you are using but left implementation up to web developers. This has resulted in less than optimal usage of HTML properties in my experience.
For structural components of a document, the best guide I've found is produced by HTML5Doctor, which gives you a flow chart of usage guidelines for these properties.
In your specific case, I'd probably omit the use of <footer> and switch how you are using <section> and <div> like this:
<section class="businesscard-container">
<div class="businesscard-details">
<span> Name :ABC</span>
<span> Title:XYZ </span>
</div>
<div class="businesscard-contact">
<span class="email"> Email:abc#abc.com</span>
<span class="phonenumber">Mobile:123-123-123</span>
</div>
</section>
HTML not have semantic for this purpose.
You have tu use microformat (http://microformats.org/). This standard is created for business card, address, recipes and many more.
Is used by search engine, browser and other.
h-card is the html microformat standard you need (http://microformats.org/wiki/h-card). An example:
<p class="h-card">
<img class="u-photo" src="http://example.org/photo.png" alt="" />
<a class="p-name u-url" href="http://example.org">Joe Bloggs</a>
<a class="u-email" href="mailto:joebloggs#example.com">joebloggs#example.com</a>,
<span class="p-street-address">17 Austerstræti</span>
<span class="p-locality">Reykjavík</span>
<span class="p-country-name">Iceland</span>
</p>