What causes the disparity in functionality between simple html and css in an Angular 2 project compared to a vanilla html website?

When incorporating the following html/css into a new Angular project created with Angular CLI using 'ng new ProjectName', I encountered issues. Despite adding the CSS to app.component.css or styles.css and the HTML to app.component.html, the Angular version seemed to be disregarding the specified height on the body. The header failed to stay fixed while scrolling in both the sidenav and content sections.

UPDATE: I discovered that removing the app-root tag from the Angular version resolved the problem. It's puzzling why the presence of app-root is causing this discrepancy.

Functional HTML/CSS:

body {
  height: 100vh;
  margin: 0;
  display: flex;
  flex-direction: column;
}
#row2 {
  display: flex;
  flex: 1 0 0;
}
.header {
  height: 64px;
    background-color: red;
}
.sidenav {
  flex: 0 0 250px;
    background-color: yellow;
}
.main-content {
  flex: 1 0 0;
  background-color: blue;
}
.sidenav, .main-content {
  overflow-y: scroll;
}
<div id="row1">
    <div class="header"></div>
</div>
<div id="row2">
    <div class="sidenav">        Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>
    </div>
    <div class="main-content">
        Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<br/>Testing<b...

Angular 2 Output: (It should be noted that in the css files, I added the css similar to the above snippet. Angular adds ngcontent tags to it and the additional flex styles are included via Chrome developer tools)

body {
  height: 100vh;
  margin: 0;
  display: flex;
  flex-direction: column;
}
#row2[_ngcontent-c0] {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-flex: 1;
      -ms-flex: 1 0 0px;
          flex: 1 0 0;
}
.header[_ngcontent-c0] {
  height: 64px;
}
.sidenav[_ngcontent-c0] {
  -webkit-box-flex: 0;
      -ms-flex: 0 0 250px;
          flex: 0 0 250px;
}
.main-content[_ngcontent-c0] {
  -webkit-box-flex: 1;
      -ms-flex: 1 0 0px;
          flex: 1 0 0;
}
.sidenav[_ngcontent-c0], .main-content[_ngcontent-c0] {
  overflow-y: scroll;
}
<html><head>
  <meta charset="utf-8">
  <title>FlexTest</title>
  <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 _nghost-c0="" ng-version="4.0.3">
  <div _ngcontent-c0="" id="row1">
  <div _ngcontent-c0="" class="header">HEADER</div>
</div>
<div _ngcontent-c0="" id="row2">
  <div _ngcontent-c0="" class="sidenav">
    Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngcontent-c0="">Testing<br _ngconten...

 

Answer №1

It's not possible to apply styles to the <body> element using CSS within components that have the default view encapsulation set to ViewEncapsulation.emulated. To style elements outside of your Angular application, you should add the CSS to the index.html file.

If needed, you can work around this limitation by turning off view encapsulation:

@Component({
  encapsulation: ViewEncapsulation.none,

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

Is there a way for me to retrieve information from a different website using a URL, similar to how Digg's submit button works

Currently, I am in the process of developing a website with the cakePHP framework. As a beginner in PHP and web programming, my goal is to implement a feature similar to Digg's submit button. This functionality would involve inputting a URL, which the ...

What are the new features for listening to events in Vue 3?

Currently, I am manually triggering an event: const emit = defineEmits<{ (e: 'update:modelValue', value: string | number): void }>() // [..] <input type="text" :value="modelValue" @input="emit(&apos ...

Is it possible to run my NPM CLI package on CMD without needing to install it globally beforehand?

I've created a new NPM package, complete with its own set of CLI commands. Let's call this package xyz, and let's imagine it's now live on npmjs.com Now, picture a user who installs this package in their project by executing npm insta ...

Substitute a single occurrence of the dollar sign with an HTML tag

I have been attempting to insert an HTML tag into a price on my e-commerce website. Specifically, I am looking to add a tag between the "$" and the numerical value (e.g. $5.00), but I am unable to locate the core file where I can place the code between the ...

Is the Kendo Spreadsheet supported in Angular 2?

We are interested in knowing if future releases of Angular2 will include Kendo Spreadsheet or the ability to export data to Excel for the grid. We currently use the Angular1 version and would like to upgrade, so having these features available in Angular2 ...

Delay the execution using Javascript/jQuery, remove the last element from the DOM

Within a jQuery AJAX call, I am using a $.each() loop. Each element in the response data triggers the addition of a portion of HTML to a container class in my index.html file. To view the code and ensure proper formatting, visit the codepen here as the pa ...

Is there a way to display the overall count of items in ReCharts?

I'm curious about how to access the additional data items within the 'payload' field of recharts when using material-ui. Despite my efforts to find relevant sources, I have not come across any references pertaining to accessing other group n ...

Troublesome bug in Angular 7: [ngModel] functionality fails to cooperate with mat-select

Having trouble implementing ngModel in the following scenario: Check out the template code below: <mat-select [ngModel]="data.dataObject[0].phase"> <mat-option *ngFor="let phase of possiblePhases" [value]=" ...

Deletion of a custom function in JavaScript

I have written some basic code to generate and remove an image using functions. Specifically, I need help with removing the image created by the function Generate() when a button linked to the function Reset1() is clicked. Here's the code snippet for ...

Creating an interface that accurately infers the correct type based on the context

I have an example below of what I aim to achieve. My goal is to start with an empty list of DbTransactInput and then add objects to the array. I experimented with mapped types to ensure that the "Items" in the "Put" property infer the correct data type, w ...

What is the reason that preventDefault fails but return false succeeds in stopping the default behavior

I'm having trouble with my preventDefault code not working as expected. While using return false seems to work fine, I've heard that it's not the best practice. Any ideas why this might be happening? if ($('.signup').length == 0) ...

Stop submission of form using react by implementing e.preventDefault();

Having trouble figuring this out.... Which event should I use to bind a function call for e.preventDefault(); when someone clicks enter in the input tag? Currently experiencing an unwanted refresh. I just want to trigger another function when the enter k ...

Changing text inside ion-header-bar directive

When utilizing the ion-header-bar directive, I have the left side designated as class="button", the middle section containing <h1> with the word "Recent", and the right side as <ng-icon>. The text on the left side is dynamically generated usin ...

Creating an interactive element in Angular: A step-by-step guide

Hey there, I'm facing a problem with creating a specific payload structure as shown in the example code snippet below. Everything was going well until I got stuck at the dynamic form part, and despite several attempts, I can't seem to figure it o ...

Identify the significance within an array and employ the filter function to conceal the selected elements

I'm in the process of filtering a list of results. To do this, I have set up a ul list to display the results and checkboxes for selecting filter options. Each item in the ul list has associated data attributes. When a checkbox with value="4711" is c ...

The images in Bootstrap 5 are displayed individually on separate lines, even though they could easily be accommodated within a

I'm facing an issue with displaying several 50x50 images in a row of 6 columns on my website. Despite the simple code setup, each image appears on its own row, creating the illusion of stacked images rather than a single row. This is the code structu ...

The solution to enabling Type checking in this scenario is simple: Begin by addressing the issue of "Not assignable," followed by resolving any

After subscribing to an observable projected by a BehaviorSubject from a service, I encountered the following errors when trying to assign the subscribed value to a local variable: error TS2322: Type '{}' is not assignable to type 'DatosAdmi ...

The onclick event is malfunctioning in Chrome when dealing with data driven by SQL

<select id='city' name='city' > <?php $dbcon = mysql_connect($host, $username, $password); mysql_select_db($db_name,$dbcon) or die( "Unable to select database"); $city_query = "SELECT city,county FROM citycatalog order by city ...

Rearranging div placement based on the width of the screen

I am currently working on building a responsive website and I need two divs to switch positions depending on the screen width, both on initial load and when resizing. Despite my efforts in researching and trying various options, I have not been successful ...

What are the steps to import a .obj 3D model into Three.js?

After incorporating your advice, here is the revised code: var renderer = new THREE.WebGLRenderer( { alpha: true } ); renderer.setSize( window.innerWidth, window.innerHeight ); element.appendChild( renderer.domElement ); var loader = new THREE.OBJLoader( ...