Components drifting apart following viewport adjustments

Can someone help me with this issue in responsiveness? When I change the viewport, my header and main elements seem to be moving far apart from each other. View the code on JSFiddle

<header>
  <div class="slider">
    <img src="picture1" alt=""> 
   <img src="picture2" alt="" >
  </div>
</header>

<main>
  <h1>Hello Hello</h1>
</main>

CSS

 body{
      margin:0;
    }

.slider{
  height: 36em; 
  width:100%;
  position: relative;
  overflow: hidden;
}

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

  main{
    margin-top: 0.1em;
   }

Answer №1

That's because a fixed height is being set

.slider{
  height: 36em; // <-- HERE
  width:100%;
  position: relative;
  overflow: hidden;
}

As the screen size decreases, the image also decreases, while the .slider maintains the same height

To fix this, remove the fixed height and change it to max-height. If the image needs to be positioned absolutely, you will have to calculate the slide's height manually using JavaScript

Jsfiddle

Javascript with jQuery

$(document).ready(function(){
    $(window).resize();
});

$(window).resize(function(){
    var height = $('.slider img').height();
    $('.slider').height(height);
});

CSS

body{
  margin:0;
}

.slider{
  max-height: 36em; 
  width:100%;
  position: relative;
  overflow: hidden;
}

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

main{
  margin-top: 0.1em;
}

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <div class="slider">
    <img src="http://www.freewalkertours.com/rio/wp-content/uploads/sites/2/2017/04/o-que-fazer-no-rio-de-janeiro-cristo.jpg" alt=""> 
   <img src="https://mmoexaminer.com/wp-content/uploads/2017/10/sw352356.jpg" alt="" >
  </div>
</header>

<main>
  <h1>Hello Hello</h1>
</main>

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

Bootstrap 4 Nav Table of Contents with Expand and Collapse Feature

Currently, I am encountering an issue with implementing a button to expand and collapse a "table of contents" in Bootstrap 4. The code I have so far can be viewed here: https://codepen.io/nht910/pen/RwwwyKB Code Snippet: <div class="main-wrapper col- ...

How can I make the burger icon responsive and centered in my navbar?

Struggling to center the burger icon in the navbar has been a bit of a challenge for me. Although it appears centered on small mobile devices, the icon shifts to the upper side on larger screens like tablets, losing its centered position. I am seeking advi ...

Is there a way to target a sibling element of another element by using its identifier in Cypress?

My current task involves clicking a checkbox within a list of table rows. The only way I can think of reaching this level is by targeting the span tag along with its corresponding name. cy.get('tr > td > span').contains('newCypressTes ...

Troubleshooting jQuery window scrolling and detecting element positions issue

I am utilizing the $(window).scroll(function() method to dynamically change classes on the navigation menu as different sections come into view on a website. Whenever a section becomes visible, the navigation class is updated to 'current'. $(win ...

Sticky positioning for dropdown menu in table column body

Greetings, I need some assistance with a formatting issue. My goal is to make the first and second columns of my table sticky by applying a "sticky" position to them. However, one of the columns contains a dropdown menu and the problem is that the content ...

What are the steps for integrating a CMS with my unique website design?

Currently, I am in the process of creating a unique website for a client using my own combination of html, css, and JavaScript. There is also a possibility that I may incorporate vueJS into the design. The client has expressed a desire to have the ability ...

React-tooltip and a challenge with Server-Side Rendering in Next.js

In my Next.js app, I make use of the react-tooltip library for tooltips. One peculiar issue that I have noticed is that whenever I refresh a page containing a tooltip, I encounter the following error: react-dom.development.js:88 Warning: Prop `dangerously ...

The call function in Tween.js fails to execute after adding an EventListener

I encountered an issue while using tween.0.6.2. The following code snippet (borrowed from the tween.js Getting Started page and slightly simplified) works perfectly: createjs.Tween.get(circle) .to({x: 400}, 1000, createjs.Ease.getPowInOut ...

Guide to animate the appearance of a div from a specific location

I have a div that smoothly slides out when a label is hovered over. HTML: <div class="cellDataViewTdmStatus divCell userSitesCol7"> <label class="lblViewTdmStatus">View Status</label> </div> <div id="tdmStatus" class="hid ...

The webpage lacked the functionality of smooth scrolling

I created a website which you can view here. Check out the code below: <div id="mainDiv" class="container"> <div id="header"> <div class="plc"> <h1><a href="#"></a></h1> ...

Implement feature to enable selection using jQuery

I have a question about implementing an <option value="fiat">Fiat</option>"> element into a dropdown list using jQuery. I've tried the code below and encountered some issues. When I click the button, I want to add the <option value= ...

How to add a background image in CSS with HTML's help

I am a beginner learning HTML and CSS, attempting to incorporate a Full Screen Background Image using CSS. Unfortunately, the code I have written is not working as expected despite following various tutorials. Below is my code: HTML file <!DOCTYPE htm ...

Leveraging both text content and element positioning with Selenium's getattribute() method

I am in the process of creating a test framework using Selenium for a website that includes an ADF generated Tree. The structure of the tree resembles the following: <table> <tr> <td> <div> <span> <a id="AN_ID" t ...

React Images: Carousel display issue with images not centered

Is there a way to center the selected image instead of it appearing on the left-hand side? Here is the current behavior: https://i.stack.imgur.com/SUWOK.jpg I am using the following packages in a sandbox environment with Next.js 11 and TailwindCSS 2.2.4: ...

Nuxt: Issue with Head function affecting title and meta tags

I need to customize the titles and meta descriptions of different pages on my Nuxt website. However, I am struggling to make it work using the head() method as indicated in the documentation. For example, in my contact.vue file: export default class Cont ...

What steps can I take to ensure that the upper and left sections of a modal dialog remain accessible even when the size is reduced to the point of overflow?

One issue I'm facing is with a fixed-size modal dialog where part of the content gets cut off and becomes inaccessible when the window shrinks to cause an overflow. More specifically, when the window is narrowed horizontally, the left side is cut off ...

What is the method for pulling information from MySQL and presenting it on a website using Node.js?

Currently, my goal is to retrieve data from MySQL and showcase it on my HTML page. The code snippet in server.js looks like this: const path = require('path'); const express = require('express'); const bodyParser = require("body-pa ...

"Enhance your website with a creative CSS3 effect: rotating backgrounds on

Greetings, fellow coders! #div1{ position: fixed; top: 50%; left: 50%; margin-left:-100px; margin-top: 420px; width: 158px; height: 158px; background-image:url(imag ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

Optimal guidelines for logging in

After successfully creating a website using HTML, CSS, and JavaScript, I am now looking to implement a login feature to provide privacy for certain users. I have noticed that some developers use PHP, while others use JS or Node.js for this purpose. However ...