Should I mark what blocks mean in data-* attributes?

I was writing a webpage today with a comment after the start of every major section. Instead of using a comment, which comes with the bulgy <!-- --> syntax, could I instead put this in a data-* attribute? This would be more for me than anything else, so I don’t want to put it in an id or class, but it would also leave it open for userscripts to more easily parse the page.

Examples

My current method:

<article> <!-- news -->
    <header> <!-- headline -->
        <h1>How To Fix Everything</h1>
    </header>

    <section> <!-- news body -->
        <p>This is the best magic there is:</p>
        <ol>
            <li>Find a problem</li>
            <li>FIX IT</li>
            <li>Back to step 1</li>
        </ol>
    </section>

    <footer> <!-- social -->
        <a href="//example.com/share">Share on Example.com!</a>
    </footer>
</article>

My proposed method:

<article data-news>
    <header data-headline>
        <h1>How To Fix Everything</h1>
    </header>

    <section data-news-body>
        <p>This is the best magic there is:</p>
        <ol>
            <li>Find a problem</li>
            <li>FIX IT</li>
            <li>Back to step 1</li>
        </ol>
    </section>

    <footer data-social>
        <a href="//example.com/share">Share on Example.com!</a>
    </footer>
</article>

2

Use a class.

A class labels the contents on it if you work semantically. That you can use a CSS selector on it is, of course, also good and useful, but in general it describes the role of the element, and that’s exactly what you are looking for.

Next to that, people are used to reading class names as semantic data describing the element.

It also reduces duplicate code because you would use a class also for the CSS anyway. Even if you don’t need the class in CSS at this moment, it can be good to attach it. That said, it has a real meaning. That will make it possible to change the styles easier without changing classes in the HTML, which is important in bigger teams.

And finally, it makes code more readable in general, because you can simply match the class. That seems (but that’s never stable) to be, performance-wise, a good solution. In terms of code organisation, it makes things much easier to deal with.

The current HTML5 spec (PR) says about custom data attributes (emphasis mine):

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.

And the definition of the class attribute contains:

[…] representing the various classes that the element belongs to […]

[…] authors are encouraged to use values that describe the nature of the content […]

So it seems that class is more appropriate than data-* for your use case.

However, it would not be wrong to use custom data attributes instead (or in addition). It’s your understanding of your content that matters, and you might argue that something is or is not a class (as only vaguely defined by the spec).

(CSS doesn’t matter for this decision at all, as you can use both attributes as hooks in your CSS.)

(Side note: Your use of section in this example is most likely wrong. Don’t use it for the “text body” of an article. Only use section here if it represents a sub-section of the parent section, i.e., when you are using sub-headings.)

I would use classes as what your doing is adding contextual classification:

<footer class="social">
  <a href="//example.com/share">Share on Example.com!</a>
</footer>

Can be read as its a footer containing social media functions, which has a more specific semantical meaning than just <header>.

This together with role attributes can be used to make html better describe subject matter.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Should I mark what blocks mean in data-* attributes?

I was writing a webpage today with a comment after the start of every major section. Instead of using a comment, which comes with the bulgy <!-- --> syntax, could I instead put this in a data-* attribute? This would be more for me than anything else, so I don’t want to put it in an id or class, but it would also leave it open for userscripts to more easily parse the page.

Examples

My current method:

<article> <!-- news -->
    <header> <!-- headline -->
        <h1>How To Fix Everything</h1>
    </header>

    <section> <!-- news body -->
        <p>This is the best magic there is:</p>
        <ol>
            <li>Find a problem</li>
            <li>FIX IT</li>
            <li>Back to step 1</li>
        </ol>
    </section>

    <footer> <!-- social -->
        <a href="//example.com/share">Share on Example.com!</a>
    </footer>
</article>

My proposed method:

<article data-news>
    <header data-headline>
        <h1>How To Fix Everything</h1>
    </header>

    <section data-news-body>
        <p>This is the best magic there is:</p>
        <ol>
            <li>Find a problem</li>
            <li>FIX IT</li>
            <li>Back to step 1</li>
        </ol>
    </section>

    <footer data-social>
        <a href="//example.com/share">Share on Example.com!</a>
    </footer>
</article>

2

Use a class.

A class labels the contents on it if you work semantically. That you can use a CSS selector on it is, of course, also good and useful, but in general it describes the role of the element, and that’s exactly what you are looking for.

Next to that, people are used to reading class names as semantic data describing the element.

It also reduces duplicate code because you would use a class also for the CSS anyway. Even if you don’t need the class in CSS at this moment, it can be good to attach it. That said, it has a real meaning. That will make it possible to change the styles easier without changing classes in the HTML, which is important in bigger teams.

And finally, it makes code more readable in general, because you can simply match the class. That seems (but that’s never stable) to be, performance-wise, a good solution. In terms of code organisation, it makes things much easier to deal with.

The current HTML5 spec (PR) says about custom data attributes (emphasis mine):

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.

And the definition of the class attribute contains:

[…] representing the various classes that the element belongs to […]

[…] authors are encouraged to use values that describe the nature of the content […]

So it seems that class is more appropriate than data-* for your use case.

However, it would not be wrong to use custom data attributes instead (or in addition). It’s your understanding of your content that matters, and you might argue that something is or is not a class (as only vaguely defined by the spec).

(CSS doesn’t matter for this decision at all, as you can use both attributes as hooks in your CSS.)

(Side note: Your use of section in this example is most likely wrong. Don’t use it for the “text body” of an article. Only use section here if it represents a sub-section of the parent section, i.e., when you are using sub-headings.)

I would use classes as what your doing is adding contextual classification:

<footer class="social">
  <a href="//example.com/share">Share on Example.com!</a>
</footer>

Can be read as its a footer containing social media functions, which has a more specific semantical meaning than just <header>.

This together with role attributes can be used to make html better describe subject matter.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật