Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Selectors Going Further with Attribute Selectors and Pseudo-Classes Substring Matching Attribute Selectors - "Contains"

why would we be unable to add a class or ID to an HTML element?

In this video, Gil mentions a use of the substring attribute selectors might be when we can't add an id or class to an HTML element. Why would this happen?

1 Answer

Kevin Korte
Kevin Korte
28,109 Points

Sometimes when you're using a content management system, or a plugin, or anything that generates it's own html, you might not have control of the classes and ids when the code is being generated on the fly. In those case, attribute selectors come in very handy.

jason chan
jason chan
31,008 Points

To add frameworks like laravel. you have to add classes through an array through php. NOT HTML ANYMORE. lols.

Even when you can modify the generated markup for a content management system it's not always the most efficient way to solve the problem - sometimes duplicating markup templates can have negative consequences for long-term maintenance.

One example of a case where you'd be unable to change the markup of something you were working on might be a legacy closed-source system. With opaque systems that are made out of antiquated tech, it can be difficult to find people who can work on changing the HTML. In those cases, attribute selectors make it possible to change the visual look of the page more drastically without adding more technical risk.