I have tried to to that using this line of code but it doesn't work
#import url("Javascriptfile.js");
and <script type="text/javascript" src="Script.js">
these two ways doesn't work.
in js file I have
export const PrimaryColor = "#4267B2";
and I want to import it in my css file how can i do it?
I do not believe this is possible in any way.
You can manipulate CSS with JavaScript to a certain extent, but it is mostly done through DOM manipulation (acting on class names) or generating CSS programmatically (but it is still CSS in the end).
I don't think it is ever possible to access "JavaScript world" from the scope of CSS.
However, if you want to use variable names in CSS to reference constants, like colors, sizes, etc., you can use the CSS custom properties feature : https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
Related
I want to use bootstrap classes and my definitons in next.js modules together. How can I do it?
First, rename your .header-top to .headerTop in header.module.scss.
Then, include your class name:
<div className={`${styles.headerTop} container d-flex ...`}>
Explanation: class names in CSS modules are automatically processed to make them unique to ensure scoping. For example, .headerTop will become something like .header_headerTop__JnCx9 (.fileName_className_randomString). Therefore, in components you need to include your class names as JavaScript expressions rather than strings. Namely, if you would have one class name, you would do: <div className={styles.headerTop}></div>. But in your case you want to include your own class name with bootstrap classes (which are not CSS modules). One way of doing it is using Template literals which allow to include expressions within strings.
You need to first install bootstrap.
npm install --save bootstrap
You can only import css in one location in NextJS.
create pages/_app.js and import the css file.
import 'bootstrap/dist/css/bootstrap.min.css';
in pages/_app.js
Head Tag and insert ALL bootstrap into it:
<Head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"></link>
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</Head>
Then in any component, once you have imported your Name.module.css, e.g.:
import styles from '../styles/Navbar.module.css';
Now you can combine self-made CSS classes with Bootstrap classes like this:
<div className={`${styles.redBorder} bg-dark`}></div>
NOTE: you CAN NOT use dashes for class names, so this will NOT work:
.red-border;
this will NOT apply in next.js
instead use CamelCase.
.redBorder;
then it works.
Its been 2 whole days i can't find any solutions.I have no solutions yets:( I'm having color states. I'll select from color picker a color, I'll update that particular color state:
Requirement is if I pick from color picker it must be passed from react js property or variables to scss variable n override them. it must be done via reacrjs to scss if it can be done from js to css then it can be done from reacr js to scss whats that one thing which m missing on it.
App.js
{
primary: '#1976D2',
secondary: '#424242',
accent: '#82B1FF',
error: '#FF5252',
info: '#2196F3',
success: '#4CAF50',
warning: '#FFC107'
}
ex: primary: '#1976D2' I'll pick in update to primary: '#ffffff' something like:
App.js
changeColor(e){
this.setState({primary:e.target.value}) // the value is updated to #ffffff
}
Now, I need to pass this.props.primary to .scss something like:
variables.scss
$primary:this.props.primary
login.scss
.btn{
background-color:$primary
}
my need is it must be dynamic if i pick from color picker it must be passed from react js property or variables to scss variable n override them
We can do it inline styling but I wanna do it the way defined above (via .scss).
Is it possible?or is the any better way?
something like this
https://vuetifyjs.com/en/style/theme
vuejs uses theme thats overides to scss variables
Vue.use(Vuetify, {
theme: {
primary: '#3f51b5',
secondary: '#b0bec5',
accent: '#8c9eff',
error: '#b71c1c'
}
})
can anyone please explain me how they r doing I'm not understanding
First. Just to clarify. You are not able to change SASS variables from browser. Because how it works: SASS code -> compiled into CSS -> CSS is sent from server to browser -> . So we actually should searching "how to override by JS something that was SASS variable in consistent way".
The only way I see doing that is using CSS custom properties. Take a look into spec to understand their caveats(e.g. it cannot be used as part of size value in #media) and about support in browsers.
I'm not really sure if SASS supports compiling variables into CSS custom properties. Take a look into css-vars mixin. With using
$css-vars-use-native: true;
you will get your variables exported as CSS custom properties. Unfortunately it means you need to change you existing styles to use var() from mixin for variables you want being able to override later
Then your code will be able to override any of custom properties as easy as
document.body.style.setProperty('--primaryColor', myColorValueFromDatePicker)
Take a look into fine and short article on how to change custom properties with JS
NB since custom properties uses the same cascade approach as any CSS you additional get ability to apply changed value on any part of DOM:
document.querySelector('#previewBlock').style.setProperty(....);
I would use Styled Components instead. There you can pass your styling by props
https://www.styled-components.com/docs/advanced
I am currently learning React and Redux. I have forked a boilerplate, and currently I am looking through all of the code to see how it fits together.
While scanning through some React Component files I found something very interesting! When setting className for many of the elements the syntax they use differs. The first time they use the following syntax:
<span className={classes['counter--green']}>
...
</span>
Then this syntax:
<button className='btn btn-default'>
...
</button>
And from there on out they use the following:
<h2 className={classes.counterContainer}>
...
</h2>
At the top of the file they import classes with the following:
import classes from './Counter.scss'
So simply my question is, why are there three different syntax for className, and which one is correct?
Starting with the simple case:
<button className='btn btn-default'>
This just sets the class name to "btn btn-default". You need to set className instead of class since you are writing JSX and that’s equivalent to setting the className DOM property, so you have to use className here.
<span className={classes['counter--green']}>
<h2 className={classes.counterContainer}>
These are both very similar. In JSX, to specify a more complex JavaScript expression, you need to put it inside curly braces. This would be equivalent to setting the className property like this:
someSpan.className = classes['counter--green'];
someH2.className = classes.counterContainer;
Finally, classes as imported using the import classes from './Counter.scss' syntax is a feature called “CSS modules”. It involves a precompiler step that properly sets the class names later and makes sure that the style definitions are rendered to the HTML.
So to answer your final question, all of these are correct. What you want to use depends on where you want to define your styles (or where they are defined). Using CSS modules makes a lot of sense if you are creating independent components that might even get reused elsewhere. Using global CSS is good when you have an existing style sheet which you just want to use (e.g. here, these class names are likely from bootstrap). And of course, you can also mix these, if you have global styles and want to add additional styles to it.
In this case, without seeing the code, i'm guessing the boilerplate is using modular css (scss).
modular css can be imported,in this case as classes. Using modular css gives you css with local scope
all css rules imported from a module as classes are prefixed with the name classes.
and are to be treated as you treat object attributes
so css rule counterContainer is used as
classes.counterContainer
css rule name counter-green cannot use dot notation(because of the hyphen), so must use square bracket and string name notation as for any javascript object attribute
classes['counter-green']
the third example btn btn-default is not imported from a css classes module but is instead imported globally. This is possibly a bootstrap class imported at root level as a link attribute on the index.html
This isn't really unique to className, but for any JSX you can use curly braces {}'s to switch from "JSX mode" back to javascript to use any JS expression. Therefore in the first example they are setting the attribute className by referencing an object called classes by the property counter--green, in the second example they are using a simple JSX string literal 'btn btn-default', and in the third example they are referencing the classes object by the property counterContainer. You can see the definition of the classes object according to the import at the end of your question.
They are all correct, they are simply different ways of doing it. By using JS expressions they are arguably more modular, by using a string literal it is easier to see in front of you what is going on, but less re-usable.
This question already has answers here:
How to control Sass Variable with javascript
(9 answers)
Closed 6 years ago.
Basically I want to use background color for each element as per user choice, user choice will be stored on database. User should webpage according to it's color profile. So, I need a idea to get that value from angular and store its value on scss variable and render it.
SCSS is a preprocessor, when you use SCSS it actually gets compiled into CSS and that's what you use.
Perhaps the best solution is to use ngStyle in the section you need.. body, div, or anywhere you want..
Example
<div ng-style="userStyle">
...
</div>
In your JS
$scope.userInfo = { backgroundColor: 'red' };
$scope.userStyle = {'background-color': $scope.userInfo.backgroundColor};
Assuming you have a userInfo object, with backgroundColor property.
This is not possible, angular is javascript code and runs in the clients browser, sass is pre-compiled to css, any variables will have been converted into the full values before it is rendered in the browser.
You would need to get angular to make a call to your server, and have the server generate a CSS file dynamically based on the value provided from angular. (I suppose you could do this on the client also, I'm sure there is a javascript library somewhere that lets you compile sass)
Scss compiled to css before build, so angular part can't control scss. You can use gulp/grunt to edit scss variables. But in your case it not helping.
The simple sulotion for you is to use ng-style.
<div ng-style="{'background-color': scopeUserColor}"><div\>
#font-face seems quote straightforward to setup in my CSS, and yet, my defined font is not being used. This is in a CakePHP 2.2 webapp, and despite trying all sorts of CSS URL path combinations, external CSS -v- inline CSS and single quotes/no quotes/double quotes in the CSS; nothing's working.
Here's the CSS:
<style>
#font-face {
font-family:TerminatorTwo;
src: url(../files/TerminatorTwo.ttf) format('truetype');
}
div {font-family: TerminatorTwo}
</style>
My app has the default CakePHP folder structure, so the CSS is in app/webroot/css and the font (just the .ttf) is in app/webroot/files
Instead of the div part in the CSS, I've tried inline style definition on a specific div. Didn't work. the code as seen doesn't work, and it doesn't work wherever I try adding quotes in the CSS, as I've seen in other examples elsewhere. All this has been trie din the latest versions of IE8 (yes, 8, yuck), Firefox and Chrome. Oh, and Chrome for Android.
I've messesd around with the path too, but to no avail.
Utterly frustrated. It's inherently so simple!
The font file is probably not loaded due to an invalid path. It's better to use the HtmlHelper for in these cases, as relative paths are bound to get messed up in a Cake install. Use the url() method to point to the files directory.
#font-face {
font-family:TerminatorTwo;
src: url(<?php echo $this->Html->url('/files/TerminatorTwo.ttf'); ?>) format('truetype');
}
Cake will make sure the URL points to the files directory in /app/webroot at all times.
Alternatively you could ditch the inline CSS and use an external stylesheet to avoid the issue with correct paths in Cake. In that case, if the CSS file is in the app/webroot/css directory, you could use the ../files/TerminatorTwo.ttf notation.