Unable to set margin-right on a relatively positioned element

In my Vue project, I am creating a dynamic card display that scrolls horizontally on mobile screens. This section functions as a testimonial carousel where users can swipe left or right to view different testimonials.

While I have successfully applied a left margin to the cards, I am facing an issue with adding a right margin to the final card so that it aligns at the center of the screen when scrolled.

You can preview the code sandbox here: . Please note that this layout is optimized for mobile but you may also notice the problem in desktop view.

The current problem occurs when scrolling all the way to the right, causing the white background of the last card to extend beyond the edge which is not the intended behavior.

<template>
  <div class="homePageTwo">
    <div class="cardHolder">
      <div class="cardSpace" v-for="card in cards" :key="card.index">
        <SlidingCard :title="card.title" :content="card.content" :icon="card.icon"/>
      </div>
    </div>
  </div>
</template>

<script>
import SlidingCard from "./SlidingCard.vue";

export default {
  name: "App",
  components: {
    SlidingCard
  },
  data() {
    return {
      cards: [
        {
          title: "Food Services",
          content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        },
        {
          title: "Assisted Living",
          content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        },
        {
          title: "Retail",
          content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        },
        {
          title: "Education",
          content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        }
      ]
    };
  }
};
</script>

<style>
.homePageTwo {
  height: 100vh;
  background-color: #f7f8fc;
  padding-top: 3rem;
}
.cardHolder {
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: mandatory;
  scroll-snap-type: x mandatory;
}
.cardSpace {
  padding: 2.5rem;
  background-color: #ffffff;
  margin-left: 1rem;
  margin-right: 1rem;
}
</style>
<template>
  <div class="slidingCard">
    <div class="photoHolder">
      <img class="homePageOneImg" :alt="alt" :src="icon">
    </div>
    <h1>{{ title }}</h1>
    <p>{{ content }}</p>
  </div>
</template>

<script>
export default {
  name: "SlidingCard",
  data() {
    return {};
  },
  props: ["title", "content", "icon", "alt"]
};
</script>

<style scoped>
.slidingCard {
  background-color: #ffffff !important;
  width: 60vw;
  display: inline-flex;
  flex-direction: column;
  position: relative;
  scroll-snap-align: center;
}
.photoHolder {
  height: 10rem;
  line-height: 10rem;
  border-radius: 90px;
  background-color: #f7f8fc;
  width: 8rem;
  margin: auto;
}
img {
  vertical-align: middle;
  height: 75px;
  width: 75px;
}
h1 {
  font-size: 18px;
  font-weight: bold;
}
p {
  font-size: 1rem;
  white-space: normal !important;
}
</style>

Answer №1

For more information on the concept of margin collapsing, please refer to this resource.

Here are a couple of straightforward solutions:

Solution 1: Include margin: 0 1rem; in the styles for cardHolder.

Solution 2: You can also add a hidden border after the last child element. You can find an example of this solution in this demo.

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

Using the `setTimeout` function to swap components

As I work on implementing a setTimeout method, the goal is to trigger an event after a .5 second delay when one of the three buttons (drip, french press, Aeropress) is pressed. This event will replace {{ShowText}} with {{ShowText2}}, which will display &ap ...

Modifying selections within a select box generated dynamically using JQuery

Looking for help on how to delegate to a static DOM element in my current situation. I need to create a dynamic select box .userDrop when .addNew is clicked, and then have the user select an option from #secDrop, triggering a change event that calls the da ...

Reflection effect on the ground in three.js

Is there a way to achieve the same effect without duplicating spheres? I attempted to replicate the functionality, but the reflections appear too large :/ var groundTexture = THREE.ImageUtils.loadTexture( "files/checkerboard.jpg" ); groundTexture.wrapS = ...

How to implement scroll spy in Bootstrap 4 to highlight parent li elements?

I have a bootstrap4 menu set up like this: <ul class="navbar-nav ml-auto"> <li class="nav-item"><a class="nav-link" href="#introduction">INTRODUCTION <span class="sr-only">(current)</span></a></li> </ul> Th ...

How can I apply innerHTML to each item in a forEach loop?

I need the innerHTML of each <a> tag to change, so that if it says <a>facebook</a>, it should be changed to <a><i class='fab fa-facebook'></i></a>. <main class="non"> <div class=" ...

Change CSS style sheets depending on the size of the viewport

I've tried to implement the method from CSS-Tricks that allows for changing stylesheets based on the viewport width using multiple .css files and jQuery. However, my code isn't functioning as expected. I've spent the past hour trying to figu ...

Video sections that adjust dynamically to fill the entire height and width of the screen without relying on the position:fixed property

My goal is to design a single-page website with multiple sections, each featuring a video that spans the height and width of the viewport (100vh and 100vw). These videos should stack on top of each other, filling the entire browser window. While I've ...

Creating Interactive Buttons with Dropdown Menus in Bootstrap

I am using a Bootstrap dropdown menu with various categories for users to select from. I want the 'Department' button to dynamically change to display the selected category after a user makes a selection. Below is the HTML code for the dropdown: ...

What is the specific order in which Vue.js components are created using the Created() method

When two components are loaded into a third component, what dictates the sequence in which the lifecycle methods for each component will execute? Main Component <template> <div> <component-a /> <component-b /> ...

Disable javascript when using an iPad

I'm working on a website with parallax scrolling that I don't want to function on iPads. Is there a way to prevent the parallax effect from happening when the site is accessed on an iPad? Any guidance on how to disable parallax scrolling based o ...

Unable to load the .js file within the Angular module

I am facing an issue with my Angular sidebar component that is trying to load a local script called sidebar.js. I have the following code in src\app\sidebar\sidebar.component.ts: ngAfterViewInit(): void { const s = document.createEleme ...

Questions and Answers - Expand/Collapse Feature for Accordions

I am currently working on enhancing an existing "FAQ" accordion page by implementing the expand/collapse feature to function seamlessly. I have successfully set up the page to begin with all sections collapsed, however, I am facing an issue where clicking ...

When the screen is minimized, my website doesn't align to the center as expected

When the screen is maximized everything looks great, but when I minimize it, the content doesn't center. I tried using margin: auto; in the "main-div" class, but then everything shifted to the top left corner. So, I added a "wrapper-div" to contain th ...

Assigning the base URL to an anchor tag's href attribute in Vue.js

In my Vue JS application, I have implemented a component that allows users to share links on Twitter and Facebook. Below is the code snippet specifically for the Twitter sharing option: <a href="http://twitter.com/share?text=Verbatim&url=https: ...

Access basePath within the Next.js AppRouter

Is there a way to access the basePath in Next.js 13 when using AppRouter? To retrieve it, we can simply get router.basePath through the useRouter hook of PageRouter by importing `import { useRouter } from 'next/router' I am currently unable to ...

The CSS hover functionality does not seem to be functioning properly within this React component

I have been working on a simple react component, and for some reason, the CSS hover effect that usually works perfectly in my stylesheets is not functioning within this specific component. I am struggling to identify the root cause of this issue. Any ins ...

Issues arise when using res.send() with ExpressJS and Mongoose

Initially, I have multiple callbacks that need to be executed before sending a res.send() to construct a JSON API: app.get('/api', function (req, res){ ... function logPagesId() { console.log("load: " +pagesId); c ...

Implementing Partial Login and Registration Views using AngularJS in conjunction with MVC5 and ASP.NET Identity

Embarking on the journey of creating a Single Page Application with log-in/register functionality using MVC5, ASP.NET Identity, and Angular feels like diving into a vast ocean of web development technologies. Despite being new to this realm, I delved into ...

Are there any JavaScript alternatives to Python's pass statement that simply performs no action?

Can anyone help me find a JavaScript alternative to the Python: pass statement that does not implement the function of the ... notation? Is there a similar feature in JavaScript? ...

Disappearing div: Using Angular 7 to hide a div element after a short

Having some trouble grasping how to handle this issue... There's a scroll animation on my page, and I'd like to smoothly hide the div after 3 seconds when it appears. The HTML code for the animated element is: <div class="scroll-animatio ...