less css not rendering (for a split second) on page load - css

I'm using less css in my stylesheets. I'm also in localhost via xampp.
I'm having an issue with my stylesheet not rendering (for a split second) on page load.
The page loads quickly without the styles, then loads just a split second later with the styles applied.
Has anyone had any similar issues using less?
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Feed</title>
<meta charset="utf-8">
<meta name=viewport content="width=device-width, initial-scale=1">
<!-- style sheets -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.5.0/font/bootstrap-icons.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght#400;500;600;700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#100;300;400;500;700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght#100;300;400;500;700&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.materialdesignicons.com/4.8.95/css/materialdesignicons.min.css" />
<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 type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/froala-design-blocks/2.0.1/css/froala_blocks.min.css" />
<link rel="stylesheet/less" href="styles.less" />
<!-- scripts -->
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/less.js/3.9.0/less.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
</head>
<body style="font-family: 'Roboto', sans-serif;">
index.php
<?php
require 'navbar/navbar.php';
require 'header.php';
?>
<!-- My HTML source here -->
styles.less
#import 'whitetheme.variables.less';
/* styles are here */
whitetheme.variables.less
#dark-font-color: rgba(0,0,0, .9);
#medium-font-color: rgba(0,0,0, .8);
#light-font-color: rgba(0,0,0, .7);
#secondary-background: #fff;
#primary-gray: #f0f2f5;
#primary-box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
#primary-blue: #4c8bf5;

less css not rendering (for a split second) on page load
LESS is not CSS.
Browsers do not natively support using LESS files (*.less) as stylesheets.
LESS is a system that generates CSS stylesheet files for you.
LESS is intended to be used as part of a build process (which you'd automate with npm run, or Gulp, or WebPack, or Rollup, or whatever the JavaScript ecosystem thinks is cool this month).
You are using an external third-party script to convert LESS to CSS in the browser, which is the cause of the delay.
Specifically, here's your problem:
<link rel="stylesheet/less" href="styles.less" />
<script src="//cdnjs.cloudflare.com/ajax/libs/less.js/3.9.0/less.min.js" ></script>
You're using a LESS processor script that runs in the browser to convert your .less file into a CSS stylesheet that browsers can natively understand - this process happens asynchronously and independently of the browser loading the page.
This is what's happening (in chronological order, as your browser loads the page):
The browser loads your / page (that's the HTML output of your index.php script).
The browser loads styles.less asynchronously.
However, browsers will load rel="stylesheet" resources synchronously and block the page render, however.
The browser loads less.min.js synchronously (as you aren't using the defer or async attributes) and it then runs and processes your LESS and converts it to CSS.
The less.min.js file will wait until less.min.js is loaded, however.
And it's during that wait-for-less.min.js-file period that the browser renders your page (fast, isn't it?) without the CSS styles that it doesn't have yet.
The best fix is to run the LESS-to-CSS conversion step as part of a project build process. I suggest you read-up on npm and the rest in order to do this properly.
Something like this:
Compiling less with webpack
Install Webpack and LESS:
https://www.npmjs.com/package/less-loader
https://webpack.js.org/guides/getting-started/
cd your-project
npm init -y
npm install webpack webpack-cli --save-dev
npm install less less-loader --save-dev
Configure Webpack + LESS:
webpack.config.js:
module.exports = {
module: {
rules: [
{
test: /\.less$/i,
loader: [
// compiles Less to CSS
"style-loader",
"css-loader",
"less-loader",
],
},
],
},
};
Run npx webpack
Edit your HTML:
Remove the <script> to less.min.js.
Update the <link> to point to the output .css file from Step 3.

Found the issue.
instead of
<?php
require 'navbar/navbar.php';
require 'header.php';
?>
I just needed to require my header first
<?php
require 'header.php';
require 'navbar/navbar.php';
?>

Related

using bootstrap with vue webapp : How to customise

I am using bootstrap-4 in my Vue webapp, But I am not able to customise this as is explained here.
I have my index.html which uses Bootstrap CDN, like following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Sample</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="/static/custom.scss" type="text/x-scss"> ==> where I am overwriting variable
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
</head>
<body>
<div id="app"></div>
<script src="/dist/client-vendor-bundle.js"></script>
<script src="/dist/client-bundle.js"></script> -->
</body>
</html>
I also tried changing the order of custom.scss and bootstrap.css in index.html as well.
Following is custom.scss, where I am overwriting variables:
$body-bg: #f5f5f5;
$body-color: #f5f5f5;
$enable-flex: true;
But this is not working.
I also tried importing this in my app component, like following:
<style lang="scss">
#import 'custom.scss';
// Following also did not work
$body-bg: #333;
$body-color: #999;
</style>
How do I overwrite these variable to customise my webapp.
You can make this a lot cleaner by using the npm package and a webpack customisation.
If you first install bootstrap in your project:
npm install bootstrap#4.0.0-alpha.5
and make sure you can use sass-loader in your components:
npm install sass-loader node-sass --save-dev
now go to your webpack config file and add a sassLoader object with the following:
sassLoader: {
includePaths: [
path.resolve(projectRoot, 'node_modules/bootstrap/scss/'),
],
},
projectRoot should just point to where you can navigate to node_packages from, in my case this is: path.resolve(__dirname, '../')
Now you can use bootstrap directly in your .vue files and webpack will compile it for you when you add the following:
<style lang="scss">
#import "bootstrap";
</style>
To customise it I'd recommend pulling in the bootstrap _variables first and then you can reference everything like normal, i.e.
#import "_variables";
// example customisation - I'd recommend importing a file here to store them all
$body-bg: $gray-dark;
// just place all your customisation work above the below import of bootstrap
#import "bootstrap";
This will mean you need to remove any CDN CSS versions or they could overwrite things, but you should now be able to fully customise bootstrap.
As another benefit this method allows you to strip out any bootstrap components you are not using. To do so rather than #import "bootstrap"; which is the entire bootstrap library, instead navigate to that file, i.e. node_modules/bootstrap/scss/bootstrap.scss and then copy across the #import's you actually want. Essentially optimising your project...
I tried following approach which worked:
cloned the bootstrap repo
modified _custom.scss in this directory.
ran npm install
ran grunt dist as is in documentation
copied generated bootstrap css and js files from here in my index.html
so my index.html has following:
<link rel="stylesheet" href="/static/bootstrap.css" type="text/css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script href="/static/bootstrap.js"></script>
As of now, I don't think customising is possible when using CDN link of bootstrap.

Inline CSS at build time using mustache/ruby

I have project built using RUBY with grunt as its asset pipeline. At build time I need to take all the styles from the /dist/all.min.css file and place them into a custom style tag in the head <style amp-custom>...</style.
When developing I include the css as a linked stylesheet in the head. Which is fine. But when this is live the styles must be inlined.
I have tried a few grunt emailer tasks which are designed to take any linked asset and inline it, however this either didn't work or would take all linked assets and try and inline them and there are a few JS links that i do not what inlined, only the stylesheet.
head during development
<head>
<title>Title</title>
<link rel="canonical" href="/index.html" data-embed-ignore>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
<!-- Linked stylesheet for developing -->
<link rel="stylesheet" type="text/css" href="{{{asset_base}}}/css/all.css" data-embed>
<script async custom-element="amp-image-lightbox" src="https://cdn.ampproject.org/v0/amp-image-lightbox-0.1.js"></script>
<script src="https://cdn.ampproject.org/v0.js" async data-embed-ignore></script>
</head>
desired head for production
<head>
<title>Title</title>
<link rel="canonical" href="/index.html" data-embed-ignore>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
<!-- Inlined styles -->
<style amp-custom>
/* Custom styles here */
</style>
<script async custom-element="amp-image-lightbox" src="https://cdn.ampproject.org/v0/amp-image-lightbox-0.1.js"></script>
<script src="https://cdn.ampproject.org/v0.js" async data-embed-ignore></script>
</head>
I am using mustache for the templating engine and was think i could somehow include the css file if its available. This way when i do the production grunt task it could remove the linked style tag and generate the file thats included via mustache. But i am not entirely sure how to do that or even if it will work.

Where to put CSS files in Grails apps?

First Grails (2.3.6) app here. Trying to add a custom CSS file to my views/index.gsp:
<html>
<head>
<link rel="stylesheet" type="text/css" href="path/to/main.css" />
</head>
<body>
...
</body>
</html>
Inside my grails-app directory, where the views subdir lives, I would have expected to see a resources or css directory, but don't see anything. So I ask: where do I place main.css so that it's available to index.gsp at runtime?
Your css should not go under grails-app/views. It should be under web-app/css/. Then you can do something like this in your GSP...
<link rel="stylesheet" href="${resource(dir: 'css', file: 'main.css')}" type="text/css">

How to use multiple CSS files in Meteor

I've started using meteor and want to know what is a good way to migrate a HTML file that refers to many CSS files. So far, I found that meteor will automatically load all CSS files in an alphabetic order. My two questions are as follows:
Where should I locate the CSS files? (or from where I can control which directories are loaded)
Is it possible to load specific CSS files in particular order?
Here are the current references I have in my HTML file, before migrating to meteor.
<!-- Web Fonts -->
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800|Shadows+Into+Light" rel="stylesheet" type="text/css">
<!-- Libs CSS -->
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/fonts/font-awesome/css/font-awesome.css">
<link rel="stylesheet" href="vendor/owl-carousel/owl.carousel.css" media="screen">
<link rel="stylesheet" href="vendor/owl-carousel/owl.theme.css" media="screen">
<link rel="stylesheet" href="vendor/magnific-popup/magnific-popup.css" media="screen">
<!-- Theme CSS -->
<link rel="stylesheet" href="css/theme.css">
<link rel="stylesheet" href="css/theme-elements.css">
<link rel="stylesheet" href="css/theme-animate.css">
<!-- Current Page Styles -->
<link rel="stylesheet" href="vendor/rs-plugin/css/settings.css" media="screen">
<link rel="stylesheet" href="vendor/circle-flip-slideshow/css/component.css" media="screen">
<!-- Skin CSS -->
<link rel="stylesheet" href="css/skins/blue.css">
<!-- Custom CSS -->
<link rel="stylesheet" href="css/custom.css">
<!-- Responsive CSS -->
<link rel="stylesheet" href="css/theme-responsive.css" />
Thank you for your help! :)
There is no need to provide reference of stylesheets in meteor. Just put your css file in client/stylesheets folder. Meteor will automatically apply these css rules.
As stated by #imslavko you can find Meteor behaviour at https://guide.meteor.com/structure.html
However these rules are more relevant for .js code and .htmltemplate files: Meteor merge and minimize all .css in a single file (as long as they are provided by you and not on a CDN) so you will find a single <link rel="stylesheet"> reference in your <head>.
Remember to put all frontend files inside client folder, to avoid unnecessary server loading and availability.
So you can choose your convenient folder structure for .css files, for example put them all in client/stylesheets or use other subfolders to better manage them.

getting started with LESS

I'm very new to web design...okay, now that that's out of the way, where do I declare variables in the LESS/CSS framework?
I'm using NetBeans IDE 7.0.1
I'm also using Bootstrap 2.0 (not sure if this matters).
I downloaded the latest version of LESS from lesscss.org but it only downloads the minified version.
Here are my links and script tags in the header:
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/realcardio.css" media="all">
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/Main.js"></script>
<script type="text/javascript" src="bootstrap/js/less-1.2.1.min.js"></script>
Can't wait to start assigning values to variables but I'm just not sure what file to do this in?
Thanks all!
You do it in your less file!
sample.less
#font_color: #666;
#font_size: 15px;
body {
color: #font_color;
font_size: #font_size;
}
Then you include this .less file:
<link rel="stylesheet/less" type="text/css" href="sample.less">
<script src="less.js" type="text/javascript"></script>
Before you include the less.js file.
FYI, I find it's actually a much better model to hook up something so that every time you save your .less file in your editor, it compiles it using the node lessc compiler into a .css file and you just include that .css file in your page.
This way I don't have to run a convert before we do a deploy at work.

Resources