SCSS - Conditional include not working and all the blocks gets executed - html

I am trying to do conditional include in scss. Here is the code:
HTML
<div class="test" data-active data-label data-comment >1</div>
<div class="test" data-active data-label>2</div>
<div class="test" data-active data-comment >3</div>
<div class="test" data-active data-comment data-label>4</div>
<div class="test" data-active>5</div>
SCSS
.test[data-active]{
background : red;
width: 50px;
height: 50px;
display: inline-block;
$margin: 0px;
&[data-label]{
background : blue;
$margin: $margin + 10px;
}
&[data-comment]{
background: yellow;
$margin: $margin + 10px;
}
&[something]{
$margin: $margin + 100px;
}
margin-right : $margin;
}
Here is the codepen link: https://codepen.io/gaurav-neema/pen/VwpPBxY
In the code, you can see that even if the element does not contain the attribute, all the blocks get executed and the margin is included.
Can anyone help in indentifying what's wrong with the code?

You are redefining $margin every time, you cannot use selectors like if statements.
Redefining with:
$margin: $margin + 10px;
$margin: $margin + 100px;
You are setting all margin-right: 100px;
I think you might want:
SCSS
$margin: 0px;
.test[data-active] {
background: red;
width: 50px;
height: 50px;
display: inline-block;
&[data-label] {
background : blue;
margin: $margin + 10px;
}
&[data-comment] {
background: yellow;
margin: $margin + 10px;
}
&[something] {
margin: $margin + 100px;
}
margin-right: $margin;
}

What you are looking for is CSS custom properties. SCSS is not dynamic and there for renders at compile. CSS custom properties are dynamic so they get applied once the condition is for filled, meaning when your class is applied it will change the value then.
Read about the difference at:
css-tricks.com
Read about CSS custom properties at:
MDN
ishadeed.com
daverupert.com
This is just 3 of so many articles about the subject.
Now demo time, I changed the property to be background-color to make it more clear for the demo.
.test[data-active] {
--background-color: aqua;
background: var(--background-color);
width: 50px;
height: 50px;
display: inline-block;
}
.test[data-label] {
--background-color: deeppink;
}
.test[data-something] {
--background-color: deepskyblue;
}
<div class="test" data-active data-label data-comment>1</div>
<div class="test" data-active data-label>2</div>
<div class="test" data-active data-comment>3</div>
<div class="test" data-active data-comment data-label>4</div>
<div class="test" data-active data-something>5</div>

Related

How to change a svg image along with text with hover in CSS

I tried to change my svg image and text on hover and it doesn't work how I want it. I want the moment my mouse crosses over any of these 2 (svg or text) to change both.
Maybe I made a mistake in the html structure or maybe in css.
Please if you can help me, thanks in advance.
.logout-image {
mask: url("../../../assets/images/log-out.svg");
background-color: #62799D;
width: 20px;
height: 20px;
margin-right: 20px;
margin-left: 20px;
}
.logout-container {
display: flex;
flex-direction: row;
align-items: center;
padding: 8px 0px 8px 24px;
width: 100%;
height: 60px;
position: static;
left: 0px;
top: 280px;
color: #62799d;
margin-top: 35vh;
}
.logout-container:hover {
background: #0c2e6e;
color: white;
}
.logout-image:hover{
background: white;
}
<ul className="SidebarList">
{sidebarData.map((value, key) => {
return (
<li
className="row"
key={key}
onClick={() => {
window.location.pathname = value.link;
}}
>
{" "}
<div className="icon">{value.icon}</div>{" "}
<div className="title">{value.title}</div>
</li>
);
})}
{" "}
{" "}
//here is the problem
<li className='logout-container'>
<div className="logout-image"></div>
<div className="title" >Logo Out</div>
</li>
//until here
</ul>
This is how I wanted to change with hover:
target result
And this is how it is working right now:
current result
The background changes only if I cross over my svg image:
example here
Add this:
.logout-container:hover > .logout-image{
background: white;
}
It turns the image and words white when logout-container is hovered.

different background colours in div tags

I am wondering if there is a more nice way to set 4 different background colours in CSS. I have to make the following setup:
Is there a more clean and nice code I can make beside this i made, or is this the only way to do it? The code is just looking really ugly after my opinion.
<div class="bg1"></div>
<div class="bg2"></div>
<div class="bg3"></div>
<div class="bg4"></div>
.bg1 {
background-color: blue;
}
.bg2 {
background-color: red;
}
.bg3 {
background-color: green;
}
bg4 {
background-color: purple;
}
Personnaly, I would do that:
<div class="square square-blue"></div>
<div class="square square-red"></div>
<div class="square square-green"></div>
<div class="square square-purple"></div>
and in CSS:
.square{
border: 1px solid black; // and all params
}
.square-blue {
background-color: blue;
}
.square-green {
background-color: green;
}
.square-purple {
background-color: purple;
}
.square-red {
background-color: red;
}
for more readability (a small usage of BEM - http://getbem.com/introduction/ ).
You can make use of nth-child/nth-of-type here.
Check the following code snippet.
.container div:nth-child(1) {
background: blue;
}
div:nth-of-type(2) {
background: red;
}
div:nth-of-type(3) {
background: green;
}
div:nth-of-type(4) {
background: purple;
}
.container {
display: flex;
flex-wrap: wrap;
}
.container div {
width: 100vw;
height: 50px;
}
<div class="container">
<div class="bg1"></div>
<div class="bg2"></div>
<div class="bg3"></div>
<div class="bg4"></div>
</div>
you could add style='background-color: #123456' to each div, if you really had to because of some restriction of a templating engine (I can't imagine such a circumstance), but what you've got is fine in the context.
EDIT: What I mean by that is if you wanted to have, say a rainbow effect. In Perl/TemplateToolkit, I could see something like:
[% for ($code = 0; $code < $whatever_max_hex_is'; $code++) { %]
<div style='background-color: [%$code%]'></div>
[%END%]
Something like that. Totally untested, but I'm sure you get the drift.

How Polymer Hero Transition works

First off I'm having a tough time understanding the fundamentals of the hero-transition within Polymer. I am attempting to build a hero transition card like the one in the example provided by them, which can be found here.
Below I've built the mini card and I'm just trying to understand the transition and how the larger card works with the smaller one.
My specific question is, how does the transition bind to each element? Do I need to complete the CSS for both before I can begin playing with the core-animated-pages? Does having an embedded template matter?
Any guidance would be extremely helpful.
<script src="../components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="../components/core-animated-pages/core-animated-pages.html">
<link rel="import" href="../components/core-animated-pages/transitions/hero-transition.html">
<link rel="import" href="../components/paper-button/paper-button.html">
<link rel="import" href="../components/core-image/core-image.html">
<link rel="import" href="../components/paper-shadow/paper-shadow.html">
<polymer-element name="chip-card">
<template>
<style>
#page2 {
width: 100%;
height: 100%;
}
#paper_shadow {
position: relative;
display: inline-block;
font-family:'Roboto', sans-serif;
font-size: 12px;
color: white;
}
#chip_body {
height: 400px;
width: 300px;
background-color: aqua;
color: black;
}
#chip_top {
background-color: deeppink;
background-image: url();
background-size: cover;
background-position: center center;
width: 100%;
position: relative;
}
#chip_bottom {
background-color: #fbfbfb;
width: 100%;
height: 20%;
position: relative;
font-size: 1.2em;
word-wrap: break-word;
}
#text {
padding-left: 5%;
padding-right: 2.5%;
overflow: hidden;
}
#coreImage {
display: block;
}
#card_container {
width: 70%;
height: 600px;
background-color: aqua;
color: black;
}
#card_right {
height: 100%;
width: 30%;
}
#card_left {
background-color: darkblue;
height: 100%;
width;
70%;
}
#card_left_top {
padding-right: 20px;
padding-top: 20px;
background-color: skyblue;
}
#circle {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: red;
}
#header_text {
}
#card_content {
width:100%;
background-color: lightcoral;
}
</style>
<core-animated-pages transitions="hero-transition" selected={{page}}>
<section>
<paper-shadow z="1" id='paper_shadow' on-mouseover="{{raise}}" on-mouseout="{{lower}}" animated=true; hero-p="" on-tap="{{transition}}">
<div id="chip_body" hero-id="chip_body" vertical layout center justified>
<div id="chip_top" flex>
<div id="coreImage">
<content select="#core-image"></content>
</div>
</div>
<div id="chip_bottom" vertical layout start-justified>
<div id='text'>
<content select="#chip_bottom"></content>
</div>
</div>
</div>
</paper-shadow>
</section>
<section id="page2">
<div id="card_container" hero-id="chip_body" on-tap="{{transition}}" hero=""></div>
</section>
</core-animated-pages>
</template>
<script>
Polymer('chip-card', {
page: 0,
raise: function() {
this.$.paper_shadow.setZ(2);
},
lower: function() {
this.$.paper_shadow.setZ(1);
},
transition: function(e) {
if (this.page === 0) {
this.$.paper_shadow = e.currentTarget;
this.page = 1;
} else {
this.page = 0;
}
}
});
</script>
</polymer-element>
you are actually very close to a working transition with the code you have.
I've implemented a more complicated hero transition on my website and took some code from there to get yours to work.
<core-animated-pages transitions="hero-transition" selected={{page}}>
<section>
<paper-shadow z="1" id='paper_shadow' on-mouseover="{{raise}}" on-mouseout="{{lower}}" hero-p on-tap="{{transition}}">
<div id="chip_body" hero-id="chip_body" hero vertical layout center justified>
<div id="chip_top" flex>
<div id="coreImage">
<content select="#core-image"></content>
</div>
</div>
<div id="chip_bottom" vertical layout start-justified>
<div id='text'>
<content select="#chip_bottom"></content>
</div>
</div>
</div>
</paper-shadow>
</section>
<section id="page2">
<div id="card_container" hero-id="chip_body" on-tap="{{transition}}" hero></div>
</section>
</core-animated-pages>
I've made but a few adjustments.
First off, any hero parent element, with the hero-p attribute, should contain just that attribute. So no need for the quotation marks :)
<paper-shadow hero-p .. >
Every element that's part of the Hero transition, needs a hero attribute.
Again, without the quotation marks. <div id="chip_body" .. hero .. >
And the same thing goes for the element you're transitioning to.
<div id="card_container" .. hero .. >
I've put a working version of your code on my website.
There's page containing the <chip-card> element and a second page containing the working template file.
Index page
Template file
Please note : I edited the reference to webcomponentsjs to conform with my folder structure.
Feel free to ask me if there's anything else!

How to add a <caption> tag in ng-grid table?

I need to display a table via ng-grid, but the table has a <caption> tag. See the yellow section in the image below. How can I achieve it?
ng-grid uses divs, not tables. You can use a custom headerRowTemplate to achieve a similar result, however.
The default header row template is here: https://github.com/angular-ui/ng-grid/blob/master/src/templates/headerRowTemplate.html
You can create your own and add a row on top, then reference it with the headerRowTemplate option in your grid options:
<script type="text/ng-template" id="myHeaderTemplate">
<div>
<div class="headerTop ngHeaderCell">
<span class="content">Submissions</span>
</div>
<div style="height: 30px; top: 30px; position: absolute">
<div ng-style="{ height: col.headerRowHeight }" ng-repeat="col in renderedColumns" ng-class="col.colIndex()" class="ngHeaderCell">
<div class="ngVerticalBar" ng-style="{height: col.headerRowHeight}" ng-class="{ ngVerticalBarVisible: !$last }"> </div>
<div ng-header-cell></div>
</div>
</div>
</div>
</script>
Make sure that the header container is sized to hold both the column headers and your caption:
.ngHeaderContainer, .ngHeaderScroller {
height: 60px !important;
}
And add styles for the caption:
.headerTop {
height: 30px;
width: 100%;
border-bottom: 1px solid #ccc;
background-color: #FFD700;
padding: 0px 0 0 6px;
}
.headerTop .content {
padding: 6px;
position: absolute;
bottom: 0;
top: 0;
color: #fff;
}
Here's a demonstration plunker:
http://plnkr.co/edit/fT1IrO?p=preview

Get value of attribute in CSS

I have this HTML code:
<div data-width="70"></div>
I want to set it's width in CSS equal to the value of data-width attribute, e.g. something like this:
div {
width: [data-width];
}
I saw this was done somewhere, but I can't remember it. Thanks.
You need the attr CSS function:
div {
width: attr(data-width);
}
The problem is that (as of 2021) it's not supported even by some of the major browsers (in my case Chrome):
You cant pass data attribute value directly in to css without pseudo type content.
Rather you can do this way.. CHECK THIS FIDDLE
<div data-width="a"></div><br>
<div data-width="b"></div><br>
<div data-width="c"></div>
CSS
div[data-width="a"] {
background-color: gray;
height: 10px;
width:70px;
}
div[data-width="b"] {
background-color: gray;
height: 10px;
width:80px;
}
div[data-width="c"] {
background-color: gray;
height: 10px;
width:90px;
}
Inline CSS variables are almost as declarative as data attributes, and they are widely supported now, in contrast to the attr(). Check this out:
var elem = document.getElementById("demo");
var jsVar = elem.style.getPropertyValue("--my-var");
function next() {
jsVar = jsVar % 5 + 1; // loop 1..5
elem.style.setProperty("--my-var", jsVar);
}
.d1 {
width: calc( var(--my-var) * 100px );
background-color: orange;
}
.d2 {
column-count: var(--my-var);
}
<button onclick="next()">Increase var</button>
<div id="demo" style="--my-var: 2">
<div class="d1">CustomWidth</div>
<div class="d2">custom columns number custom columns number custom columns number custom columns number custom columns number custom columns number custom columns number</div>
</div>
Another approach would be using CSS Custom Properties with style element to pass values from HTML to CSS.
div {
width: var(--width);
height: var(--height);
background-color: var(--backgroundColor);
}
<div
style="
--width: 50px;
--height: 25px;
--backgroundColor: #ccc;
"
></div>
<div
style="
--width: 100px;
--height: 50px;
--backgroundColor: #aaa;
"
></div>
CSS is static styling information about specific html element and not the other way around. If you want to use CSS to set the width of your div I suggest you do with the use of classes:
HTML:
<div class="foo"></div>
CSS:
.foo {
width: 70px;
}
jsFiddle
I'm just having fun with this, but a jQuery solution would be something like this:
HTML
<div class='foo' data-width='70'></div>
<div class='foo' data-width='110'></div>
<div class='foo' data-width='300'></div>
<div class='foo' data-width='200'></div>
CSS
.foo {
background: red;
height: 20px;
margin-bottom: 10px;
width: 0; /** defaults to zero **/
}
jQuery
$(document).ready(function(){
$('.foo').each(function(i) {
var width = $(this).data('width');
$(this).width(width);
});
});
Codepen sketch here: http://cdpn.io/otdqB
KIND OF AN UPDATE
Not what you're looking for, since you want to pass a variable to the width property. You might as well use a class in this case.
HTML
<div data-width='70'>Blue</div>
CSS
div[data-width='70'] {
width: 70px;
}
Sketch here: http://cdpn.io/jKDcH
<div data-width="70"></div>
use `attr()` to get the value of attribute;
div {
width: attr(data-width);
}
can you try this
$(function(){
$( "div" ).data( "data-width" ).each(function(this) {
$(this).width($(this..data( "data-width" )))
});
});