Simple math operations in GWT CssResource

I need help achieving a similar result to the following:

// design.css
@define borderThickness '2px';

.format {
  width: borderThickness + 2;
  height: borderThickness + 2;
}

The desired output is for the width and height properties to equal 4px.

Answer №1

On occasions, I utilize the following approach:

@eval BORDER_SIZE_PLUS_2 2+2+"px"; /* GWT will compute this during compilation! */

Interestingly, it only seems to function properly if there are no spaces between the + operator and the operands. Furthermore, when using @eval, you cannot incorporate constants that were previously defined with @def. Nevertheless, you can make use of constants declared as static fields in one of your Java classes:

@eval BORDER_SIZE_PLUS_2 com.example.MyCssConstants.BORDER_SIZE+2+"px";

Alternatively, you have the option of letting the computation be handled entirely by Java:

@eval WIDTH com.example.MyCssCalculations.width(); /* static method, 
                                                      no arguments! */
@eval HEIGHT com.example.MyCssCalculations.height();
.style {
    width: WIDTH;
    height: HEIGHT;
}

However, my preference aligns closely with your idea:

@def BORDER_SIZE 2;
.style {
    width: value(BORDER_SIZE + 2, 'px'); /* unfortunately not feasible */
    height: value(BORDER_SIZE + 3, 'px');
}

I don't believe this is achievable in GWT 2.0. Perhaps you may discover a more optimal solution - refer to the Developer Guide page related to this subject.

Answer №2

There is limited support for this feature using the CSS calc() function in Mozilla browsers.

This code example was borrowed (with credit!) from a post on Ajaxian

/*
* Positioning two divs side by side with a 1em margin
*/
#a {
  width:75%;
  margin-right: 1em;
}
#b {
  width: -moz-calc(25% - 1em);
}

Although not fully supported across all browsers, progress is being made towards better compatibility in newer versions of Firefox.

Answer №3

Consider incorporating a parameter into your provider method to calculate the value:

@eval baseFontSize com.example.MyCssProvider.calculateBaseFontSize(0)+"px";

@eval baseFontSize_plus_1 com.example.MyCssProvider.calculateBaseFontSize(1)+"px";

The com.example.MyCssProvider implementation may resemble this:

public static int calculateBaseFontSize(int sizeToAdd) {
    if (true) {
        return 12 + sizeToAdd;
    }
    return baseFontSize;
}

Answer №4

I really like the concept, but unfortunately it's not currently feasible. Even with CSS 3, there are no plans for such a feature.

If you're determined to create something similar, one option is to utilize PHP and configure your web server to process .css files as PHP.

For example:

<?
  $paddingSize = 4;
?>

.style {
  width: <? paddingSize + 3 ?>px;
  height: <? paddingSize + 3 ?>px;
}

However, since this method isn't considered standard practice, I would advise against implementing it.

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

VueJS - apply a rotation animation before showing the element

I'm having trouble displaying a line in different directions. While vertical and horizontal lines work fine, the diagonal direction requires rotation before it appears correctly. The issue is that it initially displays vertically before rotating to 13 ...

Dynamic navigation menu with Bootstrap's responsive multi-level design

I recently developed a multi-level navigation menu using Bootstrap. Everything seems to be working smoothly when the screen size is 1200px or above. However, I encountered an issue with the second level dropdown menu not opening upon clicking on screens sm ...

Table within a table does not occupy full height within a table cell

I encountered an issue while nesting a table within a td element. The behavior differs between browsers: in Firefox, the nested table fills the entire cell from top to bottom, but in Edge and Chrome, it is centered within the td cell and does not occupy th ...

When the filter feGaussianBlur is applied to an SVG circle, it disappears on Safari but not on Chrome

While implementing the filter with feGaussianBlur to an SVG circle, I encountered a peculiar issue. It works perfectly on Chrome, but unfortunately, the circle disappears when viewed on Safari. https://i.sstatic.net/k916B.png https://i.sstatic.net/5Bfp9. ...

Tips for adjusting the width of a div when printing

Imagine I have two container elements named container1 and container2. First, I assign a width of 30% to container1 and 60% to container2. Then add a border between the two containers. However, when I attempt to print, container1 takes up the full 100% o ...

Struggling to dynamically include an identifier to a div element within a Rails helper function

I'm currently facing a bizarre issue. Here's how my view looks: <h1>All Deals</h1> <%= sanitize print_grouped_deals(@deals) %> This is what's in my deals_helper.rb file: def print_grouped_deals(grouped_deals_by_date ...

Is there a way I can implement pagination on a bootstrap table that has its header positioned on the left side?

Recently, I stumbled upon this table on Stack Overflow and I am curious to learn how to incorporate pagination for every 5 entries. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link ...

How do you efficiently include several elements within a single column in Boostrap 5?

I've been working with Bootstrap 5 to display a grid of images similar to this link, but the images are not displaying as intended due to some CSS code. #projects img { width: 100%; height: 100%; } <section id="projects"> ...

checkbox inspired by the design of the iPhone

I'm looking to create a custom checkbox! <label id="sliderLabel"> <input type="checkbox" /> <span id="slider"> <span id="sliderOn">SELECTED</span> <span id="sliderOff">SELECT</span> ...

Enable selecting text within a Bootstrap table

When I first encountered the issue, selecting text from my bootstrap table seemed like an impossible task. My initial attempts to address this involved focusing on CSS, and I managed to find a somewhat limited solution utilizing webkit-select-user Howe ...

swap out the main picture for a new one once the thumbnail is selected (gallery feature)

Hey everyone! I recently decided to create a personal portfolio website to showcase my new passion for photography. Everything is set up at the moment and you can check it out here. However, I've been struggling to get the image replacement to work ...

(Struggles with MUI v5) Finding it challenging to position a button in the top right corner

I attempted to position a MUI button in the top right corner, but for some reason, I couldn't move the button without using margin left and right. However, this is not an ideal solution for my project. import './NavigationPage.css' import * ...

Aligning content vertically when it is hovered over

After searching through numerous articles on Stack, I have yet to find a solution to my issue. I have successfully implemented content display upon hovering over an image. However, I am encountering difficulties in vertically centering the displayed conte ...

Tips for assigning width and height to an element with no content?

I need to adjust the dimensions of an empty element on my webpage. For a visual representation, you can view this jsfiddle link: http://jsfiddle.net/tzxv5zb3/ Currently, I have attempted to set the width and height using inline styling, like so: <spa ...

What causes the CSS Position Fixed Property to fail?

Can someone help me fix my navbar so that it remains fixed when I scroll? Currently, it moves up as I scroll down. Below are the source codes I'm using: * { margin: 0; padding: 0; box-sizing: border-box; } h1 { margin: 1rem; } ul { posit ...

Text in React Native exceeding one line is causing it to extend beyond the screen boundaries

I am having trouble displaying the last messages in my simple contact list as the text is getting cut off the screen. Below is the code I am using: (the readableTimestamp should be positioned behind the text.) View style={{ fontSize: 16, backgro ...

Missing inkbar in Material UI tabs when using ReactJS

For some reason, the small inkbar is not appearing under this tab. I suspect it might be a css issue that I can't seem to figure out. It's possible that I'm missing something I don't know about or maybe the height of the tab is too high ...

The div escapes the container and falls down to the one below it

I am encountering an issue with the layout of my outer container, which contains a column of numbers and animated text. The problem arises when the animated text, supposed to be beside the number column, drops under the numbers before bouncing back up as i ...

Unable to eliminate a persistent purple dashed section within the div

After gaining some knowledge of CSS and flexbox, I decided to create a Landing brochure site. However, I encountered an issue that I couldn't solve on my own during the building process. I'm attempting to eliminate the Purple dashed area by exten ...

What strategies can be used to avoid a single dangling word at the end of a line within an HTML element?

I am in the process of developing a straightforward webpage with marketing content. One aspect I find unfavorable is when a line of text stretches too long and breaks onto the next line, which is acceptable; however, it often splits in a manner where there ...