When utilizing media queries, the div element struggles to adjust to the width of the browser

I need assistance in making the left box and the right box stack vertically when viewed on a screen width of 600px or less. I have been able to achieve this for the navigation bar, but I am facing issues with implementing the same for the boxes. I have tried using floats, widths, and max-widths without success. My desired outcome is to have the right box appear below the left box without any overlapping or overflow.

.topnav {
  background-color: pink;
  overflow: hidden;
  max-width: 80%;
  margin-left: auto;
  margin-right: auto;
}

.topnav a {
  float: left;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  font-weight: bold;
}

.sidebar {
  background-color: #fff5db;
  width: 20%;
  display: inline-block;
  box-sizing: border-box;
  float: left;
  position: absolute;
}

.links-box {
  background-color: none;
  width: 100%;
  border: 1px solid black;
  display: inline-block;
  box-sizing: border-box;
  float: left;
  padding: 10px;
}

.right-content {
  background-color: #fff5db;
  position: relative;
  left: 21%;
  max-width: 79%;
  position: absolute;
  right: 0px;
}

@media only screen and (max-width: 600px) {
  #logo {
    max-width: 100%;
  }
  .topnav {
    float: none;
    max-width: 100%;
  }
  .topnav a {
    float: left;
    width: 100%;
    padding: none;
  }
  .sidebar {
    max-width: 100%;
    width: 100vw;
    float: left;
    clear: both;
    box-sizing: content-box;
  }
  .introduction {
    max-width: 100%;
    width: 100%;
    float: left;
  }
  .right-content {
    max-width: 100%;
    width: 100%;
    float: left;
  }
}
<div class="topnav">
  <a href="">link1</a>
  <a href="">link2</a>
  <a href="">link3</a>
  <a href="">link4</a>
  <a href="">link5</a>
  <a href="">link6</a>
</div>


<div class="sidebar">
  <div class="links-box">left box</div>
</div>

<div class="right-content">right box</div>

(fiddle)

Answer №1

It is necessary to set position: static for both the elements .sidebar and .right-content within the media-query.

Note: I am uncertain of your end goal, but it appears that you may be complicating the position property unnecessarily.

Answer №2

After a series of trial and error, I found success by revamping my code to incorporate the 'flex' property along with 'justify-content: center'. This allowed me to effortlessly align the two boxes next to each other without the need for manual positioning using absolute values.

Implementing media queries following this adjustment proved to be a much smoother process.

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

Eliminate the gaps within CSS3 columns

I'm attempting to achieve an effect that looks like: C---------------------------------------) | Address 1 | Phone Numbers | | Address 2 | Times place is open | (---------------------------------------) However, the spacing with th ...

Enhance your textbox with more detailed descriptions than just displaying NaN

I am working on a form that includes select boxes. If a user selects an option from "Convert From" and another option from "Convert To" but does not enter a number in the input field, instead of displaying NaN in the result text box, I would like to show ...

Implementing CSS keyframes when a specified PHP condition is satisfied

I am looking to implement an opening animation on my website that should only play when a user visits the site for the first time. I want to avoid displaying the animation every time the page is reloaded, so it should only run if a cookie indicating the us ...

Setting up an image DIV for a dynamic overlay effect

Here is a JSFiddle that works correctly with fixed height and width: http://jsfiddle.net/69td39ha/2/. The animation activates correctly when hovered over. I attempted to adapt the above example to be responsive without fixed dimensions. However, I'm ...

Ensure the checkbox is aligned properly with its border and outline for a seamless user experience

Having trouble with my customized checkbox CSS. The outline and border aren't consistent when focusing or clicking. I attempted to apply the same properties to the focus pseudo class with no success. Below is the HTML and CSS code for my checkbox: HT ...

Opt for utilizing local data table files instead of relying on a content delivery network (CD

I'm in the process of minifying the CSS in my project and am looking to replace CDN files with offline files. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/> <link h ...

Hover effects can be applied to CSS tabs

There seems to be a CSS issue on Chrome where the background image is not hiding when clicking on next tabs. I have used the following code: .paymentBank ::after { content: " "; } You can view the live code [here][1] and I have also attached a scree ...

What is the best way to conceal a bootstrap directive tooltip in Vue.js for mobile users?

Currently, I'm tackling a project with Vuejs and Laravel. There's a tooltip directive incorporated that relies on Bootstrap's functionality. Check it out below: window.Vue.directive("tooltip", function(el, binding) { // console ...

Switch between input and the input is not displayed inline

I've been struggling to align the Toggle button with the input box for quite some time now. I really need them to be inline so that they form a grid properly as everything is dynamic. I have experimented with using a Div on its own instead of a label ...

Ways to conceal the label without using JavaScript when focusing

I am trying to find a way to hide the label "phone number" when the input is in focus by simply clicking on it. I have attempted using CSS but I need a more effective solution. Please let me know if you can help. <div class="form-row"> ...

Is there a way to bypass cells within an html table?

Creating an online board game and looking to have the cells go around the board. Wondering if anyone knows a way to skip the cells in the middle? Attempted using   but it doesn't seem to be effective. Here is a snippet of the code: #board { ...

What is the best way to switch the direction of the arrows based on the sorting order?

Is there a way to dynamically change the direction of arrows based on sorting, similar to the example shown here? sortingPipe.ts: import { SprBitType } from '../spr-bit-type/sprBitType'; import { Pipe, PipeTransform } from '@angular/core& ...

Text Alignment in a Responsive Design

Is there a way to create a responsive container that automatically centers all items inside it on the page, regardless of the screen size? Thanks :) Images are not included in this code snippet, but here's the code: HTML <!DOCTYPE html> <h ...

CSS code that causes the animated photo banner to reset instead of continuously looping back to the first

I followed a tutorial and created this banner using HTML and CSS. However, I encountered an issue where instead of the banner continuing in loops, it keeps resetting. Since we haven't covered JavaScript yet in university, I'm looking for help to ...

iPhone footer hides table content

Encountering issues on an iPhone while attempting to display a "timeline" consisting of a table content. The problem arises when 30 rows are present, but only 28 are visible on the screen. The remaining two rows are hidden behind the footer, requiring scro ...

Stylish CSS animation enhancing an image

Can a flash effect like the one in this image be created using only CSS? I attempted to use -webkit-filter, but it did not produce satisfactory results. https://i.sstatic.net/Eg49c.jpg ...

Loading Kendo JS on the client side proves to be rather time-consuming

Working on my project, I have encountered a problem with the kendo ui scheduler where the downloading of the kendo js and css files on the client side is causing slowness on our website. To address this issue, we are attempting to download the kendo js and ...

Is there a way to incorporate an animated element within the track of my circular progress bar?

My circular progress bar features two paths, with one path increasing in length as data is received, eventually turning the entire circle red. Here is the SVG HTML code: <path d="M 50,50 m 0,-47 a 47,47 0 1 1 0,94 a 47,47 0 1 1 0,-94" stroke="#A9B0B7" ...

What is the step-by-step process for executing the following functions: 1. magnify -> 2. spin 360 degrees -> 3. restore to original size before magnification -> 4. switch colors on and

Can you show me a demo featuring the following functions in order: 1. enlarge -> 2. rotate 360 degrees -> 3. resize to original size -> 4. change color (toggle) $(document).ready(function() { $('.tlist td div').click(function() { ...

Semantic HTML and unambiguous: both

We are making an effort to enhance the semantics of our HTML, and one element that stands out in our code related to presentation is <div class="clear"></div> For instance, if we consider the following semantic HTML structure: <div class= ...