开发者

Applying just one property from a CSS class to an element

开发者 https://www.devze.com 2023-04-04 16:45 出处:网络
I\'m working with a number of large interchangeable style sheets. I need to use the border colour from one of the classes as the border for a div. The class in question has a number of properties, I o

I'm working with a number of large interchangeable style sheets. I need to use the border colour from one of the classes as the border for a div. The class in question has a number of properties, I only want the border. Is there any way of doing this with CSS? I'd be happy with a CSS3 solution if it degrades nicely.

Of course I can use JS to do it, I know how with JQuery. But I was hoping to avoid that.

Lyle

Update: As I feared not possible, why hasn't CSS3 provided a solution to this? As I said I'm working with a number of large interchangeable style sheets, the re-f开发者_Python百科actoring suggestions simply aren't workable and they'd not only be a large job in themselves, but have far reaching implications :( I'll just have to do it with JQuery.

JQuery solution (JQuery.css doesn't like shorthand, like border or border-color):

var border = $('.class').css('border-top-color');

$('div').css('border-color', border);


This is simply not possible in CSS alone, unless you alter the way the CSS declarations work.

For example:

.class1 {
    background: green;
}
.class1, .class2 {
    border: 1px solid red;
}

...and the HTML:

<div class="class1"></div>
<div class="class2"></div>

Or:

.class1 {
    background: green;
}
.class2 {
    border: 1px solid red;
}

...and then in your HTML:

<div class="class1 class2"></div>
<div class="class2"></div>


Nope. Try to refactor your stylesheets. You can add multiple classes to one element.

<div id="mydiv" class="borders black"></div>
0

精彩评论

暂无评论...
验证码 换一张
取 消