{"id":1950422,"date":"2021-07-30T09:40:35","date_gmt":"2021-07-30T13:40:35","guid":{"rendered":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/?post_type=tribe-knowledgebase&#038;p=1950422"},"modified":"2023-07-13T06:41:32","modified_gmt":"2023-07-13T10:41:32","slug":"css-custom-properties","status":"publish","type":"post","link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/css-custom-properties\/","title":{"rendered":"CSS Custom Properties"},"content":{"rendered":"\n<p>The Events Calendar styles elements with CSS, and those stylesheets are loaded in a particular order. Because CSS cares about how things are ordered, styles later in the order have greater weight.<\/p>\n\n\n\n<p>We do our best to load our stylesheets <em>after<\/em> the theme stylesheets. This way, the plugin&#8217;s styles are prioritized, preventing possible conflicts with stylesheets from the theme, or even another plugin. That said, there are some cases where we are unable to load the calendar&#8217;s stylesheets after the others.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" class=\"wp-image-1950423\" src=\"https:\/\/images.theeventscalendar.com\/kb\/uploads\/2021\/07\/css-stack.png\" alt=\"\" \/>\n<figcaption>That&#8217;s a lot of styles!<\/figcaption>\n<\/figure>\n\n\n\n<p>If the calendar&#8217;s stylesheets are loaded before the theme&#8217;s stylesheets and the styles conflict with each other, it&#8217;s still possible to load styles <em>after<\/em> ours, allowing you to fix those conflicts.<\/p>\n\n\n\n<p>As part of our effort to prevent style conflicts with themes, The Events Calendar uses CSS custom properties (also known as CSS variables).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-are-css-custom-properties\">What are CSS custom properties?<\/h2>\n\n\n\n<p>Custom properties allow us to define a variable and assign a value to it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n--tec-color-accent-primary: #334aff;\n<\/pre><\/div>\n\n\n<p>If the value of the variable updates, then that change is applied everywhere the variable is used in the CSS. Now we can use that variable anywhere we&#8217;d like, just as if it was a regular CSS property value:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n.tribe-common .tribe-common-c-svgicon {\n  color: var(--tec-color-accent-primary);\n}\n<\/pre><\/div>\n\n\n<p>This is the same as writing:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n.tribe-common .tribe-common-c-svgicon {\n  color: #334aff;\n}\n<\/pre><\/div>\n\n\n<p>The difference is that the custom property gives us flexibility to change values across the board without any additional development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-using-css-custom-properties\">Using CSS custom properties<\/h2>\n\n\n\n<p>If you are already <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/guide\/customization\/#customizing-styles\">overriding the calendar&#8217;s CSS with your own styles<\/a>, it probably looks something close to this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n.tribe-common .tribe-common-c-svgicon {\n  color: #000;\n}\n<\/pre><\/div>\n\n\n<p>Adding that snippet to the <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/k\/wordpress-customizer-2\/\">Additional CSS panel in the WordPress Customizer<\/a> overrides the plugin&#8217;s style with the custom property property value.<\/p>\n\n\n\n<p>Custom properties provide us with a more straightforward way to change that color value. Instead of locating the CSS class you want to update, then updating the color directly on the class, it&#8217;s possible to re-define the custom property value on the document <code>:root<\/code> element<sup><a id=\"fn1-return\" href=\"#Footnotes\">1<\/a><\/sup> instead.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n:root { --tec-color-accent-primary: #f00; }\n<\/pre><\/div>\n\n\n<p>Now, the value of the <code>--tec-color-accent-primary<\/code> custom variable is <code>#f00<\/code> instead of <code>#334aff<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-we-use-css-custom-properties\">Why we use CSS custom properties<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>As a site owner,<\/strong> this means you can add a touch of custom CSS to change The Events Calendar&#8217;s styles easily across the entire site, or target a specific instance.<\/li>\n<li><strong>As a theme creator, <\/strong>this means you can easily bundle styles that affect The Events Calendar&#8217;s styles to match your theme.<\/li>\n<li><strong>As a plugin author,<\/strong> this means you can <em>really<\/em> use the CSS in both directions, either overriding the base styles or incorporating them into your plugin.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-examples\">Examples<\/h2>\n\n\n\n<p>Let&#8217;s look at a few examples that use CSS custom properties to override The Events Calendar plugin styles.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-change-the-primary-button-color\">Change the primary button color<\/h3>\n\n\n\n<p>Let&#8217;s say you want to add a button to an event page<sup><a id=\"fn2-return\" href=\"#Footnotes\">2<\/a><\/sup>, and you want it to use the same color as The Events Calendar buttons (<code>#334aff<\/code>). So you add some custom CSS for your button in the Additional CSS panel of the WordPress Customizer, and in it, you set the background color:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n.button-class-name {\n  background-color: var(--tec-color-button-primary);\n}\n<\/pre><\/div>\n\n\n<p>Re-load the page and the plugin&#8217;s custom property will change the background color of the button to the same blue used throughout The Events Calendar.<\/p>\n\n\n\n<p>And if you want to change the color value of the custom property, then you could do that either by:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using the <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/k\/wordpress-customizer-2\/\">WordPress Customizer settings<\/a>.<\/li>\n<li>Changing the value in the Additional CSS panel of the WordPress Customizer.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n:root {\n  --tec-color-button-primary: #f8a100;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-calendar-s-custom-properties-to-style-a-custom-element\">Using the calendar&#8217;s custom properties to style a custom element<\/h3>\n\n\n\n<p>Let&#8217;s say you&#8217;ve created a button you are injecting next to the calendar export button in the calendar footer, and you want it to be styled just like the other buttons in The Events Calendar.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" class=\"wp-image-1950628\" src=\"https:\/\/images.theeventscalendar.com\/kb\/uploads\/2021\/08\/calendar-footer-export-button-custom.png\" alt=\"Showing an event for August 13 that includes the event information on the left and a photo of a smiling woman with headset looking at a computer screen. Below that is the calendar footer which includes pagination to view next and previous event. Below that are two buttons, one called My Custom Button and another called Export Events.\" \/><\/figure>\n\n\n\n<p>You could put the same CSS class on the button in your HTML:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;button class=&quot;tribe-common-c-btn&quot; name=&quot;custom-button&quot;&gt;My Custom Button&lt;\/button&gt;\n<\/pre><\/div>\n\n\n<p>But let&#8217;s say we want that custom button to be red instead of blue. We <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/guide\/customization\/#customizing-templates\">open up the calendar template file<\/a> in a code editor and add an inline style to the button HTML:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;button class=&quot;tribe-common-c-btn&quot; name=&quot;custom-button&quot; style=&quot;--tec-color-button-primary:red&quot;&gt;My Custom Button&lt;\/button&gt;\n<\/pre><\/div>\n\n\n<p>That&#8217;s right, we can use CSS custom properties directly in the HTML as an inline style, and still reassign the value of it. This way, the custom property is changed just on this one instance and all other buttons remain blue.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" class=\"wp-image-1950630\" src=\"https:\/\/images.theeventscalendar.com\/kb\/uploads\/2021\/08\/calendar-footer-export-button-custom-edited.png\" alt=\"The same image as before, but the button that says My Custom Button now has a red background.\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-changing-a-custom-property-value-with-javascript\">Changing a custom property value with JavaScript<\/h3>\n\n\n\n<p>One of the benefits of custom properties is that they can be changed by JavaScript running on the page. Let&#8217;s look at a somewhat silly example, but one that shows off the power of custom properties.<\/p>\n\n\n\n<p>Let&#8217;s say we are using the exact same HTML as the previous example, just without inline styles. We can use JavaScript to listen for that button to be clicked, then swap the custom property value with another value on that click:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ndocument.addEventListener(\n  &#039;click&#039;,\n  event =&gt; {\n    \/\/ If the clicked element doesn&#039;t have the right selector, bail\n    if ( ! event.target.matches(&quot;.tribe-common-c-btn&quot; ) ) { return; }\n\n  event.target.style.setProperty( &#039;--tec-color-button-primary&#039;, &#039;red&#039; );\n\n },\n  false\n);\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-footnotes\">Footnotes<\/h2>\n\n\n\n<ol class=\"has-text-color has-background wp-block-list\" style=\"background-color: var(--global-palette8); color: #141827;\">\n<li>The <code>:root<\/code> is the highest level in HTML. Assigning custom properties to the <code>:root<\/code> ensures that they are available and applied everywhere. <a href=\"#fn1-return\">\u2b91<\/a><\/li>\n<li>Remember. the styling that powers this doesn&#8217;t load on non-event pages, so if you are working with an element you plan to use on non-event pages, then avoid using the custom property since it can have an impact across other areas of your site. <a href=\"#fn2-return\">\u2b91<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>The Events Calendar styles elements with CSS, and those stylesheets are loaded in a particular order. Because CSS cares about how things are ordered, styles later in the order have greater weight. We do our best to load our stylesheets after the theme stylesheets. This way, the plugin&#8217;s styles are prioritized, preventing possible conflicts with&#8230;<\/p>\n","protected":false},"author":3,"featured_media":1955565,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_swpsp_post_exclude":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"ep_exclude_from_search":false,"footnotes":""},"categories":[116,24],"tags":[23,25],"stellar-product-taxonomy":[161],"class_list":["post-1950422","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css-styling","category-customizing","tag-css","tag-customizations","stellar-product-taxonomy-the-events-calendar"],"acf":[],"taxonomy_info":{"category":[{"value":116,"label":"CSS &amp; Styling"},{"value":24,"label":"Customizations"}],"post_tag":[{"value":23,"label":"CSS"},{"value":25,"label":"Customizations"}],"stellar-product-taxonomy":[{"value":161,"label":"The Events Calendar"}]},"featured_image_src_large":["https:\/\/images.theeventscalendar.com\/kb\/uploads\/2023\/02\/social-share-1024x538.png",1024,538,true],"author_info":{"display_name":"Jaime Marchwinski","author_link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/author\/jaimetri-be\/"},"comment_info":0,"category_info":[{"term_id":116,"name":"CSS &amp; Styling","slug":"css-styling","term_group":0,"term_taxonomy_id":116,"taxonomy":"category","description":"","parent":24,"count":33,"filter":"raw","term_order":"0","cat_ID":116,"category_count":33,"category_description":"","cat_name":"CSS &amp; Styling","category_nicename":"css-styling","category_parent":24},{"term_id":24,"name":"Customizations","slug":"customizing","term_group":0,"term_taxonomy_id":24,"taxonomy":"category","description":"","parent":0,"count":110,"filter":"raw","term_order":"0","cat_ID":24,"category_count":110,"category_description":"","cat_name":"Customizations","category_nicename":"customizing","category_parent":0}],"tag_info":[{"term_id":23,"name":"CSS","slug":"css","term_group":0,"term_taxonomy_id":23,"taxonomy":"post_tag","description":"","parent":20,"count":48,"filter":"raw","term_order":"0"},{"term_id":25,"name":"Customizations","slug":"customizations","term_group":0,"term_taxonomy_id":25,"taxonomy":"post_tag","description":"","parent":0,"count":177,"filter":"raw","term_order":"0"}],"_links":{"self":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1950422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=1950422"}],"version-history":[{"count":1,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1950422\/revisions"}],"predecessor-version":[{"id":1955926,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1950422\/revisions\/1955926"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media\/1955565"}],"wp:attachment":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=1950422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=1950422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=1950422"},{"taxonomy":"stellar-product-taxonomy","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/stellar-product-taxonomy?post=1950422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}