How to set the background image with Angular? - html

<div class="class1">
<app-home></app-home>
<router-outlet></router-outlet>
</div><div class="class1">
<app-home></app-home>
<router-outlet></router-outlet>
</div> <!-- This is the home.component.heml -->
.class1{
background-image: url(/Users/md.nuhelnawazchowdhury/Desktop/Shoppie/andalib/src/assets/img/Shoppie1.jpeg);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
I can't get the home page to show the background image. The files above are app.component files.

First start by putting your images inside assets folder.
Assets folder is automatically created by Angular CLI, when you create a new project.
Now it depends on where you wanna use the background image.
If you are in app folder you can do somthing like this:
background-image: url(../assets/Shoppie1.jpeg);
if you are deeper, say you created a component in its own folder, you will have to do something like:
background-image: url(../../assets/Shoppie1.jpeg);
I created this github demo if you wanna see how it is done: link

Yout path should be /assets/img/Shoppie1.jpeg
background-image: url("/assets/img/Shoppie1.jpeg")
Open Developer tools > Network tab and check if the image is getting downloaded while the app is loaded.

Try using a url in single quotation for a url as shown below!
<div class="class1">
<app-home></app-home>
<router-outlet></router-outlet>
</div><div class="class1">
<app-home></app-home>
<router-outlet></router-outlet>
</div> <!-- This is the home.component.html -->
.class1{
background-image: url('/Users/md.nuhelnawazchowdhury/Desktop/Shoppie/andalib/src/assets/img/Shoppie1.jpeg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
But for the best practice of developing an angular app I would recommend to create a separate folder for images inside an asset folder that would be the best way to track and make an application clean.

Related

How do i call an image from a folder using css

I am trying to call an image from a folder using css.
I have tested the css with background color, so i know it is linked and working properly.
what I've tried:
I have tried to call an image from outside the image folder.
I have tried to change the image type to png.
And i have tried a different image.
html
<section id="hero">
<div class="hero container">
<h1>testing</h1>
</div>
</section>
css
#hero{
background-image: url(img/Computer_repair(1).jpg);
}
Your code works (see fiddle) so I'm assuming you just have the image path wrong.
So when you say...
#hero{
background-image: url(img/Computer_repair(1).jpg);
}
...that means where ever your css file is saved, there also needs to be a folder called img and inside that folder is where you need to put Computer_repair(1).jpg
Your code looks fine. I suspect there's a problem with the image path. Try this:
background-image: url('/img/Computer_repair(1).jpg');
The problem was it was not working with the (1).
I Needed to change from Computer_repair(1).jpg to Computer_repair_1.jpg
HTML
<section id="hero">
<div class="hero container">
<h1>testing</h1>
</div>
</section>
Css
#hero{
background-image: url(img/Computer_repair_1.jpg);
}

why background-image property is not working?

I've index.html file and img folder residing in the same directory,
I want to have the background image in div with .img1.
so I'm doing something like this :
Index.html
.img1 {
background-image: url("img/bg-header.jpg");
position: relative;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
<div class="img1">
<div class="ptext">
<div class="dark-overlay">
<div class="home-inner text-center">
<h2 class="display-4 text-center">PULSES</h2>
<br>
<p> HALUTZIM 26 TEL AVIV<br><br>+4410600</p>
<br>
<br>
<div class="arrow">
<i class="fas fa-chevron-down"></i>
</div>
</div>
</div>
</div>
</div>
this should set the background image of the div, but it is not working ?
Note:
As I mentioned index.html style.css and img folder are residing in the same directory, I've images inside img folder.
Am I doing something wrong ?
Please help thank You
Try to render the image as an in-line image in your page using tag just to confirm that the path is ok. If the path is ok, check the div's width and height then play around with background-postion property.
If still not working, post your html head.
I would try background-image: url("../img/bg-header.jpg");
Complementing my answer with Wayne's good explanation in the comments below:
"the relative path from a CSS file is based on its location. Thus the same CSS file can be used for HTML files in different locations. The css file is not in the same directory as the html. Hence ../ or /img/bg-header.jpg if the /img/ is off the root www directory."

how to put background image path from remote web site?

I need grab images from Imgur to My web page background image
<div style="background-image:url(); width:100%; height:100%;" class="zoom" >
My image path is https://imgur.com/a/NbUpX
then how can I put this image to my background image url?
You need to copy the right image path: https://i.imgur.com/QdMWFHZ.jpg
Go to https://imgur.com/a/NbUpX -> right click on image -> Copy image address
After this you need to put it into background-image:url('https://i.imgur.com/QdMWFHZ.jpg');
Here you can find some documentation for background-image using.
Remember to set an height for your div, because in your solution will be 100% of nothing so this can be a solution:
<div style="background-image:url('https://i.imgur.com/QdMWFHZ.jpg'); width:100%; height:200px; background-size: contain; background-repeat: no-repeat;" class="zoom" >
Update image url like this
<div style="background-image:url('https://i.imgur.com/QdMWFHZ.jpg'); background-repeat:no-repeat; width:100%; height:100%;" class="zoom" >
Open your image path, after that right click on the image and click "Copy image address".
And then paste address on your code.
<div style="background-image:url(https://i.imgur.com/QdMWFHZ.jpg); width:100%; height:100%;" class="zoom" >
or in your CSS directly as background-image: url("https://i.imgur.com/QdMWFHZ.jpg")
since you need background of webpage it would be part of the body attribute like (CSS):
body {
background-image: url("https://i.imgur.com/QdMWFHZ.jpg")
}
You can test it here

Display single SPECIFIC Instagram photo - possible?

Emphasis on specific photo here.
I'm aware of all the Instagram plugins out there, and the Instagram API, however I'm trying to figure out a way to display a single specific photo from Instagram. Not by tag, or by username, but preferably from the Instagram URL, or even with a photo ID.
I want something similar to this: (where the Instagram photo covers the background of the div)
<div class="instagram-photo"
style="background-image:URL(*instagram_photo_url_here*);
background-size:cover">
</div>
I'm happy to link to the photo for photo credit and all that.
Is this possible? I've searched for hours and nothing comes up, I've got to be overlooking something.
Took me some time to come up with an answer, Instagram provides an easy to get the media directly using Media redirect, by adding /media/?size={size} to end of any photo permalink
Desired size of the resulting media. Supported values are t
(thumbnail), m (medium), l (large). Defaults to m.
Examples;
https://www.instagram.com/p/8l2vIrhQQ_/media/?size=l
https://www.instagram.com/p/7TDiYJBQdf/media/?size=m
https://www.instagram.com/p/cn5KZWmJp1/media/?size=t
And if you're lasy or want bulk changing at once, I wrote a jQuery snippet for this; Demo
Here's your final HTML for your attempt
body {
padding:10px;
}
.instagram-photo {
display:block;
margin-bottom:15px;
background:#ffffff;
height:500px;
border:2px solid #ddd;
}
<div class="instagram-photo" style="background-image: url(https://instagram.com/p/-FP1PjBQRd/media/?size=l); background-size: cover;"></div>
<div class="instagram-photo" style="background-image: url(https://instagram.com/p/8l2vIrhQQ_/media/?size=l); background-size: cover;"></div>
<div class="instagram-photo" style="background-image: url(https://instagram.com/p/-XNiICnxXO/media/?size=l); background-size: cover;"></div>
<div class="instagram-photo" style="background-image: url(https://instagram.com/p/cn5KZWmJp1/media/?size=l); background-size: cover;"></div>
<div class="instagram-photo" style="background-image: url(https://instagram.com/p/7TDiYJBQdf/media/?size=l); background-size: cover;"></div>

Ionic background img on div not loading LOCAL images but loads images from internet urls

I have this div which is loading perfectly image url from the internet but not my local image, very weird:
<div class="col biggest-grid dir-medico-grid" ng-click="showDirMedico()">
<img src="../img/dirmedico.png" class="img"/>
<p>DIRECTORIO MÉDICO</p>
</div>
So when I try this it works with no problem:
.dir-medico-grid{
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/652/confectionary.png);
}
but if I try this:
.dir-medico-grid{
background: url(../www/img/doctors.jpg);
}
Now it doesn't work as you can se my path is correct i'm going from ionic.app.scss to doctors.jpg:
I have tried with background: url(../www/img/doctors.jpg) no-repeat center center fixed;, with '../www/img/doctors.jpg' and "../www/img/doctors.jpg" with background-imageinstead of background but nothing seems to work this is very weird.
Use only img/dirmedico.png in html
<img src="img/dirmedico.png" class="img"/>
And for css
.dir-medico-grid{
background: url("../img/doctors.jpg");
}
The thing is for html, all templates are rendered on index.html which is in same directory with img.