可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
On my website, I'm constantly doing style="font-size: #ofpx;". However, I was wondering if there's a way to do it with scss so that, when I declare a class, it would also change the font size. For example:
<div class='col-lg-4 font-20'>whatever here</div>
and this would change my font-size to 20. If I did font-30, it would change my font-size to 30 and etc...
What I have so far:
.font-#{$fontsize} { font-size: $fontsize; }
回答1:
This can't be done for arbitrary sizes. The nature of SCSS is that is needs to be flattened down to CSS before it gets applied to the HTML. What you are asking for, however, is essentially to create rules at run-time rather than compile-time.
What you're asking for is also a code smell. It smells like your markup isn't semantic enough. The purpose of a CSS class is to group objects with similar characteristics, but you're using them instead to describe the styles they impart. I would suggest stepping back and reconsidering what it is that you really want.
You obviously have details of certain elements that are context-dependent. For example, maybe you are applying these rules to buttons when you want to make them smaller or larger than usual. You need to identify the scenarios in which the buttons change. Maybe they are 20% smaller if they are in a modal dialog? Then write your normal .button rules, and also create rules for .modal .button which make it smaller.
If you're positive that you want to define font-size for each element within the HTML, just continue using inline styles. The only reason inline styling is frowned upon is because it combines model and view logic in a way that harms reusability; however, what you are requesting does so in exactly the same way. This is what inline styles were made for. Don't re-invent the wheel.
With all of that said, you can use sass loops to automatically generate classes for integers within a range. For example:
@for $i from 1 through 100 { .font-#{$i} { font-size: #{$i}px; } }
This is not a good idea. Pragmatically speaking it doesn't offer any advantages over just using inline styles and with large ranges your resulting file will be larger (which affects website load times).
Here is a runnable jsFiddle example of this technique.
回答2:
Just going to add, mixins are great, but if you want a util class (attach a class to an element, get that font-size applied to it, do a for-loop in SCSS like so..
@for $i from 1 through 4 { $fontsize: 10px * $i; .font-#{$i} { font-size: $fontsize; } }
compiles to
.font-1 { font-size: 10px; } .font-2 { font-size: 20px; } .font-3 { font-size: 30px; } .font-4 { font-size: 40px; }
If you want the class to match the # of pixels...
@for $i from 1 through 4 { $base: 10; $fontsize: $base * $i; .font-#{$fontsize} { font-size: $fontsize + 0px; } }
Which compiles to
.font-10 { font-size: 10px; } .font-20 { font-size: 20px; } .font-30 { font-size: 30px; } .font-40 { font-size: 40px; }
Codepen example.
回答3:
Of course, inline style tags are bad form. So yes, you should add some classes for font size, or just set font size on the elements you need to as you go. Up to you. If you want, you could use a mixin like so:
@mixin font-size($size) { font-size: $size; } .some-div { @include font-size(10px); }
But that's probably overkill unless you get a group of rules that usually go together.
回答4:
You can use mixins like this
@mixin font($fontsize) { font-size: $fontsize; } .box { @include font(10px); }