Customize Swiper js: How to adjust the Gap Size Between Slides using rem, em, or %?

Is there a way to adjust the spacing between slides in Swiper js using relative units such as 2rem?

The entire page is designed with relative units. All measurements are set in rem, which adjusts based on the viewport size for optimal adaptability.

For instance:

* {
   padding: 0;
   margin: 0;
   box-sizing: border-box;
   outline: none;
   font-size: calc(100vw / 1440 * 8);
}

.swiper-slide {
   display:flex;
   max-width: 33.33%
   align-items: center;
   justify-content: center;
   background-color: green;
   color:white;
   font-size: 4rem; (equivalent to 32px)
   padding: 2rem; (equivalent to 16px)
}

However, I encountered an issue with the space between the slides...

The documentation specifies that for spaceBetween: string | number, only a string value can be assigned via JavaScript. Setting a right margin using CSS does not work and the documentation advises against it.

I tried adjusting both the margin and removing it using nth-child for the first and last slide - but this did not yield the expected results. The gap property on the wrapper also did not work.

Even attempting to reassign it in JavaScript through a variable did not produce the desired outcome.

I am seeking assistance. Here is the sandbox - https://codepen.io/divside/full/eYPKNxN The objective is to maintain a consistent distance of 2rem between slides

Any ideas... whether it's gap/margin/padding/ rem in JS or variables... all suggestions are welcome.

Answer №1

Unfortunately, the spaceBetween property is only in pixels and cannot be set in rem.

One workaround could be to define a global constant in your JavaScript file that represents how many pixels one rem is equivalent to, then use that constant as a modifier in your code.

For example:

const rem = 16; // 1rem equals 16px

// continue with your code

spaceBetween: rem * 2,

// add more code

Check out the modified pen here: https://codepen.io/Crist-bal-D-az-lvarez/pen/abRKdyP

For more information, refer to the swiper documentation on spaceBetween:

An updated version of the pen that dynamically updates the value on window resize can be found here: https://codepen.io/Crist-bal-D-az-lvarez/pen/rNqKGWg

Answer №2

illustration one can assign margin: 0 1.2rem to the slide components and set margin: 0 -2.4rem for the swiper-wrapper element this configuration will ensure proper functioning

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

Encountering an error when utilizing Firefox version 35 with protractor

When running my Angular app scenarios with Chrome, everything runs smoothly. However, I encounter a halt when trying to run the scenarios on Firefox version 35.0b6. Any help would be greatly appreciated. Thank you in advance. I am currently using Protract ...

Is it possible to leverage the className attribute in Material UI components?

Currently using React alongside Material-UI(MUI) for the frontend development. Encountering an issue with the Button Component in MUI, specifically the className prop. Struggling to apply custom styles using classes defined in the style.css file. Conside ...

dojo.xhrGet evaluating script tags

I have a piece of code that is similar to the following: var targetElem = dojo.byId('xyz'); var xhrArgs = { url: 'Welcome.do?call=JS', preventCache: true, load: function(data){ targetElem.innerHTML = data; dojo.parser.par ...

Access the current page's URL using JavaScript

I have a unique URL structure that looks like this: index.html#page:page.php As I am loading my pages dynamically using AJAX, I want to create hotlinks to load specific pages. Currently, I am using the following function: function getdata(file){ $ ...

Adding a div with a canvas element is malfunctioning

I've been experimenting with using canvg to convert an SVG within a div into a canvas. So far, the conversion is working fine but when I try to copy the innerHTML of the div to another div, it's not functioning properly. The canvas is being gener ...

Has the user successfully authenticated with Google?

Possible Repetition: How to verify if a user is logged into their Google account using API? I'm working on a website that displays a Google Calendar. My query is: Can I determine whether a user is currently logged into a Google Account? If it&ap ...

Using ajax to submit a request to the controller

I'm currently developing an ASP.NET Core MVC application and have a registration page set up. My goal is to return View with errors if the model state is false: @model WebApplication2PROP.Entities.UserRegister @* For more information on enabling M ...

Nesting CSS classes allows for more specific targeting of

I am a bit rusty on CSS, it's been about 5-7 years since I last worked with it. Could someone help me come up with a solution to my problem? Here's the ideal design I have in mind: table.ctable { class:collapsible collapsed; } I know this syn ...

Tips for overlaying text on the background in Next.js:

I'm currently working on creating an image element with overlay text. Check out my jsx code below: <div className={styles.img}> <img src={src} alt="" /> <p>{`(${size})`}</p> </div> And here is t ...

What is the method to display HTML code using JavaScript within a span element in a JSP page?

While working on a jsp file, I came across a span tag with the following code: <span id="divBasicSearchResults" style="height:100%;"></span> Below is the Javascript function that generates HTML content for this span: function renderBasicSear ...

Creating dynamic stylesheets with the help of PHP and JavaScript

Generating tooltips based on a dynamically changing background image in CSS is my goal. Within my file named my_css.php, I have implemented the following code: <?php header('content-type: text/css'); $i = $_GET['index']; ...

Dynamically change the fill color of an SVG using styled-components

I've been attempting to target the fill property of my SVG using CSS in styled-components without any luck. Here is my SVG: <svg width="65" height="19" viewBox="0 0 65 19" fill="none" xmlns="http://www. ...

Establish starting dimensions and remember the previous sizes of allocations

I successfully implemented a draggable split panel using a code library from johnwalley on GitHub called https://github.com/johnwalley/allotment. My goal is to achieve the following functionalities: Clicking on Expand or collapse the allotment B should e ...

Using setInterval on a batch of freshly generated div elements

I am interested in creating a small website where I can display multiple clocks for various time zones. However, I have encountered an issue with the setInterval function. Below is the code snippet: function addClock () { $("#container").append('& ...

What is the method to retrieve the value of a JSON object when the key matches the value of another JSON object?

Looking to expand my knowledge in javascript with an interesting challenge. Is there a way to retrieve the value of an object using another object's value as the key? For example: Obj1 = {"name":"John", "age":30, "car":null}; Obj2 = {"John":{"count ...

Anticipate the completion of Subject callback execution

Take a look at the code snippet below: const mA = async () => { try { const subscription = myEmitter.subscribe(url => getD(url)); const la=()=>{...}; return la; } catch (error) { throw error; } }; ...

I keep receiving the same error (ENOENT) for all npm commands

Currently, I am running windows 8.1 x64 with all the latest updates installed. I encountered an error while using nodejs version 8.9.1 when running the command "npm -v". As a result, I decided to uninstall this version and switch to version 8.9.3. However ...

Ways to display notifications when the user is not actively browsing the website?

How can websites display notifications even when the user is not actively on the site? Take Facebook messenger, for instance. Even with the page closed, notifications still pop up. The same goes for Twitter, which also sends push notifications. I ...

Mastering the art of simultaneously running multiple animations

Utilizing two functions to apply a Fade In/Fade Out effect on an element. Confident in the functionality of both functions, as testing them sequentially in the Console proves their effectiveness. However, executing: fadeIn() fadeOut() seems to result in ...

Is it possible for a website administrator to view the modifications I have made to the CSS and HTML code?

Is it possible for a website to detect any temporary changes made to their CSS or Html through developer tools, even though these changes are not permanent? ...