Creating a dynamic slider with CSS and JavaScript

My slider functions correctly, but I want to add arrows underneath the blocks like in the image. How can I achieve this?

https://i.sstatic.net/TpMnw.png

The image shows arrows placed under the blocks. Here is the CSS:

body {
  margin: 0;
}

.slider {
  position: relative;
  overflow: hidden;
}

.slider__wrapper {
  display: flex;
  transition: transform 0.6s ease;
}

.slider__item {
  flex: 0 0 50%;
  max-width: 50%;
}

.slider__control {
  position: absolute;
  top: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  color: #fff;
  text-align: center;
  opacity: 0.5;
  height: 50px;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, .5);
}

.slider__control:hover,
.slider__control:focus {
  color: #fff;
  text-decoration: none;
  outline: 0;
  opacity: .9;
}

.slider__control_left {
  left: 0;
}

.slider__control_right {
  right: 0;
}

.slider__control::before {
  content: '';
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  width: 20px;
  height: 20px;
  background: transparent no-repeat center center;
  background-size: 100% 100%;
}

.slider__control_left::before {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E");
}

.slider__control_right::before {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3E%3C/svg%3E");
}

.slider__item>div {
  line-height: 250px;
  font-size: 100px;
  text-align: center;
}

HTML:

  <div class="slider">
    <div class="slider__wrapper">
      <div class="slider__item">
        <div style="height: 250px; background: orange;">1</div>
      </div>
      <div class="slider__item">
        <div style="height: 250px; background: green;">2</div>
      </div>
      <div class="slider__item">
        <div style="height: 250px; background: violet;">3</div>
      </div>
      <div class="slider__item">
        <div style="height: 250px; background: coral;">4</div>
      </div>
    </div>
    <a class="slider__control slider__control_left" href="#" role="button"></a>
    <a class="slider__control slider__control_right slider__control_show" href="#" role="button"></a>    
  </div>

I am trying to replicate the arrow design below the blocks in the image using only CSS. I have already implemented the slider functionality without adding JavaScript code. Can you help me with this?

Answer №1

Take a look at the revised design on this link: https://codepen.io/thecenteno/pen/BbobGN

After making some CSS updates, I also wrapped the controls in a container to enhance control over them. Key changes included:

/* Added: */

.slider__controls {
  display: flex;
  justify-content: center;
  flex-flow: row;
}
.slider__control {
  /* Added: */
  border-radius: 50%;

  /* Updated: */
  position: relative;
  top: 0;
  width: 50px;

  /* The Same: */
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  text-align: center;
  opacity: 0.5;
  height: 50px;
  background: rgba(0, 0, 0, .5);
  /*
    REMOVED: transform: translateY(-50%);
  */
}

You can now experiment with different styles starting from this foundation. Utilizing Flex works well especially when the elements are contained within a parent div.

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

Aligning the iframe within a div element

When I was trying to code an iframe that would resize with the screen, I encountered a problem where I couldn't center it. Despite attempting all the suggestions provided in THIS thread, I still couldn't get it to work. Can someone please point o ...

``Only Firefox supports jQuery animations, all other browsers fail to render them properly

This particular issue is a bit complex to articulate, so I'll begin by providing the code and then proceed to elaborate on the problem as best as I can. I'm assuming that you've already compared the results in Firefox and other browsers. He ...

Is there a way to apply toggling and styles to only the card that was clicked in Vue.js?

DisplayBooks.vue consists of a single page responsible for showcasing books fetched from a backend API. Each card on the UI features two buttons - ADD TO BAG and ADDED TO BAG. When a user clicks on the ADD TO BAG button of a specific card, it should toggle ...

Semantic UI Troubles: Unraveling the Sidebars and #pusher Defects

Hey there, I could really use some assistance with an issue I'm facing. I have this particular structure that's causing me trouble. <div id="toolbar" class="toolbar overlay-displace-top clearfix toolbar-processed"> ... </div> < ...

Retrieving Information From a Targeted div Identifier with PHP

Possible Duplicate: read XML tag id from php How can I retrieve data from a specific div ID using PHP? I want to extract data from a div with the ID of <div id="content">, so that all the content within that div can be stored in a variable. Alt ...

Issue with HTML form not updating MySQL database with PHP

Oh no, this PHP situation is really confusing me! I've been at it for a month now and this is the kind of challenge I thought I would face four weeks ago. Sigh! I keep getting an "Undefined index" warning, and the database is updating with a date of ...

What Could be the Reason Behind the Sharp Decline in My Google PageSpeed Insights Score?

Optimization On my desktop, I have successfully achieved a high page speed score of 96 on this site: https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fwww.usstoragecenters.com%2Fstorage-units%2Fca%2Falhambra%2F2500-w-hellman-ave&am ...

html/css: managing content overflow in web pages

While studying CSS, I created a sample page and encountered an issue where two of my divs were overflowing the content of the body, with the footer appearing above those divs. body { background-color: white; } #div1 { margin: 50px; text-align: jus ...

Arranging cells and input boxes in a table

Within my HTML document, I am currently working on one of three nested tables inside the <form> tag. The goal is to have input fields with text boxes aligned to the right of specific text columns. However, I am facing an issue where certain cells, li ...

Desktop sharing in HTML5/H.264 format for optimal performance and compatibility across all

Looking to showcase my desktop through live streaming over http to multiple users. The initial plan is to provide a real-time read-only view of the desktop for various users. In the future, we may consider granting users access to control the desktop using ...

Using jQuery and CSS to animate DOM elements hidden with display: none

I have a question regarding hiding elements on a webpage. When I set a div to display: none, the other elements in the DOM will abruptly move up to fill its space. Is there a way to animate this movement so that it transitions smoothly and gently instead ...

Designing a webpage layout with DIV elements that span across multiple rows

I am attempting to create a layout that features two columns, with the right column spanning two rows. I am looking to accomplish this using only DIV tags: +-----------+-----------+ + + + + + + +-----------+ ...

I'm not sure what's causing the issue with my image URL not functioning properly

Here is the path to the image I am trying to display: https://i.sstatic.net/Vn6SA.png This is the code snippet I have in my file: <div class="container-fluid"> <a class="navbar-brand" href="/"> <img src= ...

Challenges with CSS floating and centering in responsive design

My footer consists of 3 elements structured in the following way: <div class="footer"> <div class ="jumperMenu"> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> ...

Include specific javascript files only for smartphones

Recently, I encountered an issue with a JavaScript code that swaps background images on scroll down. To address the problem with debounce, I ended up setting different debounce times for various browsers (I am aware this is not the ideal solution, but it&a ...

Fixing the position of an HTML element to remain fixed on the screen

Currently, I am working on a responsive website that includes a menu button for mobile devices. Upon clicking this button, users are able to scroll through the menu content. To address the scrolling issue, I have utilized overflow: hidden; and position: fi ...

Encountering a problem within a Maven project during execution

Greetings, I am relatively new to maven and seeking some assistance. Recently, my Maven project has started displaying a 404 error on the browser when I attempt to run it. I initially created this Maven project using the webapp archetype and everything w ...

The required keys [:id] are not included in the route matches

As a beginner in Rails, I've encountered similar issues that I can't seem to resolve. Here are my routes: resources :users do resources :items end My model setup is as follows: class Item < ActiveRecord::Base belongs_to :user end cl ...

What is the best way to toggle DOM classes in React using Material-UI components?

Currently utilizing Material UI alongside React, I have a div element: <div className={classes.div}></div> I am attempting to dynamically add a conditional class to it: <div className={classes.div + divActive ? `${classes.div}__active` : &a ...

Error encountered in Angular CLI: Attempting to access property 'value' of an undefined variable

I am encountering an issue while trying to retrieve the values of radio buttons and store them in a MySql database. The error message I receive is TypeError: Cannot read property 'value' of undefined. This project involves the use of Angular and ...