Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of a CSS property in SASS?

Tags:

sass

I'd like to store the current value of a property for later use. It's already been solved for jQuery.

The issue is that I'm using a @mixin to apply a CSS hack in several places (Justified Block List) and I'd like to restore the font-size property in .block-list * (currently all text in sub-elements is just collapsed).

Unsatisfactory workarounds:

  • Save the global default font size in a separate file and pass it to the @mixin on @import. This is of course in the general case not the same font size as the objects which the mixin is applied to.
  • Save the font size whenever you change it, and pass that. This tangles up the files involved, since it's not very elegant to @include the typography stylesheet in several otherwise unrelated files.
  • Use more jQuery.

Possibly satisfactory workarounds:

  • Override the font size with a stronger rule on the first ancestor which changes it. This could be tricky to determine.
like image 929
l0b0 Avatar asked Feb 22 '12 12:02

l0b0


People also ask

Can I use CSS custom properties in sass?

So, it is possible to use Sass with CSS custom properties. In fact, this would be the recommended route to follow since it would allow a smooth JavaScript-CSS interaction. The only downside to this approach is it does not work with discontinued or outdated browser versions that don't support CSS custom properties.

What is the property value of CSS?

The used value of a CSS property is its value after all calculations have been performed on the computed value. After the user agent has finished its calculations, every CSS property has a used value. The used values of dimensions (e.g., width , line-height ) are in pixels.

Can I use CSS variables in sass?

Sass variables are all compiled away by Sass. CSS variables are included in the CSS output. CSS variables can have different values for different elements, but Sass variables only have one value at a time.

Is sass the same as SCSS?

SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.


1 Answers

There's no way to tell the computed value of a property until the styles are actually applied to a document (that's what jQuery examines). In the stylesheet languages, there's no "current" value except the initial value or the value you specify.

Save the font size whenever you change it, and pass that seems best, and @BeauSmith has given a good example. This variant lets you pass a size or fallback to a defined global:

=block-list($font-size: $base-font-size)
  font-size: 0
  > li
    font-size: $font-size
like image 103
sam Avatar answered Nov 08 '22 20:11

sam