CSS background without using url - css

Is there a way in CSS to include a background without using url()
Ex, I can't do this:
#blah {
background:url("either image or image data");
}
Is there a workaround where I can achieve the same thing but not using the keyword "url" (The word "url" is prohibited.

You can use an <img> tag as your background image, no URL in the CSS is necessary. Something like this:
HTML
<img src="image.jpg" class="bg">
<div id="page-wrap">
</div>
CSS
img.bg {
min-height: 100%;
min-width: 1024px;
width: 100%;
height: auto;
position: fixed;
top: 0;
left: 0;
}
#media screen and (max-width: 1024px) {
img.bg {
left: 50%;
margin-left: -512px;
}
}
See here for more information and a demo: http://css-tricks.com/perfect-full-page-background-image/

No, you cannot. That is the way CSS works when including external content. To prohibit the keyword url on the web is a rather strange thing.

No need to make it so complicated !!!
Here is the explanation :
When using the url() value, you have three choices:
relative path based on the root domain of the CSS file — begin with a slash (/) as shown above
relative path based on the immediate directory of the CSS file — no slash (url(path/to/image.png) )
absolute URL, if you are linking to an external resource — url(http://example.com/path/to/image.png)
Source: https://html.com/attributes/body-background/
So basically, if you have your image in the folder image, next to to the foler styles (in which you css file might be), here is what you need to do :
background: url(../images/Chat.jpg)
../ means you go back one level above your current folder/workplace.
Here is what I used:
background: url(../images/Chat.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

Related

How do I add a background image to the home page only?

I'm finding it difficult to insert my background image only on the home page. I have 6 pages but I only want the background image to be on the home page. Annoyingly the background image is on all of my 6 pages. Any help would be appreciated , thank you.
header {
background-image: url(img/markus-spiske-PIJwPMtzBI0-unsplash.jpg);
background-size: cover;
background-position: center;
margin-top: 200px;
height: 100vh;
background-attachment: fixed;
}
That is because your header tag in the CSS file will always get called on the no matter the page it is.
To solve this, you need to give your header an extra "name" so to speak. In this case giving it an "id" is enough.
#homepage-header{
background-image: url('path/to/image')
}
//As you can see here, instead of calling the <header> im now calling the #homepage-header
<header id="homepage-header">
</header>
//I added a unique ID to <header> tag allowing me to target this header specificly on the homepage.
For sake of completeness, I will add this alternative solution.
If this is just for one or two exceptions, you could also inline the CSS code on the specific page where you want this image to display. Like this:
<div></div> <!-- this is the image div -->
<style>
header {
background-image: url(img/markus-spiske-PIJwPMtzBI0-unsplash.jpg);
background-size: cover;
background-position: center;
margin-top: 200px;
height: 100vh;
background-attachment: fixed;
}
</style>
This way, you can keep using the header selector for whatever you want, and only on that specific page, the header will use the above CSS code.

Cover entire div with image

for my joomla 3.x website i'm using a template based on the EF4 framework. I assigned width "5" to position top2-1 and "7" to position top2-2.
I wanted to create a custom html module for the homepage (http://francescaleso.com/it/) in position top2-1 to show a background image completely filling it.
I have tried as suggested on this site doing like this:
.homeimg {
overflow: hidden;
background-size: cover;
background-position: center;
background-image: url(/images/headers/home-image.jpg);
}
And the module only has
<div class="homeimg"></div>
The path of the image is correct, because if i try "open in new tab" it gets displayed correctly, however it won't show on my homepage.
Make these modifications in the CSS.
.homeimg {
background-size: cover;
background-position: center;
background-image: url(/images/headers/home-image.jpg);
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
----edit--- if i play a bit with absolute and relative positions, i get this:
the problem is that the image isn't the full height of the unscrolled space, and also has an unwanted padding or something on the left...

Using CSS Attribute Selectors to target the src of background-image

I am using using the background-image attribute to assign images to a range of div on my site. However, with background-image attributes, I also need to assign background-size to get it looking right.
This works fine mostly, but I need to change the background-size attribute based on the file type used in the background-image attribute. For example, as standard I want to user background-size: cover; but when the background-image is an SVG I want to use background-size: auto;
Is this possible using CSS attribute selectors? If not, any other solution?
My attempt (SCSS):
&.bg-image {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
min-height: 500px;
max-height: 700px;
&[src$=".svg"] { background-size: auto; }
}
If background-image is your only inline CSS property, you can do this:
.bg-image {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
min-height: 300px;
max-height: 700px;
}
.bg-image[style^="background-image:"][style$=".svg)"] {
background-size: 103px 94px;
}
<div class="bg-image" style="background-image: url(https://upload.wikimedia.org/wikipedia/en/8/80/Wikipedia-logo-v2.svg)"></div>
If background-imageis not the only property but it is the last one, you can use this selector: [style$=".svg)"].
Finally, the most general case, for any location of background-image in the style attribute use this selector: [style*=".svg)"].
Even with the loosest selector: [style*=".svg)"] (or jpg, or png...) the only declaration the selector can possibly apply is the background-image.
The other approach is to add data-type=svg or what have you to the divs and then target them in CSS [data-type=svg].
Or you could use img instead, as in your example.

Large image for drupal home using css injector

Based on this page :
- http://css-tricks.com/perfect-full-page-background-image/
I have used CSS injector to make my home page :
- http://tailored.bike/
I believe I had this code working properly
.front h1.title { display: none; }
body {background: none;}
html {
background: url(/sites/default/files/u44/framed_home_2013_10_19.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Then for some reasons I had to change the domain name and I can't have it work back.
Does any one have an idea ?
Regards
Julien
edits:
Does nok work neither with full http://... image url.
It works if I uncheck "Preprocess CSS"... I let it this way for now.
Just tested in Chrome and your background image reference is still mapped to your old domain. Your CSS should read:
html {
background:url(http://tailored.bike/sites/default/files/u44/framed_home_2013_10_19.jpg) no-repeat center center fixed;
/* Other CSS stuff */
}
Fix that reference and the image loads fine.

Windows 8 - Use image for background

I'm creating my first Windows 8 app and I have a problem with CSS!
I have a folder images and background texture bg.png inside it. Also, stylesheet is in css folder.
CSS:
#contenthost {
height: 100%;
width: 100%;
background: url("../images/bg.png") repeat;
}
But nothing happen! I tried background: #999 which works. What should I do?
I tried your example with path to the image relative to the app root and it worked without any issues:
#contenthost {
height: 100%;
width: 100%;
background-image: url('/images/logo.png');
background-repeat: repeat;
}
In terms of your code this would be:
#contenthost {
height: 100%;
width: 100%;
background: url('/images/bg.png') repeat;
}
You can also save the image in your project folder.
And load it in the html page like this:
<img id="mainImg2" src="ms-appdata:///Local//bckgrnd.jpg" />
If it is not the path (using absolute vs. relative as above would fix that) you also need to make sure you have an element with an id = contenthost.
CSS let's you do both id and classes so if you start getting into styling page controls you will see a heavy use of classes like (.mypage .subsection).
Finally, you can always set the background in any individual html file directly on the body tag like:
<body style="background-image: url('/images/bg.png');">

Resources