In my case, I'm using SASS variables for colors, font-size, and media-queries. I've created them in their own files and then imported to one single index.scss file. Then when I need to use in any .module.scss I just import at the top like:
#import 'src/constants/index.scss';
It is work just fine for colors, font-size...but when comes media queries, the variables are ignored.
works: #media screen and (max-width: 740px)
does not work: #media screen and (max-width: $small)
I even simplified them like so:
$small: 740px;
$medium: 1024px;
$large: 1100px;
then I've applied these variables in font-size and works fine, they just not works in media queries.
Why is that? Any help?
Cheers!
Try this:
#media (max-width: $small) {
//code
}
I had built a website with React.js and webpack.
I want to use Google fonts in the webpage, so I put the link in the section.
Google Fonts
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet">
And set CSS
body{
font-family: 'Bungee Inline', cursive;
}
However, it does not work.
How can I solve this problem?
In some sort of main or first loading CSS file, just do:
#import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro:regular,bold,italic&subset=latin,latin-ext');
You don't need to wrap in any sort of #font-face, etc. the response you get back from Google's API is ready to go and lets you use font families like normal.
Then in your main React app JavaScript, at the top put something like:
import './assets/css/fonts.css';
What I did actually was made an app.css that imported a fonts.css with a few font imports. Simply for organization (now I know where all my fonts are). The important thing to remember is that you import the fonts first.
Keep in mind that any component you import to your React app should be imported after the style import. Especially if those components also import their own styles. This way you can be sure of the ordering of styles. This is why it's best to import fonts at the top of your main file (don't forget to check your final bundled CSS file to double check if you're having trouble).
There's a few options you can pass the Google Font API to be more efficient when loading fonts, etc. See official documentation: Get Started with the Google Fonts API
Edit, note: If you are dealing with an "offline" application, then you may indeed need to download the fonts and load through Webpack.
Google fonts in React.js?
Open your stylesheet i.e, app.css, style.css (what name you have), it doesn't matter, just open stylesheet and paste this code
#import url('https://fonts.googleapis.com/css?family=Josefin+Sans');
and don't forget to change URL of your font that you want, else working fine
and use this as :
body {
font-family: 'Josefin Sans', cursive;
}
If you are using Create React App environment simply add #import rule to index.css as such:
#import url('https://fonts.googleapis.com/css?family=Anton');
Import index.css in your main React app:
import './index.css'
React gives you a choice of Inline styling, CSS Modules or Styled Components in order to apply CSS:
font-family: 'Anton', sans-serif;
you should see this tutorial: https://scotch.io/#micwanyoike/how-to-add-fonts-to-a-react-project
import WebFont from 'webfontloader';
WebFont.load({
google: {
families: ['Titillium Web:300,400,700', 'sans-serif']
}
});
I just tried this method and I can say that it works very well ;)
Here are two different ways you can adds fonts to your react app.
Adding local fonts
Create a new folder called fonts in your src folder.
Download the google fonts locally and place them inside the fonts folder.
Open your index.css file and include the font by referencing the path.
#font-face {
font-family: 'Rajdhani';
src: local('Rajdhani'), url(./fonts/Rajdhani/Rajdhani-Regular.ttf) format('truetype');
}
Here I added a Rajdhani font.
Now, we can use our font in css classes like this.
.title{
font-family: Rajdhani, serif;
color: #0004;
}
Adding Google fonts
If you like to use google fonts (api) instead of local fonts, you can add it like this.
#import url('https://fonts.googleapis.com/css2?family=Rajdhani:wght#300;500&display=swap');
Similarly, you can also add it inside the index.html file using link tag.
<link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght#300;500&display=swap" rel="stylesheet">
(originally posted at https://reactgo.com/add-fonts-to-react-app/)
In your CSS file, such as App.css in a create-react-app, add a fontface import. For example:
#fontface {
font-family: 'Bungee Inline', cursive;
src: url('https://fonts.googleapis.com/css?family=Bungee+Inline')
}
Then simply add the font to the DOM element within the same css file.
body {
font-family: 'Bungee Inline', cursive;
}
Another option to all of the good answers here is the npm package react-google-font-loader, found here.
The usage is simple:
import GoogleFontLoader from 'react-google-font-loader';
// Somewhere in your React tree:
<GoogleFontLoader
fonts={[
{
font: 'Bungee Inline',
weights: [400],
},
]}
/>
Then you can just use the family name in your CSS:
body {
font-family: 'Bungee Inline', cursive;
}
Disclaimer: I'm the author of the react-google-font-loader package.
Had the same issue. Turns out I was using " instead of '.
use #import url('within single quotes'); like this
not #import url("within double quotes"); like this
I can see there are various different ways to include google fonts in react app. Let's explore the most preferred and optimum way.
#import vs link
The two options that google font provides are using link and #import. So now the question directs toward decision in between #import and link. There is already a Stack Overflow question regarding this comparison and here is a reference from the accepted answer
<link> is preferred in all cases over #import, because the latter
blocks parallel downloads, meaning that the browser will wait for the
imported file to finish downloading before it starts downloading the
rest of the content.
So, it's most preferable to use the link tag that google font provides
How to use link and why in a react app?
I have seen few answers giving this method as a solution but I want to make it more clear why it is most preferable.
After using create-react-app to initialize the project, you can see a comment in the index.html file inside the public folder as below.
You can add webfonts, meta tags, or analytics to this file. The build step will place the bundled scripts into the tag.
So you can simply include the link tag that google font provides in the head section of the above file.
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet">
Then you can use it in the CSS file and import in JSX
font-family: 'Bungee Inline', cursive;
In case someone needs it, you can use #fontsource. They have all of the Google Fonts and seems easier than most of the solutions here.
If anyone looking for a solution with (.less) try below. Open your main or common less file and use like below.
#import (css) url('https://fonts.googleapis.com/css?family=Open+Sans:400,700');
body{
font-family: "Open Sans", sans-serif;
}
Edit index.css
#import url("https://fonts.googleapis.com/css2?family=Poppins:ital,wght#0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");
body {
margin: 0;
font-family: "Poppins", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
I added the #import and the #font-face in my css file and it worked.
Add link tag in index.html on root directory inside public folder.
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet"/>
then use it in any css file.
Create a new folder fonts in your src folder.
Download the google fonts locally and place them inside the fonts folder.
Open your index.css file and include the font by referencing the path.
#font-face {
font-family: 'Roboto';
src: local('Roboto'), url(./fonts/Roboto/Roboto-Regular.ttf) format('truetype');
}
now you can use font link this
.firstname{
font-family: Roboto, serif;
color: #0004;
}
It could be the self-closing tag of link at the end, try:
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet"/>
and in your main.css file try:
body,div {
font-family: 'Bungee Inline', cursive;
}
In some cases your font resource maybe somewhere in your project directory.
So you can load it like this using SCSS
$list: (
"Black",
"BlackItalic",
"Bold",
"BoldItalic",
"Italic",
"Light",
"LightItalic",
"Medium",
"MediumItalic",
"Regular",
"Thin",
"ThinItalic"
);
#mixin setRobotoFonts {
#each $var in $list {
#font-face {
font-family: "Roboto-#{$var}";
src: url("../fonts/Roboto-#{$var}.ttf") format("ttf");
}
}
}
#include setRobotoFonts();
I want to have a file just to store the colors and some other settings that I am going to use in my css styles. Because I don't want to specify the same color in different files multiple times. How can I achieve that with css modules?
For example:
setting.css
$primary-color: #785372;
$secondary-corlor: #22b390;
Button/styles.css
.button {
background: $primary-color;
display: flex;
}
From your samples that looks like Sass (which can be used in conjunction with CSS modules). If so then just import the partial containing the variables.
#import 'path/to/variables.scss';
If there's no Sass involved then postcss-modules-values is what your looking for:
variables.css
#value primary: #785372;
#value secondary: #22b390;
styles.css
#value primary, secondary from './path/to/variables.css';
.button {
background: primary;
display: flex;
}
EDIT:
Actually there's even more options, still through PostCSS plugins. postcss-simple-vars or postcss-custom-properties, the later having the clear advantage to stay close to the CSS specification.
They all share the same requirement though, importing the configuration file in a way or another.
You can do this with using CSS Custom Properties (here's a tutorial I've found).
In your settings.css file, do (the `:root':
:root {
--primary-color: #785372;
--secondary-corlor: #22b390;
}
Then, you can use those constants in the same, or a different file
.container {
color: var(--primary-color);
}
If you're using them in a different file, be sure to import both stylesheets, for example:
import './Button/styles.css'
import './settings.css'
Also, according to this answer, you can do this in html as well, by linking the two stylesheets:
<link href="settings.css" rel="stylesheet">
<link href="Button/style.css" rel="stylesheet">
I have a phone number on a website. It looks good on a laptop, but on a mobile device half of the number jumps to the next line. It doesn't look good.
So how can I create a line break that will only work on a mobile device sized screen?
I am not very experienced in coding, so please be specific :) Thanks a lot for any help!
Why not just do a line break as a class in the tag?
<br class="mobile-break">
and in CSS
#media screen and (min-width: 600px) {
.mobile-break { display: none; }
}
If you use Bootstrap 4 just use the break like this:
<br class="d-md-none">
You could look into the CSS word-break property to prevent words/strings being cut in half. If it is specifically line breaks you want to use then appending a class to the element such as <br class="br-on-mobile"> and setting it to display: none in the CSS should prevent it from doing anything normally.
You can then use a media query to display the line break at specific mobile screen sizes, for example:
.br-on-mobile {
display: none;
}
#media screen and (<Your conditions here>) {
.br-on-mobile {
display: static;
}
}
EDIT: static is invalid value for display. Using inherit should fix the issue. See this Fiddle
EDIT: The header of your page must also have <meta name="viewport" content="width=device-width, initial-scale=1"> to allow for correct scaling/application of media queries.
You could also achieve this by wrapping the number in a span element and setting this to display: block when on mobile devices, although your issue with the media queries below will also apply to this.
If you're looking into this in the future for something that works in Tailwind CSS, I found this to mirror the Bootstrap and BULMA style implementations (choose your breakpoint between sm/md/etc):
<br class="md:hidden">
Instead of space between the number, add this is non-breaking space which will prevent a new line being added.
This should work on BULMA: <br class="is-hidden-desktop" />
Similar questions have been asked here before, but after reading through them I've not yet found an answer that works with my site.
I've built the site around Bootstrap but added some of my own media queries. Live test site is at: http://agoodman.com.au
The sections being changed by the media queries are "our fees" and the "map" overlay. If you're on a laptop, resizing the browser makes these sections display as blocks.
My stylesheet links:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/user.css" rel="stylesheet">
"User.css" is just a separate file because I wanted to be able to keep and update bootstrap's main framework as necessary. User.css overrides the styles in bootstrap. My media queries in user.css are as follows:
#media screen or handheld(max-width: 979px) {
.fee-buttons {
height: auto;
font-weight: 600;
position: relative;
padding: 0;
margin: 0;
list-style: none;
}
.fee-buttons .transformation {
width: 100% !important;
height: 300px;
position: relative;
z-index: 10;
left:0;
}
.fee-buttons .hourly, .fee-buttons .membership {
float: none;
width: 100% !important;
}
li.button{
overflow:visible;
}
}
#media screen or handheld(max-width: 995px) {
#overlay {
position: relative;
display: block;
width: auto;
right: 0;
padding:1em;
}
}
As I said, on desktop browsers this works fine, but on mobile browsers it's not working at all. I've tested both on an iPhone 4 (using safari) and on an HTC Desire (using the stock android browser) and both display the same way - ignoring the media query and just displaying the full website with lots of really squished and unflattering content.
Any ideas on what I'm doing wrong here?
EDIT:
Here are screenshots of what it's SHOULD look like at a small screen width:
And what it currently looks like on Android and iPhone, where the device is ignoring my media queries:
Sorry to answer my own question, but I found something that worked.
There was an error with the way I set up the media query. Instead of
#media screen or handheld(max-width: 995px)
I changed the query to
#media handheld, screen and (max-width: 995px)
as suggested by this guy: https://stackoverflow.com/a/996820/556006
and it worked perfectly across all devices. Thanks to those who offered suggestions, upvotes to all of you.
displaying the full website with lots of really squished and unflattering content.
This might be due to the fact that your media queries target large screens with a width of 979 and 995 pixels. Mobile screens are much smaller.
To target something like an iPhone 4 you need a max-width of 960px (that's why bootstraps default is at 960) for landscape and 480px for portrait.
Since you can't target all possible screen sizes bootstrap offers a sensible list of default widths which you should stick to too.