4

I'm using Play framework.

In a template it is possible to do the following to include css.

    <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">

But what I would like to do is to include the css right into the webpage

<style>@[include the static file contents]</style>

Is that possible?

biesior
  • 54,554
  • 10
  • 118
  • 177
Anton
  • 2,034
  • 20
  • 39
  • I'm just wondering... why would you want to inline your stylesheet file? – Roman Jun 28 '15 at 16:34
  • So there is no need for an extra request to get it. It's not a big deal, but why not? – Anton Jun 28 '15 at 17:25
  • 1
    I strongly recommend to **not** do this. You gain nothing from it. All modern browsers will cache your external stylesheet. It will be transferred only once. Also don't forget the rules of optimization: 1. Don't. 2. Don't yet (http://c2.com/cgi/wiki?RulesOfOptimization) – Roman Jun 28 '15 at 17:46
  • @Roman If you think of email clients they mostly will not load external stylesheets. So there is a use case. – Spenhouet Aug 17 '17 at 09:54

1 Answers1

5

As others mentioned it is NOT preferred way, anyway you can use common 'tags' technique for doing this,

just create a file views/tags/yourStyles.scala.html with content:

<style>
    * {
        background: orange;
    }
</style>

so in your template/view you can use it as (sample):

<head>
    <title>@title</title>
    <link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
    <link rel="shortcut icon" type="image/png" href="@routes.WebJarAssets.at("images/favicon.png")">
    @tags.yourStyles() <!-- <-here ->
</head>
biesior
  • 54,554
  • 10
  • 118
  • 177