Rails Haml element styling - css

I have a custom style in my css for a background image I want to apply:
.specialbg {
background-image: url('http://i.imgur.com/XMuKF.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I want to apply this style to my body element in HAML, but I'm not sure how the arguments are setup. I'm thinking that the following should work:
%html
%head
%body
.specialbg
But it doesn't. I've read the haml styling reference manual, but can't see where I'm wrong -- I've just adopted haml, and I'd appreciate any help or pointers!
Stack Q's I've referenced:
scaml "illegal nesting" error
Background images do not render on Ruby on Rails application

To add the class to your body element, you would just append the class to the tag like this:
%html
%head
%body.specialbg
It could be a typo in your example, but the way your example is set up, you are putting your body tag within the head tag
If you put the .specialbg class on its own line, it will create a div inside of your body tag with class='specialbg'

Related

How to set background url for css files in thymeleaf?

I have a thymeleaf template where I don't have CSS files imported and wanted to declare style attribute for the body element with background-image: url{/image.jpg} property with relative image URL. I wanted to load the URL without including the application context name(/myapp/) it. It is similar to the problem over here, except it din't work for me.
CSS:
<style>
body {
background: url(../img/Background.jpg)
no-repeat center center fixed;
background-size: cover;
}
</style>
But the above CSS doesn't work and it accessed the image at
http://localhost/img/Background.jpg.
So, I had to give the value as url(/myapp/img/Background.jpg) for the image to load properly.
I have the mvc:resources configuration properly set in spring-context.xml for /img/ request to load properly and it works in other places.
spring-context.xml
<mvc:resources mapping="img/**" location="/WEB-INF/img/" />
So how to load the background url image css value dynamically using thymeleaf's relative url?
So, here's how to set dynamic relative paths in background image url property in the css using thymeleaf's inline text value,
<style th:inline="text">
body{
background: url{[[#{/img/Background.jpg}]]}
no-repeat center center fixed;
}
</style>
which loads the image using relative path and we don't have to specific the 'myapp' context name in the url.
In my case that helped:
change brackets from curly to round
<style th:inline="text">
body{
background: url([[#{/img/Background.jpg}]])
no-repeat center center fixed;
}
</style>
An alternative is:
<body th:style="'background: url(/img/la-paz-city.jpg) no-repeat center center fixed;'">
...
</body>
that is all πŸ˜‰
<body th:style="'background-image:url(' + #{/images/background.jpg} + '); background-repeat: no-repeat, repeat; background-size: cover;'">
</body>

Using a photo as my website's background

I have a random picture of a moose lying around so I wanted to use it as my ruby on rails website's background. I fumble online for a bit and here is what I got.
html {
background-image: url(images/moose.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
The above does not work, my background is still white. What am I doing wrong?
The picture of a moose is located in app/assets/images/moose.jpg
I've tried using html body { } as oppose to html and background as oppose to background-image
<html>
<body style="background-image: url(output.png);">
Hello this is a text line.</body>
</html>
I assume your image to be in same directory of your html page. Otherwise give the relative path to image like C:/images/output.png
You can't use html, you have to use body. It should be:
body {
background:url('images/moose.jpg') no-repeat center center fixed;
}
Open chrome inspector and look at what background-image styles are loading.
url function requires a string, you need to use single quotes to pass in the string file you are trying to load.
background-image:url('images/moose.jpg') no-repeat center center fixed;
Here is what the def of URL says on w3schools. http://www.w3schools.com/cssref/pr_background-image.asp

Edit background on Wordpress CSS style sheet

I'd like to edit some background properties on my Wordpress site.
Using Chrome's Inspect Element I can see that currently I have:
body.custom-background {
background-color: #f6f6f6;
background-image: url('http://patchwood.ca/wp-content/uploads/2013/06/bktopright.png');
background-repeat: repeat;
background-position: top left;
background-attachment: fixed;
I would like to edit background-repeat to no-repeat and background-position to right.
Sounds simple but when I look in the Editor in Wordpress the selector does not exist. It turns out that this styling appears to be within the html on line 53 of the home page.
If it was a handcoded website I would just update the stylesheet but since it's a database driven Wordpress site it's more difficult to know where to edit.
I wonder if there is a means of adding styling to the body element background that overrides any other properties? So basically, if I was to add to the very bottom of the stylesheet the following code to override anything else.
body.custom-background {
background-repeat: no-repeat;
background-position: top right;
}
I did try just adding that but with no success. Any ideas?
its showing up in the header - by the looks of it - its probably a custom background image set in the wordpress backend.
a rather round about way of fixing this can be to add
<style>
body.custom-background {
background-repeat: no-repeat !important;
background-position: top right !important;
}
</style>
to your header or footer
It seems like it's hard-coded, as you suggest. It's located within the <head> tag, which means it's probably part of header.php. Instead of editing style.css, why not look in header.php and change it there (or better yet, delete the reference in <head> and move .custom-background's style information to style.css)?
Edit According to the Codex, custom backgrounds are enabled using the following line in functions.php:
add_theme_support( 'custom-background' );
Removing this line (and then setting your desired background properties in style.css) should do the trick.
i have to manage background in inspect element css this is success css
code:
<style>
body.custom-background {
background-attachment: fixed;
background-color: #F6F6F6;
background-image: url("http://patchwood.ca/wp-content/uploads/2013/06/bktopright.png");
background-position: right center;
background-repeat: repeat-y;
}
</style>

Understanding a CSS Background property attribute

I came across this piece of CSS which a certain website has used for implementing full screen background image :-
html {
background: url(../images/bgrnd.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
color:#ffffff;
}
I looked at all the attributes and went through all the background properties on w3schools.com and understood everything but one. The center paramenter is repeated twice and I don't understand why, nor to which attribute it belongs to. Is it for background-position property ??? But then, only one center would do I guess....umm....so ? what am I missing ?
X and Y coordinates. First "center" is related to "X" and second to "Y". And, yes, it is about background-position
One center for horizontal align and the other for vertical align (in that order)

Stretching Background Image in IE 8 and Below

I want to load an image via css that stretches to the entire screen. The css:
body {
background: url(images/reelgoodguide2.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
overflow: auto;
margin: 0px;
padding: 0px;
}
Which works perfectly in Chrome and Firefox and IE9.
There is a conditional in the html to include an additional css file if the browser is IE8 or IE7. This css contains:
body{
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/reelgoodguide2.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/reelgoodguide2.jpg', sizingMethod='scale')";
}
But instead of alphaimageloader stretching the image to the entire screen, the image remains centered and it and does not resize.
Note: when I open developer tools I can see that the css file is there. When I disable the filter property, nothing happens. Any tips to what Im doing wrong?
Thanks in advance.
OKAY... After a grueling bit of fiddling, this is what i figured out:
First:
 The filter path to an image is relative to the html document, not the css file. FRUSTRATING
Second:
  I got this to work by applying the filter to html instead of body.
I had read that the IE workaround were not to be applied to html but what I noticed was that there was (after I had cleared up the image path problem) a stretched bacground image but it was behind everything else.
I think it's not possible with CSS alone. Search for supersized. You will find some JavaScript libs.
One more useful answer to solve IE8 background issue is:
Download backgroundsize.min.htc and put it inside your project.
Now simply add these lines in your css:
.class_name{
//your other properties
background-size: cover;
-ms-behavior: url(backgroundsize.min.htc);
}
NOTE: use the url according to your project setup.
Enjoy this simple solution. :)

Resources