Tips on how to apply styling to child components that are nested within parent components in a

My parent component HTML code is below:

<div class="col-md-6">
          <div class="categories">
            <div class="title">All Categories</div>
            <nested-cat [data]="data" [key]="key" (action)="onClicked($event)" class="cat-container"></nested-cat>
          </div>
      </div>
    </div>

This is the HTML code for my child component:

<ul class="categorys" *ngIf="items.length">
  <li class="cat"  *ngFor="let item of items">
    <div class="content">
      <span class="name">{{item.name}}</span>
      <span class="action">
          <button md-icon-button><md-icon color="primary" (click)="hello('hello')">edit</md-icon></button>
          <button md-icon-button><md-icon color="warn">delete</md-icon></button>
      </span>
    </div>
    <nested-cat *ngIf="item[key].length" [key]="key" [data]="item[key]" (action)="onClicked($event)"></nested-cat>
  </li>
</ul>

I came across this and this links which talk about using :host(selector) and :host-context(selector) to target parent and child components. However, I'm struggling to implement this in my scenario.

In order to style the parent, I used the following CSS:

:host ::ng-deep nested-cat{
    width: 100%;
    padding:0;
}

It works well, but when attempting to target the first ul element, the style is applied to all ul elements.

:host(nested-cat) ::ng-deep ul:first-of-type{
    padding:0; // This will affect all ul 
}

How can I specifically target the first child (ul) of the first nested-cat and set its padding to 0?

Update:

You can view a plunkr example here.

Here's the working solution:

:host ::ng-deep .categories>nested-cat>ul {
      padding: 0 4px;
}

https://i.sstatic.net/u7WEV.png

Answer №1

Here is the solution to your problem:

:host ::ng-deep .content{
  width: 100%;
  margin-left:-40px
}

DEMO

Answer №2

For this particular scenario, the following CSS selector should work effectively:

.bg > nested-cat > ul {
    padding-left: 0;
}

The > symbol represents the immediate child selector in CSS. This means that it targets elements that are directly nested within the specified parent element, without selecting any descendants located deeper in the hierarchy.

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

Input field, upon receiving focus, triggers a subtle shift in the position of the submit button

Thank you for the assistance, I believe this issue should be an easy fix. I have added a custom :focus style to my input that removes the default outline and adds a box shadow and border. However, when the input field is focused, the submit button located ...

Unable to view post in a single page

Having trouble with my Wordpress blog on mobile. I can't seem to open posts in a single page, as there is no response when clicking or touching the post on the main page. The div class for the main page post is entry-content. Here are some styling co ...

Guide to creating a Twitter Bootstrap (Bootstrap 4 beta) menu dropdown that activates on hover instead of click

Looking for a way to create a Twitter Bootstrap (Bootstrap 4 beta) menu dropdown that activates on hover instead of click? Note: I specifically need an example of how to hover over a submenu dropdown. Here is the code I am currently using in my Angular pr ...

Using RMarkdown to incorporate custom CSS styling in your documents

Having some trouble with my HTML report created from RMarkdown and applying CSS. The browser version displays correctly, but when printed, the styling doesn't come through. Below is an example of the RMarkdown code: --- title: "Table" output: html_ ...

Adjusting layout to second row based on screen width using CSS media query

My div contains the following elements: <div> <a class="postLink" href="{{post.authorLink}}" target="_blank">{{post.author}}</a> <span class="">Published: {{ post.published}}</span> <a class="postLink" href="{ ...

Downloading videos from WebRTC getDisplayMedia in Angular 8 is not supported

Currently utilizing the NPM package in conjunction with Angular 8 found here: [ https://www.npmjs.com/package/webrtc-adapter ] to mimic the WebRTC getDisplayMedia functionality showcased here: [ ] I have successfully managed to initiate and terminate a r ...

Discovering a particular string within a jQuery array object: Tips and tricks

One thing that is confusing me is the jQuery array object. To explain further: I have two arrays, one called brandsLink and the other called floorLink. When a user clicks on a link, I am saving the brand name in a variable called brandName, and then checki ...

Angular chat integration

In my application, I have a parent component called "chat" with two child components - "sidebar" (which displays the user list) and "conversation detail" (which shows the chat with each user). The functionality I am aiming for is that when a user is clicke ...

Responsive design for iPads and smartphones is essential in ensuring a seamless user

Currently in the process of creating my own personal website, I have been diligently using Chrome Canary to ensure that all my media queries are properly set up. While everything looks great on Canary and functions well in various device modes within the b ...

Is there a way to divide v-progress linear into 4 pieces in Vuejs, or are there alternative design options for achieving this in Vuetify 2?

I have set up a table in Vuetify 2 with a v-progress-linear component to monitor the user's remaining time. Initially, my implementation was simple like this. https://i.sstatic.net/x373G.png However, we decided to split it into 4 sections for better ...

Encountering issues with the dependency tree while trying to install npm packages

I'm encountering an exception while attempting to install npm packages using the npm i command. The error message is displayed in this link: https://i.sstatic.net/x8N3W.png Despite trying to reinstall Node.js and turning off the proxy with these com ...

Bridging the space between two divs with Bootstrap 4

<div class="row"> <div class="input-group col-md-6"> <div class="form-group "> <asp:Label runat="server" ID="Label1" ClientIDMode="Static" Style="margin-left:15px" Text="GL Code" Font-Size="20px" Class="text-bold lblcaption ...

The service.subscribe function in Angular's Component Constructor is not functioning properly after the update

There are two components in my project, a parent and child component, and I am using a shared service to transfer data between them. The structure of the Service Class is as follows: export class AddItemDataTransferService { // Observable string sourc ...

What is the functionality of the keydown event?

Whenever the input text value is not empty, I want my .removetext div to be displayed, but it's not working correctly. When I type something after the second character, my .removetext gets hidden, but it shouldn't be hidden when the input is not ...

The ::before pseudo element is malfunctioning when used in the makeStyles function

I am currently utilizing the makeStyles function in React, but I seem to be facing an issue where the content within the ::before pseudo-element is not displaying. Strangely enough, when the content is an image it works fine, but text does not render. Jus ...

Having trouble accessing the application on localhost

I'm diving into the world of Docker! I'm looking to build a personalized docker image for an Angular application using a Dockerfile. I've successfully created the image and got the container up and running, but unfortunately, I'm unable ...

Navigating using Javascript library in Angular 2 framework

I am currently utilizing Parse, an external JS library, within Angular JS 2. Nevertheless, I am encountering issues when attempting to call the function gotoMain() from within a callback function of Parse. It appears that certain elements are not being l ...

Devices are not displaying media queries as expected

@media screen (max-width: 750px) or (screen and (max-width: 750px) (-webkit-min-device-pixel-ratio: 2),(min-resolution: 192dpi)) { div.body, div.body div.links, div.body div.links div, div.footer, div.footer div { display: block ...

What are the steps to generate a production build directory in a React project?

Currently, I am developing a website utilizing react for the frontend and node.js for the backend. However, I'm unsure of how to deploy it to the live server. After conducting some research, I learned that I need to create a build folder. To provide a ...

using node-sass with several different starting points

I am currently working on a project where my main entry point is structure.scss, and each platform has its own second entry point. Here is the folder structure: scss/ | |-- components/ # Modular Component Files | |-- login.scss ...