简体   繁体   English

所有组件的角度全局CSS

[英]Angular global css to all components

I have a single html file and a single css file, which i want to convert to angular 2 way => separate some objects in the document to different components... So i created an angular app and components, and i don't want to change anything in my global css file. 我有一个HTML文件和一个CSS文件,我想将其转换为有角度的2种方式=>将文档中的某些对象分离为不同的组件...所以我创建了有角度的应用程序和组件,但我不想更改我的全局css文件中的任何内容。

I'm trying to apply a global css in my "index.html" to all my child components but it seems that each component uses the css relative to itself.. What i mean is for example if in my "all.css" file i have these lines: 我正在尝试将“ index.html”中的全局​​css应用于所有子组件,但似乎每个组件都使用相对于其自身的css。例如,我的意思是如果在我的“ all.css”文件中我有这些行:

.col-lg-3 {
   width: 25%;
}

In my child component the 25% width will be relative to the component width and not to my document's < body > width... Which will 'cause my component to be 25% of the body, but the inner objects of my component will be 25% of the 25%... 在我的子组件中,25%的宽度将相对于组件的宽度,而不是文档的<body>宽度...这将“导致我的组件占主体的25%,但是组件的内部对象将是25%中的25%...

What should i do? 我该怎么办?

Thanks 谢谢

EDIT: 编辑:

What i try to achieve is that each css element will be relative to the < body > tag of the document instead of the parent element... For example: I have 3 components: 我试图实现的是每个css元素都相对于文档的<body>标记而不是父元素...例如:我有3个组件:

App -> Parent -> Child 应用->父->子

I want the Child's component element width percent to be relative to the App component instead of the parent component... 我希望孩子的组件元素宽度百分比相对于App组件而不是父组件...

I don't completely understand what you are trying to do as there is no code provided. 我没有完全理解您要做什么,因为没有提供任何代码。 However there are many solutions based on what you are looking to achieve. 但是,有许多解决方案基于您要实现的目标。

(a) You can either move the div outside of the current parent or define a specific width for the inner div. (a)您可以将div移到当前父对象的外部,也可以为内部div定义特定的宽度。

(b) If you just need to always have the inner div's width based on the main content area, you can use the viewport width (b)如果只需要始终根据主要内容区域设置内部div的宽度,则可以使用视口宽度

.col-lg-3 {
   width: 25vw;
}

This will setup width to 25% of viewport width 这会将宽度设置为视口宽度的25%

(c) If you need to dynamically change width during runtime and there are specific elements that need to change, you can capture the viewport width after the Body loads and change the width of the inner div using (c)如果需要在运行时动态更改宽度,并且需要更改某些特定元素,则可以在Body加载后捕获视口宽度,并使用以下方法更改内部div的宽度

document.getElementById('elementsId').style.width = 0.25 * window.innerWidth + 'px' //you can use screen.width also

(d) Use less.js that allows you to dynamically change a CSS class during run time. (d)使用less.js,它允许您在运行时动态更改CSS类。 See http://lesscss.org/ for more details 有关更多详细信息,请参见http://lesscss.org/

In your less CSS add this 在较少的CSS中添加此

@myinnerWidth:25%
.col-lg-3 {
   width: @myinnerWidth
}

and then in your controller change the myinnerWidth dynamically 然后在您的控制器中动态更改myinnerWidth

less.modifyVars({
  '@myInnerWidth': 0.25 * screen.width + 'px'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM