Can a CSS class contain two other classes - html

I just had the idea of organizing my work as follows:
Create very basic CSS classes, for example this :
.backgroundRed {
background-color:red;
}
.backgroundGreen {
background-color:green;
}
.fixed300 {
width:300px;
}
.percent100 {
width: 100%;
}
.centered {
margin: auto;
}
.centeredTextHorizontally {
text-align:center;
}
.colorWhite {
color:white;
}
and then use 2-3-4 of them simultaneously, to create what I want, for example:
<div class = "backgroundGreen fixed300 centered">
<div class="centeredTextHorizontally">300px wide green stripe with centered text</div>
<div class="centeredTextHorizontally colorWhite">Centered white text</div>
</div>
</div>
Im sure you get the idea.
Now this has the problem that if in the future we want to change the web site, we need to edit the HTML of all those DIVs, which breaks the very pupropse of using CSS in the first place.
So I would like to be able to define CSS classes as follows
.navbar {
.colorRed;
.backgroundColorGreen;
}
etc etc. So that if the website colors need to be changed, for example, I only change the .navbar and not the DIVs in the HTML.
Is it possible to perform something like the above and how ?

Its not possible with pure css. you will need to look into a CSS pre-processor. Two popular ones are called Sass and Less. These links should give you more information on them:
Sass
Less
This will help you get started with your specific problem:
including another class in Sass

.navbar {
.colorRed;
.backgroundColorGreen;
}
This is not css rule. You have to use class like below -
.navbar.colorRed.backgroundColorGreen {
/* Your css styles */
}
Notice, there is no space between class name and dot . next to it.

Related

New classes don't work, but old classes work perfectly fine

When I create a new class and try to use it, it does not work.
Here is the CSS I am using:
/* NEW */
.Test {
color:purple;
}
/* OLD */
.TitleFont {
font-family: MiriamLibre-Regular;
color: white;
margin-left: auto;
margin-right: auto;
width: 8em;
}
As you can see, the class commented NEW is one one I just created, and the one commented OLD is the one I had created a while ago.
I can use the new class with a h1 tag like this:
<h1 class="Test">This is a test.</h1>
When I go to the page it does not show the purple it was supposed to show. (It does the same for anything, I just put color:purple; as an example)
Here is an image of it not working.
I can replace the class used in the HTML with the old class like this:
<h1 class="TitleFont">This is a test.</h1>
And it works perfectly fine. (All my other classes from a while ago work fine too.)
You can see it working here.
How can I fix this?
In HTML, you have this:
<h1 class="Test">
So the class you use there is .test
But your CSS rule looks like this:
.TestClass {
color:purple;
}
That's for a class named .TestClass (which you don't use in your HTML code...)

How can I simply article labels? I'm reusing the same 10 lines of code, with the only difference being the label color

I am designing a basic news-ish blog. Let's say on my news blog I have about 10 categories, and each of those categories will have a label that will appear to the top right of the card image. Now each label will have it's own specific identifying color. So for example, the video label will be red, the lifestyle label will be green, programming will be orange... etc.
The code I have on the bottom works, but my problem with it is that it is reusing the same 10 lines of css, with the only changing factor being the label color.
Is there a more efficient way of doing this?
.article-tag-news{
...
background-color: #ff8fd2;
...
}
.article-tag-games{
...
background-color: #f4f4f4;
...
}
.article-tag-videos{
...
background-color: #123456;
...
}
I would use a general class for the main css for the labels .article-tag and add the color with an new class tag .news etc.
.article-tag{
... //all the css applied to all the tags
}
.news{ //or .article-tag.news depending on your code
background-color: red;
}
And in your HTML use
<label class="article-tag news"></label>
You can use a class for all tagged articles:
.article-tag-all{
...
}
.article-tag-news{
background-color: #ff8fd2;
}
And use it like:
<article class="article-tag-all article-tag-news"></article>
Note: I am on my phone right now, formatting would be appreciated.
While keeping your current markup, styles common to all articles can be grouped in a rule with an attribute selector like:
[class="article-tag-"] {
/* common styles */
}
.article-tag-particular {
background-color: salmon;
}
My 2ยข: problem with <label class="article-tag news"></label> is that from now you can't style the .news class anywhere else OR you can't style it independently/must always style .something-something.news {}.

how to add more specificity to css class .background-color-blue so it overrides default div's background color

I have this CSS class
.background-color-blue {
background-color: #00C0FF;
}
and I want to be able to use it on some elements if needed.
I now it is not very semantic, but it is used for HTML template that is meant to be easy to use, small in size, and universal.
I want to be able to use it on elements like some panels, sidebars, modals, top-bar menu , or whatever I want
of course it works, but only for divs that do not have background-color already specified.
in this case:
<div class="modal background-color-blue"></div>
.modal has already specified bg color to #fff. now it does not work - it stays white.
I have found two solutions for that:
.background-color-blue {
background-color: #00C0FF !important;
}
and
div.background-color-blue {
background-color: #00C0FF;
}
I am not sure about these... which solution is better? Or is there any other solution that would work better?
I think this method:
.modal.background-color-blue {
background-color: #00C0FF;
}
is not good since I would have to do it with any similar element.
and
<div class="modal">
<div class="background-color-blue">
</div>
</div>
also isn't good since modal already has some padding.
Just use !important it will help to override
.background-color-blue {
background-color: #00C0FF !important;
}
Take a look at this : when-using-important-is-the-right-choice
If you think that modal will be able to have different colors, i guess you'd like this (ITCSS - BEM inspired). This will help you to stay at a low specificity rate, preventing you to have some problems with future classes or have to overwrite them.
Exemple here :
http://codepen.io/AxelCardinaels/pen/ZGVKzp
HTML :
<div class="container">
<div class="modal modal--grey">
<h1 class="modal__title"> Modal Title</h1>
<p class="modal__text">Hello this is the content of the modal !</p>
</div>
</div>
CSS :
/*Base Class */
.modal{
width:40%;
margin:0px auto;
padding:20px;
text-align:center;
border-radius:10px;
box-shadow:1px 1px 1px rgba(1,1,1,0.3);
}
/*Attribute classes for the modal, just make a choice in your HTML ! */
.modal--grey{
background:rgb(220,220,220);
border:1px solid rgb(200,200,200);
}
.modal--blue{
background:rgb(65,105,225);
border:1px solid rgb(58,95,205);
}
The proper usage is
! important
Normally it should have been blue background. Perhaps something else that is preventing the elements of blue.
But don't forget that; the last !important taken into account.

Getting element by ID in coffeescript

I'm trying to create a HTML widget:
HTML:
<div>
<h1 class="title" data-bind="title">Title</h1>
<div>
<h1 id = "dc1" class="dc">DC1</h1>
</div>
<div>
<h1 id = "dc2" class="dc">DC2</h1>
</div>
<p class="updated-at" data-bind="updatedAtMessage"></p>
</div>
And I need to be able to set the background color of the id="dc1" and id="dc2" elements dynamically in CoffeeScript. I plan to do this by adding a class with a background color setting:
SCSS:
&.up {
background-color: green;
}
&.down {
background-color: red;
}
.dc {
background-color: orange;
font-size: 30px;
float: left;
width: 50%;
}
So far I have managed to set the whole widget background but not the child elements mentioned above:
I have been using:
CoffeeScript:
$(#node).removeClass('up down')
$('#dc1').removeClass('up down')
$('#dc2').removeClass('up down')
$(#node).addClass('down')
$('#dc1').addClass('down')
$('#dc2').addClass('up')
Note ultimately I will add the classes depending on some data rather than hard coding them to 'up' or 'down' in the coffeescript.
But nothing happends.. Am I getting selecting the id="dc#" elements correctly?
If it helps with context I'm doing this for Dashing
Your SCSS doesn't make sense so I'd guess that your missing an error from the SCSS-to-CSS conversion. An & in SCSS is a reference to the parent selector:
& will be replaced with the parent selector as it appears in the CSS
so have &.up at the top level makes no sense and should generate an error. If we fix the SCSS so that .up and .down apply only to .dc:
.dc {
/* ... */
&.up {
background-color: green;
}
&.down {
background-color: red;
}
}
then everything seems to work just fine.
Demo: http://jsfiddle.net/ambiguous/9y9uywm9/
You can use Sassmeister (and other similar online tools) to see what SCSS thinks of your original SCSS.

linking two css classes in asp.net

I am new CSS styles and HTML.I have css file called layout.css in which we have a class called
.Grid {
width:100%;
direction:rtl
}
.tblsMonth {
direction :rtl
}
I have created another css file with class as below:
.textdirection {direction:ltr}
Can i make for .Grid and .tblsMonth direction property to read from my css file .textdirection
As far as I know, you can't combine them in the CSS, but you can in HTML:
direction.css
.textdirection {direction:ltr}
layout.css
.Grid {
width:100%;
}
.tblsMonth {
}
HTML
<div class"Grid textdirection"></div>