Let's say you are builing a reuseable component. The style structure like this:

div > input

If you want to style this input field, it can be quite easy, we can just use :host selector:

:host input {
outline: none;
border: none;
}

But one day you figure out that hard cord input into the component might not be a good idea. You want to use content projection 'ng-content' to do that. Now the html stucture boecomes:

div > ng-content

Even you know the ng-content will be the input field. But he will find that your styling no longer works.

This is because Angular style encapsulation. It tries to preserve the style of projection element (because the element is not part of component, it has its own styling, we don't want to break that).

So the way to solve this problem is using CSS '/deep/' selector.

:host /deep/ input {
outline: none;
border: none;
}

'/deep/' break angular style encapsulation. You can think it is a global css styling which can overwrite everything ..:P Sounds pretty prowerful, and a little bit damage, yes! it is.

For example, if you doning like this :

/deep/ input {
outline: none;
border: none;
}

You will find that, all the input fields in your app has been affected!! So to be safe, use with :host selector.

[Angular] How to styling ng-content的更多相关文章

  1. angular报错:angular.min.js:118Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq

    报错代码如下: <div ng-controller="HelloAngular"> <p>{{greeting.text}},angular</p& ...

  2. Angular CLI 启动 版本ng 4

    npm install -g angular-cli ng -v ng new project_name cd project_name ng serve 浏览器打开输入 localhost:4200

  3. angular 2 - 001 ng cli的安装和使用

    angular cli 创建项目和组件 ng new my-app --skip-install cd my-app cnpm install ng serve localhost:4200 angu ...

  4. Angular源代码学习笔记-原创

    时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...

  5. 利用angular打造管理系统页面

    1 创建一个新的angular应用 ng new adminSystem 2 利用WebStorm打开adminSystem应用 3 借助AdminLTE这个开源项目来辅助开发 AdminLTE项目: ...

  6. 使用路由延迟加载 Angular 模块

    使用路由延迟加载 Angular 模块 Angular 非常模块化,模块化的一个非常有用的特性就是模块作为延迟加载点.延迟加载意味着可以在后台加载一个模块和其包含的所有组件等资源.这样 Angular ...

  7. Angular07 利用angular打造管理系统页面

    1 创建一个新的angular应用 ng new adminSystem 2 利用WebStorm打开adminSystem应用 3 借助AdminLTE这个开源项目来辅助开发 AdminLTE项目: ...

  8. angular常见问题总结

    本文引自:https://www.cnblogs.com/zhoulujun/p/8881414.html 这篇是对angularJS的一些疑点回顾,是对目前angularJS开发的各种常见问题的整理 ...

  9. Ngnice-国内ng学习网站

    今天给angular新手介绍一个国内开源的ng学习网站http://www.ngnice.com/这是由一批ng爱好者在雪狼大叔的带领下共同开发完成,致力于帮助更多的ng新人,他们分别是: ckken ...

随机推荐

  1. 联合体union用在何处?

    程序设计刚開始学习的人在学习时,总想问:"这个东东有什么用?"于是,在建设有关的教学资源时,也便总从这个角度,试图给出一些案例,这是一个将刚開始学习的人作为教学目标人群的人该干的事 ...

  2. 使用注解的方式配置Servlet

    提到Servlet的配置,大多数人想到的应该都是在web.xml中配置吧.有没有更简洁的方式呢?今天就学到了採用注解的方式配置Servlet. 此方式尽管简便.但当然也存在问题. 採用注解的有点:你能 ...

  3. js34

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. onvif开发之设备发现功能的实现--转

    忙了一个多月,onvif总算告一段落了.这几个星期忙着其他的项目,也没有好好整理一下onvif的东西.接下来得好好整理一下自己的项目思路和项目经验,同时将自己的一些心得写出来,希望对人有所帮助. 相信 ...

  5. jQuery自定义插件规范

    <ul class="list"> <li>导航列表 <ul class="nav"> <li>导航列表1< ...

  6. JS和安卓 IOS的交互 例子式记录

    (function () { var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexO ...

  7. oracle 日期

    http://blog.itpub.net/126211/viewspace-712986/

  8. D3.js中对array的使用

    由于D3类库和array密切相关,我们有必要讨论一下D3中的数据绑定以及在数组内部运算的方法. 1.D3中的数组 和其他编程语言一样,D3的数组元素可以是数字或者字符等类型,例如: someData= ...

  9. 6.在单机上实现ZooKeeper伪机群/伪集群部署

    转自:https://blog.csdn.net/poechant/article/details/6633923

  10. 18. springboot整合jsp

    转自:https://blog.csdn.net/u012562943/article/details/51836729