SASS/SCSS mixins with multiple outputs - sass

For a while now I've been running into a problem with a large site I'm working on.
The main problem is that our site is offered in two languages: English and Chinese.
The Chinese site is also offered from a different domain: domain.com and domain.com.cn
This brings some issues with it in all our CSS work, mainly fonts and another thing is the domains, since images are offered from our CDN we have to put absolute links into our css, but this also means we have to change it for our Chinese site
body.en .title { background: url(http://media.domain.com/title.png); }
body.cn .title { background: url(http://media.domain.com.cn/title.png); }
The same goes for fonts, the fonts we use for our English version don't support Chinese characters so we use a different font for Chinese characters
body.en .title { font-family: "Our English font" }
body.cn .title { font-family: "Our Chinese font" }
I'm now wondering if SASS could help with this, at the moment I just have a different file (_cn.scss) that changes all links and fonts, specifically for the Chinese version.
But let's say I have this mixin to output some css for fonts:
#mixin font-english($font-size: 16px, $font-weight: 300, $line-height: 22px) {
font-family: "English font";
font-size: $font-size;
font-weight: $font-weight;
line-height: $line-height;
}
body.en .title { #include font-english(14px, 700, 18px); }
Would there be a way to make it automaticly output the Chinese font as well? Right now I have to do this all manually, the same goes for the urls, could I write a mixin that when I use it like
body.en .title { #include background(image.png); }
it outputs both
body.en .title { #include background(http://media.domain.com/image.png); }
and
body.cn .title { #include background(http://media.domain.com.cn/image.png); }
I know this probably isn't possible, but I'm just looking for a better solution, right now I have to go through all the css again and change urls and fonts to the Chinese version.

If you're able to use compass, this is actually pretty easy with their url helpers.
I think what you'd need to do is setup two config groups in config.rb, one for the english version and one for the chinese version.
You can set things like the image url, and generated image url in the config. Images should then be reference not as #include background(foo.png) but as background: image-url('foo.png'). When you run your compass task, for the english version of the site, you'd get the urls you specify for english images (what an "english image" is, I have no idea :))
Fonts are slightly different since you're probably working with completely different files. Two approaches you could take:
1) use compass's font-url helper to reference the fonts, similar to the way you would use the image-url helper. The challenge here is that you would presumably need to name the font the same thing because it'd be referenced as src: font-url('font.woff');. This might not be exactly what your'e looking for
2) Use a different SASS file for Chinese fonts. With SASS's partials, this shouldn't be too much of an issue.
You could basically have all styles as they are, call them through english_styles.scss, and reference _english-fonts where you've specified the fonts you want. You'd do the same for the Chinese fonts.
Then, when you run Compass, you'd get two different stylesheets with all sorts of correct url goodness.
edit for body class approach
I think you'd want to use the "parent selector" (not sure if that's what it's called...) so your mixin for images might be something like this:
#mixin imageOutput($image) {
.en & {
background: url('http://media.domain.com/#{$image}');
}
.cn & {
background: url('http://media.domain.com.cn/#{$image}');
}
}
.class {
#include imageOutput('file.png');
}
compiles to:
.en .class {
background: url("http://media.domain.com/file.png");
}
.cn .class {
background: url("http://media.domain.com.cn/file.png");
}
Fonts would be similar:
#mixin fontOutput($enFont, $cnFont) {
.en & {
font-family: $enFont;
}
.cn & {
font-family: $cnFont;
}
}
.item {
#include fontOutput('comic sans ms', 'china sans ms');
}
compiles to:
.en .item {
font-family: "comic sans ms";
}
.cn .item {
font-family: "china sans ms";
}
More information on parent selectors is available here: http://thesassway.com/intermediate/referencing-parent-selectors-using-ampersand

Related

Using Japanese characters in DOMPDF?

I'm trying to get my PDF's created using DOMpdf to work correctly with Japanese characters. I have it working fine with French etc, but Japanese characters don't show (just get ???)
Here is how the HTML page looks:
...but the PDF comes out as:
The code I'm using is pretty simple:
<?php
// include autoloader
require_once '/path/to/admin/invoices/autoload.inc.php';
define("DOMPDF_ENABLE_REMOTE", true);
define("DOMPDF_UNICODE_ENABLED", true);
// reference the Dompdf namespace
use Dompdf\Dompdf;
$order_id = $argv[1];
$content = file_get_contents("/path/to/admin/invoices/html_versions/$order_id.html");
// // instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($content);
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>
From what I can see, it wants a font specifically for Japanese.
What font would work?
Can I have it running alongside the normal font? i.e I still want to be able to show stuff like íóé etc
UPDATE: I'm getting a bit closer!
I've downloaded the CyberCJK TTF font (as suggested here: https://github.com/dompdf/dompdf/issues/938 )
Then I tweaked my CSS to:
#font-face {
font-family: CyberCJK;
font-style: normal;
font-weight: normal;
src: url("https://example.com/cgi-bin/admin/invoices/lib/fonts/DejaVuSerif.ttf") format("truetype");
}
* {
font-family: 'CyberCJK', sans-serif;
}
#page { margin: 0px; } body { margin: 0px; }
html { font-size: 14px/1; font-family: 'CyberCJK', sans-serif; overflow: auto; }
..and now I get partial characters:
I have also tested uploading my HTML page on: http://www.htm2pdf.co.uk , and it comes out perfectly as a PDF, and is also only 64kb, compared to 13mb+ using DOMPdf. Something must be up!
I'm really at a loss as to what else to try :/

ionic 3 alert font-sizing

i'm using the ion-select => ion-option as an input for my ionic 3 project. It gives an alert on selection, however the font-size of the alert body is really small and i have tried all sass options to increase its font-size all in vain. I have tried using .alert-md,.alert-tappable. Is there any way i can increase this font.
.scss
.ios, .md {
page-add-stock {
/*.alert-md .alert-checkbox-label{
}*/
.alert-radio-label {
font-size: 3rem;
}
}
}
.html
<ion-select [(ngModel)]="category" formControlName="category" >
<ion-option>Pesticides</ion-option>
<ion-option>Seeds</ion-option>
<ion-option>Herbicides</ion-option>
<ion-option>Fertilizers</ion-option>
<ion-option>Farming tools</ion-option>
</ion-select>
Html ion-select that generates the alert, just in case.
I cannot find any Ionic Sass variable related to the labels (I found one for the message, the title and the subtitle of the alert, but not for the labels), so you can always use a css style rule to set the right font-size:
.alert-radio-label {
font-size: 3rem;
}
The default is font-size: 1.6rem; for Android, and font-size: 1.4rem; for iOS.
UPDATE
Please notice that the alert html element is outside the page, so that style rule should be placed in the app.scss file.

Iterate over theme-variable files in SCSS

I want to create different css-themes for a WordPress theme by using theme setup files. The setup (simplified) would be as following:
/themes/_theme1.scss
/themes/_theme2.scss
/components/_file1.scss
/components/_file2.scss
/theme.scss
The idea is to enable easy theming by adding a class to the body of the document like .theme-theme1 or .theme-theme2. In the files _theme#.scss I want to define variables like text colour, font sizes and so on. In _file#.scss the actual styles are defined.
My question now is, how to iterate over the theme setup files while filling up the files.scss.
Sample idea, Background colour:
body {
###foreach themefile###
&.theme# {
background-color: $background-color;
}
###/foreach###
}
I know how to do this with only one theme available in the resulting CSS file, but I want to make ALL themes available in the resulting CSS. Feel free to ask more details as I am not sure if I explain me right.
Is there a way to create this stylesheet via some kind of foreach loops through variables in theme files or does it have to be done with extra scss-rules per theme file?
This is somewhat possible using a combo of #import with a #mixin to generate the styles. This method should produce minimal repeated code.
Here's how we'll setup the files.
- scss
- themes
- _theme1.scss
- _theme2.scss
- _theme.scss
- styles.scss
The _ prefix on some of the files prevent them from being compiled into CSS to keep our build nice and clean. Now let's go through the contents of the files:
_theme1.scss
$theme-name: 'theme1';
$primary-color: red;
$primary-font-size: 24px;
_theme2.scss
$theme-name: 'theme2';
$primary-color: blue;
$primary-font-size: 12px;
This is an oversimplified example but should give the basic idea. Each theme file will contain only variables.
_theme.scss
#mixin themestyle() {
body.#{$theme-name} {
p {
color: $primary-color;
font-size: $primary-font-size;
}
.bordered {
border: 3px solid $primary-color;
}
}
}
The themestyle mixin will contain all the styles for each theme, using the variables from the /themes/_theme*.scss files. The body.#{$theme-name} will create a selector like body.theme1 or body.theme2, depending on the current value of the $theme-name variable.
In this demo I'm styling on a p tag but this could easily be extended to all elements/selectors for your site. The important thing to remember is all styles need to be inside the body.#{$theme-name} selector.
Now the final, and least DRY part. The styles.scss file will import each theme file then call the themestyle mixin to generate the styles for each theme.
styles.scss
#import 'themes/theme';
/* Theme 1 Styles */
#import 'themes/theme1';
#include themestyles();
/* Theme 2 Styles */
#import 'themes/theme2';
#include themestyles();
The repeated #import/#include is required because it's not possible to #import within a loop or mixin, or this could be optimized a bit more.
Once styles.scss is compiled the output will be:
/* Theme 1 Styles */
body.theme1 p {
color: red;
font-size: 24px; }
body.theme1 .bordered {
border: 3px solid red; }
/* Theme 2 Styles */
body.theme2 p {
color: blue;
font-size: 12px; }
body.theme2 .bordered {
border: 3px solid blue; }
These themes can now be implemented by adding a class to the body tag, like <body class="theme1"> or <body class="theme1">.
Here's a Cloud9 project showing the setup.

How to have Conditional SASS for Internet Explorer

I was wondering if there is way to have conditional statements in SASS for Internet explorer
FOr e.g. lets say this is what I am looking for:
.container {
background-color: black
}
if ie10 {
.container {
background-color: yellow;
}
}
I found a few hacks that work fine like for e.g.
#media screen\0 {
.container {
background-color: yellow;
}
}
This one works fine but it is a HACK!! And I want to avoid it because who knows when IE fixes this issue and then I would have to re-write my whole code.
Here is one more hack that works
.container {
background-color: black;
background-color: yellow\0/
}
So in this case the second statement is read by IE but not by chrome and Mozilla or Safari so this also does the trick but again, this is also a HACK!!
I dont want to use any kind of hacks in to my project because they dont have a certain life-time.
So is there anyone who has figured out a way to apply IE conditional SASS without using hacks but using something official.
I would really appreciate the help.
You could just import an ie partial (ie10.scss)
base SCSS
.ie10 {
#import "ie10";
}
HTML
<!--[if IE 10]> <html class="ie10"> <![endif]-->

Using #-moz-document url-prefix() to target Firefox in SCSS

Is there an easy way to use "#-moz-document url-prefix()" to target Firefox in SCSS documents.
I have tried the following, but the parent reference "&" does not work in this context.
#nav li{
display: table-cell;
#-moz-document url-prefix(){
& {
display: inline-block;
}
}
}
I run into little issues like that from time to time. I mainly use this technique to fix IE8. Here's my fix:
First I install: https://github.com/rafaelp/css_browser_selector
This gives me browser and rendering classes for each browser:
<html class="gecko firefox firefox26 mac">
Then in my SCSS, I can do this:
.foo{
display: block;
.firefox & {
display; none;
}
}
This example hides .foo in Firefox. Using a & after the selector looks back up the tree. Isn't SASS awesome?!?!?
Alternatively, install the script and create a separate firefox.scss and just start it like so:
.firefox {
// Do stuff here
}
As a rule, feature-detection using something like Modernizr is easier than playing to specific browsers, but sometimes you need to address various browser issues.

Resources