CSS Property Value from Class Name

Using class name in CSS value

CSS attr

Theoretically, this type of thing is what the CSS attr property could be used for when browser support exists. Note that this won't work now, but when browser support does exist, it might look something like this:

HTML

<span class="impact" data-shadow="#008080">Impact</span>

CSS

.impact {
/* you text and positioning styles here */

text-shadow: 2px 2px attr(data-shadow);
}

You can read more about the attr property here:
https://developer.mozilla.org/en-US/docs/Web/CSS/attr


But for now...

Your best bet is probably to continue to use JavaScript, but instead of appending the hex code to the class name, store the hex value in a data attribute of the element, allowing you to keep the class name consistent for all instances of that element.

HTML

<span class="impact" data-shadow="#fff">Impact</span>

CSS

.impact {
/* your text and position styles here */
}

JS

var el = document.querySelector(".impact"),
shadow = el.dataset.shadow;

el.style.textShadow = '2px 2px ' + shadow;

Here's a JSFiddle for reference: http://jsfiddle.net/galengidman/xx6r1n2o/

Vue - bind a css class name with a property value

You had a typo :parent:'top-bar' -> :parent='top-bar' and your class binding would always pass the 'parent' string as a class. Learn more here.

I also made a small working example:

Vue.component('parent1', {
template: '<div><dropdown-menu :menu="link" :parent="top_bar"></dropdown-menu></div>',
data () {
return {
link: 'a link',
top_bar: 'parent1'
}
}
});

Vue.component('parent2', {
template: '<div><dropdown-menu :menu="link" :parent="top_bar"></dropdown-menu></div>',
data () {
return {
link: 'another link',
top_bar: 'parent2'
}
}
});

Vue.component('dropdown-menu', {
template: '<div class="dropdown" v-bind:class="parent">{{ menu }}</div>',
props: ['parent', 'menu']
});

var vm = new Vue({
el: '#app'
});
.parent1 {
color: red;
}

.parent2 {
color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.js"></script>

<div id="app">
<parent1></parent1>
<parent2></parent2>
</div>

How to get CSS class property in Javascript?

For modern browsers you can use getComputedStyle:

var elem,
style;
elem = document.querySelector('.test');
style = getComputedStyle(elem);
style.marginTop; //`20px`
style.marginRight; //`20px`
style.marginBottom; //`20px`
style.marginLeft; //`20px`

margin is a composite style, and not reliable cross-browser. Each of -top -right, -bottom, and -left should be accessed individually.

fiddle

Append text, when property value is equal to css class name

Your if condition seems to be wrong. It could look like this:

if(index.classList.contains(ind.slot)){
index.innerText = ind.name;
}

Now you have another problem: the divs are not created in order. First, you are creating the div for slot two, and then the div for slot one. The texts are inserted correctly, however the slots are in the wrong order. If you change people to look like this, it should work again:

const people = [
{name: 'Tim', slot: 'one'},
{name: 'Tom', slot: 'two'}
];

If you want the elements to be sorted automatically, you can change your people array to use numbers for the slots. That way, you can sort them:

var people = [
{name: 'Tom', slot: 2},
{name: 'Tim', slot: 1}
];

people.sort((personA, personB) => {
return personA.slot - personB.slot
});

With those changes, your code should now work. Here's a working snippet:

const body = document.querySelector('body');
var people = [ {name: 'Tom', slot: 2}, {name: 'Tim', slot: 1}];
people.sort((personA, personB) => { return personA.slot - personB.slot});
const createDiv = (arg) => { const div = document.createElement('div');
div.classList.add('name', arg.slot);
body.append(div);
const namesClass = document.querySelectorAll('.name');
people.forEach((ind)=>{ namesClass.forEach((index)=>{ if(index.classList.contains(ind.slot)){ index.innerText = ind.name; }; }); });
return div;}
const render = () => { people.forEach(function(index){ createDiv(index); });}
render();

Dynamic CSS property value that depends on its class/id name

With pure CSS no. The only way with CSS is to create all the classes you'll use on the start:

.m-t-10{ margin-top: 10px }
.m-t-20{ margin-top: 20px }
.m-t-30{ margin-top: 30px }
/* etc margin */

.p-t-10{ padding-top: 10px }
.p-t-20{ padding-top: 20px }
.p-t-30{ padding-top: 30px }
/* etc padding */

Or you can use LESS/SCSS/some other CSS preprocessor to create a mixin/function which creates those classes.

Boostrap V4 included the same feature so check the source code to get the idea.



Related Topics



Leave a reply



Submit