3

I have an Angular 2 app. In this app i have few components. I want to add a background image only to login component. How do i do that?

body{
    background-image: url('https://s.zkcdn.net/Advertisers/d36ea4926dfa4978ab2556ba7688692c.png') ;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    height:100%;

}

I have this style for the login component. but it's not covering the entire windows. it's covering only the form section.

1

5 Answers 5

15

I faced the same problem.But below code works good for me.

Add a following css code in global css file (style.css).

.bg-img
{
    background-image: url('/assets/images/back.jpg');
    background-position: center;
    background-repeat: no-repeat; 
}

and add the below code in ngOnInit section in required component.ts file.(ex:login.component.ts)

ngOnInit() {
    document.body.classList.add('bg-img');
}
Sign up to request clarification or add additional context in comments.

6 Comments

This works, but somehow whenever I change the component, the background gets stuck until i refresh the page. Is there any solution to this?
You can remove the class on ngOnDestroy lifecyle-hook then it will not stuck whenever you change your component
@franpen or you can in component1: document.body.classList.remove('bg-img-login'); document.body.classList.add('bg-img-register'); and in component2: document.body.classList.remove('bg-img-register'); document.body.classList.add('bg-img-login'); And ofc, in parent component you have to remove both of them.
Mmm, but then you will have to remove it in every other component, which is not very practical.
@Mr.Wizard that’s the issue, I haven’t found a practical solution to this. Nevertheless, I’ll test your idea when I get home
|
3

I am using angular 7. This is how I achieved full background image in a component. in login.component.html

<div class="bg-img">

 </div>

then in the login.comonent.css

.bg-img{
 min-height: 100vh;
width: 100%;
background: url('../../../src/assets/loginimage.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

Comments

0

In the @Component decorator of your login component, you can add a styleUrls field. Doing so will allow you to use a style sheet for your login component only.

@Component({
  selector: 'your-selector'
  templateUrl: ['path/to/your/html/component'],
  styleUrls: ['path/to/your/stylesheet'],
})

You can then, in your style sheet, change your background only for the login component

3 Comments

body{ background-image: url('s.zkcdn.net/Advertisers/…) ; background-position: center; background-repeat: no-repeat; background-size: cover; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; height:100%; }
I have this style for the login component. but it's not covering the entire windows. it's covering only the form section.
You could try to add a div encapsulating your body and set the background of this div. Not sure why you have this result, check how your components are one in another
0

I am using Ionic 3. But Ionic uses Angular.

In my login.html page I have this:

<ion-content class="background">
 ...
</ion-content>

I think you can try with a:

<body class="background">
</body>

And in my login.scss page I have this:

page-login {
    .background {
        // Path of my picture
        background-image: url('../assets/background.gif');
    }
}

Best regards,

Comments

0

// in div father

.bg-component {
    background-image: url('../../../assets/images/backgroundweb/[email protected]') !important;
    background-repeat: no-repeat;
    background-size: 100%;
    position: fixed;
    width: 100%;
    height: 100%;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.