The CSS was modified once the router-outlet was added

As I was working on creating a login page within Angular, I initially included the code in the app.component.html file like this:

app.component.ts

<div class="container">
   <app-login></app-login>
</div>

The corresponding CSS looked like this:

login.css

.form-group {
  display: block;
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin: 0 0 5px 0;
}

app.component.css

.container {
  display: grid;
  height: 100%;
}

app-login {
  width: 100%;
  max-width: 274px;
  place-self: center;
}

However, after implementing routing in my application, I switched to using the <router-outlet> component to display the login page, which caused the CSS styling to no longer appear as intended...

app.component.ts

<div class="container">
   <router-outlet></router-outlet>
</div>

Do check out the following stackblitz for a live example.

Update I have managed to resolve the missing CSS issue (see below).

However, it seems that something is causing the content to be pushed downward. How can I adjust the CSS to restore the original layout?

Answer №1

Make sure to add the following line:

encapsulation: ViewEncapsulation.None

In order for styles to affect components nested within the AppComponent, it is necessary to specify this setting on the AppComponent.

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  encapsulation: ViewEncapsulation.None
})
export class AppComponent  {
   name = 'Angular';
}

It is recommended to apply styles directly to the components they were designed for.

You can see a live example in this Stackblitz demo.

Answer №2

Your styling for <app-login> in the app.component.css file, setting a fixed width and center alignment, may not work as expected due to Angular's emulated view encapsulation. This means that when using routers, the name of <app-login> could change dynamically at runtime, rendering your CSS classes ineffective.

It's recommended to write component-specific styles in their respective CSS files rather than in the root component's CSS file.

Check out the updated example on Stackblitz

See the result: https://i.sstatic.net/g9TkP.jpg

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing a server-side PHP environment alongside a dynamic JQuery slider

I recently completed a PHP course on Codecademy and wanted to dive into a new project to further enhance my skills. However, I've hit a roadblock while working on it - specifically having trouble with the AJAX functionality. I'm struggling to ide ...

Changing div height based on the height of another dynamic div

Looking to create a box that matches the height of the entire page, live height minus another dynamic div called #wrapping. I have code that offsets it instead of removing the height of the div... function adjustBoxHeight() { var viewPortHeight = $(wind ...

Setting the Formgroup status to Invalid will only occur if there are errors in multiple fields, not just one

I'm having an issue with my form where setting passportrank as error does not change the formgroup status to Invalid. Additionally, when I navigate to the next section, the inline error message of "You are not eligible" also gets cleared. ngOnInit() ...

Cease and Begin Page Overflow / Scroll with a Single Click Feature

Background: I recently followed a tutorial on how to create a lightbox using HTML, CSS, and JavaScript. However, I encountered an issue where I wanted to disable the scrollbar functionality when the lightbox is displayed, preventing users from scrolling ...

Display a popup div while communicating with an AJAX server

As I work on my website's interaction with my AJAX server, I am looking to display a central popup on the screen. Since some tasks take a few seconds to complete, I want to visually indicate to users that an operation is in progress. For example, cons ...

I'm puzzled as to why my JavaScript code only functions correctly when I place it after the HTML, despite having a window.onload event handler in place

Just beginning my journey in a CS class and seeking guidance for my lack of basic knowledge. I've noticed that this JS code only works if placed after the HTML, not within the head tag. Shouldn't window.onload handle that? Can someone clarify wha ...

Extract the chosen document from an input within the Electron application form

Need help with this form <form> <input type="file" name="idp" id="idp" onchange="uploadFiles();"/> </form> Once a user selects an image, I want to move it to a specific folder and save its full name in a variable for database storage. ...

Generate a single-page PDF document mirroring the content of a website

I'm facing a dilemma. I need to generate a one-page PDF file without any gaps between the pages. Despite trying numerous plugins, add-ons, scripts, and software, all of them produce multiple pages. What I really require is to have all the pages in a s ...

How exactly does the indexOf() method function when used with an array as the search value?

For some reason, when I input single letter words like "s" or "z" into the array it functions properly. However, in this particular case, it's not working and I can't figure out why. It seems to be related to the condition, as when I just use "do ...

Merge HTML/CSS with JavaFX's FXML and customize the style using CSS

Quick Summary: I have a cool button on CodePen that rotates and grows when hovered over using HTML/CSS. I want to incorporate this button into my JavaFX GUI, built with SceneBuilder and FXML files. The GUI currently features buttons numbered 1-4, and I wo ...

Unable to collapse the Bootstrap dropdown menu

For the life of me, I can't seem to find a solution to my problem. I'm currently working on creating a responsive navbar for my website by following a tutorial. The goal is to make it mobile-friendly with a button that expands the menu on smaller ...

When attempting to retrieve the first element of an array using TypeScript, the browser unfortunately returned an error stating that the property was undefined

After creating a method called getFooterContent to retrieve data and display it using the method, I encountered the following error: platform-browser.umd.js:1900 ERROR: TypeError: Cannot read property 'content' of undefined getFooterConten ...

Experience a login page similar to Google's with a fully functional back button

I'm currently working on a login page that requires the user to enter their username and have it validated before proceeding. Once the username is confirmed as valid, a new input field for password entry should appear. However, I'm facing an issu ...

Using TypeScript, apply an event to every element within an array of elements through iteration

I have written the code snippet below, however I am encountering an issue where every element alerts the index of the last iteration. For instance, if there are 24 items in the elements array, each element will alert "Changed row 23" on change. I underst ...

ALERT: Circular dependency has been detected

test2\test2.services.ts import { test1Component } from '../test1/test1.component'; constructor(public tsoComp: test1Component ) { } betState test1.component.ts import { test2Service } from '../test2/test2.services'; constructor( ...

Get an angular xml file by utilizing the response from a C# web API download

I am trying to download an XML file from a database using a Web API in C#, which returns the file as byte[]. How can I properly read these bytes and convert them into an XML file on the client side using Angular? Despite attempts with blobs and other metho ...

Issue with Bootstrap's pull-right class causing elements to overlap

I need help positioning a single button above a list, with the button to the right. Whenever I try using the pull-right class on the button (or its containing div), the button ends up being covered by the list. <div> <div class="pull-right"& ...

Setting up a checkbox to accept either a 0 or 1 value from a database

I am trying to integrate an HTML table where each table data cell contains a drop-down menu and a checkbox. These elements are connected to a database table named "checkbox" which contains values of 1s and 0s. I want to dynamically display the check box as ...

Ensure that the input button for uploading is only accessible when checked and is marked as

Most of my previous questions have been about input boxes and SQL, as I am still in the learning process. Any help is greatly appreciated. My current question revolves around displaying a button to upload an image using PHP (although for this example, I w ...

Content overflowing the container - Designed to adapt to different screen sizes

I'm currently working on a project within the Bootstrap 3 framework and struggling with an issue regarding text overflow in extra small view. The text is not behaving as expected, as it should stick to its container and break accordingly. I've ex ...