Defining styles for combinations of classes - css

Is there a way to define styles for a combination of classes? For example, I'd like my HTML to look like this, but the output to render in the appropriate color:
<span class="red">Red Text</span><br/>
<span class="green">Green Text</span><br/>
<span class="red green">Yellow Text</span><br/>
Edit: The above seems to be confusing people when it was just an example; so here is another example:
<style>
.style1 { background-color: #fff; }
.style2 { background-color: #eee; }
.style1.highlight { color: red; }
.style2.highlight { color: blue; }
</style>
<ul>
<li class="action style1">Do Action 1</li>
<li class="action style2">Do Action 2</li>
<li class="action style1 highlight">Do Action 1</li>
<li class="action style2 highlight">Do Action 2</li>
</ul>
<script language="javascript" type="text/javascript">
$("li.action").bind("click", function(e) {
e.preventDefault();
// Do some stuff
$(this).addClass("highlight");
$(this).unbind("click");
});
</script>
Again, this is just an example, so don't get hung up on alternating elements or anything like that. What I'm trying to avoid is having to duplicate the bind function for each different styleN or having to write an elseif structure that checks for each styleN class. Unfortunately this code doesn't work in IE 6 or 7 - the highlighted text for both .style1 and .style2 elements end up being blue.

You can select on multiple classes:
span.red.green { color: yellow; }
That will apply to any span element with red and green classes. Which may not be what you want, since it will also apply to, say:
<span class="red green blue">white</span>
Note that this doesn’t work right in IE 6.

Sure.
.red { color: red; }
.green { color: green; }
.red.green { color: yellow; }

You could do this using Javascript, although I it would be better just to create an yellow class, if your interested into the javascript method please comment (I need to think it out, and it would be more efficient just to already do this in your logic layer)
EDIT | Even more edit (added regex for current colors)
Okay, you're not interested but I'm still going to write it because it seems like fun:
var re = /([0-9]?)/g;
$("span").each( function(){
$this = $(this);
results = re.exec( $this.css('background-color')); // background is something like: 'rgb( 0, 132, 12)'
var sRed = results[1];
var sGreen = results[2];
var sBlue = results[3];
/* OLD!
var sRed = '0';
var sGreen = '0';
var sBlue = '0';*/
if($this.hasClass('red')) { sRed = '255'; }
if($this.hasClass('green')) { sGreen = '255'; }
if($this.hasClass('blue')) { sBlue = '255'; }
$this.css('background-color', 'rgb(' + sRed + ',' + sGreen + ',' + sBlue ')';
});
You will need jQuery for that, although you could probably do it without jQuery.

why do you want to do this? it may be possible, but i don't see the benefit. Would you also define class="short fat" and class="tall thin" or what about class="light dark"? Classes should be simple and specific for clarity and reuse. Multiple inheritance (fake or otherwise) should be avoided when it is unnecessary and potentially confusing.
class="red green"
is confusing
class="yellow"
is concise and clear
EDIT: i saw the second example, my advice remains unchanged. One class is concise and clear, two is potentially confusing. Assuming that this is supposed to work (and I think it is), expending great effort fighting bugs in the most popular browser (IE) is probably not worthwhile.

Here is my workaround:
<style>
.style1 { background-color: #fff; }
.style2 { background-color: #eee; }
.style1 .highlight { color: red; }
.style2 .highlight { color: blue; }
</style>
<ul>
<li class="action style1"><span>Do Action 1</span></li>
<li class="action style2"><span>Do Action 2</span></li>
<li class="action style1"><span class="highlight">Do Action 1</span></li>
<li class="action style2"><span class="highlight">Do Action 2</span></li>
</ul>
<script language="javascript" type="text/javascript">
$("li.action").bind("click", function(e) {
e.preventDefault();
// Do some stuff
$(this).children("span").addClass("highlight");
$(this).unbind("click");
});
</script>
It's not as elegant as I'd hoped. It uses an extra element for each item; but at least it's fairly clean still.

maybe something like this
.red
{
color: red;
}
.red_green
{
color: #AS8324;
}
and you can use your html code
<span class="red">Red Text</span><br/>
<span class="green">Green Text</span><br/>
<span class="red_green">Yellow Text</span><br/>
I don't know this is what you exactly want but this is my approach. I hope it helps.

Related

How to deal with cascading priority in CSS?

Let's say I have links looking like buttons all over my app. They are orange, unless they are "disabled" (having no href):
a.button {
background-color: orange;
}
a.button:not([href]) {
background-color: grey;
}
Now, I'm not sure how to allow certain buttons look different in their context, but keep the disabled ones as they were. Let's say I need the "buttons" inside my <footer> to be green, or - as usual - grey if disabled:
footer a.button {
background-color: green;
}
The problem is that this rule has higher priority, as it's more specific. How can I allow disabled buttons in the footer to still be grey without repeating my code? I know I can use !important, but please assume that my real-life example is more complex and I want to avoid using it.
Use CSS variables. You define the default value and you simply set the variable to define a new one.
a.button {
background-color: var(--main, orange);
}
a.button:not([href]) {
background-color: var(--disable, grey);
}
footer#foo a.button { /*I am adding an ID to make it really more specific*/
--main: green;
}
<a class="button">a link</a>
a link
<footer id="foo">
<a class="button">a link</a>
a link
</footer>
Check out http://qnimate.com/dive-into-css-specificity/ to see a full list of CSS specificity.
Assuming you have more than one a.button in your footer, we'll skip using a plain id selector. You could pair an id and attribute selector, using the title attribute to identify all disabled "buttons":
index.html
<a class="button">a link</a>
a link
<footer id="foo">
<a class="button" title="disabled">a link</a>
a link
</footer>
and styles.css
#foo a[title="disabled"] {
color: green;
}

How to avoid redundant for something like BEM modifiers

Recently when I started to use my own implementation of methodology based on BEM I stuck on modifiers for nested elements.
I want to change link color to red when product-desc-name has class mark.
The following example presents the problem.
What should I do to keep the final style the same but without duplicating class names?
.product-desc {
&-name {
&.mark {
/* this section is ugly */
.product-desc-link {
color: red;
}
}
}
}
<ul class="product-desc">
<li class="product-desc-name">
<a class="product-desc-link">Param1</a>
</li>
<li class="product-desc-name mark"> <!--add class .mark-->
<a class="product-desc-link">Param1</a>
</li>
</ul>
This is a typical weakness of BEM. I have search for long, but do not seen any good solution for this so I make my own.
At first I would change the class name. Because UL element should be call 'product-desc-list'. The LI element 'product-desc', as this is in fact exactly a product description for a product.
The more important is the condition of the product. Therefore the selection of the element should be mentioned first. This allows several blocks to be used for one component.
The first is the component definition. The next define possible states like selected, in progress etc.
Here is an example for illustration
// your product in default definition.
.product-desc {
&--link {
text-decoration: underline;
}
}
// your product in mark state definition
.mark {
.product-description {
&.--link{
font-weight: bold;
}
}
}
<ul class="product-desc-list">
<li class="product-desc">
<a class="product-desc--link">Param1</a>
</li>
<li class="product-desc mark"> <!--add class .mark-->
<a class="product-desc--link">Param1</a>
</li>
</ul>

CSS selected/active property

I have twelve <a href> links that lead to different categories. As a means of orientation for the user I would like to emphasise the very category (<a href>-button) that the user is in right now.
How can I achieve this in CSS? I read about selected and active, but I haven't been able to make it work yet.
This is one of the links/buttons:
<span class="category_item"></span><span class="category_description">Handy & Co.</span>
The corresponding CSS:
.category_item {
display:inline-block;
background:url(../img/category_item/ph.png) no-repeat;
width: 45px;
height: 45px;
margin-right: 11px;
margin-bottom: 20px;
}
.category_item:hover {
background:url(../img/category_item/hover.png);
}
.category_description {
position: absolute;
font-size: 11px;
color: #000;
margin-top: 43px;
margin-left: -62px;
z-index: 1;
opacity: 0;
}
Thank you in advance!
You can run some jquery code when you load the page that checks the link urls with the current page's url and setting a class on the links that match.
JSFiddle: http://jsfiddle.net/og4o1tdh/2/
something like this:
HTML:
<div id="categories">
<span class="category_description">Google</span>
<!-- jsfiddle code is apparently run on fiddle.jshell.net -->
<span class="category_description">JSFiddle</span>
</div>
JS:
$('#categories a').each(function (){
var link = $(this).attr('href');
if (window.location.href.indexOf(link) > -1) {
$(this).find('span').addClass('currentCategory');
}
});
CSS:
.currentCategory {
color: orange;
font-weight: bold;
}
To give a special class to an anchor when a user clicks you can use simple javascript and jQuery.
Give all the anchor's you want to be in the scope of this a class for instance:
HTML:
<a class="nav-link" href="http://www.google.com"> Google </a>
<a class="nav-link" href="http://www.yahoo.com"> Yahoo </a>
Javascript:
$(".nav-link").on("click", function() {
$(this).addClass("active");
});
To make sure you only have one anchor with "active" class I would do the following:
$(".nav-link").on("click", function() {
$(".nav-link").removeClass("active");
$(this).addClass("active")
});
There is no built-in way of knowing which link is the current one. The easiest way may be to use javascript to check the current URL by document.URL and add a CSS class to the link with an equal href attribute. Then, you may style this class in CSS.
CSS doesn't know what page you are on.
To do this you will have to change your HTML markup, for example: to add:
<a class="current-page" href="index.php?category=handy&location=&sort=" ...
on the relevant link which you can use to 'hook' an new CSS style onto:
.current-page { color: red; }
The alternative is to use Javascript to 'read' the URL and apply a style.
You could...
Simply add a unique classname to the body tag or (some element that wraps around the anchor tags). And then style your links accordingly. This option is quite easy if you have access to change the HTML in your pages:
HTML
<body class="category_handy">
...
<a href="..." class="category_handy">
<span class="category_item"></span>
<span class="category_description">Handy & Co.</span>
</a>
....
</body>
CSS
body.category_handy a.category_handy {
color:red;
}
body.category_dandy a.category_dandy {
color:yellow;
}
body.category_something a.category_something {
color: blue;
}
If you don't have access to directly edit each page, you may have to dynamically check the URL, and then add a classname (like "current") to the anchor tag who's href attribute matches.
Either way, the solution will not involve "css only".

AngularJS Tabular Navigation Selected Styling

I am creating a navigation structure. I tried to use AngularStrap and Bootstrap, but as soon as I injected them into my app, Angular failed. I found this link and constructed my navigation tab-bar. I like how easy it is to customize. My problem is, I don't know how to apply the css for the selected tab in angular. I can't apply an id to an element conditionally, and when I try and break up the css into multiple classes, the tabs don't display the same way.
<ul class="tablist">
<li ng-repeat="tab in tabList" ng-click="setSelected($index);">
{{tab.title}}
</li>
</ul>
vs.
<ul class="tablist">
<li id="selectedTab">Admin</li>
</ul>
What is the best way to apply the selected formatting? See this Fiddle for a more fleshed out example.
Updated: http://jsfiddle.net/dLemh/6/
to have background color change use css important on the class
ng-class="{selected: isSelected(tab)}"
$scope.currentSelectedTab = {};
$scope.setSelectedTab = function(tab){
$scope.currentSelectedTab = tab
}
$scope.isSelected = function(tab){
if(tab == $scope.currentSelectedTab){
return true;
}
return false;
}
Please let me know if anything
I found this SO Post that I was able to use and fix the CSS formatting with regards to their priority.
.tablist li.selectedTab a {
background: none;
border: 2px solid #6B74C6;
border-bottom: 0px;
background-color: #F3F3F3;
color: #0378D9;
text-decoration: none;
}
<ul class="tablist">
<li ng-repeat="tab in tabList" ng-click="setSelected($index);" ng-class="{ selectedTab: $index === selected}">
{{tab.title}}
</li>
</ul>

CSS How to change background color of text?

I have just 3 words on my webpage that I want to have colored background: Red, Green and Blue. There are 2 components to my questions:
How should I do this? Put this in a style sheet or just hard code it in the webpage?
Either way, please show me how to do what I want each way so I can decide. I am an absolute beginner with css as you can tell.
BTW, I am doing this in an aspx page if it makes any difference.
For inline styles:
<span style="background-color:red;">Red Stuff...</span>
<span style="background-color:green;">Green Stuff...</span>
<span style="background-color:blue;">Blue Stuff...</span>
For Css File
.red{
background-color: red;
}
.green{
background-color: green;
}
.blue{
background-color: blue;
}
and in your html;
<span class="red">red stuff</span>
<span class="green">green stuff</span>
<span class="blue">blue stuff</span>
The choice should depend whether you want to use these properties on other places. If so go with a style sheet. (IMHO go with a seperate style sheet anyway)
You need to put each word in a tag, and give each tag a background color.
Something like this:
<span style="background-color: red;">Word 1</span>
<span style="background-color: green;">Word 2</span>
<span style="background-color: blue;">Word 3</span>
Method 1
HTML
<div>
<p id="red">Red</p>
<p id="green">Green</p>
<p id="blue">Blue</p>
</div>
CSS
p#red
{
background-color: red;
}
p#green
{
background-color: green;
}
p#blue
{
background-color: blue;
}
Method 2
HTML
<p><span>Red</span><span>Green</span><span>Blue</span></p>
CSS
p>span:nth-child(1)
{
background-color: red;
}
p>span:nth-child(2)
{
background-color: green;
}
p>span:nth-child(3)
{
background-color: blue;
}
Add this in you HTML inside the head
<link rel="stylsheet" type="text/css" href="#foo" />
Name you stylesheet CSS file and put the relative/absolute path in the above href
Can anyone tell me how to add syntax coloration to my post so I can add an answer to this question.I'll tell how to generate that code with c#.
So the code is :
string[] cols = { "red","green","blue"} // or any other color
string[] words = { "","","" } // Put your words inside quotes
string res = "";
for(int i = 0;i < 3;i++)
{
res+= "<span style=" + "\" + "background-color:"
+ cols[i] + "\" + " >" + words[i] + "</span>";
}
// the code till now should either be in script tags
// or in the code file linked with your web page
// If you are using visual studio that is the name of the pate.aspx.cs
// now use in the aspx page
<% Response.Write(res); %> // I am not sure if the semicolon is required here.
This is if you want to do it on the server side.
I suggest putting that in style sheet. I personally like to avoid having any "hard css" (not sure about the right word).
<head>
<style type="text/css">
span.textRed
{
background-color: red;
}
span.textGreen
{
background-color: green;
}
span.textBlue
{
background-color: blue;
}
</style>
</head>
<body>
<span class="textRed"> Red </span>
<span class="textGreen"> Red </span>
<span class="textBlue"> Red </span>
</body>

Resources