Comments on: [attribute] https://css-tricks.com Tips, Tricks, and Techniques on using Cascading Style Sheets. Wed, 19 Jan 2022 15:31:27 +0000 hourly 1 https://wordpress.org/?v=6.5.5 By: Johan https://css-tricks.com/almanac/selectors/a/attribute/#comment-1788393 Wed, 19 Jan 2022 15:31:27 +0000 http://css-tricks.com/?page_id=14152#comment-1788393 How can I find this element: <p data-title="&nbsp;">hello</p>?
using p[data-title="&nbsp;"] (which doesn’t work)

]]>
By: Ken https://css-tricks.com/almanac/selectors/a/attribute/#comment-1763155 Thu, 24 Sep 2020 22:10:04 +0000 http://css-tricks.com/?page_id=14152#comment-1763155 Hello! I do not understand the 3rd sentence in this article. It seems like there might be a grammar mistake, or else I am just not getting the message being conveyed. Please review.. Thanks

]]>
By: John Ulric https://css-tricks.com/almanac/selectors/a/attribute/#comment-1752698 Thu, 21 Nov 2019 21:16:57 +0000 http://css-tricks.com/?page_id=14152#comment-1752698 Wondering about a relevant use case for the |= selector, I learned that it is often used to match language codes, with [lang|=”en”] matching both “en-US” and “en-UK”.

]]>
By: Khaki https://css-tricks.com/almanac/selectors/a/attribute/#comment-1750253 Sun, 21 Jul 2019 18:40:38 +0000 http://css-tricks.com/?page_id=14152#comment-1750253 You can use the :checked selector.

]]>
By: Chris Coyier https://css-tricks.com/almanac/selectors/a/attribute/#comment-1682249 Wed, 27 Mar 2019 13:44:03 +0000 http://css-tricks.com/?page_id=14152#comment-1682249 In reply to Edson.

div[data]

]]>
By: Edson https://css-tricks.com/almanac/selectors/a/attribute/#comment-1682231 Wed, 27 Mar 2019 13:14:04 +0000 http://css-tricks.com/?page_id=14152#comment-1682231 Take this example:

Let’s say I want to select all DIVs starting with those “data” attributes.
I started trying div[attr^=”data”] with no success of course.

Any help?

]]>
By: anon-dev https://css-tricks.com/almanac/selectors/a/attribute/#comment-1614348 Wed, 10 Jan 2018 17:40:35 +0000 http://css-tricks.com/?page_id=14152#comment-1614348 In reply to Gumnos.

This would work

:root{
  --optional: hidden;
}

.optional{
  display: var(--optional);
}

select[value="other"] ~ .optional {
   --optional: block;
}



]]>
By: Ivan https://css-tricks.com/almanac/selectors/a/attribute/#comment-1612208 Tue, 03 Oct 2017 19:47:43 +0000 http://css-tricks.com/?page_id=14152#comment-1612208 Is it possible somehow to extract meta tag content ?

]]>
By: LordRegent https://css-tricks.com/almanac/selectors/a/attribute/#comment-1611985 Sun, 24 Sep 2017 04:30:39 +0000 http://css-tricks.com/?page_id=14152#comment-1611985 In reply to Gumnos.

Input and Select

I handle this by having my startup JS give input and select elements having attribute data-value an “onchange” handler (or “change” event listener) that does this.dataset.value=this.value (remember to use .bind(elm) or a factory function). Although this solution is not pure CSS, it’s a mere whiff of boilerplate JS code to let CSS handle dependent display elements like magic in most any project (adapt to use your framework’s selector function and iterator):

$$('input[data-value], select[data-value]').forEach(elm => {
    elm.onchange=(function(){this.dataset.value=this.value}).bind(elm)
    elm.dataset.value=elm.value
})

Then the CSS for @Gumnos’ post would need only a minor change, to prefix “data-” in the attribute selector:

<style>
 .optional {
  display: hidden;
 }
 select[data-value="other"] ~ .optional {
  display: block;
 }
</style>

On a related topic…

For a checkbox (and radiobutton) solution, I set specific classes on the controlling and target elements, and craft CSS to handle an element hierarchy of both child and following-sibling. The CSS selectors for the controlling classes are combined with :checked or :not(:checked) as needed.

This solution is pure CSS, plus the relationship between elements is explicitly stated in the markup, so it’s easy for the designer to understand and manage.

Depending on your project you can limit yourself to a couple of simple selectors like .controlFollowing:checked and .targetFollowing (provided you put each set in a wrapper element), or you can have incredibly detailed multi-step control by using selectors such as .controlFollowing1:checked .targetFollowing1 and .controlFollowing1:not(:checked) .targetFollowing1 and extend the numbers as needed. I’ve built up a library file I link when I need this feature, but I caution you not to extend the idea more than a project warrants.

In live code, most of the time, a small simple style set suffices:

<style>
.controlFollowing ~ .targetFollowing
,.controlChild > .targetChild
{display: none; }
.controlFollowing:checked ~ .targetFollowing
, .controlChild:checked > .targetChild
{display: initial; }
</style>

     Toggle? 
         Yes, Virginia...
    


     Toggle, too? 
          Extend? 
            Far out! 
        
    

]]>
By: Gumnos https://css-tricks.com/almanac/selectors/a/attribute/#comment-1610071 Fri, 07 Jul 2017 13:21:37 +0000 http://css-tricks.com/?page_id=14152#comment-1610071 In reply to Gumnos.

Sorry, “checked” may have been a poor choice of example since the :checked would suffice. A better example might be the .value of a select-list

<style>
 .optional {
  display: hidden;
 }
 select[value="other"] ~ .optional {
  display: block;
 }
</style>
<p>
Please let us know why you rated us "Excellent"
</p>
<select>
  <option value="service">Great service</option>
  <option value="price">Great price</option>
  <option value="adorable">Adorable puppies!</option>
  <option value="other">Other</option>
</select> 
<label class=optional>Please let us know what other reason
<input type="text">
</label>

AFAIK, there’s no way in pure CSS to change layout based on the value of a select drop-down or the text-value of an input box. So if I wanted to make my other text-input only show when “Other” is selected, it currently requires JS.

]]>
By: Stijn https://css-tricks.com/almanac/selectors/a/attribute/#comment-1610047 Thu, 06 Jul 2017 20:23:08 +0000 http://css-tricks.com/?page_id=14152#comment-1610047 In reply to Gumnos.

input:checked:after{}
you mean like this…

]]>
By: Thor https://css-tricks.com/almanac/selectors/a/attribute/#comment-1609755 Sat, 01 Jul 2017 20:30:12 +0000 http://css-tricks.com/?page_id=14152#comment-1609755 Whow! Why didn’t I think of this before, just fabulously powerful and convenient!

]]>
By: Gustavo Costa https://css-tricks.com/almanac/selectors/a/attribute/#comment-1606600 Tue, 07 Feb 2017 16:22:34 +0000 http://css-tricks.com/?page_id=14152#comment-1606600 If I want to select many icons for img[icone], adding class, would it be img[icone][class^=”icosv-“]:before?

]]>
By: Philip https://css-tricks.com/almanac/selectors/a/attribute/#comment-1606410 Mon, 30 Jan 2017 12:30:56 +0000 http://css-tricks.com/?page_id=14152#comment-1606410 Wow! Thanks for this explanation, after carefully reading over and over I was able to apply it to a complex challenge and it work.

Thanks once again

]]>
By: Avinash https://css-tricks.com/almanac/selectors/a/attribute/#comment-1603870 Fri, 02 Sep 2016 13:43:03 +0000 http://css-tricks.com/?page_id=14152#comment-1603870 Which software should I use to learn CSS plz reply as fast as you can

]]>