Automate inserting stylesheet into HTML - html

How do I automate turning something like ...
<link rel="stylesheet" href="css/main.css">
... into something like ...
<style>
/* lotsa CSS rules */
</style>
... in an HTML document?
I currently do it manually. I also use a postCSS with a bunch of plugins. So, I start with a separate CSS file and I use a link tag initially. When I'm done working on the CSS, I comment out the link tag and add a style tag and copy all of the CSS into the document. Vim makes this a little easier:
:read css/main.css
I already use gulp for automating some of my workflow, so I prefer a solution that can be easily integrated with that.
Gulp has been a great benefit for me, but I might not understand enough about how it works. I tried searching the plugins and I found too many that look like they'll do what I want. Some of them seem to process the CSS and HTML to add style tags to the individual elements, which is not what I want.
gulp-smoosher
gulp-css-inliner
gulp-inline

It looks like Grunt has a task for this. Check out the github repo here.
Install the plugin:
npm install grunt-inline --save-dev
Enable the plugin inside your gruntfile:
grunt.loadNpmTasks('grunt-inline');
In your project's Gruntfile, add a section named inline to the data object passed into grunt.initConfig().
grunt.initConfig({
inline: {
dist: {
src: 'src/index.html',
dest: 'dist/index.html'
}
}
})
Which should turn this:
<link href="css/style.css?__inline=true" rel="stylesheet" />
Into this:
<style>
/* contents of css/style.css */
</style>`

Related

Localize entire stylesheet to one div?

On my laravel app, I'm using a forum package called "chatter".
This forum is injected into my master layout, so it looks like this:
nav bar
chatter package
footer
It's injected into a container called <div id="chatter">, and its styles are found in the style sheet chatter.css, which is separate from my main sheet.
The problem is, some of the styles in this sheet are conflicting with my nav and footer. Furthermore, some of the styles in my main sheet are affecting the forum (albeit minimally, so I don't mind making the changes manually).
I can't change the markup, but I can edit the styles.
So how could I make it so that all the styles found in chatter.css ONLY apply to what's inside of <div id="chatter">?
Add #chatter to every style in chatter.css like this
#chatter table{...}
#chatter tr{...}
#chatter td{...}
etc.
If style is for level above the chatter div then add after like this:
html #chatter{..}
body #chatter div{...}
You will have to namespace your CSS as user Nawed Khan pointed out but there is a much simpler way to do that than changing each of your styles manually. This method uses less to handle it for you.
Drop this in a file called chatter.less.
#chatter {
#import (less) 'chatter.css';
}
Then you need to include it on your page...
<link rel="stylesheet/less" type="text/css" href="chatter.less" >
Then you need to include less.js AFTER you've included your .less file.
<script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js"></script>
In your .less file you might have to mess with the path to chatter.css, I believe it's going to be relative to whatever file you are including the .less file on.

Angular JS directive - location of CSS file relative to HTML

My directive uses an HTML file. The HTML file uses a CSS stylesheet. I need to distribute the directive js, HTML and CSS files, so the CSS location definition needs to be relative to the HTML.
Note: This is how I solved the location of the HTML, I have pending to solve the location of the CSS file.
I put the CSS file in the same folder as the HTML file, and then defined in the HTML:
<link rel="stylesheet" href='somefile.css'>
however this points to the domain root, not to the HTML file location.
Any ideas how to achieve this?
I think there are two ways to fix your issue as you don't know where the directives are located:
Solution 1 - Ancient HTML Way
If the length of the CSS is small, you can directly include it in your template HTML itself, through the style tag.
<style type="text/css">
Add style rules here
</style>
Solution 2 - The Angular Way(most recommended)
Use ngHref directive.
In your directive.js code, you can just put the path of the directive.html to a scope/rootScope variable and then you can access it from the directive.html
In directive.js
link: function (scope, iElement, iAttrs) {
scope.htmlPath = <path of templateURL>;
}
In directive.html
<link rel="stylesheet" ng-href="{{ htmlPath }}/filename.css">
Note:
I hope you are not using Gulp to build the angular JS code. But, If your are using gulp, you can add a gulp task and pipe all the CSS to a single CSS and can inject to your index.
You need to make separate directories for css and html(template files)
and use full path from root to the css folder
<link rel="stylesheet" href='/angualr/css/style.css'>

Load svg as path

I use webpack via CLI, like webpack --watch.
Goal is to compile JS with SASS and put in some separate html page.
I have some links to svg files in my sass, looking like that:
label {
background: url(../images/checkbox.svg);
}
The problem is that I never get it in the browser after compiling. I have following situations:
If I compile it using svg-loader, in browser I see compiled css is something like that:
background: url([object Object]);
Here is suggested to use svg-url-loader instead.
If I use mentioned svg-url-loader, I have whole page css messed up which makes browser to show raw text instead of styled page.
If I use file-loader, I get css compiled to background:
background: url(37efc0ccedf6fe109636ad1416c29425.svg)
and as I put resulted bundle.js to some other place, I am not happy with copying some files I already have.
If I use raw-loader, I get some xml instead of file name in the url(...), which just doesn't work, showing "wrong property value" error in the css.
I would be happy to get just regular path instead of all that things, like
background: url(../images/checkbox.svg);
So what is correct approach to handle svg in my situation? Thank you.
Ok, I found solution:
{
test: /\.svg$/,
loader: 'file?name=/resources/[path][name].[ext]'
},
{
test: /\.png$/,
loader: 'file?name=/resources/[path][name].[ext]'
}
This way loader preserves name and path and prepends additional /resources/folder.

Bootstrap is overriding custom CSS in Ruby on Rails 4 website?

I used http://www.gotealeaf.com/blog/integrating-rails-and-bootstrap-part-1 to install Bootstrap on my Rails 4 website.
Basically, I installed the gems:
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
And then in my application.css.scss I have:
...* defined in the other CSS/SCSS files in this directory. It is
generally better to create a new
* file per style scope.
*
*= require_tree .
*= require_self
*/
#import "bootstrap-sprockets";
#import "bootstrap";
/*some css code that does work.*/
It's working perfectly, aside from one snag. In my css file for my
/*
Filters
*/
#filters{
position: relative;
}
.searchbox{
width:100px;
}
.filter{
display: inline-block;
}
and then in my view, I have
<div id="filters">
<input type="text" class="form-control filter"/>
<input type="text" class="form-control filter"/>
</div>
However, the inputs are not displayed side by side. If I remove form-control in the inputs class, it does work. I checked in chrome, and basically the filter class's display:inline-block was being crossed out by the bootstrap form-control.
I'm not very good at css, but as far as I can tell, rails is putting bootstrap's css file after mine, which is causing bootstrap to be more "important".
I looked and looked, but I couldn't find a way to have my css loaded AFTER the bootstrap css (if I'm even right over the source of this confusion)
Hope you can help. Thanks in advance!
In css, rules that appear later in the same stylesheet or in a different stylesheet which loads later in the html will override similar ones already defined. For example
p { color: blue; }
p { color: red; }
will produce red text.
Your #import statements for bootstrap are referenced by the require_self line which appears after the require_tree . line. As a result, the bootstrap css will appear at the end of your compiled stylesheet and override any rules with the same selectors.
With sass, you are recommended not to use sprockets as you can't really control the source order but rather use #import for each of your sass partial files.
Reversing the two require lines might work well enough for you. Otherwise I would suggest you remove all the sprockets directives and comments above your #imports, move any code below into its own partial and explicitly #import each partial in the exact order you want.
Make sure your custom CSS in linked to from your HTML after the bootstrap links.
HTML head
link bootstrap
link custom CSS
First:
Follow Bootstrap application.css.scss order.
Second:
Run these command in console in order:
rake assets:clobber (Remove all compiled assets)
RAILS_ENV=production bin/rake assets:precompile
Third:
Restart server with production mode.

Is there any ready to use Bootstrap css file with prefix

I have web application which uses Foundation.
I am not good at foundation and i have to develop few pages where i want to use Bootstrap but i dont want to mix with other company css.
SO i was looking if i can wrap all bootsrap inside some class like bootstrap. so that if i want to use bootsrap . i can use like
<div class="bootstrap"> <table class="table">
</div>
I don't know sass and all that.
is it possible to download some bootstrap css from online with some pre defined prefix
If you don't use SASS or LESS, you can use http://www.css-prefix.com/
Make a short prefix (my recommendation)
If you use a space, then it will be a parent class.
Paste in the compiled version of the CSS file
Click the run button
Result snippet:
.tb.col-xs-1, .tb.col-sm-1, .tb.col-md-1, ...
Indeed you could use namespaces, see: How to namespace Twitter Bootstrap so styles don't conflict, http://lesscss.org/features/#features-overview-feature-namespaces-and-accessors and
In the case you need Bootstrap's CSS for tables only, you can compile the following Less / SASS code, after downloading the source code at http://getbootstrap.com/getting-started/#download:
**less / SASS **
.bootstrap {
#import "variables";
#import "mixins/table-row";
#import "tables";
}
As you see you can use the same code for Less and SASS, notice that the order of the imports does matter when compiling the SASS version.
update
The accepted solution only prefix classes (or selector having a class). In the case that you want to use Bootstrap's CSS to style your HTML tables. Your prefixed don't have bootstrap's styles for the table, th and caption selectors.
Even when you have never used Less / CSS before you can do the prefixing with Less (or SASS) easily leveraging an online compiler. A list of online Less compilers can be found at: http://lesscss.org/usage/#online-less-compilers. Also codepen has an online LESS and SASS compiler.
The only thing you have to know is what files to import. Bootstrap's Less files are well organized. You should always import variables.less and mixins.less. The mixins.less imports all other mixins. Mixins do not output, so importing all of them will slow down the compilation, but do not appear in the compiled CSS code.
In the case you want a prefixed version of the table CSS you can run the following code in one of the online compilers:
.bootstrap {
#import url("https://raw.githubusercontent.com/twbs/bootstrap/master/less/variables.less");
#import url("https://raw.githubusercontent.com/twbs/bootstrap/master/less/mixins.less");
#import url("https://raw.githubusercontent.com/twbs/bootstrap/master/less/tables.less");
}
An demo can be found at: http://codepen.io/bassjobsen/pen/PwPNBP
It sounds like you want to namespace Bootstrap, which is pretty easy to do using SASS (which Foundation uses, I believe). So in your SASS file (should have a .scss extension) you can import Bootstrap within a class name like this:
.bootstrap {
#import 'bootstrap';
}
And then you can reference Bootstrap in your HTML like this:
<body class="bootstrap">
<div class="col-xs-12 col-sm-6">
<table class="table"></table>
</div>
</body>
You can download SASS version of Bootstrap here: https://github.com/twbs/bootstrap-sass. Drop that SCSS file into the same directory as your main SCSS file and then you can import.
Imo the answers to this question are problematic. If you introduce a generic class to namespace everything, such as this...
.bootstrap {
#import 'bootstrap';
}
You are effectively increasing the CSS specificity.
What you really need is a tool that migrates Bootstrap to change the class names themselves.
For example,
E.g.
<div class="alert">
<div class="something alert">
<div class="something alert alert-danger">
Should become
<div class="bs-alert">
<div class="something bs-alert">
<div class="something bs-alert bs-alert-danger">
It should leave "something" alone because this class does not occur in Bootstrap.
To my knowledge such a tool does not exist (open source).
#Wolfr's answer has described the problem pretty well - in 2019 http://www.css-prefix.com/ doesn't work anymore, and all the other solutions that are in google top10 only increase css specificity by wrapping all bootstrap classes with the parent class.
That approach isn't bullet-proof:
e.g. if you make
.custom_namespace .col-3 {
width: 25%;
}
this won't protect you from someone using !important to bootstrap class for whatever reason some would do it.
.col-3 {
width: 99% !important;
}
Instead, there is actually one work I found through npm, where developer has indeed implemented that custom prefix AND even made it work with bootstrap's JS file. But it's only for Bootstrap 3: https://www.npmjs.com/package/bootstrap-sass-namespace
This will generate all bootstrap classes in form of e.g. .custom_prefix_col-3 {}
One more option if you need truly namespaced Bootstrap 4 css:
In my case I needed Bootstrap 4, so I opened bootstrap css file, ran search and replace in Sublime Text with Regular Expression, using this value for search:
\.(?<TAG>[a-z]{1,3}) - this captures all the beginnings of the classes by using period and first one to three letter characters as opposed to just searching for period (.) and risking to mess up float values for CSS properties.
and for replace I used .the_prefix_I_need\1
This allowed me to produce truly isolated bootstrap 4 css file that for sure won't be messed up if someone somewhere at the websites where my app is included will decide to redefine some bootstrap classes with !important.