3

I want to define global variables in one css file and use the variables in other css files.

Is this possible?

Global.css:

:root {
    --main-color: #192100;
    --main-background: #89b66b;
}

html, body {
    min-height: 100%;
    width: 100%;
    font-family: Arial;
    color: var(--main-color);
}

SomeFile.css:

.some-rule {
    display: table;
    border-radius: 12px;
    border: 4px solid var(--main-color);
    padding-left: 10px;
    padding-right: 10px;
    color: var(--main-color);
}

Html: (Global.css if referenced before SomeFile.css)

<link href="Global.css" rel="stylesheet" type="text/css" />
<link href="SomeFile.css" rel="stylesheet" type="text/css" />
4
  • 2
    no can do. you need a preprocessor like sass or less to achieve such a thing Commented Aug 18, 2017 at 13:30
  • recently i came across nicolasgallagher.com/custom-css-preprocessing Commented Aug 18, 2017 at 13:30
  • Wait for CSS4 or use sass Commented Aug 18, 2017 at 13:31
  • Why not just have a class of root that you just add to elements you want to have it on? Commented Aug 18, 2017 at 13:33

2 Answers 2

1

You can't do that with CSS. Try A CSS Preprocessor like Less or Sass.

less.css sharing variables across files

SASS - use variables across multiple files

Sign up to request clarification or add additional context in comments.

2 Comments

Ended up using @import for now. But I'll read more about Less and probably use that in the future
@SteinTheRuler How did you use @import for this? Thanks.
0

CSS itself doesn't use variables. However, you can use another language like SASS or LESS

1 Comment

True, but check the browser compatibility table. You really don't want to use this just yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.