CSS relative url to images - css

I am developing on my localhost - http://localhost/mysite, and within that I have the following directory structure:
-assets
--css
---styles.css
--images
---background.png
Within the styles.css file, I would have something the following, which works fine on localhost
background-image: url(../images/background.png);
However, the live server works off a subdomain - http://test.liveserver.com. This means that the css rule has to change as follows:
background-image: url(/assets/images/loginlogo.png);
Not sure on the best way to get around this?

With the directory tree you are providing you don't have to change your path.
.. means the parent directory of the CSS file so the assets/ directory is implied.

Related

I can't access public folder from css file of my app

I'm trying to access the logo image which is in public folder while my css folder is in src. I'm also using sass and I took into consideration that we need to write the path relatively to css file - not sass.
I tried different ways - with absolute path, with quotes and without - just hoped that maybe something will work.
I found someone's code https://codesandbox.io/s/musing-rosalind-21lsd?file=/src/App.js and played with it - I created same conditions as I have and it worked, but when I go back to my project it doesn't.
I'm using background-image: url('/logo.swg'), and it says Error: Can't resolve '/logo.swg'.
I'm aware of ejecting and webpack configuration changes, also I know that if I change the css and sass folders' path moving it outside of src it may theoretically work (with an absolute path I guess - since React does not allow us to refer to files outside of src directory).
I'm wondering if something changed in React, does anyone know? It works when I write the url as inline style, and it works when I import it in js files.
Thanks in advance.
versions:
react^17.0.1
react-scripts^4.0.1
create-react-app^4.0.2
You can use the following list as quick reference:
/ = Root directory
. = This location
.. = Up a directory
./ = Current directory
../ = Parent of current directory
../../ = Two directories backwards

Can't find Wordpress Footer image url

I've been teaching myself Wordpress theme development and have a custom made theme on my website(made by another dev a while ago). I want to be able to download the footer background image but I can't seem to find the correct url for it. In the Style.css file the footer background image is declared as
background: url(images/layout/bg-footer-boards.jpg)
But mysite.com/images/layout/bg-footer-boards.jpg, mysite.com/wp-content/images/layout/bg-footer-boards.jpg, and mysite.com/themes/mytheme/wp-content/images/layout/bg-footer-boards.jpg all give me 404's. I think this is just something with the file path that I'm missing but any help would be much appreciated!
Css url() is relative to the file rather than the webroot.
So images/layout/bg-footer-boards.jpg
refers to a folder called images/ located in the same folder as your .css file.

Laravel 5 where to place and how to access image inside css

In Laravel 5, in what directory should I store images (for backgrounds) and how to access it inside a CSS file. My CSS file is on public/css directory. I've tried to place the image in public/images and public/assets/images but didn't work and even I couldn't access the image directly from web browser.
In CSS you can access images, when they are in public/images, like
../images/imgename.jpg
In the blade template, you can use something like
"{{ asset('images/imagename.jpg') }}"
public is fine. You should access them in your css file via, e.g., /images/xy.jpg or /assets/images/xy.jpg. The / at the beginning makes sense to refer to the root directory.
If this does not work, please provide paths of images, css and an example css file that does not work with the images.
All files inside of public/ are accessible via web browser, just put the images or css there. Example:
public/images/foo.png
public/css/style.css
Normally in this structure is only needed to do background: url('../images/foo.png').
However, some people has problems using absolute path like background: url('/images/foo.png'), why? because the laravel installation is made under htdocs/ or public_html/ having the following structure:
/home/user/public_html/laravel/public/
If the above structure is used by you, you need background: url('/laravel/images/foo.png')
You can just use : url(http://localhost:8000/imgs/image_name.extension) as follow my exemple : http://localhost:8000/img/spacer.png but pay atention do it in the dev enviroment in deploy change localhost:8000 for your domain name
For me the answer was to use a tilde (~) instead of dots:
So the full line in the CSS became:
background: url('~/storage/images/DownArrow.png') no-repeat right #fff;
The storage folder being in the public folder.
With Laravel 5.7 I had been running 'npm run dev' and getting:
Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './storage/images/DownArrow.png' in 'C:\xampp\htdocs\websitename\resources\sass'
Thanks to 'mlachance' in another forum for the tip. He also says 'This is not well documented in the Vue CLI chapter on static assets.'

File path for css background images

I am working on a local project at the minute, I am using some background CSS images the file route is
/Volumes/Macintosh HD/Users/William/Desktop/websites/webtest/images/plane.png
if i change the route to /webtest/images/plane.png will the person receiving see the images if I copy this whole project into a zipped up file with that route?
Sorry this is a NOOB question.
It also depends how your website is structured. Assuming that your project looks like this, the paths of your images should be relative to the folder:
website
webtest
images
plane.png
css
style.css
Your paths in style.css should look like:
.class {
background: url('../images/plane.png') 0 0 no-repeat;
}
No, the recieving person will probably have problems, I think.
It'll be better to use ./images/plane.png or only images/plane.png if the page is something like /Volumes/Macintosh HD/Users/William/Desktop/websites/webtest/index.php
because / will access the root of the current IP-address or domain. It also depends on the css files location if you're using one.
Hope it helps ;)

CSS root directory

I have a style sheet where I include background images.
background: url(../Images/myImage.png);
problem is, pages from different directories use this css!
My CSS files are in a CSS folder, images in an Image folder, and my html pages are in many different folders depending on their content and meaning to the website.
All my pages inherit this css as it is the MAIN theme.
The path used in the above example is a relative path. And obviously, this path only works for some of the pages. ALL i need is to link the images in the css from the ROOT folder. Therefore every path is correct no matter where the file is in the folder structure!
I have tried:
~/Images/myImage.png
./Images/myImage.png
/Images/myImage.png
Images/myImages.png
I don't think a root folder selector exists... but I hope it does :/
/Images/myImage.png
this has to be in root of your domain/subdomain
http://website.to/Images/myImage.png
and it will work
However, I think it would work like this, too
images
yourimage.png
styles
style.css
style.css:
body{
background: url(../images/yourimage.png);
}
click here for good explaination!
All you need to know about relative file paths:
Starting with "/" returns to the root directory and starts there
Starting with "../" moves one directory backward and starts there
Starting with "../../" moves two directories backward and starts there (and so on...)
To move forward, just start with the first subdirectory and keep moving forward
I use a relative path solution,
./../../../../../images/img.png
every ../ will take you one folder up towards the root. Hope this helps..
For example your directory is like this:
Desktop >
ProjectFolder >
index.html
css >
style.css
images >
img.png
You are at your style.css and you want to use img.png as a background-image, use this:
url("../images/img.png")
Works for me!
This problem that the "../" means step up (parent folder) link "../images/img.png" will not work because when you are using ajax like data passing to the web site from the server.
What you have to do is point the image location to root with "./" then the second folder (in this case the second folder is "images")
url("./images/img.png")
if you have folders like this
then you use url("./content/images/img.png"), remember your image will not visible in the editor window but when it passed to the browser using ajax it will display.
In the CSS all you have to do is put url(logical path to the image file)

Resources