简体   繁体   English

动态内嵌响应式 CSS 样式

[英]Responsive CSS styles inline on the fly

I am currently working on a highly design orientated site based on wordpress CMS.我目前正在开发一个基于 wordpress CMS 的高度设计导向的网站。

Currently I have a responsive main stylesheet linked externally for the core css.目前,我有一个外部链接的响应式主样式表,用于核心 css。 As the site relies heavily on spacing and alignments of both text and images it has become necessary to add inline css using style= HTML to sometimes override the external CSS.由于该站点严重依赖文本和图像的间距和对齐方式,因此有必要使用style= HTML 添加内联 css 有时会覆盖外部 CSS。

The problem I have is that in most cases certain elements such as margins need to be a different percentage in the mobile view than the desktop view to balance the visual composition.我遇到的问题是,在大多数情况下,某些元素(例如边距)在移动视图中需要与桌面视图中的百分比不同,以平衡视觉组合。 Is there any way to add responsiveness to the inline CSS based on screen width as can be done in an external style sheet?有什么方法可以像在外部样式表中那样根据屏幕宽度向内联 CSS 添加responsiveness

So far the only way I can think of achieving this is through jQuery amending the external CSS based on the users screen width however this would mean setting up strict rules in the JS eg: for desktop view having margins set at 70% and for mobile setting them to 90%.到目前为止,我能想到的唯一方法是通过 jQuery 根据用户屏幕宽度修改外部 CSS,但这意味着在 JS 中设置严格的规则,例如:对于将边距设置为 70% 的桌面视图和移动设置他们达到 90%。

If it could be possible to do this inline using html style then this would give my client stricter control and more flexibility.如果可以使用 html 样式内联执行此操作,那么这将为我的客户提供更严格的控制和更大的灵活性。 Luckily my client is well versed in CSS.幸运的是,我的客户精通 CSS。

You could always add a block of css inline with a style element: 您总是可以添加一个内联CSS块的样式元素:

<style type="text/css">
@media screen and (min-width:800px) {
   .inlineOverride {
     /* add more styles here */
   }       
}    
</style>

<div class="inlineOverride">
</div>

It's worth mentioning that HTML5 has introduced a scoped attribute that you can set on the style element to limit the specified style information to the style element's parent element, and that element's descendants. 值得一提的是,HTML5引入了一个范围限定的属性,您可以在style元素上设置该属性,以将指定的样式信息限制为style元素的父元素以及该元素的后代。

It isn't widely supported yet, so shouldn't be relied on, but it could be useful in the long term to help prevent inline styles like this from "leaking" into other parts of the document. 它尚未得到广泛支持,因此不应该依赖它,但是从长远来看,它有助于防止这种内联样式“渗入”文档的其他部分。

This question/answer might be helpful for you(read it thoroughly) use @media for adjusting your properties of css according to device width-height. 这个问题/答案可能对您有所帮助(请仔细阅读),请使用@media根据设备的width-height来调整css的属性。 What does @media screen and (max-width: 1024px) mean in CSS? @media屏幕和(最大宽度:1024像素)在CSS中是什么意思?

In modern Browsers you can (kind of) archive this by using custom css properties (css variables):在现代浏览器中,您可以(有点)使用自定义 css 属性(css 变量)将其存档:

 @media (max-width: 1100px) { * { color: var(--color-sm) } }
 <a style="--color-sm: #11f">Text</a>

(Expand Snippet or open in full page to get the other behavior) (展开 Snippet 或全页打开以获取其他行为)

(Color is used for simplicity of presentation, just use margin or any other css property according to your use case.) (颜色用于简化演示,只需根据您的用例使用边距或任何其他 css 属性。)

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

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