Search code examples
cssemacsorg-mode

how to tell org-mode to embed my css file on HTML export?


What's the best way to instruct org-mode to embed all the css from my stylesheet into a single HTML file, rather than including a link to it as it does by default?


Solution

  • I faced this problem recently and none of the suggestions/answers worked for me. I finally found a solution in this link, which is to write your own function as follows and put it in you .emacs or init.el file.

    (defun my-org-inline-css-hook (exporter)
      "Insert custom inline css"
      (when (eq exporter 'html)
        (let* ((dir (ignore-errors (file-name-directory (buffer-file-name))))
               (path (concat dir "style.css"))
               (homestyle (or (null dir) (null (file-exists-p path))))
               (final (if homestyle "~/.emacs.d/org-style.css" path))) ;; <- set your own style file path
          (setq org-html-head-include-default-style nil)
          (setq org-html-head (concat
                               "<style type=\"text/css\">\n"
                               "<!--/*--><![CDATA[/*><!--*/\n"
                               (with-temp-buffer
                                 (insert-file-contents final)
                                 (buffer-string))
                               "/*]]>*/-->\n"
                               "</style>\n")))))
    
    (add-hook 'org-export-before-processing-hook 'my-org-inline-css-hook)