Minimizing the use of the calc() function within a string variable

I'm attempting to accomplish this in LESS: @screen-lg : 1200px;

@laptop : ~"only screen and (min-width: 768px) and (max-width: @{calc(screen-lg - 1px)})";

I want to target until 1199px, but it doesn't seem to be working as expected.

Is it feasible to use the calc() function within a string variable?

Thank you.

Answer №1

I believe eliminating the need for calc is possible by using the following code:

1

@screen-lg: 1200px;
@screen-lg-max: (@screen-lg - 1);

@laptop: ~"only screen and (min-width: 768px) and (max-width: @{screen-lg-max})";

@media @laptop {
    color: red;
}

2

@screen-lg: 1200px;

@laptop: ~"only screen and (min-width: 768px) and (max-width:" (@screen-lg - 1) ~")";

@media @laptop {
    color: red;
}

3

(Less 1.7.0):

@screen-lg: 1200px;

.laptop(@styles) {
    @media screen and (min-width: 768px) and (max-width: (@screen-lg - 1)) {
        @styles();
    }
}

.laptop({
    color: red;
});

The CSS output of all three methods will be:

@media only screen and (min-width: 768px) and (max-width: 1199px) {
  color: red;
}

Answer №2

When it comes to handling media queries and compiling less at build time, the process involves evaluating them at runtime. In order to achieve this, utilizing client-side Javascript is essential. For more information on how to implement this, check out . By recompiling the less script each time there's a window resize, the task can be completed efficiently with Javascript rather than using less.

If you decide to pursue this approach, it may require wrapping your less variable inside a media query in reverse. However, the feasibility of such a method remains uncertain.

Answer №3

With Less being processed during the compilation stage, you can simply utilize the pre-existing mathematical functionalities without the requirement of using calc. The following code snippet demonstrates this concept effectively:

@screen-lg : 1200px;
@laptopsize: @screen-lg - 1px;

@laptop: ~"only screen and (min-width: 768px) and (max-width: @{laptopsize})";

@media @laptop {
    div{background: black;}
}

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

Centering Tabs in React-Bootstrap

By default, Bootstrap-react's tabs align to the left. <Tabs defaultActiveKey={2} id="uncontrolled-tab-example"> <Tab eventKey={1} title="Tab 1"> Tab 1 content </Tab> <Tab eventKey={2} title="Tab ...

Menu secured in place within the wrapper

My website is contained in a wrapper with a max width, and I have a fixed side menu that can be toggled with a button. The problem I am facing is keeping the fixed side menu within the page wrapper. Fixed elements are typically positioned relative to the ...

Adjust the source of the HTML5 video tag based on the orientation of an iPad

I am currently coding an HTML5 page specifically for iPad Mobile Safari, which includes a video tag for embedding video files. I have successfully implemented CSS3 media queries to determine the orientation of the iPad. My next challenge is to dynamically ...

html5 aside along with navigation

Check out this snippet of my html code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>European Networking</title> <link href="css/style.css" rel="stylesheet" type="text/css"> <link href="css/fonts ...

The secondary linked stylesheet is not affecting the layout

I'm encountering an issue with linking a second stylesheet to my HTML document, and I'm struggling to identify the (hopefully glaringly obvious) problem. My method of linking stylesheets in the head is as follows: <!DOCTYPE HTML PUBLIC "-//W ...

Styling an unordered list with CSS in HTML is a powerful

I am working with an unordered list where each list element contains two spans: span A and span B. My goal is to style these elements so that they are displayed horizontally on the screen, with span A above span B in each set. Example: spanAItem1 sp ...

Tips for maintaining visibility of the selected option within a dropdown menu

When I choose one item from a dropdown list of 3, the selected item is still visible in the dropdown along with the other two. I would like all three items to be displayed even if one is selected. reference dropdown Based on the reference image, even thou ...

Finding all instances of a specific class in Sublime Text 2

Is there a way in Sublime Text 2 to search for every instance of a specific class applied to an element? For example, if I have 20 .jsp's and I want to find every element with the class "sample," is there a method to do this beyond just searching for ...

What is the best way to remove a border piece with CSS?

Currently, I'm attempting to achieve a matrix effect purely through HTML and CSS. One method I have come across involves applying a solid border and then removing certain parts at the top and bottom. Does anyone know if it's possible to create th ...

Adjusting the size of a neighboring div to match the width of the audio control component

Looking for a solution with CSS styling: I have a parent div element and two nested div elements, one containing an audio control of unknown length. My query is how to make the first nested div adjust its width based on the audio control. The goal is for ...

Can you provide step-by-step instructions on how to customize styles with MUI in my application?

I am encountering issues with MUI while attempting to customize the color of my buttons. The two methods I have tried so far have been unsuccessful. For example, Method 1: import React from 'react' import { Typography } from '@mui/material& ...

Is it possible to change the button class within a div while ensuring only the last one retains the change?

Here is a code snippet I'm using to switch between classes for buttons: $('button').on('click', function(){ var btn=$(this); if(btn.attr('class')=='tct-button'){ btn.removeClass('tct-button ...

The JavaScript code that functions properly in Codepen is not functioning on my HTML page

Can you help me understand why this code is working fine in CodePen, but not on my HTML page? I'm unable to identify the error that may have occurred. Any assistance would be greatly appreciated! I suspect there's an issue with connecting some jQ ...

Ensure a consistent number of draggable elements in HTML/JavaScript

Below is the HTML/JS code snippet: <!DOCTYPE HTML> <html> <head> <script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.targ ...

Align images to the left in Typora for a cleaner look

Typora typically aligns all images in the center by default. https://i.stack.imgur.com/TrqIM.png In my search through the github.css, I was unable to locate any instances of img. $ grep img "/Users/me/Library/Application Support/abnerworks.Typora/themes ...

Is there a way for the remaining divs in a column to match the width of the widest div?

I'm attempting to ensure that the divs in a column take the width of the widest element, with the exception of the top div which should span 100% of the screen. How can this particular layout be achieved? Please refer to the image below for a clear ex ...

The resource was recognized as a stylesheet but was delivered with the MIME type text/plain in golang

I am encountering an issue while developing my webpage using golang. server file (main.go): package main import ( "net/http" "io/ioutil" "strings" "log" ) type MyHandler struct { } func (this *MyHandler) ServeHTTP(w http.ResponseWriter, ...

Jquery panoramic slider - Dynamically adjusting image width to fit screen size

Currently, I am attempting to implement a popular panning slider called Jquery Panning Slider for a website. The details regarding the markup structure, CSS, and jQuery can be found here: Slider Details. However, I encountered an issue with this slider. ...

Deactivate button using Javascript

Can anyone assist me with this issue I am having? I currently have a button set up as follows: <input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/> I need to disable the button using jQuery, standard javascript ...

What is the best way to show a div after successfully sending a post value using AJAX?

How do I show this specific div after a successful AJAX post? This is what I want to display: <div class="love" id="love_0" style="border-radius: 3px; padding: 8px; border: 1px solid #ccc; right: 13px; background: #fff; top: 13px;"> <a class ...