Picture of Chuck Lorenz
How to test background colors using "_classes"
by Chuck Lorenz - Friday, 4 September 2015, 11:47 PM
 

Due to some recent questions about using the framework's "_classes" attribute, I thought it might be useful to have an example to reference. Please share your critiques, comments, questions, and especially your own techniques that help to clarify this essential theming strategy.

Here's a quick and practical way to test the use of CSS classes in the Adapt framework.

If you follow along, you'll set up a class that you can use to test colors as backgrounds in your Adapt course.

We'll start by setting up a Less variable for use within a CSS class. Then we'll define the class within the Adapt framework. We'll assign it to an article and see its effects. And finally we'll change the value of the test variable to see how we can experiment with other colors. This technique works with the first version of Adapt, but screen shots and files referenced in this example are from version 2.

1. With an Adapt course already set up and tested, locate within the Vanilla theme the file named colors.less. Use the file tree in the image for guidance, though the values in your colors.less will likely differ.

2. Less variables begin with @. Create a new variable for testing colors. The name is unimportant as long as it is distinctive to you. I chose @test.

3. Assign it a reference to a color in a format that will be recognized by Less. Two formats are depicted in the image: hexadecimal (or simply hex) and RGB. You certainly can use a color that already appears in colors.less. You should end up with something similar to these two models:
@test: #078292; or @test: rgb(98,193,191);

4. Next we'll create a class that will use this new variable and that we'll assign to an article very soon. Look at the second image for simple models of CSS classes. Two of them utilize Less within the style rules. Copy the third class, .color-test to a Less file of your choice. I recommend you use src/theme/adapt-contrib-vanilla/less/src/theme-extras.less. You can see in my examples that the same variable @test appears in two places. This is key. If you created a variable with a different name, be sure to substitute it for @test wherever it appears in the pictures.

5. Let's assign this class to an article. Locate and open src/course/en/articles.json. As depicted in the third image, assign the name of your class to one of the articles in its "_classes" property. Do not include the dot that precedes classes in CSS style sheets.

6. Now we can check its impact on the course. Build the course and start the server. This is typically done by running the following commands in the root of the framework directory using terminal or git bash or a command prompt.
grunt build
grunt server
Once the server is active, browse to your course and review the article to which you attached the color-test class. Does your background display a new color?

Some final points:
-  Test a new color simply by assigning a different hex code or RGB to your test variable in colors.less. Then restart the server.
-  If you use the command grunt dev to build the course and then start the server in a different terminal, you can see the results of your changes without having to restart the server. (Remember ctrl+c stops the server.)
-  Less permits the manipulation of variables with functions (http://lesscss.org/functions/#color-operations). The first picture demonstrates this:
@test: lighten(@blue, 35%);

Transparency can be achieved without using alpha in RGB: @test: fade(@blue, 50%);
-  You can leave this code in your files without interfering with your course.

I hope this helps folks who are just getting started with Adapt, and I hope others who are experienced will jump in to clarify any issues or errors.

Cheers!
Chuck

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Tuesday, 8 September 2015, 6:14 PM
 

Thanks for taking the time to write that thorough explanation, Chuck!

I've followed your instructions and have run into a couple issues and was wondering if you know why that might be.

1) When added to a contentObject, the color doesn't apply as I expect. It seems to add it behind the object itself, as seen here. The blue is my primary color, but I'm hoping to change that to the red color.

contentObjects

2) The same as above seems to be happening when I add it to a component. Also note that the entire page seems to have shifted to a left justification (see below)

Components

Here's how my code works:

- In my colors.less, I have a custom color @red: #d60037 and @innovativeBusiness: @red

- In my theme-extras.less, I have:

// Custom Classes
.arend-custom-class {
background-color: @innovativeBusiness;
color: @font-color;
float: left;
margin: 0 @item-padding 12px;
}

- To my contentObject and component, I added "arend-custom-class" to the "_classes" line.

 

Any idea why I'm not achieving the result I hoped for? Thanks!

 

Picture of Chuck Lorenz
Re: How to test background colors using "_classes"
by Chuck Lorenz - Tuesday, 8 September 2015, 7:33 PM
 

Hi Arend,

You're making progress. The custom-class I offered you, wasn't thought out in a way to produce a meaningful style. It was meant to indicate the variety of things that one could put into a class. BUT you've got it in place and now have something you can look at and trust that it is having an effect on your code.

Let's take care of the easy thing first: delete the line "float: left" It won't help you experiment right now; it will get in the way. There are a number of things that one can achieve with a basic knowledge of HTML and CSS, but other things require a little more understanding. You may be at the point where you need to step back and review CSS and/or Less in order to achieve what you want to do.

When you apply a class via the "_classes" attribute, it is applied to a single <div> and the source code determines which div, we do not. CSS/Less allows us to target that div and its children. All styles we throw into the class will be applied directly to the div. In your case, the red is being applied to <div class="menu-item"> (think of it as the root of the menu item). But the blue background is a property of its child, <div class="menu-item-inner">. You can discover this using Chrome's developer tools or other such free tools built into browsers. Your class needs to target the child div, and this is where Less comes in. Let's strip the class down to basics.

.arend-custom-class {
background-color: @innovativeBusiness; 
color: @font-color; 
}

Now let's target the child div:

.arend-custom-class {
    .menu-item-inner {
background-color: @innovativeBusiness; 
color: @font-color; }
}

If you want to target the menu title for a bold style, you'll have to use Less to step down yet another level:

.arend-custom-class {
    .menu-item-inner {
background-color: @innovativeBusiness; 
color: @font-color; 
.menu-item-title {
font-weight: @font-weight-bold;
}
}
}

(assuming your fonts.less includes that extra variable)

I'm writing quickly and haven't tested this class, but I think it's good. Try it out and report back. Let me know if I made a mistake and we'll take the next step together.

Chuck

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Tuesday, 8 September 2015, 9:23 PM
 

Awesome, that seemed to do the trick and it makes sense now. 

It's apparent to me that I need to study less and json a bit!

Picture of Chuck Lorenz
Re: How to test background colors using "_classes"
by Chuck Lorenz - Tuesday, 8 September 2015, 10:26 PM
 

FYI.

Notice I included "color: @font-color;" even though you were only exploring background color. If you are building a class focusing on a background-color, it often makes sense to specify the font color variable at the same time. The issue is contrast. Most often fonts are specified to be a shade of black/gray (@font-color). But if your new background-color is dark (navy blue), you may need your font to go white (@font-color-inverted).

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Wednesday, 9 September 2015, 3:01 PM
 

I actually figured that part out on my own :)

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Monday, 21 September 2015, 4:48 PM
 

Hi Chuck.

Coming back to this topic again. How might I customize the main header along the same lines to have it color-coded like I did with the articles? Am I right in assuming the main header is at the page level? Looking at my example screenshots above, I currently have the Innovative Business Models assigned a red color. I would like to have the main header when clicking into that section be the same, but I don't see anywhere I can assign my custom class to it.

Along the same lines, how might I add images to the header?

Thanks, as always!

Arend

Picture of Thomas Berger
Re: How to test background colors using "_classes"
by Thomas Berger - Thursday, 24 September 2015, 7:08 AM
 

Hello Arend,

to change the color of the main-header for certain pages, add a class to the contentObjects.json eg red-section. To style the main header for this section you would do something similar to this:

.red-section {
  .page-header {
    background-color: @your-color;
    ...
    more styles for this Element
  }
} 

 

Adding background images to the header is almost the same as adding a background color to it. If the Image you would like to use is located here: /course/en/images/bg-image.jpg your css rules to add a image would be this:

.page-header {
  background-image: url('../../course/en/images/bg-image.jpg');
}

Please keep in mind that this will add a background image to all of the page headers.

For a quick introduction about all the possible properties I recommend http://www.w3schools.com/css/css_background.asp

 

Thomas

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Thursday, 24 September 2015, 5:57 PM
 

Thanks Thomas! That helped me get the colors customized. I know a little CSS but I'm not all that experienced. What I'm struggling with now is adding an image to the header, though. 

I've got custom classes written in my theme-extras.less folder for each of my colors that I've added to my contentObjects. An example class looks like this:

.redTheme {
       .menu-item-inner {
       background-color: @redComponent;
       color: @font-color-inverted;
       }
       .page-header {
       background-image: url("course/en/images/disruptOr.png");
       background-position: left;
       background-repeat: no-repeat;
       background-color: @redComponent;
      }
}

However, I don't see my image anywhere when I compile. All I see is my header with the red color and my text.

I'm also a bit fuzzy as to working with the header in general. I've seen example courses, like this or this, which have what look to be transparent headers. I think for this particular project I'd like to keep the header colors but reduce the height a bit so all my objects are displayed a bit higher up when the learner first opens the course (I realize this is also dependent on the text I have in the header). I'm not sure where this is being controlled in the source code.

I'd also like to find a way to add custom images to the header on each contentObject, but I know you mentioned that as not possible with the way you suggested last. 

I am tremendously grateful for all the patient assistance the experts at the forum have given me! 

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Thursday, 24 September 2015, 6:31 PM
 

I'm also looking to add our logo to the navigation bar so it's on every page. Stumped there too.

Picture of Matt Leathes
Re: How to test background colors using "_classes"
by Matt Leathes - Friday, 25 September 2015, 10:29 AM
 

I generally do this by modifying navigation.hbs and/or navigation.less in the theme folder

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Friday, 25 September 2015, 3:10 PM
 

Hi Matt. 

So what is it I'm doing wrong when I try to place an image? How would the code look if I added it to navigation.less (as little as I know about editing .less, I know zilch about editing .hbs at this point).

I swear I'm going to figure this stuff out some day. 

Arend

Picture of Matt Leathes
Re: How to test background colors using "_classes"
by Matt Leathes - Friday, 25 September 2015, 7:36 PM
 

.hbs files are pretty simple, they're just little snippets of HTML that allow you to build up a page using small, manageable chunks of HTML - and more importantly allow you to pull JavaScript data into that HTML using curly braces.

The idea is to try and keep code to make stuff appear on the page out of JavaScript as much as possible so that JavaScript is just dealing with data, not the UI.

You edit navigation.hbs to include either an <img> tag to display your image, or a <div> that you can then add the image to using LESS. navigation.less is then used to style this.

So for example I might add this to navigation.hbs:

<div class="navigation-logo"></div>

And then this to navigation.less:

.navigation-logo {
    background-image: url(assets/nav_logo.png);
    background-position: center;
    background-repeat: no-repeat;
    width:100px;
    height: @navigation-height;
    float: left;
}

The above was taken from a v1 Adapt course BTW, I have no idea if the LESS variables are correct for v2 or not, sorry.

 

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Monday, 19 October 2015, 4:29 PM
 

Hi Matt.

I added my logo to the navigation bar using the navigation.hbs and navigation.less technique. However, I've got it centered and when the screen responds to the phone view the progress bar overlaps the logo.

Here is my code at the moment:

  .navigation-logo { background-image: url(assets/logo.png);
background-position: center;
background-repeat: no-repeat;
width:164px;
height: @navigation-height;
float: middle;
display: block;
margin-left:auto;
margin-right: auto;

 Ideally, I'd like to have it respond to the page width for the phone view so that the logo adjusts to the left at the right width. Any thoughts on what that code would look like? 

Thanks,

Arend

Picture of Chuck Lorenz
Re: How to test background colors using "_classes"
by Chuck Lorenz - Monday, 19 October 2015, 6:07 PM
 

Hi Arend,

Looks like you may want to employ a media query to handle the phone's smaller viewport. Because you are using Less, you can modify your code like this:

.navigation-logo { 
    background-image: url(assets/logo.png); 
    background-position: center;
    background-repeat: no-repeat;
    width:164px;
    height: @navigation-height;
    float: middle;
    display: block;
    margin-left:auto;
    margin-right: auto;
    @media all and (max-width: @device-width-small) {
        //CSS or Less style needed to move the logo where you want
    }
}

I haven't attempted to provide the actual style rule that might work with the media query bcause I don't know what your page looks like. Since the progress bar is usually on the right, you might try "float: left;" or you might try adding some padding-left to the logo.

Picture of Arend Raifsnider
Re: How to test background colors using "_classes"
by Arend Raifsnider - Monday, 19 October 2015, 7:22 PM
 

I had a hunch it would be with a media query. That worked great with float: left.

Thanks, as always!