Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background image only for a particular component in angular 6

I have created new component called login,I have to set background image for the component(login.component.html),only for that component.I have tried many solutions provided in stack overflow.But the image is not setting to full height of the body. Below are my project files.

index.html

   <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <base href="/">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" type="image/x-icon" href="favicon.ico">
     </head>
     <body>
      <app-root></app-root>
     </body>
   </html>

app.component.html

    <router-outlet></router-outlet>

app.module.ts

   import { BrowserModule } from '@angular/platform-browser';
   import { NgModule } from '@angular/core';
   import { AppComponent } from './app.component';
   import { LoginComponent } from './login/login.component';

   @Component({
     AppComponent,
     LoginComponent
   })
   imports: [
     BrowserModule,
     RouterModule.forRoot([
       { path: '', pathMatch: 'full', redirectTo: 'login' },
       { path: 'login', component: LoginComponent },

           ])

          ],
        providers: [],
        bootstrap: [AppComponent]
             })

       export class AppModule { }

login.component.html

    <div class="inventory-body">
        <!--content goes here-->
    </div>

login.component.css

    .inventory-body {
    margin: 20px 0px;
    color: white;
    padding: 20px;
    height:auto;
    background-image: url('/assets/img/1.jpg');
     }

Here is the login.component.ts file

    import { Component, OnInit } from '@angular/core';

    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
     })
   export class LoginComponent implements OnInit {

    constructor() { }

    ngOnInit() {

      }

     }
like image 802
Shankar Avatar asked Dec 10 '22 05:12

Shankar


2 Answers

in global style(style.css)

    html, body, app-root {
     height: 100%;
     margin: 0;
     }

In (login.component.html)

    <body  class="inventory-body">

    </body>

Next write css(login.component.css) to the selector present in (login.component.html)

    .inventory-body {
     position: fixed;
     min-width: 100%;
     background-image: url("/assets/img/img-1.jpg");
     background-repeat: no-repeat;
     background-size: 100%;
     background-position: center;
     background-size: cover;
     }
like image 133
Shankar Avatar answered Jan 05 '23 19:01

Shankar


.inventory-body {
 position: fixed; 
  top: 0; 
  left: 0;
  min-width: 100%;
  min-height: 100%;
  background-image: url('')
}
like image 34
Arunkumar Avatar answered Jan 05 '23 18:01

Arunkumar