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>
2
  • 2
    Just a word of warning: this will probably not give you any boost in terms of performance/compression, but maybe that's not why you do it ... Commented Jun 14, 2019 at 12:52
  • 1
    @oligofren I need it for a little tool I am building Commented Jun 14, 2019 at 13:48

3 Answers 3

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>'
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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.

1 Comment

I just need to do this for a little tool I am building

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.