"Customize the appearance of your theme by adjusting the background and text colors when making a

Is there a way to dynamically change the style of a webpage based on user selection using CSS in AngularJS?

I've searched online, but haven't found a solution that works for me.

I have a CSS file with different styles and I want to apply these styles to various divs by changing background color, text color, and font style as needed.

    $scope.model.webPageSkin = 'Default';  

    // create a list of themes  
    $scope.bootstraps = [  
        { name: 'Plain', url: 'Plain' },  
        { name: 'Blue', url: 'Blue' },  
        { name: 'Green', url: 'Green' } , 
        { name: 'Default', url: 'default' } , 
    ]; 

**Plain.css**

.ChangeColor  
{  
    font-family: "Source Sans Pro",Calibri,Candara,Arial,sans-serif;  
    font-size: 15px;  
    line-height: 1.42857143;  
    color: #333333;  
    background-color: #ddd;  
}  


**default.css**

.ChangeColor  
{  
    font-family: "Source Sans Pro",Calibri,Candara,Arial,sans-serif;  
    font-size: 15px;  
    line-height: 1.42857143;  
    color: #333333;  
    background-color: #3c8dbc;  
}  

<div class="wrapper>
     <header class="main-header">
          <nav class="navbar navbar-static-top" ng-class="{ChangeColor: 
           model.webPageSkin !important;}">
          </nav>
     </header>

<div class="row">
<select  ng-model="model.webPageSkin" ng-options="bootstrap.url as bootstrap.name for bootstrap in bootstraps">  
                </select>
</div>
</div>

Any suggestions on how to achieve this dynamic style switching are welcome!

Answer №1

When using ng-class in AngularJS, it is important to remember that it evaluates an expression, not the CSS styling you may be trying to apply.

Check out the ngClass API documentation

To correctly use ng-class, make sure your markup specifies that you want the 'ChangeColor' css class applied when the model.webPageSkin evaluates to true.

ng-class="{'ChangeColor': model.webPageSkin}">

Answer №2

If you want to modify your code, try the following approach:

ng-class="{'ModifyStyle': model.pageSkin==='Standard'}">

The ng-class directive is effective with conditions that result in either true or false. When the condition evaluates to true, the specified class will be applied.

Answer №3

/*----Custom1.css---*/

body{
background-color:yellow;
}


/*----Custom2.css-----*/

body{
background-color:pink;
}

/*------Custom3.css------*/

body{
background-color:purple;
}

/*-----Custom4.css----*/

body{
background-color:orange;
}
 <!DOCTYPE html>
<html >
    <head>
        
        <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script >
            var app=angular.module('mainApp',[]);
            app.controller('myctrl',function($scope){ 
                 $scope.webPageSkin="Default"; 
          console.log($scope.webPageSkin);

    $scope.bootstraps = [  
        { name: 'Custom1', url: 'Custom1.css' },  
        { name: 'Custom2', url: 'Custom2.css' },  
        { name: 'Custom3', url: 'Custom3.css' } , 
        { name: 'Custom4', url: 'Custom4.css' } , 
    ]; 

            });
        </script>
        
    </head>
    <body ng-app="mainApp" ng-controller="myctrl">
        <div class="wrapper">
        
     <header class="main-header" ng-class="webPageSkin.name">
          <nav  class="navbar navbar-static-top">
          </nav>
     </header>

<div class="row">
<select ng-model="webPageSkin" ng-options="bootstrap.name for bootstrap in bootstraps" ng-init="webPageSkin = bootstraps[0]">
                </select>       
</div>
</div>

<link rel="stylesheet" type="text/css" ng-href="{{webPageSkin.url}}">
    </body>
</html>

Create four custom css files with different styling options and make any necessary edits. Your task is now completed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble Arising from the Lack of Coordination Between CSS Transition and JavaScript Update Triggered by Element

I'm currently working on a web development project that involves a list of clickable elements. When one of these elements is clicked, it should become active and trigger a CSS transition (such as a transform) with a duration of 200ms. Additionally, I ...

Is your webpage slow to respond after a page-refresh? (Delayed HTML rendering causing lag)

Whenever I adjust the screen size to that of a phone or any device smaller than 768px, the search bar doesn't display in its proper position until the page is refreshed. It should be correctly placed right from the start. Furthermore, when I resize th ...

Alphabetic divider for organizing lists in Ionic

I am currently working with a list in ionic that is fetched from a controller and stored in localStorage. My goal is to add alphabetic dividers to the list, but I am facing some confusion on how to achieve this. Here is a snippet of the code: app.js $ion ...

Center the image within a Grid item using Material UI's alignment feature

I am currently utilizing the Grid component from Material UI. I'm facing an issue where the image inside a Grid item is aligned to the left instead of being centered. <Grid container direction="row" justify="center" alignIte ...

Maximizing Bootstrap's Potential: Achieving Right Alignment for Divs

I am facing a challenge with aligning my div in the layout_cshtml. Currently, it is aligned to the left and the search bar and dropdownlist are not displaying properly. This negatively impacts the user experience. Additionally, my navigation menu is not ...

Tips for creating a responsive layout to ensure that H2 achieves its optimal font size in a one-line DIV

Is there a way to ensure that an H2 headline fits within the width of a DIV element? I am looking for a solution where the font size of the H2 automatically adjusts to display its full text without overflowing or breaking into a second line inside the DIV ...

Unable to include an additional parameter within a JavaScript function

I'm having trouble adding a parameter to the Openpopup() function - every time I try, I get an error. Can someone help me out with this? Thanks in advance. return "<a onclick='return Openpopup()' class='btn btn-info btn-lg clickBtn& ...

Set all form fields to ng-touched programmatically when the form is submitted

HTML: <div class="form-group" ng-class="{ 'has-error' : form.firstName.$invalid && form.firstName.$touched }"> <label for="firstName" class="control-label"> First Name </label> <input t ...

Tips for customizing WTForm fields with unique CSS classes

I've recently put together a basic form snippet: class PostForm(FlaskForm): # for publishing a new blog post title = StringField("Title", validators=[DataRequired()]) submit = SubmitField('Post') The structure of my HT ...

I wonder which Material Design repository is the source of this library

I am attempting to replicate the CSS style used in this particular Material Design-inspired web application: https://i.stack.imgur.com/utalx.png After exploring Materialize CSS and MUI, I have not been able to pinpoint the exact framework responsible for ...

What is the best way to add a darker tint to the background behind my overlay panel in jQuery

After grabbing the panel code from the jQuery Mobile website and integrating it into my file, I am interested in changing the appearance. I want to either darken or blur the background content when the panel is displayed. I attempted to use filter:blur(5px ...

Adding a line and text as a label to a rectangle in D3: A step-by-step guide

My current bar graph displays values for A, B, and C that fluctuate slightly in the data but follow a consistent trend, all being out of 100. https://i.stack.imgur.com/V8AWQ.png I'm facing issues adding lines with text to the center of each graph. A ...

AngularJS encountered an issue while uploading the file: The current request does not meet the multipart request requirements

I am facing an issue while trying to upload a file using AngularJS. The error message "The current request is not a multipart request" keeps showing up despite my attempts to find a solution online. I would really appreciate it if someone could provide ass ...

Troubleshooting a JQuery accordion malfunction within a table

I am populating the data dynamically. However, the Accordion feature is not functioning correctly. JSFiddle link http://jsfiddle.net/aff4vL5g/360/ Please note: I am unable to modify the HTML structure. Current table Desired output The first accordio ...

Is there a more efficient method for managing nested promises?

I am currently facing a challenge as a new developer. In the scenario I'm working on, there is a possibility of encountering a lock issue when the user saves their work. The user should be given an option to override the lock if it occurs. Since REST ...

Step-by-step guide to implementing a sticky header while scrolling

How can I implement a fixed header on scroll, like the one seen on this website: www.avauntmagazine.com Here is the HTML for my header: <div class="bloc bgc-wild-blue-yonder l-bloc " id="bloc-1"> <div class="container bloc-sm"> &l ...

Two interdependent select fields

I am currently working on creating two select fields where, upon selecting an option in the first field, some options in the second field should be hidden. I have almost achieved this, but my script is unable to locate certain options in the first select f ...

fewer security personnel leads to a higher success rate

I've tried searching for it online, but unfortunately, I couldn't find any helpful results. What I'm attempting to achieve is a mixin that can lighten or darken a color based on a given percentage. If the percentage is less than 0, then I w ...

AngularJS Service that handles several independent queries

I'm struggling with creating an AngularJS service that fetches data from multiple HTTP requests. Despite my efforts, I can't seem to get it working. The REST call flow is as follows: Get /index which returns an array of URLs Call each URL and ...

Struggling to align content vertically within an article and in need of some guidance

Still struggling with vertical alignment of content within an article. I've tried using tables and the vertical-align property but can't seem to make it work. Any suggestions for centering the content responsively on the right-hand side? The lef ...