including a media query within a mixin

After accumulating multiple CSS files, I've noticed many instances of:

@media all and (max-width: 400px), (orientation: portrait) {
    ....//Different styles relevant to each component in the respective file
}

Working with Stylus has been my method.
One day, my boss instructed me to modify that media query to:

@media all and (max-width: 400px), (max-height: 400px) {
    ....
}

Now, I have the task of scouring through all my files and implementing this new media query.

I'm wondering if there's a way to incorporate this media query into a mixin or another solution?

Answer №1

Referencing a question that was previously answered, I have a helpful workaround:

Within the variables.styl file

smartphoneWidth = 748px
tabletWidth     = 1012px

mqSmartphone = "only screen and (max-width: " + smartphoneWidth + ")"
mqTablet     = "only screen and (max-width: " + tabletWidth     + ")"

Then, in other stylus files, you can utilize:

@media mqSmartphone
    // Implement styles here

@media mqTablet
    // Implement styles here

Though not flawless, this approach may be considered more refined than using unquote()

Answer №2

When working with a stylus, it is possible to create unique block mixins for defining certain styles. One example that could be utilized is:

widerthan(var)
  condition = 'screen and (min-width: %s)' % var
  @media condition
    {block}

narrowerthan(var)
  condition = 'screen and (max-width: %s)' % var
  @media condition
    {block}

mobile = 748px

This can then be implemented in your code like so:

button
  +narrowerthan(mobile)
    flex-direction column

Answer №3

To work around Stylus's lack of support for interpolation or variables in media queries (or block arguments for mixins), you can use the following hack:

$my_query = "@media all and (max-width: 400px), (orientation: portrait) {"

unquote($my_query)
…
unquote("}")

Then, whenever you need to modify the query, simply update the $mu_query variable.

The only downside, aside from the less-than-ideal syntax, is that this method won't function properly with nested media queries.

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

Ensuring Consistent Height for a Responsive HTML Document Across Devices

My html page is facing a problem when printing from different Android devices. The page height changes dynamically based on label updates through JavaScript, and I pass this height to adjust the printed page (approximately 856x3000 pixels). However, issu ...

Ways to dynamically modify the CSS attributes of a webpage during the page load event

I'm looking to incorporate the background:rgba(226, 93, 141, 0.76) attribute into the body:after psuedo-element once my page has finished loading. Below is my HTML and CSS code: <style runat="server" id="myStyle" > body:after{ position:fix ...

Images that have been resized on Android appear blurry when viewed on the second page

Customizing the Toolbar: #main_bar { background: url(../images/logo.jpg) 99% 50% no-repeat; background-size: auto 80%; } The original size of logo.jpg is 220x266px The toolbar has a fixed height of 46px Therefore, the background image appears t ...

Problem with CSS layout on Internet Explorer 9

I've encountered some layout difficulties in IE9 and I'm struggling to identify the root of the problem. Even though I'm using Foundation 5, I don't believe that's the issue. The content is within a Foundation grid set at large-12 ...

Struggling to make background image behave as desired in CSS

After spending hours scouring forums, I still haven't been able to solve this issue. I am trying to make one of my divs serve as the background for my entire website. Additionally, I want it to start from a specific point and not cover the entire pag ...

What is the reason behind the necessity of having content inside div tags in order for the style to take effect

Upon analyzing the code provided, it was observed that if there is no content inserted between the tags as shown in <div id="header"></div, the corresponding style does not get applied. Is it mandatory to always have content placed between the t ...

Changing between two images using HTML and CSS

I am currently designing a WordPress theme and I would like to create an effect where two different thumbnail images switch on hover. The code that I have come up with so far looks something like this : <a class="thumb" href="posturl"> <img src= ...

What is the method to combine multiple style values in vue.js?

Is there a way to create a div with a box shadow where the values can be changed using a slider? I am having trouble using more than one value stored in a data variable. Any suggestions on how to achieve this? <div :style="{ borderRadius: x_axis y_ ...

Displaying text on hover and showing a text box when a link is clicked using CSS and JavaScript

Hovering over the text will display "Edit," and clicking on it will reveal a text box. This function will be used to update content using ajax. HTML: <table> <thead> <tr> <th> Name </th> <th> Age </th> ...

How can I integrate a timer into an Angular carousel feature?

I have put together a carousel based on a tutorial I found on a website. Check out the HTML code for my carousel below: <div class="row carousel" (mouseover)="mouseCheck()"> <!-- For the prev control button ...

Adjusting Navigation bar size according to the size of the screen

I am currently working on a project where I want the navbar (and logo) to shrink when users scroll down on larger screens where the mobile menu button is not visible (> 768px). On smaller screens, I need the navbar to remain at a fixed smaller size. De ...

Choosing an option will display a specific div while hiding another using JQuery

My question involves a Select Option <select class="input_select" name="customer_type" id="central_company"> <option value="standard">Standard</option> <option value="central">Central Station</option> </select> ...

Hidden footer error has been resolved despite background visibility

Greetings! This marks my debut post on this platform. I've encountered a minor setback with the current website project I am working on. As I have implemented a CSS parallax header, I aimed to create a distinctive footer as well. The concept was to ...

Tips for positioning an inner div within an outer div

Code Snippet: <div style="width: 300px; position: absolute; right: 25%; top: 5%; -webkit-box-shadow: 2px 4px 40px 0px rgba(0,0,0,0.75); -moz-box-shadow: 2px 4px 40px 0px rgba(0,0,0,0.75);box-shadow: 2px 4px 40px 0px rgba(0,0,0,0.75);"> <secti ...

Is it possible to modify the style using jQuery?

There's an issue with an error message that I need to handle in certain scenarios: <div id="PickerPopupNoResultsMessage" style="color:red; font-weight: bold; " class="ui-helper-hidden"> Please select Name first. </div> The error mess ...

Which specific element of CSS should be adjusted to ensure compatibility with Internet Explorer?

Struggling with my one-page website layout, I made some adjustments to have six sections in the HTML. Everything seemed to work fine on all browsers except for Internet Explorer. It kept redirecting me to a weird spot between the second last and last two s ...

Tips for deleting text on a webpage either prior to or following a certain character

Is there a way to remove everything after either 'am' or 'pm' in the date (the excerpt) on a WordPress site that has a list of headlines followed by a source attribution and a date? Example: NBA Finals: Warriors will face Cavaliers ye ...

Commence the element on a fresh line

I am looking to format the code below so that the .filter-containers are displayed next to each other in rows. If a .filter-container has a .header-container, it should start on a new line and the remaining .filter-containers will continue to sit next to i ...

The CSS3 Animation must come to a halt once it reaches the 75% mark

Is there a way to pause a CSS3 animation at 75% and prevent it from resetting to 0% when completed, or alternatively add a 10-second delay at 75%? Currently, the animation restarts at 0% once it reaches 100%. div { width: 100px; height: 100px; bac ...

Create CSS styles that are responsive to different screen sizes and

What is the best approach to ensure that these styles are responsive on any mobile device? nav ul ul { display: none; } nav ul li:hover > ul { display: block; } nav ul { background: #0066cc; background: linear-gradient(top, #0066cc 0%, #bbbbbb 10 ...