Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important information on the platform change.

cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Variable in CSS Stylesheets

Variable in CSS Stylesheets

David08
Long-established Member

Hello,

Is it possible to use a general variable of HMI, that by means of an action when clicking on a widget modifies the value in HTML5 colour code and in the Stylesheets the value is modified and the colour is applied?
For example click on an image and write value in a string variable #F22, which is a colour in Hexadecimal and this variable read in a stylesheet and change the colour of the object.

Thanks.

4 REPLIES 4

webiq-sk
Frequent Contributor

In CSS you do not *change* definitions, you override them with additional CSS code. In WebIQ you usually use CSS Modifiers for that: https://www.smart-hmi.com/user/download/deliver/docs/documentation-manual-webiq-designer-2.15-ee64/i... 

If you really want to do dynamic inline styling of an HTML element (do you really need it to be totally flexible to use any of 16.7 million colors?) then you can simply write a LocalScript that sets the style of an HTML element directly with Vanilla JS: https://www.geeksforgeeks.org/how-to-change-the-color-of-html-element-in-javascript/

Please note that WebIQ does nothing with your CSS and JavaScript and sends it unmodified to the browser so you can do anything here that you can do on any other website.

Once again: on websites you don't modify CSS dynamically, you override existing CSS by a more specific or !important selector.

HmiGuide
Community Moderator
Community Moderator

If I understand the question correctly, CSS variables are a possible solution for your question. Whether this makes sense for your application, you must decide on the basis of webiq-sk's remark. We use this mechanism in the template for cartesian handling system  to make the colours of the widgets editable at runtime. (Light, dark & customer specific design)

Below some basic infos. For more info see: Using_CSS_custom_properties 

Css variables
- can be used in CSS
- be read and written via JS
- Names must start with "--" e.g. "--my-var"

CSS example with variable
.one {
background-color: var(--my-var);
}
// get variable
let sVal = getComputedStyle(document.documentElement).getPropertyValue("--my-var")

// set variable
document.documentElement.style.setProperty("--my-var", "#FFAA00");

HmiGuide
Community Moderator
Community Moderator

Example project version 2.15.7 attached

webiq-sk
Frequent Contributor

As pointed out by @HmiGuide you can of course use CSS variables, but still need to override the existing CSS selectors anyway, because WebIQ does not currently use CSS variables (which will change in 2.16 though).

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist