2

Is there a module or gulp option which can minify html and css into a single line?

For example

<style media='screen' type='text/css'>
    .text {
        padding: 1px 1px 1px 1px;
        font-size: 12px;
    }
</style>

<!-- now for html -->

<div style='width:550px;' >
    <div style='float:left;font-size:1.2em;' class='text'>
        Title goes here
    </div>
    <div style='width:60px;float:left;' class='text'>
        <span style='font-size:0.8em;'>
            ®
        </span>
    </div>
    <div style='float:left' class='text'>
        Some paragraph text
    </div>
    <div style='float:left;padding-top:10px;' class='text'>
        <span style='font-style:italic;'>
            A footer to the paragraph
        </span>
    </div>
</div>

Can the above me minified onto a single line using node.js so it looks like the below.

<style media='screen' type='text/css'>.text {padding: 1px 1px 1px 1px;font-size:12px;}</style><!-- now for html --><div style='width:550px;' >  <div style='float:left;font-size:1.2em;' class='text'>MY BRILLIANCE</div><div style='width:60px;float:left;' class='text'>      <span style='font-size:0.8em;'>®</span> </div>  <div style='float:left' class='text'>       With release comes growth, through challenge comes wisdom, let us show you the way. </div>  <div style='float:left;padding-top:10px;' class='text'><span style='font-style:italic;'>Absolute Equal Acceptance through Thought, Conscience and Reunion.</span></div></div>
user6248190
  • 1,233
  • 2
  • 16
  • 31

3 Answers3

1

There is a very popular package called html-minifier.

A minimal example from the site:

var minify = require('html-minifier').minify;
var result = minify('<p title="blah" id="moo">foo</p>', {
  removeAttributeQuotes: true
});
result; // '<p title=blah id=moo>foo</p>'
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35
1
var minify = require("html-minifier").minify;

var html = `<style media='screen' type='text/css'>
.text {
    padding: 1px 1px 1px 1px;
    font-size: 12px;
}
</style>

<!-- now for html -->

<div style='width:550px;' >
<div style='float:left;font-size:1.2em;' class='text'>
    Title goes here
</div>
<div style='width:60px;float:left;' class='text'>
    <span style='font-size:0.8em;'>
        ®
    </span>
</div>
<div style='float:left' class='text'>
    Some paragraph text
</div>
<div style='float:left;padding-top:10px;' class='text'>
    <span style='font-style:italic;'>
        A footer to the paragraph
    </span>
</div>
</div>`;

var result = minify(html, {
  collapseWhitespace: true,
  minifyCSS: true,
  quoteCharacter: "'"
});

console.log(result);

Output

<style media='screen' type='text/css'>.text{padding:1px 1px 1px 1px;font-size:12px}</style><!-- now for html --><div style='width:550px'><div style='float:left;font-size:1.2em' class='text'>Title goes here</div><div style='width:60px;float:left' class='text'><span style='font-size:.8em'>®</span></div><div style='float:left' class='text'>Some paragraph text</div><div style='float:left;padding-top:10px' class='text'><span style='font-style:italic'>A footer to the paragraph</span></div></div>
10101010
  • 1,781
  • 1
  • 19
  • 25
0

Try to keep HTML files and CSS files separately. And learn SASS. After learning SASS you would be like, "Why I am using CSS". With SASS you will be able to minify CSS. Go to https://sass-lang.com/install and download "Prepros". There is also a free version available, just download and install it.

Ritambhar Das
  • 77
  • 1
  • 5