how to use the image in the <img> on Angular 5 - css

I have a local image in my folder. but I can't use this image. Folders structure:
I want to use logo.jpg in login.component.html. I am using this code:
<img src="img/logo.jpg">
But isn't work for me

TL;DR: Supposing you are using #angular/cli try to move your img folder under src\assets and then do <img src="assets/img/logo.jpg">.
Standard way is to put assets to assets folder under your app source root. The builder is told in angular.json to include assets by defining (for example):
projects.<your-project>.architect.build.options.assets: [
"src/assets",
"src/favicon.ico",
"src/manifest.json"
]
Whatever is defined there will be copied verbatim to your dist folder.
Note that assets must be under src root, somewhere. Therefore trying to add "src/../img" to config will fail. At least with my config and CLI.
Edit: Just noticed you mention Angular5. That is long time ago but IIRC the config file there was named .angular.json (see the dot). The structure of config is slightly different, just look for "assets".

you need to provider the entire relative path
i think it's:
src="../.././img/logo.jpg"
or maybe
src="../../../img/logo.jpg"

change the path to: '../../../img/logo.png'
or Move the imgs to under assets in src, src/assets
and then refer in your img src as:
img src="assets/img/logo.png">

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

How to keep relative paths in CSS for images and fonts in Laravel Mix

When Laravel Webpack/Mix executes and converts SASS to CSS, it changes the relative paths like url('../images/background.png'); to absolute paths like url('/images/background.png');.
Is it possible that the paths are not updated and kept as relative?
The reason is that the deployment could be in different folders on different servers so that the relative paths will work on all folder structures. E.g., it will work on http://www.example.com/ as well as http://www.example.com/subfolder/. But if an absolute path is used, then in the case of the subfolder, the CSS will not find the images and fonts.
Is there some other best practice to handle this situation?
From the docs:
By default, Laravel Mix and Webpack will find example.png, copy it to your public/images folder, and then rewrite the url() within your generated stylesheet. As such, your compiled CSS will be.
As useful as this feature may be, it's possible that your existing folder structure is already configured in a way you like. If this is the case, you may disable url() rewriting like so:
mix.sass('resources/assets/app/app.scss', 'public/css')
.options({
processCssUrls: false
});
https://laravel.com/docs/5.5/mix#working-with-stylesheets

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.'

bundling css works but fonts not loading

I have the following folders structure in my MVC site:
-- Content -- Notebook -- css
-- Content -- Notebook -- fonts
Content is directly under the root for the site. In my css folder i have a file which has a relative path
url('../fonts/fontawesome-webfont.woff?v=4.0.3')
My bundle currently looks like:
bundles.Add(new StyleBundle("~/bundles/Content/Notebook/css").Include(
"~/Content/Notebook/css/animate.css",
"~/Content/Notebook/css/font.css",
"~/Content/Notebook/css/font-awesome.min.css",
"~/Content/Notebook/css/app.css"
));
This is rendered using
#Styles.Render("~/bundles/Content/Notebook/css")
this works for the css files, but the font file isn't loading, i see that it is looking for it here http://localhost/MySite/bundles/Content/fonts/fontawesome-webfont.woff?v=4.0.3
I saw that and then tried to change my bundle name to
~/Content/Notebook/css
thinking that would get the relative path to work also, if i remove the "bundles" from the name,
but doing that causes the css files not to load. Why wouldn't the css files load? If I had the word "bundles" back to the name it works again. Also any idea on how to get the fonts to load along with the bundle?
When you do this:
url('../fonts/fontawesome-webfont.woff?v=4.0.3')
you are referring to the fonts via a relative path. If you then put your CSS bundle at /bundles/Content/Notebook/css it will look in bundles/content/fonts since that's combination of your relative path and where the browser see your css-file.
A few possible options (either):
Change your bundle path:
bundles.Add(new StyleBundle("~/Content/Notebook/css") ...
(the reason your css files didn't load when you removed bundles was that you didn't change the name of the stylebundle)
and
#Styles.Render("~/Content/Notebook/css")
Reference your fonts with an absolute path:
url('/Content/notebook/fonts/fontawesome-webfont.woff?v=4.0.3')

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