技术控

    今日:0| 主题:63445
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Custom Attributes for our Spacing Classes

[复制链接]
Smilingatme嫣然 发表于 2016-9-30 17:49:18
138 1
While using this method on our projects, we noticed our class attribute was getting extremely cluttered with a variety of spacing classes on a single element. Here’s an example:
  As you can see, there’s an overload of spacing-based class names littering the codebase. Scanning over this code with your text editor or devtools can become overwhelming. So how can we simplify this?Mark and I discussed a possible solution for this problem and came up with something like this:  
  We decided to use a custom attribute called `spacing` to separate our spacing classes from the stylistic classes. Our spacing classes only handle margin top while our stylistic classes set the background, borders, padding, etc. This ensures all the classes applied to an element are specifically for visual styling whereas any value in the spacing attribute is specifically for our vertical spacing throughout the project.
      SCSSFile  

  Here is a snippet of our    .spacing.scss. We are using    include-mediato handle our media queries.  
  In this snippet, we are defining our spacing variables as well as setting our default    CSSrulesets. we use a complex    CSSselector to provide two collaborative, abilities. First, the ‘begins with’ selector is used to allow a default spacing to be set. Then using a wildcard match we can override that spacing on specific breakpoints using our custom `large()` syntax”. In some cases the spacing can vary from screen size to screen size. Following our default classes, we have a    CSSruleset to target spacing at different screen sizes.  
  Following the default spacing classes, we’re using    CSSselectors to target any value in our attribute. If we don’t define `xsmall-xlarge` then it won’t be available. This is to keep the generated    CSSsize small and reduce extraneous code that we may never even use.  
  Putting it all together

  The browser reads the example like this:  
  
       
  • Give each direct child of the article element a margin-top of 14px on small screens and 20px on large screens.   
  • For small to medium screens, give the direct child elements a margin top of 10px   
  • On large screens give the direct child elements a margin-top of 60px  
  Some may ask, why aren’t you using “data-” to define your custom attribute? This is still up for discussion here on the development team. Sure, we might run into some issues further down the line, but that’s as easy as doing a `replace all` to fix our spacing attributes. We’re testing this method with one of our current projects and it’s going pretty smoothly. Does adding “data-” to our custom attribute really matter nowadays? I’d like to hear your opinions.
MR._ZHAO 发表于 2016-11-1 03:11:30
顶起!
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表