Switch from using pixels to using rems by utilizing the search and replace feature in VS

I have an older CSS project that primarily uses px as the default unit of measurement.

Now, I am looking to revamp the project and need to change all instances of px to rem using the search and replace feature in VSCode. The conversion ratio is set at 10px = 1rem

In this conversion process, 20px should be transformed to 2rem. However, having it displayed as 2.0rem is also acceptable, as I can easily remove any trailing zeros afterwards.

If the measurement in pixels is less than 5px, then it should not be replaced.

Keep in mind that some rules may contain decimal points for pixel values, which can be omitted if needed.

  • Sample data
font-size: 10px;
border: 0px;
margin-top: 4px;    // dont touch
margin-bottom: 12px;
margin-right: 50%;
margin-left: -12px;
padding-left: 120px;
padding-right: 8px;
border-radius: calc(50% - 1px);   // dont touch
grid-template-columns: 1fr 44px 1fr;
... (remaining code unchanged)
  • Desired output after replacement
font-size: 1rem;
border: 0rem;
margin-top: 4px;    // dont touch
margin-bottom: 1.2rem;
margin-right: 50%;
margin-left: -1.2rem;
padding-left: 12rem;
padding-right: .8rem;
border-radius: calc(50% - 1px);   // dont touch
grid-template-columns: 1fr 4.4rem 1fr;
... (modified lines only shown above)

The attempted regex pattern so far has been:

\b(?![1234]px)(\d*)(\d)(?:\.(\d+))?px\b

Replacing with:

$1.$2$3rem

To test the regex pattern, visit this link

Answer №1

To optimize the process, consider using the . in the replacement along with the groupings $1.$2$3rem. You can then proceed to replace .0rem; with 0rem;

\b(?![1-4](?:\.\d+)?px)(?<!(?<!\S)\.)(\d*)(\d)(?:\.(\d+))?px\b

Explanation

  • \b Represents a word boundary
  • (?! Indicates a negative lookahead
    • [1-4](?:\.\d+)?px Matches a digit 1-4 followed by an optional decimal point and subsequent digits, ending with px
  • ) Closes the lookahead
  • (?<!(?<!\S)\.) Negative lookahead ensuring there is no preceding whitespace before the . to avoid partial matches
  • (\d*)(\d) Captures groups: optional digits followed by a single digit
  • (?:\.(\d+))? Optionally captures a decimal point followed by one or more digits
  • px\b Matches px followed by a word boundary

Check out the Regex demo for more information

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

jmpress: Scrolling through Absolutely Positioned Element (Slide) with dynamic height

Check out the Live Site I'm currently working on a website using jmpress, but I'm facing difficulties in implementing scrolling functionality for my content. While I can scroll in Chrome by selecting text and dragging down, I want to achieve thi ...

How can I make Firefox render the padding in a textarea the identical way as in a div?

In my efforts to ensure a consistent line width inside a textarea across IE8, Firefox, and Safari, I have encountered an issue where Firefox seems to add an extra pixel of padding compared to the other browsers. This results in text wrapping differently an ...

"Ensuring Your SVG Icon is Perfectly Centered: A Step

My SVG icon is currently aligned to the left, but I want to center it. I tried using the following code, but it doesn't seem to be working: .parent{ display: flex; align-items: center; font-size: 13px; } span{ margin-right:10px } https://i.sstatic.ne ...

Display a Loading Indicator within a Bootstrap 4 Modal Dialog

I have a question regarding Bootstrap 4 and a simple trick I am trying to implement. I am looking to display a Loading indicator similar to Twitter every time the user opens the modal. I have some code that is working well, but I seem to have misplaced som ...

Styling Descendants Conditionally in Angular 2

.parent { color: blue; } .parent * { color: red; } .child { color: black; } .grandchild { color: green; } <div class="parent"> Parent <div>Child <div>GrandChild</div> </div> </div> Imagine a scenari ...

How to design a bell curve using CSS styling

Exploring CSS shape design, I am aiming to create a classic bell shape reminiscent of the holiday season. While the top and bottom balls are not essential to my design, here is the basic outline I'm striving for: This is what I have been able to achi ...

Fixed width layout in Bootstrap 3 with a footer that spans the full width of the

Currently, I am utilizing Bootstrap 3 with a fixed width set-up. The footer on my site consists of two columns, each with a different background color. In order to ensure that the content in the footer aligns properly with the rest of the website, I want ...

The dropdown div does not activate when the image is clicked

When the image in the simple dropdown is clicked, it fails to activate the dropdown feature. This issue was encountered while working on a Shopify project, making it challenging to troubleshoot. To demonstrate the problem, I have re-created the scenario i ...

Is it possible to prevent the fade out of the image when hovering over the text after hovering over the div or image, causing the text to fade in?

Using React and CSS. I have set up my application to display a faded image on hover, with text that fades in over it. However, I am facing an issue where touching the text with my cursor removes the fade effect. Does anyone have a solution for preventing ...

The toggle function for the classList ('open') is functioning correctly and can be seen in the inspect element tool, however, the website is not displaying the associated style

How can I make the .nav show styles in .open after clicking #menu-icon? Note: I used Bootstrap to create the nav HTML <!-- logo --> <div class="logo"> <img src="assets/logo.png" alt="logo g's shop& ...

What is the best method for closing the open portion of an accordion menu?

I'm experimenting with creating a collapsible accordion feature, but I'm facing a challenge in figuring out how to close the currently open accordion pane. The accordion functions smoothly when expanding sections, but I'm stuck on how to col ...

Creating a stylish CSS shadow effect for a collection of elements

In my Ionic2 project, I have an <ion-list> consisting of various items. Currently, these items are plain, but I desire to add some styling such that each item in the list displays a subtle shadow effect, similar to the one shown in the first image of ...

Ways to emphasize the index navigation link when on the main page

Currently, there is a web design project that I am tackling and have encountered a slight hiccup that needs resolving. The issue revolves around highlighting different navigation links based on the URL of the current page. This functionality works seamless ...

Stellar.js is malfunctioning

I've been attempting to implement a parallax effect using Stellar.js with two image tag elements, but I'm encountering issues. Despite trying various configurations, including following the Stellar.js creator's tutorial scripts closely, noth ...

adjusting the space between lines for paragraphs with exceptionally large text

Utilizing Bootstrap allows for the adjustment of paragraph font sizes to appear quite large, mimicking the size and style of a heading without the need for an actual heading level HTML tag. I am hesitant to use a genuine <H2> tag to alter the visual ...

Issue with Bootstrap 4 navbar z-index not functioning as expected

Hello, I am struggling to place my navbar on top of an image using bootstrap 4. I have set the CSS properties for both elements, but it doesn't seem to be working. Here is the code I am using: <header> <nav class="navbar navbar-toggl ...

Can HTML tag attributes be accessed in CSS within the Shadow-DOM?

As I work on developing a custom component using StencilJS, I find myself needing to adjust the outline behavior when users navigate through it with either a keyboard or mouse. My component employs ShadowDOM and I aim to extract an HTML tag attribute from ...

How can I avoid using the appendTo method in jQuery?

There seems to be an issue in my HTML code, could you please take a look at the code snippet below: HTML: <div class="agent_select_wrap"> <select name="menu-114" class="wpcf7-form-control wpcf7-select" aria-invalid="false"> <op ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...

Leveraging SASS for injecting a variable into a mixin where the declaration is in string format

I have been developing a SASS mixin that allows for the input of various variables to construct a font-awesome declaration in the following format: @mixin after-font-awesome($unicode, $size, $margin, $color, $weight) { &:after { content:$unicode ...