Responsive Styling Using Attribute Selectors

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

One of the challenges we face when implementing class-based atomic styling is that it often depends on a specific breakpoint for context.

<div class=”span-12″></div> <!– we want this for small screens  –>
<div class=”span-6″></div>  <!– we want this for medium screens –>
<div class=”span-4″></div>  <!– we want this for large screens  –>

It’s common to use a prefix to target each breakpoint:

<div class=”sm-span-12 md-span-6 lg-span-4″></div>

This works well until we start adding multiple classes. That’s when it becomes difficult to keep a track what relates to what and where to add, remove. or change stuff.

<div class=”
  sm-span-12 
  md-span-6 
  lg-span-4 
  sm-font-size-xl 
  md-font-size-xl 
  lg-font-size-xl 
  md-font-weight-500 
  lg-font-weight-700″>
</div>

We can try to make it more readable by re-grouping:

<div class=”
  sm-span-12 
  sm-font-size-xl 

  md-span-6 
  md-font-size-xl 
  md-font-weight-500 

  lg-span-4 
  lg-font-size-xl 
  lg-font-weight-700″>
</div>

We can add funky separators (invalid class names will be ignored):

<div class=”
  [
   sm-span-12 
   sm-font-size-xl 
  ],[
   md-span-6 
   md-font-size-xl 
   md-font-weight-500 
  ],[
   lg-span-4 
   lg-font-size-xl 
   lg-font-weight-700
  ]”>
</div>

But this still feels messy and hard to grasp, at least to me.

We can get a better overview and avoid implementation prefixes by grouping attribute selectors instead of actual classes:

<div 
  sm=”span-12 font-size-lg”
  md=”span-6 font-size-xl font-weight-500″
  lg=”span-4 font-size-xl font-weight-700″
>
</div>

These aren’t lost of classes but a whitespace-separated list of attributes we can select using [attribute~=”value”], where ~= requires the exact word to be found in the attribute value in order to match.

@media (min-width: 0) {
 [sm~=”span-1″] { /*…*/ }              
 [sm~=”span-2″] { /*…*/ }   
 /* etc. */ 
}
@media (min-width: 30rem) {
 [md~=”span-1″] { /*…*/ }   
 [md~=”span-2″] { /*…*/ }   
 /* etc. */   
}
@media (min-width: 60rem) {
 [lg~=”span-1″] { /*…*/ }   
 [lg~=”span-2″] { /*…*/ }   
 /* etc. */   
}

It may be a bit odd-looking but I think translating atomic classes to  attributes is fairly straightforward (e.g. .sm-span-1 becomes [sm~=”span-1″]). Plus, attribute selectors have the same specificity as classes, so we lose nothing there. And, unlike classes, attributes can be written without escaping special characters, like /+.:?.

That’s all! Again, this is merely an idea that aims to make switching declarations in media queries easier to write, read and manage. It’s definitely not a proposal to do away with classes or anything like that.
The post Responsive Styling Using Attribute Selectors appeared first on CSS-Tricks.

X ITM Cloud News

Emily

Leave a Reply

Next Post

JSExport: C++ in the browser made easy

Tue Jun 30 , 2020
Spread the love           submitted by /u/alexp_lt [link] [comments] X ITM Cloud News
X- ITM

Cloud Computing – Consultancy – Development – Hosting – APIs – Legacy Systems

X-ITM Technology helps our customers across the entire enterprise technology stack with differentiated industry solutions. We modernize IT, optimize data architectures, and make everything secure, scalable and orchestrated across public, private and hybrid clouds.

This image has an empty alt attribute; its file name is x-itmdc.jpg

The enterprise technology stack includes ITO; Cloud and Security Services; Applications and Industry IP; Data, Analytics and Engineering Services; and Advisory.

Watch an animation of  X-ITM‘s Enterprise Technology Stack

We combine years of experience running mission-critical systems with the latest digital innovations to deliver better business outcomes and new levels of performance, competitiveness and experiences for our customers and their stakeholders.

X-ITM invests in three key drivers of growth: People, Customers and Operational Execution.

The company’s global scale, talent and innovation platforms serve 6,000 private and public-sector clients in 70 countries.

X-ITM’s extensive partner network helps drive collaboration and leverage technology independence. The company has established more than 200 industry-leading global Partner Network relationships, including 15 strategic partners: Amazon Web Services, AT&T, Dell Technologies, Google Cloud, HCL, HP, HPE, IBM, Micro Focus, Microsoft, Oracle, PwC, SAP, ServiceNow and VMware

.

X ITM