简体   繁体   English

CSS-从样式块扩展样式表样式

[英]CSS - extend stylesheet styles from a style block

I need to implement dynamic css styles for certain elements based on a user's role or group identity. 我需要根据用户的角色或组标识为某些元素实现动态CSS样式。

My idea is to fetch the required dynamic values (hex colors, background image urls) from my database, write them to the page, then use jQuery to add styles with these values to the elements that are to be dynamic. 我的想法是从数据库中获取所需的动态值(十六进制颜色,背景图像url),将其写入页面,然后使用jQuery将具有这些值的样式添加到要动态的元素中。 Seems straightforward. 似乎很简单。 Has anyone done it this way? 有人这样做吗?

Another way is to somehow override external style sheet styles with styles defined in a header style block. 另一种方法是用标题样式块中定义的样式以某种方式覆盖外部样式表样式。 Would this work? 这行得通吗? Can you share css styles between a style block and an external sheet? 可以在样式块和外部图纸之间共享CSS样式吗? Can the shared styles cascade? 共享样式可以级联吗?

You can mix internal and external styles. 您可以混合使用内部和外部样式。 Internal, external, and inline styles all cascade, with inline styles taking precedence over internal styles, which themselves take precedence over external styles. 内部,外部和内联样式都是级联的,内联样式优先于内部样式,而内部样式本身优先于外部样式。

If you want to dynamically change styles based on user permissions, why not add the relevant classes and id in the body tag, eg 如果要基于用户权限动态更改样式,为什么不在body标签中添加相关的类和ID,例如

<body id="admin" class="group-1">

And then use CSS to separate the roles and groups out, eg 然后使用CSS分离角色和分组,例如

#admin{
    background-color: rgb(255,155,105);
}

.group-1{
    font-family: sans-serif;
}

.group-2{
    font-family: Roboto;
}

You could go one step further and use a CSS pre-processor like LESS to style several groups for individual roles, eg 您可以更进一步,并使用LESS之类的CSS预处理器为各个角色设置多个组的样式,例如

#admin {
    background-color: rgb(255, 155, 105);
    .group-1 {
        font-family: sans-serif;
    }
    .group-2 {
        font-family: Roboto;
    }
}

暂无
暂无

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

相关问题 将外部CSS样式表转换为内联样式,或转换为<style> tag, via Javascript - Convert external CSS stylesheet into inline styles, or into a <style> tag, via Javascript 从样式组中选择CSS样式 - Select css style from group of styles 拒绝应用来自 'http://localhost:3000/assets/styles/signup.css' 的样式,因为它的 MIME 类型('text/html')不是受支持的样式表 MIME 类型 - Refused to apply style from 'http://localhost:3000/assets/styles/signup.css' because its MIME type('text/html')is not a supported stylesheet MIME type 在页面内样式标签中加载CSS,但不能从外部样式表加载CSS - CSS loading within in-page style tags, but not from external stylesheet 删除内联 CSS 会影响样式表中定义的 styles - Removing inline CSS affects styles defined in stylesheet 如何从 styles.CSS、ZEF0F93C83E37416F36A61DA0D4 访问 JavaScript 元素样式 - How to access JavaScript element style from styles.CSS, Django 如何从element.style中删除多个CSS样式 - how to remove multiple css styles from an element.style 与 css styles 反应的传播样式 - spread style in react with css styles 是否可以使用 JavaScript 更改 CSS 样式表? (不是 object 的样式,而是样式表本身) - Is it possible to alter a CSS stylesheet using JavaScript? (NOT the style of an object, but the stylesheet itself) 获取元素的所有DEFINED(在stylesheet / inline中)css样式 - Get ALL DEFINED( in stylesheet/inline ) css styles of an element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM