Creating a responsive design for an absolutely positioned element

Currently, I am working on an image slider that features two arrow keys. The issue arises when I attempt to adjust the viewport to a smaller size. While the image slider responds by changing its size accordingly, the arrow keys do not follow suit. I am unsure of how to link them to the image slider in order to make them responsive. This problem became apparent when I switched from my monitor to a PC with a smaller resolution and noticed that the right arrow key disappeared.

Here is an example illustrating my dilemma: jsfiddle

<div class="container">
  <div class="slider"><img src="https://i.ytimg.com/vi/suIr-M1p8yU/maxresdefault.jpg" alt="">           </div>

  <div class="arrows index">
    <a href="#" class="left"><</a>
    <a href="#" class="right">></a>
  </div>
</div>



 .container{

  max-width:1100px;
  margin: 0 auto;
  width:100%;
  height: auto;
}
.slider{
  position:relative;
}

.slider img{
  position:absolute;
  width:100%;
  height:auto;
  left:0;
  top:0;
  object-fit:cover;
}

.arrows {
  font-size: 3.125em;
  position: relative;
  width: 100%;
}

.arrows .left {
  position: absolute;
  color: white;
  top: 8em;
  left: 0.4em;
  border: 0.02em solid #fefefe;
  border-radius: 0.14em;
  padding: 0em 0.4em 0.2em;
  background: #F2B8A2;
}

.arrows .right {
  position: absolute;
  color: white;
  top: 8em;
  left: 20.2em;
  border: 0.02em solid #fefefe;
  border-radius: 0.14em;
  padding: 0em 0.4em 0.2em;
  background: #F2B8A2;
}

Answer №1

Why have you set the left property of your right arrow to such a high value? It might work better if you use the right property instead:

.arrows .right {
  position: absolute;
  color: white;
  top: 8em;
  right: 0.4em;
  border: 0.02em solid #fefefe;
  border-radius: 0.14em;
  padding: 0em 0.4em 0.2em;
  background: #F2B8A2;
}

To adjust the vertical alignment, consider removing the absolute positioning of your image. This will ensure that your slider matches the height of your image. You can then position the arrows absolutely within the slider (with some changes in the HTML), which should resolve the issue.

Answer №2

It seems that the issue is caused by giving the arrow right a fixed left space. To fix this, I recommend changing left: 20.2em; to right: 0.4em, resulting in:

.arrows .right {
  position: absolute;
  color: white;
  top: 8em;
  right: 0.4em;
  border: 0.02em solid #fefefe;
  border-radius: 0.14em;
  padding: 0em 0.4em 0.2em;
  background: #F2B8A2;
}

I see that part of the issue was already addressed while I was writing my response about making the buttons stick with the image.

The CSS solution would be:

.container{
  max-width:1100px;
  margin: 0 auto;
  width:100%;
  height: auto;
}
.slider{
  position:relative;
  border: 1px solid black;
}

.slider img{
  /* position:absolute; updated */ 
  width:100%;
  height:auto;
  left:0;
  top:0;
  object-fit:cover;
}

.arrows {
  font-size: 3.125em;
  position: relative;
  width: 100%;
}

.arrows .left {
  position: absolute;
  color: white;
  bottom: 1em; //updated position
  left: 0.4em;
  border: 0.02em solid #fefefe;
  border-radius: 0.14em;
  padding: 0em 0.4em 0.2em;
  background: #F2B8A2;
  transition: 0.3s linear;
}

.arrows .right {
  position: absolute;
  color: white;
  bottom: 1em;//updated position
  right: 0.4em;
  border: 0.02em solid #fefefe;
  border-radius: 0.14em;
  padding: 0em 0.4em 0.2em;
  background: #F2B8A2;
  transition: 0.3s linear;
}

As for the HTML implementation:

<div class="slider"><img src="https://i.ytimg.com/vi/suIr-M1p8yU/maxresdefault.jpg" alt="">
      <div class="arrows index">
        <a href="#" class="left"></a>
        <a href="#" class="right"></a>
      </div>
</div>

By integrating the buttons into the slider and aligning them from the bottom instead of the top, you should be able to resolve the issue.

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

Unable to adjust the color of the font

I am trying to create a toggle effect for the colors of a fontawesome icon when clicking on a text link, but it only works with a button. Here is the HTML code snippet: <div class='post'> <i class='fas fa-heart' id='h ...

What happens to CSS specificity when JavaScript is used to change CSS styles?

When JavaScript modifies CSS, what is the specificity of the applied styles? For example: document.getElementById("demo").style.color = "red"; Would this be deemed as inline styling? ...

Enhance your picture links with a:hover box-shadow styling

I've been struggling to add a box shadow on hover to a Facebook icon image. I assumed it would be straightforward, but as always, I was wrong. I'm working with a child theme in WordPress because I recently started, thinking it would be an easy wa ...

Animation issue in Material UI autocomplete label feature

Hello, I am currently delving into the world of React and Material UI. I have been working on implementing the Material UI auto complete feature with chip functionality. You can see my progress here: https://codesandbox.io/s/runh6. Everything seems to be w ...

Tips for adjusting font sizes within the same HTML header?

Looking to design an HTML header in the following format: "(10.3.4) Version1: vs (10.3.4) Version2:" Need the version numbers to be smaller than "Version1" and "Version2." Any ideas on how to achieve this? ...

HTML and CSS for an off-canvas menu

Looking to create an off-canvas menu that smoothly pushes content out of view instead of cropping it. Additionally, I want to implement a feature that allows the menu to close when clicking outside of it. I found the code for the off-canvas menu on W3Scho ...

Tips for centering div content using bootstrap

https://i.sstatic.net/GtIi0.pngI am trying to align 3 divs in a row at the center of the page. <div class="row"> <div class="center"> <div class="col-sm-4" id="ph-container"></div> <div class="col-sm-4" id="do-contai ...

Unable to center div container with margin set to auto

I'm struggling to center my container on the website. My goal is to have it centered and take up about 80% of the page so I can incorporate an image slider and text inside. Using margin: 0 auto doesn't seem to be achieving what I want. The backgr ...

Animate in Angular using transform without requiring absolute positioning after the animation is completed

Attempting to incorporate some fancy animations into my project, but running into layout issues when using position: absolute for the animation with transform. export function SlideLeft() { return trigger('slideLeft', [ state('void&a ...

How can you turn off CSS3 animations for browsers that don't support them

Upon page load, a fade-in animation is applied to the main container. Although it functions properly in most browsers, it seems to have an issue in IE(9). Is there a method to identify if the user's browser does not support CSS3 animations and disabl ...

What is the best way to layer four images in a 2x2 grid over another image using CSS as the background

I am attempting to place 4 images of the same size on a 5th image defined as the background. Currently, it looks like this: It works perfectly when the height is fixed, but in my case, the height may vary causing this issue: This problem isn't surp ...

Perfectly aligning when the width is undetermined

When centering elements with dynamic widths, I typically use the code below: .horz-vert-center { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } However, a common issue I face is that the element resizes prematur ...

implement a night mode with a single click

I attempted to implement a dark mode using jQuery, but it is not working as expected. I am seeking assistance. I would like to enable the dark mode when clicking on the #btntheme ID, but for some reason, it is not functioning correctly. My goal is to be ...

Is there a way to make two parallelograms overlap seamlessly and adjust in size automatically using HTML?

Recently delving into the world of HTML and CSS, I've set my sights on designing something unique involving parallelograms that adjust dynamically. My vision includes four overlapping parallelograms starting from the top at 25%, 50%, 75%, and finally ...

Looking to spice up your jQuery modal dialog with unique and varied styles?

Can anyone explain why the grid displayed in our jQuery modal dialog (jqGrid) appears with different styles, specifically font size, compared to the grid on our main screen? I would appreciate any insights or suggestions. ...

Using jQuery to submit data via POST method without having the page reload

I have a link in the footer that, when clicked, jumps to the header with a # in the address bar. Is there a way to prevent this and stay in the footer? Here is the code snippet: <a href="#" class="clickIn" id="1" attrIn="Like"><span style="color ...

CSS - Discovering the reason behind the movement of text post generation (animation)

I have a question regarding CSS animation, specifically the typewriting effect. I was able to successfully achieve the typewriting effect using animation. However, I noticed that even though I did not set any animation for transforming, once the text is g ...

Choose only the li elements that have been specifically clicked on

As a beginner, I am working on a project where I need to select the specific li element that is clicked using jQuery. However, I am facing an issue where clicking on one li selects all of them at once. Here is the link to my code snippet: JS FIDDLE This ...

Attempting to gather data from an HTML form and perform calculations on it using JavaScript

Looking for help with extracting user input from HTML and performing mathematical operations in JavaScript. Coming from a Python background, the variable system in JavaScript is confusing to me. Can someone provide guidance on how to achieve this? <div ...

Integrate database values into your CSS by dynamically changing styles using PHP

I'm attempting to dynamically change my CSS. The CSS value I need is stored in a database table called goals in the order database, which contains id and goal_1. Here's the code snippet I'm working with: <?php $conn = mysqli_connect ...