<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Damian Wajer</title>
    <description>The latest articles on DEV Community by Damian Wajer (@damianwajer).</description>
    <link>https://dev.to/damianwajer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F500761%2F0ac9549c-0734-4174-b75c-2fe768a464a3.jpg</url>
      <title>DEV Community: Damian Wajer</title>
      <link>https://dev.to/damianwajer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damianwajer"/>
    <language>en</language>
    <item>
      <title>Accessible card component with pure (S)CSS (no JavaScript, the pseudo-content trick)</title>
      <dc:creator>Damian Wajer</dc:creator>
      <pubDate>Sat, 27 Nov 2021 23:51:55 +0000</pubDate>
      <link>https://dev.to/damianwajer/accessible-card-component-26o4</link>
      <guid>https://dev.to/damianwajer/accessible-card-component-26o4</guid>
      <description>&lt;p&gt;When creating the card component, sometimes it’s advisable (or required by design) to make the whole card clickable. But how to do so without compromising the usability? Below I share a useful pseudo-content trick to make the whole card clickable and maintain its accessibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;the whole card needs to be clickable&lt;/li&gt;
&lt;li&gt;within the card there is also a “read more” link&lt;/li&gt;
&lt;li&gt;inside a card, there are other separate links to different URL-s&lt;/li&gt;
&lt;li&gt;you don’t want to harm the usability, e.g. allow the user to open all links in a new tab with a mouse right-click (context menu on touch devices)&lt;/li&gt;
&lt;li&gt;support custom styles for hover and focus states&lt;/li&gt;
&lt;li&gt;one last requirement: user should be able to select and copy the text within a card to the clipboard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How would you approach this task? Just a regular card component wrapped in an &lt;code&gt;a&lt;/code&gt; element? Or maybe &lt;code&gt;onclick&lt;/code&gt; in JavaScript directly on a &lt;code&gt;div&lt;/code&gt; or &lt;code&gt;article&lt;/code&gt; element (don’t do that!)?&lt;/p&gt;

&lt;p&gt;How to handle such a case and maintain the accessibility in a simple and elegant way?&lt;/p&gt;

&lt;h2&gt;
  
  
  Possible solution
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Set &lt;code&gt;position: relative&lt;/code&gt; on the container element&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;position: absolute&lt;/code&gt; on the link’s &lt;code&gt;:after&lt;/code&gt; pseudo-content&lt;/li&gt;
&lt;li&gt;Set value of &lt;code&gt;0&lt;/code&gt; for &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt;, and &lt;code&gt;left&lt;/code&gt; properties on link’s &lt;code&gt;:after&lt;/code&gt; pseudo-content&lt;/li&gt;
&lt;li&gt;Combine it with &lt;code&gt;:focus-within&lt;/code&gt; and &lt;code&gt;:hover&lt;/code&gt; to style different states&lt;/li&gt;
&lt;li&gt;Enhance it even further and make the text selectable with &lt;code&gt;z-index&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If you want to add other links inside a card, use styles from &lt;code&gt;card__separate&lt;/code&gt; to make it selectable (and/or clickable).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  HTML
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#optional"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card__separate"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Optional&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card__separate"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Lorem ipsum&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#card-link"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"card__link"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Link&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SCSS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Style hover and focus states.&lt;/span&gt;
  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:focus-within&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Make the content selectable.&lt;/span&gt;
  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__separate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;z-index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Make the whole card clickable.&lt;/span&gt;
  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;__link&lt;/span&gt;&lt;span class="nd"&gt;::after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;Check clickable area and try tab keyboard navigation in the following example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://jsfiddle.net/damianwajer/hbv2zajs/embedded/result,html,css//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits &amp;amp; further reading
&lt;/h2&gt;

&lt;p&gt;I first saw the pseudo-content trick technique on &lt;a href="https://inclusive-components.design/"&gt;Inclusive Components&lt;/a&gt; website. Check it out for other solutions to card issues and more inclusive components examples.&lt;/p&gt;

</description>
      <category>css</category>
      <category>a11y</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
