How to disable CSS in a specific partial in Rails

Currently, I am working on a Rails 3.2 application where I utilize a partial called _invoice_pdf.html.erb to showcase an invoice. This same partial is also used to generate an invoice PDF. However, there seems to be an issue with wicked pdf as it does not include the CSS when creating the PDF. Despite this, the appearance of the PDF is still acceptable.

My goal now is to disable the CSS when displaying the partial on the screen itself.

Does anyone know if there is a specific command or method to exclude the CSS from being applied to a particular view (partial)?

I truly appreciate any assistance provided!

Answer â„–1

If you want to achieve this using only HTML and CSS, keep in mind that the CSS method may not work properly on Internet Explorer and certain mobile browsers. To ensure full browser compatibility, consider adjusting your CSS code to individually override each property affecting your invoice.

Custom CSS Code

#custom_invoice {
  all: initial;
  * {
    all: unset;
  }
}

HTML Implementation

<div id="custom_invoice">
  <%= render "_invoice_pdf.html.erb"%>
</div>

Answer â„–2

Typically, CSS is not typically placed in partials; instead, it is usually included in the layout or view file that incorporates the partial. If needed, you can either omit the CSS in the specific view or selectively exclude it in the layout. For example:

<%= stylesheet_link_tag 'application' unless @conditional_flag_set %>

Answer â„–3

One way to address this issue in your application is by implementing a custom layout and CSS file specifically for this purpose. However, based on your feedback regarding Camden's response (where only the 'outer' elements require styling), this solution may not be optimal.

If that approach is not feasible, you could consider dynamically removing CSS using JavaScript within your partial. For example, you can eliminate an external CSS file using the following JS snippet:

$('link').remove(); // removes all <link> elements
$('link').not('.myCSSToKeep').remove(); // removes all <link> elements except those with the class='myCSSToKeep'

If manipulating styles via JavaScript is necessary, you can target specific classes and adjust properties accordingly:

$('.myClass').css('color', #fff); // etc.

In the scenario where the PDF styles have their own dedicated CSS file, incorporating them using the initial method should prove effective... at least if my interpretation of the requirements is correct :)!

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

Creating a banner using four grid elements, with each element containing four child elements, is a simple process that can

Can you assist me in recreating my idea from an image? I am having trouble understanding how to select and place other elements, and then return them to their original positions after deselecting... I attempted to use a grid but it did not work properly. ...

What could be causing the issue of images not loading in Chrome while they are loading perfectly in Mozilla when using CSS3?

Recently, I dove into a project utilizing react with webpack, and everything was smooth sailing until I encountered the CSS hurdle. Despite successfully configuring webpack, trouble brewed when it came to styling. Specifically, setting the background image ...

Most effective method for displaying 100 images on a single page?

I have a webpage on my site that displays 100 images, but loading them one at a time makes it look unattractive. Is there a way to make it more elegant and visually appealing? ...

Scroll to the end of the main div's horizontal position instead of using offset().left

I'm struggling with an issue in my code. <script type="text/javascript"> $(function() { $('ul.nav a').bind('click',function(event){ var $anchor = $(this); /* ...

The Media Query Menu is not aligned in the center for smaller screens, functioning in both medium and extra-small sizes

@import url('https://fonts.googleapis.com/css?family=Dosis'); * { box-sizing: border-box; } /*Xtra Small */ body { font-family: 'Dosis', sans-serif; line-height: 1.618em; font-size: 18px; background: #383231; c ...

Displaying background images as list items on a web page outside of

I am facing an issue with an unordered list within a div where the list item images are floating outside the div. Does anyone have a solution for this? You can see an example of the problem on Any assistance would be greatly appreciated! ...

What is the best way to incorporate a fixed-position button that spans the full width of the screen?

After trying numerous solutions on stackoverflow, I have yet to find one that works for me. a.buttongc { border-radius: 5px; background: #f5b220; color: #fff; font-size: 17px; height: 44px; line-height: 42px; color: #fff; text-decoration ...

Overlapping Bootstrap rows in small screens

I'm currently working on a frontend project using bootstrap 5, specifically utilizing the grid system to ensure responsiveness across different devices. However, I've encountered an issue in my code that I can't quite figure out. The probl ...

Having trouble loading the Active Record adapter for 'mysql2'

As I work on upgrading my app, I encountered an error during the deployment process: An issue arose with the 'mysql2' Active Record adapter. It seems that a required gem is missing. The error message states: can't activate mysql2 (< 0. ...

I'm experimenting with crafting a new color scheme using MUI, which will dynamically alter the background color of my card based on the API

I am attempting to create a function that will change the colors based on the type of Pokemon. However, I'm not sure how to go about it. Any suggestions or ideas!? Check out where I'm brainstorming this logic [This is where the color palette sh ...

Setting the margin to 0 auto to create a CSS Grid layout featuring an Image taking up the entire right side

I’ve been grappling with finding a consistent method to ensure uniform white space for all containers like this: https://i.stack.imgur.com/wtk0G.png Important: The grey rectangle signifies an image. My goal is for it to extend to the viewport's en ...

What is the best way to adjust the width of floating divs to completely fill the space they occupy?

On the first picture, there are six equal divs displayed. As the screen size increases, the width of the divs also grows to fill up their space, like a table cell or another div. If there is enough space in the first row to accommodate the fourth div, it s ...

This code is only functional on JSFiddle platform

I encountered an issue with my code recently. It seems to only work properly when tested on jsfiddle, and I can't figure out why it's not functioning correctly on codepen or when run from local files. Why is this code specific to jsfiddle? When ...

Integrate XML information into a webpage's table layout

I am currently working on integrating an XML file into my HTML and displaying it in a table. A snippet of the XML is provided below: <?xml version="1.0" encoding="UTF-8"?> <product category="DSLR" sub-category="Camera Bundles"> <id>0 ...

Does adding !important to the @font-face rule validate it?

Can anyone help me with a problem I'm facing? I want to ensure that users are forced to use the web font instead of the font installed on their computers because the formatting is slightly different. I've looked into using !important but I'm ...

Guide on making text stack up in response to changing the size of the browser window

Issue arises when resizing the browser window, causing the first text to break. The objective is to display the entire sentence without breaking words, regardless of window size. Attempts have been made with queries, but the correct solution remains elusiv ...

Encountering a challenge while attempting to adjust the navigation bar at the top as the user scrolls through the page

I've been working on making the navigation bar stay fixed at the top of the page when the user scrolls, but I'm running into some issues. It seems that certain elements are overlapping the navigation bar, causing them to hide the navigation bar a ...

Codignator does not attach the array elements together

While building a registration form for my website, I encountered an issue with passing single element values into an array in CodeIgniter. The values are not being passed correctly, resulting in an error message about an empty alert. How can I resolve th ...

LabeFor does not automatically create the display-lable class attribute

When setting up a new MVC project, the default Site.css file typically includes the following styles: /* Styles for editor and display helpers ----------------------------------------------------------*/ .display-label, .editor-label { font-weight: ...

Adding a scroll bar: A simple guide

My project includes hidden menus and submenus, but I'm having trouble accessing the last menu because there is no scroll bar. How can I add a scrollbar to my project? If you'd like to check out my project, click here. ...