How to set inline style for element in HAML

2019-03-14 10:06发布

问题:

Here is my code:

<div class='some' style='position: absolute; left: 300; top: 300;'>..</div>

It parses only style='position: absolute', and doesn't parse the other styles. How can I achieve this?

回答1:

It would have been handy if you'd posted the HAML you're using, but this is how it's done:

%div.some{ :style => "position: absolute; left: 300px; top: 300px;" }


回答2:

No need to use %div:

.some{ style: 'position: absolute; left: 300px; top: 300px;' }


回答3:

Another approach in addition to the hash one by Dan Cheail is such:

%div.some(style='position: absolute; left: 300; top: 300;')


回答4:

If you are looking inline css for image :

<%= image_tag( 'image_name.png', style: 'height: 25px; width: 200px; position: absolute' ) %>


回答5:

Requested a hash special case at: https://github.com/haml/haml/issues/787 to allow us to write:

%div{ style: { display: "none", width: "50px" } }

much like is possible for class: ["class1", "class2"].