Modify a variety of CSS styles based on boolean values

I am currently working on a web page that needs to adapt based on the local time of the user. Depending on whether it's morning, afternoon, or evening, I want the background image and color scheme to change accordingly. I have made some progress using document.getElementById();, but individual changes for each element are proving to be too time-consuming.

I've been wondering if there is a more efficient way to apply multiple CSS rules based on boolean conditions. I tried searching for something like @input css, but my search didn't yield any useful results.

Edit: To clarify, what I'm looking for is a concise method to adjust various CSS properties in different divs and IDs dynamically. For instance, changing the color: of #messagebased on the time of day.

Answer №1

By using JavaScript, you have the ability to assign various classes to your body element based on the time of day. These classes can then be utilized to style other elements on your webpage.

const hour = new Date().getHours();

let className = 'morning';

if (hour > 21) {
  className = 'night';
} else if (hour > 18) {
  className = 'evening';
} else if (hour > 12) {
  className = 'afternoon';
}

document.body.classList.add(className);
.bg {
  background: red;
  color: white;
}

.night .bg {
  background: black;
}

.evening .bg {
  background: orange;
}

.afternoon .bg {
  background: yellow;
}

.morning .bg {
  background: green;
}
<html>
<head>
</head>
<body>
  <div class="bg">
    <p>Lorem ipsum...</p>
  </div>
</body>
</html>

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

Obtain the value of "data-something" attribute from an <option> element within a <select> element

Can anyone help me with this HTML snippet? <select id="something"> <option value="1" data-something="true">Something True</option> <option value="2" data-something-else="false">Something Else False</option> </selec ...

npm installation for phonegap failed due to shasum check discrepancy

I've attempted numerous times, but I keep encountering this error (shasum check failed) 4784 error Error: shasum check failed for C:\Users\FENGXI~1\AppData\Local\Temp\npm-7004-QbpFFte5\1387269030233-0.28223602287471 ...

New ways to update Vue.js data without the need to bind HTML to methods

In my Vue.js application, I have a file where users can choose from different graphs by setting the activeOrderView variable through a @click event. Each graph has its own set of x-axis categories and y-axis values. The issue I am facing is that when a us ...

Strategies for selecting elements in jQuery that are displayed and not hidden

Here is the structure of my HTML code : {# Navigation for xs screens #} <div class="small-screen-nav hidden-lg hidden-md hidden-sm col-xs-12" style="padding: 0;"> <ol class="breadcrumb" style="padding: 0px 7.5px"> <li><a h ...

Exploring the world of DOM manipulation in AJAX websites?

Hello everyone, I am fairly new to developing extensions but I am eager to learn. I am currently working on creating an extension that specifically blocks certain Twitter icons. Through a helpful tutorial, I have been able to gather a lot of information. I ...

Searching for columns should be at the top of an angular datatable, not at the bottom

In my Angular 7 project, I am utilizing the library found at this link. I have followed the example provided, which can be seen here. Everything is working perfectly, except for the position of the search columns. I would like the search columns to appear ...

Error with JavaScript slideshow The slideshow using JavaScript seems to

I'm currently utilizing a script from the WOW Slider (free version) that looks like this: var slideIndex = 0; function showSlides() { var i; slides = document.getElementsByClassName("mySlides"); dots = document.getEle ...

What is the technique for creating a repeating image rather than stretching it?

Is there a way to repeat a background image to fit its content? I have a background image that needs to be repeated in this manner: https://i.sstatic.net/qVKkp.png However, when using the following code: .imgbord { background: url('https://i.imgur ...

How can the marionette controller object generate unique prototype methods that are dynamic?

Currently, I am working with Marionette to develop an application with multiple pages. The process of instantiating views and displaying them through the appRegion in each controller/router method feels repetitive. My goal is to streamline this process by ...

Interactive React Dropdown Component

As a relatively new React user, I am attempting to develop a custom component that will showcase a list of items within a select menu. The goal is to allow the user to make a selection from the menu and then click an "Add" button below it. Upon clicking th ...

Ways to verify that express middleware triggers an error when calling next(error)

I attempted to capture the error by using next() when stubbing it, but unfortunately it did not work. Below is the function: async getUser (req, res, next) { try { if (!req.user) { throw new CustomError('User not found', 404) } ...

Adding Firebase Authentication to my AngularJS website

I am currently creating a school website using AngularJS. My goal is to incorporate account functionality through firebase authentication. However, I have limited experience with Javascript and find working with AngularJS and implementing firebase to be ...

Eliminate the initial item in each list item

I am trying to delete the first element inside each li element, but my current code only removes the a element from the first li and not all li elements. For example: This is what I have: <ul class="myclass"> <li class="product-category produc ...

A guide to utilizing CSS to showcase the content of all four div elements in HTML body simultaneously

Is there a way to use CSS to display the content of all 4 divs in the same place on the HTML body, based on the URL clicked? Only one div should be displayed at a time in the center of the page. For example, if URL #1 is clicked, it should show the conten ...

Tips for Creating a Fixed-Width Element

I am struggling with creating text wrapping around a box inside a full width page template on Wordpress. The plugin developer suggested wrapping the text inside a fixed width element, but I am unsure how to do this. Any assistance would be greatly apprecia ...

Icon fonts are not compatible with AngularJS, causing difficulties in their usage together

Incorporating icon fonts from Icomoon in my AngularJS framework project has been a challenge. Due to Strict Contextual Escaping (SCE), I am unable to properly generate these icons, resulting in the display of HTML number codes like "&#xe006;" instead o ...

Why is my main.scss having trouble importing other files?

I am experiencing issues with importing my scss file. First, I created a file called main.scss and added some code to it. All the codes are functioning properly and showing up on the webpage. Next, I created two folders named settings and elements. In ...

How to dynamically alter a PHP variable using a JavaScript button click on the existing webpage

I've spent the last hour scouring stackoverflow for answers to my problem, but trying out solutions has only resulted in errors - perhaps because I'm attempting to implement it on the same page. I've experimented with saving the value to a ...

Stop receiving notifications for channel events in Stream with the GetStream.io Chat API

I'm having trouble understanding how to stop listening for channel events in JavaScript using the Stream Chat API. According to the documentation: // remove the handler for all "message.new" events on the channel channel.off("message.new", myChanne ...

Is the ng-style not displaying the background image with the interpolated value?

I have been attempting to update the background image of a div using angular ng-style. Below is the code I am working with: <div class="cover-image" ng-style="{'background-image' : 'url({{data.image}})'}"></div> However, ...