Problem with Angular Slider

I'm in the process of creating a carousel component in Angular, but I'm facing an issue where the carousel is not appearing as it should. Below is the code for my carousel component.

carousel.component.html:

<div class="carousel">
  <div class="carousel-container">
    <div *ngFor="let image of images; let i = index" [ngClass]="{'slide': true, 'active-anim': currentSlideIndex === i}">
      <img [src]="getImageUrl(i)" alt="">
    </div>
    <button class="btn-carousel prev" (click)="prevSlide()">
      <img src="../../../../assets/icons/prev.svg" alt="Previous">
    </button>
    <button class="btn-carousel next" (click)="nextSlide()">
      <img src="../../../../assets/icons/next.svg" alt="Next">
    </button>
    <div class="container-dots">
      <div *ngFor="let image of images; let i = index" class="dot" [ngClass]="{'active': currentSlideIndex === i}" (click)="currentSlideIndex = i"></div>
    </div>
  </div>
</div>

carousel.component.css:

.carousel-container {
  max-width: 650px;
  height: 500px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  border-radius: 36px;
  margin-left: 85px;
  margin-right: 60px;
  width: 100%;
  float: left;
}

@media (max-width: 1199px) {
  .carousel-container {
    margin-right: 20px;
    margin-left: 20px;
  }
}

@media (max-width: 1000px) {
  .carousel-container {
    margin: 100px auto;
    width: 95%;
    float: none;
    max-width: none;
    align-items: center;
  }
}

.slide {
  width: 100%;
  height: 100%;
  position: absolute;
  opacity: 0;
  transition: opacity ease-in-out 0.4s;
}
.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.active-anim {
  opacity: 1;
}

.btn-carousel {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #f1f1f1;
  border: 1px solid rgba(34, 34, 34, 0.287);
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.btn-carousel img {
  width: 25px;
  height: 25px;
  pointer-events: none;
}

.prev {
  top: 50%;
  left: 20px;
  transform: translateY(-60%);
}

.next {
  top: 50%;
  right: 20px;
  transform: translateY(-60%);
}

.container-dots {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
}
.dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 3px solid #f1f1f1;
  margin: 0 5px;
  background: #f1f1f1;
}
.dot.active {
  background: rgb(32, 32, 32);
}

carousel.component.ts:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-carousel',
  templateUrl: './carousel.component.html',
  styleUrls: ['./carousel.component.css']
})
export class CarouselComponent {
  @Input() images: string[];
  currentSlideIndex = 0;

  nextSlide() {
    this.currentSlideIndex = (this.currentSlideIndex + 1) % this.images.length;
  }

  prevSlide() {
    this.currentSlideIndex = (this.currentSlideIndex - 1 + this.images.length) % this.images.length;
  }

  getImageUrl(index: number): string {
    return this.images[index];
  }
}

This is how I implement my carousel:

<app-carousel [images]="['../assets/images/img1.jpg', '../assets/images/img2.jpg', '../assets/images/img3.jpg']"></app-carousel>

Current appearance of my carousel:

Desired appearance of my carousel:

I've noticed that the layout and styling of my carousel are not meeting my expectations. Specifically, I aim to have the carousel with a fixed width of 650px, a height of 500px, and horizontally centered on the page. Despite applying these styles, the carousel is not displaying as intended.

If anyone could review my code and offer suggestions on how to address this issue, I would greatly appreciate it. Thank you!

Answer №1

I applied the :host selector to ensure that the slider always spans the full width and uses flexbox, allowing the child element to be centered using

align-items: center;justify-content: center;
. Additionally, it was necessary to define the dimensions of the initial component element .slider as flex in order for it to expand based on its contents.

Upon implementing the following CSS styles, the slider functionality appeared to function correctly!

.container-slider {
  position: relative;
  overflow: hidden;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  border-radius: 36px;
  margin-left: 85px;
  margin-right: 60px;
  width: 100%;
  float: left;
}

.slider {
  display: flex;
  flex-grow: 1;
  max-width: 650px;
  height: 500px;
}

:host {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

Experience the Stackblitz 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

The toggling feature seems to be malfunctioning as the div section fails to display

I'm facing an issue with my Django project while working on a template. I want to toggle the visibility of a div element between hiding and showing, but the function I used isn't working for some reason. I borrowed the function from a different t ...

What are some alternative ways to arrange this main navigation bar?

Check out the website, below the map you'll find an unordered list: HTML code: <ul class="play_navigation"> <li class="active"><a href="#" class="barino_story_bottom">The Story</a></li> <li><a href="#" ...

Tips for converting a string array constant into a union type

I have a string array that I want to use to create a new type where the properties correspond to the elements in the array. There are different types of arrays and I have a function that generates different output types based on the input array. const RG ...

What is the best way to insert fresh input values into a different element?

click here to view image description I am working with an input element where I take input values and store them in an element. However, I would like to know how to input new values into the next element. Any assistance would be greatly appreciated. Thank ...

Adjust the font size based on the dimensions of the container

I'm currently working on a script that dynamically sets the font-size based on the container's dimensions and the length of the text. This is what I have implemented so far. window.onload = function () { var defaultDimensions = 300; ...

The process of manipulating the content of an HTML textarea using Selenium with Python

http://neomx.iwedding.co.kr/roundcube I attempted to change the content of a textarea, specifically the tracking number. My goal was to replace "9400111206213835724810" with "487289527385922090". However, I encountered an error message stating "ElementNot ...

Ways to alter the CSS class of individual labels depending on the content of textContent

In my HTML template, I'm using jinja tags to dynamically create labels from a JSON object. The loop responsible for generating this content is detailed below. <div class="card mb-0"> {% for turn in response %} <div class="card-he ...

React components need to refresh after fetching data from an API

I am currently working on a React application using TypeScript and integrating JSONPlaceholder for simulating API calls. I have successfully set up everything I need, but I am encountering an issue with re-rendering components that display response data fr ...

Labels are overlapping each other and being aligned to the right side

Working with the most up-to-date version of Bootstrap, I am currently designing a search bar featuring a dropdown menu preceding the search button. Upon clicking the dropdown, various controls are revealed. Below is the HTML code for this configuration: ...

Explore the features of Angular4 including the router and optional children parameters

I am interested in creating paths like these: matches/:page/:team/:season with the flexibility of having :team and :season as optional parameters. For example, I would like to be able to use URLs such as: matches/results/4/2017 or matches/results/4 or ...

What is the correct way to trigger an event specified as a string parameter in the emit() function?

My current goal is to pass the emit name as a string (for example, 'showComponent') from child to parent. I then want to trigger another emit in the emitAction(callbackName: string) function, and finally execute the showComponent() function. I&a ...

Python Selenium: How to interact with an element nested within multiple HTML tags

Currently, I am working on automating a website using selenium webdriver in Python. However, I have encountered an issue when trying to click on a specific tag. The website that I am attempting to automate is quite old and contains numerous iframes and <& ...

After my jQuery functions are executed, my CSS rules are loaded

Struggling with the communication between CSS and jQuery, I find myself torn. In CSS, my rules are often very specific, like this: div#container > div#contentLeft { //Code here } However, when I add jQuery to spice up my site, the CSS rules seem to ...

Saving JSON data in a variable or array post subscription: What's the preferred method?

I have been receiving JSON files in the following format: {streetAddress: "Kosterlijand 20", postalCode: "3980", city: "Bunnik", country: "Netherlands"} Although the length of these files varies, the structure always remains the same: {key: "string valu ...

Could Typescript decorators be used as mixins?

In the process of developing a complex Angular2 application, I have created a base class that serves as the foundation for my components: export abstract class ReactiveComponent implements OnInit, OnDestroy, AfterViewInit { abstract ngOnInit(): void; ...

Scheduled task for sending emails using NodeJS

Currently working on my debut Node + Express app (MEAN) and seeking to integrate automatic email sending feature. Idea is for users to set reminders that the mailer will dispatch on specified dates. Considering incorporating the default Nodemailer + Node ...

The spacing in the Superfish menu is not aligned correctly

I am encountering a spacing issue with a superfish drop down menu. The problem can be observed here: To view the issue in action, visit this link: Essentially, all of the submenu items are floated left with a 50% width. I am looking for a solution to rem ...

Adding a div to the preceding div based on matching IDs in AngularJS

Here is a representation of my layout in HTML: <div ng-repeat="message in messages"> <div ng-class="{'messages messages--sent': userId == message.id, 'messages messages--received': userId != me ...

Having Issues with Cms Installation

I've been experimenting with a content management system that I plan to use for a project, but I encountered some errors. It seems like any "dynamic" elements are not displaying properly. Warning: mysqli::prepare() [mysqli.prepare]: (42S02/1146): Tab ...

Preventing the image from being displayed when it cannot fully show is achieved through blocking the background repeat

Is there a way in CSS to only display a background image if it can be fully shown without setting the width in pixels? I want to avoid showing partial background images. Is there a CSS property that can help with this? [] <div align="left" style="pad ...