Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS - for h1-h6 style

Tags:

css

sass

I have SCSS style like this, I want to use @for from SCSS to write this more efficient.

So far:

@for $i from 1 through 6 {
  h#{$i} {
    $size: {$i} * 1.4;
    @include font-size($size);
  }
}

Note: don't mind calculation of size, its just for test

but syntax is not right.

Expected output

h1 {
  @include font-size(3.2);
}

h2 {
  @include font-size(2.4);
}

h3 {
  @include font-size(1.8);
}

h4 {
  @include font-size(1.6);
}

h5 {
  @include font-size(1.3);
}

h6 {
  @include font-size(1.2);
}
like image 823
Ing. Michal Hudak Avatar asked Jan 19 '16 13:01

Ing. Michal Hudak


People also ask

What is the difference between h1 and h6?

Definition and Usage The <h1> to <h6> tags are used to define HTML headings. <h1> defines the most important heading. <h6> defines the least important heading. Note: Only use one <h1> per page - this should represent the main heading/subject for the whole page.

What is SASS in Javascript?

Sass stands for Syntactically Awesome Stylesheet. Sass is an extension to CSS. Sass is a CSS pre-processor. Sass is completely compatible with all versions of CSS. Sass reduces repetition of CSS and therefore saves time.

What is the default size of text in h1 HTML element?

H1: 32 pt (30–34pt) H2: 26 pt (24–28pt) H3: 22 pt (20–24pt) H4: 20 pt (18–22pt)


1 Answers

The main issue is your h1 increasing in size as it gets to a higher number (because you are using $i incrementally). You can escape that by reducing the h-number size using the formula 7 - $i.

@for $i from 1 through 6 {
    h#{7 - $i} {
        fontsize: $i * 1.4em;
    }
}

The output here is:

h6 { font-size:1.4em }
h5 { font-size:2.8em }
h4 { font-size:4.2em }
h3 { font-size:5.6em }
h2 { font-size:7em }
h1 { font-size:8.4em }

Which seems to make sense. The original error you were getting was this:

Invalid CSS after "$size:": expected expression (e.g. 1px, bold), was "{$i} * 1.4;"

Because you can simply use $i as a number without special denotation.

To get the numbers to match with your question, you should actually find a way to calculate them on the fly - and the numbers you have shown above are not a pattern, so they are not mathematically controllable. Here's what you could do:

@for $i from 1 through 6 {
    h#{7 - $i} {
        fontsize: 3.4em / 6 * $i;
    }
}

The reason this cannot be computed mathematically like your question desires is: h1 - h2 = .6em, h2 - h3 = .6em, h3 - h4 = .2em => That last one does not fall into line with any particular pattern.

like image 78
somethinghere Avatar answered Oct 19 '22 10:10

somethinghere