How to split css (above/below fold) for best performance - html

My style.css is a Render Blocking CSS.
I want to load critical css (above the fold css) as early and quickly as possible, and less important css (below the fold) later on.
How can I do it? I would have played around with different tools online if only they were scientific (that is repetitive between trails) but now I guess I want a theoretical approach.
One option is to have everything as default, that is just load the style.css as it comes even though it is render blocking. This is where I am today and I want to get away from this.
Or I could use option rel="preload" on style.css, so at least it would not render block?
Or I could split style.css into style_above_fold.css and style_below_fold.css and preload style_above_fold.css. And hopefully style_below_fold.css would not be render blocking.
Preload both style_above_fold.css and style_below_fold.css, but then maybe style_above_fold.css would not be prioritized above style_below_fold.css, and maybe that would be just like preloading the whole style.css.
Preload style_above_fold.css and put styles in style_below_fold.css as inline code instead (I have to my surprise discovered that preload css loads before inline css (or at least preload is prioritized before inline code)).
Put styles from style_above_fold.css as inline css and put the request for style_below_fold.css in the footer.
Just like 6, but instead preload style_above_fold.css. You see, if the tools online were sensitive enough, I could measure whether inline css or preload gives better performance.
Can someone advise my what to do if I want to maximize the performance?
Is it the same for JavaScript code?

You's should be separate each css file by feature,
ex: in homepage you should be create css file is need in homepage, not a combine style of all page in website. This will make a css file is small size and faster.
Beside you can render this css is inline your css it will useful.
If you use bootstrap or other third party, you can think about use javascript to load this file like code bellow:
function loadFile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
Usage: loadFile("your_css.css", "css")
Hope it can help!

One thing that makes an awesome improvement in above-the-fold content is applying a min-height: 100vh to your first section of above-the-fold content and make sure the elements aren't animated or video (unless you've optimized and lightened the video as well). That made a dramatic change in my Core web vitals.

Related

Is there any way to make a BASE URL which applies even if HREFs are "absolute"?

I'm developing a website which will live on https://www.example.com/. While developing, and later as a test site, it's at http://127.0.0.1/temp-dir-for-my-project/.
This means that I currently have a bunch of hrefs in the HTML, as well as CSS files, starting with /temp-dir-for-my-project/, which obviously break once I'm done and upload it to the live site. Over there, it should be / instead.
Sadly, the BASE element, which I thought would solve this, only applies to relative paths. For example, ./meow.html with /temp-dir-for-my-project/ as the BASE would refer to /temp-dir-for-my-project/meow.html, but /meow.html in the same situation would be... /meow.html, because it's an "absolute" path.
Before you say "just use relative paths, then!", well... If I do that, I have to keep track of in which "dir" I am. For example, for the webpage at https://www.example.com/test.html, I could do: ./other.html and it would work both on the live site and in my test site (assuming the BASE is set). But the webpage at https://www.example.com/subdir/test.html would have to link to './../other.html' or else it wouldn't link to the correct page.
This gets messy. I wish I could use "absolute" paths and still have the BASE be the... base. Is there a way, or am I forced to use ./../../blabla... for any page located in subdirs (whether those be real subdirs or just how the URL is rewritten to look)?

How can I set a custom style for viewing images?

How can I do that? Because I don't really know the word but I hope somebody knows this.
If you guys don't understand this then there is a link.
https://media1.tenor.com/images/0f097ed319d498c2bda3d87ba4f6ff10/tenor.gif?itemid=12846096
It's a gif but they set it into a cool style and I don't know how to set it like that.
The page most likely isn't actually a gif image, it could be some file structure like
> tenor.gif
>> index.html
where tenor.gif is actually a folder, with index.html inside it. Per W3C standard, folders automatically open up index.html if there is one directly inside it.
You could put whatever html code you want in index.html and styling.
You can check the contrast of the styles in the CSS documentation, they usually add you to the styles and filters, so they are fully configurable

How get css files from website?

I'm trying to get the content of css files of a website....
<link href="/files/includes/templates-css-main.css" rel="stylesheet" type="text/css" />
For example, it's the link of css ref of http://paceoil.ca/. When I tried to send a request for getting a css file to this url http://paceoil.ca/files/includes/templates-css-main.css, I got an unexpected result.
Any help? Thanks in advance.
On the particular website in question, the reason you're not seeing what you expected is because they've used an uncommon technique called #import to load several stylesheets into one. In general, your method is correct -- http://paceoil.ca/files/includes/templates-css-main.css is indeed a link to their stylesheet.
Inside that stylesheet, you'll see the following statements:
#import 'templates-css-reset.css';
#import 'templates-css-layout.css';
#import 'templates-css-type.css';
#import 'templates-css-nav.css';
#import 'modules-mod_superfishmenu-tmpl-css-superfish.css';
These lines simply load the contents of other .css files all together. The other CSS files can be found at:
http://paceoil.ca/files/includes/templates-css-reset.css
http://paceoil.ca/files/includes/templates-css-layout.css
http://paceoil.ca/files/includes/templates-css-type.css
http://paceoil.ca/files/includes/templates-css-nav.css
http://paceoil.ca/files/includes/modules-mod_superfishmenu-tmpl-css-superfish.css
It should also be noted that on some websites (most commonly on huge websites like facebook), CSS files are not necessarily statically generated. Some servers run "CSS-preprocessing" which allows them to embed code in CSS that is executed and translated before your browser ever sees it. In cases like this, it is impossible to view that code unless the owner shares it with you.
press F12, resources. then you should see the css file of a site

Separately loading in Bootstrap components

I'm working on a large web project that involves many developers, and I would like to slim down a package of Bootstrap3 and keep only what we're using. Basically, the idea is to cut out any extra overhead when the page loads in the browser.
So there are 2 ways I can go about doing this:
I can either...
a.) remove any extra parts from the library, create a new build, and then load that into our project.
For example:
<!-- Custom build of a slimmed down Bootstrap -->
<script src="/bootstrap/3.0.3/js/bootstrap.min.js"></script>
Or...
b.) modularize the entire Bootstrap3, separate each component into its own file, and write the entire build into the html, while commenting out the things we don't need.
For example:
<!-- All Bootstrap components-->
<script src="/bootstrap/3.0.3/js/glyphicons_bootstrap.min.js"></script>
<script src="/bootstrap/3.0.3/js/buttongroups.min.js"></script>
<script src="/bootstrap/3.0.3/js/inputgroups_bootstrap.min.js"></script>
<!-- <script src="/bootstrap/3.0.3/js/navs_bootstrap.min.js"></script> DON'T NEED THIS -->
<script src="/bootstrap/3.0.3/js/navbars_bootstrap.min.js"></script>
<!-- <script src="/bootstrap/3.0.3/js/breadcrumbs_bootstrap.min.js"></script> DON'T NEED THIS -->
<script src="/bootstrap/3.0.3/js/pagination_bootstrap.min.js"></script>
etc...
The advantage of using the second option would be that it would give the other developers more control of the bootstrap components that we're loading into the project, without having to go and rebuild it again. If in any event in the future they need to load some new Bootstrap components, they can just uncomment that line of code. That would make this more flexible for other developers to use, and it wouldn't restrain them from developing further throughout the project using Bootstrap.
What are some thoughts about this however? Would pulling more files into the project (as opposed to pulling in one large file) increase the overhead at loading time? Is this against "good/best practice"?
If you go to the https://github.com/twbs/bootstrap/tree/master/js all the js is separated out in the js folder. The dist folder has the compiled files.
However, one file is less http requests and faster loading. The bootstrap.min.js file is small. Plus once it's cached by the browser you don't need to worry about loading.
Fewer files, faster loading because there are fewer http requests. And for that matter, you can use Bootstrap's CDN for their js and their respond.js.
It's the CSS that needs slimming down if you don't use certain components. I never use their navbar (two levels only), modal (no images, iframes etc), and other things, so I don't use those things. I use the buttons, grid, panels, and other stuff. I use LESS and CodeKit. Just // comment out the components you are not using and recompile. that's where you'll see the biggest gains in slimming it down.
In bootstrap.less if you do this:
//#import "bootstrap/popovers.less";
//#import "bootstrap/component-animations.less";
Then all the resulting CSS won't be part of the final bootstrap.css file
The first way is better to use in production,
the second is better when you develop your site
Below you can find a good list of minification tools for you, that help you to combine js files automatically.
Is there a good JavaScript minifier?
In the first place not loading what you don't use will always the best option. As mentioned by #cab in general reduce http(s) requests should give faster loading.
Bootstrap has a modular structure and the idea behind is you only have to load (or compile) what you need. Compiling your own copy of bootstrap will be relative easy. The result will be one javascript file and one css, both containing the components you need.
Bootstrap's customizer (http://getbootstrap.com/customize/) will be the easiest way to compile your own copy, you can select the component Javascript / CSS you will need, and download your copy.
Second option is to download the source from github and compile this. I use grunt mostly to do this Bootstrap refers to http://bower.io/ to do this. Before compiling comment out what you don't need. For CSS you can do this in less/bootstrap.less and for javascript in Gruntfile.js (around line 73).

Two CSS files with different settings

I am developing this site, it got my CSS file, and also have references to other CSS files for plugins (like treeview for example).
The problem is that in CSS the plugin as it modifies the the elements for example, in my file and also modify this element.
The browser is considering the plugin CSS and not mine.
How can I force the browser to use my tag to CSS for example.
Can not just remove the CSS plugin because there are other tags that you must configure it.
It would be better to reverse the order by wich the files are loaded.
However, if that is not an option, you can add " !important" to the CSS property you want to take precedence (ex: "margin:0px !important").
After that, unless some other file has that keyword too, it won't matter in wich order the files are loaded.
This is very useful sometimes, but it is not a best practise.
If I'm understanding your question it gas to do with when you are including the CSS files in your HTML documents.
Example: in mystyles.css you have #myelement{margin:0} and in plugin.css #myelement{margin:20px}
If plugin.CSS is brought in your HTML doc after mystyles.CSS, plugin.css take precedence.
link href="mystyles.CSS"
link href="plugin.CSS"
Sounds like you want to reverse the order to
link href="plugins.CSS"
link href="mystyles.CSS"
Now #myelement will have a 0px margin.
Hope that helps.