简体   繁体   English

从 html(.erb) 在 sass/css 中动态设置 div colors

[英]Dynamically set div colors in sass/css from html(.erb)

I'm having trouble finding the best way to do this.我很难找到最好的方法来做到这一点。 I want to store the primary and secondary colors of objects in my database, then I want to display a divider using the two colors dynamically.我想在我的数据库中存储对象的主要和次要 colors,然后我想动态显示使用两个 colors 的分隔线。

For instance...例如...

在此处输入图像描述

Notice the black parts of the two divs注意两个 div 的黑色部分

Most of the html/css solutions use css' :before & :after pseudo selectors to add the black part of the two divs.大多数 html/css 解决方案使用 css' :before & :after伪选择器来添加两个 div 的黑色部分。 (Checkout code posted below). (结帐代码发布在下面)。 My initial thought is that I want to be able to pass the colors to the css/scss to set the before and after.我最初的想法是,我希望能够将 colors 传递给 css/scss 来设置前后。 After a little research, I realized that you can't do :before/:after via inline styles, which I would've said was an ugly solution anyway.经过一番研究,我意识到你不能通过内联 styles 执行:before/:after ,无论如何我会说这是一个丑陋的解决方案。

I'm more comfortable in React css in js, where this is a really simple task.我在 js 中使用 React css 更舒服,这是一个非常简单的任务。 I am also open to scrapping this html/css aspect and doing it differently since it seems overcomplicated with the way that I'm going about it.我也愿意放弃这个 html/css 方面并以不同的方式做它,因为它似乎与我的处理方式过于复杂。

Any help would be great.任何帮助都会很棒。 Thank you so much.太感谢了。

.tcd-primary {
  min-height: 100px;
  position: relative;
  width: calc(50% - 30px);
  float: left;

  &:after {
    content: '';
    position: absolute;
    top: 0;
    width: 0;
    height: 0;
    left: 100%;
    border-right: 50px solid transparent;
    border-top: 100px solid black; /* this should be the primary color instead of black */
  }
}

.tcd-secondary {
  min-height: 100px;
  position: relative;
  width: calc(50% - 30px);
  float: right;

  &:before {
    content: '';
    position: absolute;
    top: 0;
    width: 0;
    height: 0;
    right: 100%;
    border-left: 50px solid transparent;
    border-bottom: 100px solid black; /* this should be the secondary color rather than black */
  }
}

Here's a simplified version of the html.erb:这是 html.erb 的简化版本:

<% teams.each do |team| %>
  <p><%= team.name %></p>

  <div class="tcd-primary" style="background: <%= team.primary_color %>;"></div>
  <div class="tcd-secondary" style="background: <%= team.secondary_color %>;"></div>
<% end %>

Since these are just colored divs with no text we can take advantage of the inheritance of currentColor . 由于这些只是没有文本的彩色div,因此我们可以利用currentColor的继承。

That is, if we define a text color, then the currentColor value can be used for the background and/or borders. 也就是说,如果我们定义文本颜色,那么currentColor值可用于背景和/或边框。

The currentColor keyword represents the value of an element's color property. currentColor关键字表示元素的color属性的值。 This lets you use the color value on properties that do not receive it by default. 这使您可以在默认情况下不接收颜色的属性上使用颜色值。

If currentColor is used as the value of the color property, it instead takes its value from the inherited value of the color property. 如果将currentColor用作color属性的值,则它将取自color属性的继承值。

MDN MDN

 .tcd-primary { min-height: 100px; position: relative; width: calc(50% - 30px); float: left; background: currentColor; } .tcd-primary:after { content: ''; position: absolute; top: 0; width: 0; height: 0; left: 100%; border-right: 50px solid transparent; border-top: 100px solid currentColor; } .tcd-secondary { min-height: 100px; position: relative; width: calc(50% - 30px); float: right; background: currentColor; } .tcd-secondary:before { content: ''; position: absolute; top: 0; width: 0; height: 0; right: 100%; border-left: 50px solid transparent; border-bottom: 100px solid currentColor; } 
 <div class="tcd-primary" style="color: gold; "></div> <div class="tcd-secondary" style="color:orange;"></div> <div class="tcd-primary" style="color: green; "></div> <div class="tcd-secondary" style="color:silver;"></div> 


If you want to get really up to date you could use a single div with an angled linear gradient and CSS Custom Properties 如果您想真正了解最新信息,可以使用具有角度线性渐变和CSS自定义属性的单个div

 div { height: 100px; background-image: linear-gradient( 135deg, var(--color-1) 60%, transparent 60%, transparent 62%, /* this is your gap */ var(--color-2) 62% ); } 
 <div style="--color-1: gold; --color-2: orange;"></div> <div style="--color-1: green; --color-2: silver;"></div> 

Dynamic CSS Background Color动态 CSS 背景色

If you're using a framework like Vue and you are receiving your data from a database that contains stuff like the colors etc, you could either have specific classes but that gets tedious...如果您使用的是 Vue 之类的框架,并且您正在从包含 colors 等内容的数据库中接收数据,那么您可以拥有特定的类,但这会变得乏味......

I recently found that you could pass a css variable to your html as a style property and then use that variable in your css...我最近发现您可以将 css 变量作为样式属性传递给您的 html,然后在您的 css 中使用该变量...

Of course, this needs to be edited to change the below red to a variable of your choosing, this is just the concept.当然,这需要编辑以将下面的红色更改为您选择的变量,这只是概念。

<style>
    .myDiv {
        height: 200px;
        width: 200px;
        background: var(--backgroundColor);
    }
</style>
<div class="myDiv" style="'--backgroundColor: red;'>Some block</div>

From here, you would use Javascript to either change the value of your css variable with the below从这里,您将使用 Javascript 来更改 css 变量的值,如下所示

const root = document.querySelector(':root');
root.style.setProperty('--backgroundColor', 'blue');

Or in Vue logic simply dynamically change the value with something like this或者在 Vue 逻辑中简单地用类似这样的东西动态改变值

<div class="myDiv" :style="'--backgroundColor: ' + backgroundVariable + ';'">
    Some Block
</div>

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

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