Responsive design involves ensuring that web elements such as divs are properly aligned

I am currently working on aligning 2 divs in a specific way that is responsive. I would like the right div to stack on top of the left div when the screen width reaches a certain point, as opposed to them both taking up 50% of the container's width.

The form is located on the left side while the content is on the right. My goal is to have the content display on top of the form to better fit a mobile screen width. The current setup works well for desktop and laptop screens, but not for smaller devices.

Can this be achieved with CSS alone or will I need JQuery to accomplish this? Please inform me if you need further clarification.

Here is the CSS code:

#left {
float:left;
width:65%;
border-radius: 25px;
background-color: #F8F8F8 !important;   
}

#right {
float:right;
width:35%;
background-color: white !important;
padding-left: 40px;
}

Answer №1

From my understanding, the JSFiddle linked below demonstrates exactly what you are looking for.

http://jsfiddle.net/username/example/2/ (A big thank you to Username for resolving the CSS issue)

Here is a method to achieve this using pure CSS and @media queries:

#left {
    float: left;
    border: 1px solid black;
    width:calc(65% - 26px);
    border-radius: 25px;
    background-color: #F8F8F8 !important;
}
#right {
    border: 1px solid black;
    float: right;
    width:calc(35% - 40px); 
    background-color: white!important; 
    padding-left: 40px; 
} 
@media screen and (max-width:500px) {
    #left { 
        display:block; 
        width:100%; 
    } 
    #right { 
        display:block; 
        width:100%;
   }
} 

Answer №2

One way to achieve this is by utilizing media queries to determine the screen width at which you want the stacking effect to take place.

For example, if you decide on a width of 768px, you can remove the float properties and set the widths to 100%. By structuring your HTML in such a way that the content for the right section appears before the content for the left section, it will naturally display above the left side when rendered.

Answer №3

#wrapper {
  display: flex;
}

#top {
  order: 1;
  width:60%;
  border-radius: 20px;
  background-color: #EAEAEA !important;   
}

#bottom {
  order: 2;
  width:40%;
  background-color: #FFFFFF !important;
  padding-left: 30px;
}

@media only screen and (max-width: 767px) {
  #top, #bottom { width: 100%; }
  #top { order: 2; }
  #bottom { order: 1; }
}
 

If you are not concerned about supporting IE9 and earlier versions, you can utilize display: flex and adjust the order of the elements to stack as desired at specific breakpoints.

Answer №4

Can you create a website layout without using jQuery and only relying on CSS? The answer is yes! All you need is a solid grid system.

Instead of manually coding media queries to adjust your layout based on screen size, consider utilizing a grid system. Not only will it handle edge cases more efficiently, but it will also save you from having to start from scratch each time. Some popular grid systems worth exploring include:

If simplicity is key for your project, starting with Skeleton might be the way to go. It offers an efficient grid structure without overwhelming you with unnecessary features like pre-styled buttons. However, if you prefer a CSS framework that covers more aspects of design, Foundation and Bootstrap are popular choices worth considering.

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 integration of cypress-cucumber-preprocessor with multiple testing frameworks appears to be experiencing compatibility issues

I am trying to set up a connection between Cypress and Cucumber, and I came across this plugin: https://www.npmjs.com/package/cypress-cucumber-preprocessor However, I am having trouble with my implementation as it seems to be missing. I have also added th ...

What methods can be employed to reduce additional background tasks when altering a state in a React component?

Trying out Code I experimented with creating a React exercise code that showcases a bus and its seats. Reserved seats are marked in red and cannot be selected, while the remaining seats can be chosen by clicking on them and selecting a gender from a popup ...

Send information through a form by utilizing a personalized React hook

I'm having difficulty understanding how to implement a hook for submitting a form using fetch. Currently, this is what I have. The component containing the form: const MyForm = (): ReactElement => { const [status, data] = useSubmitForm('h ...

Issue with the intersection of mouse and camera rays in three.js

I've been working on a simple program that involves creating a clickable 3D object in Three.js. I've referenced my code from When I click directly on the object, it works as expected, but upon examining the resulting array, I noticed that the ob ...

Utilizing the arr.push() method to replace an existing value within an array with a new object, rather than simply adding a new

Seeking help to dynamically render a list of components that should expand or shrink based on values being added or removed from an array of custom objects. However, facing an issue where pushing a value into the array only replaces the previous value inst ...

Concealing JW Player Video Link within Source Code

<div id="movie">...</div> <script> player("film").customize({ width:800, height:400, playlist: [{ file: "https://movielink.mp4", image: "https://moviepic.jpg", tracks: [{ file: "http://movi ...

Unusual characteristics of decision-making

Here is a snippet of my JavaScript code: function getSelectedText(){ if(window.getSelection){ select = window.getSelection().getRangeAt(0); var st_span = select.startContainer.parentNode.getAttribute("id").split("_") ...

Utilizing Repurposed Three.js Shapes

I have been experimenting with Three.js to visualize the path of a particle in a random walk. However, I encountered an issue where geometries cannot be dynamically resized so I had to come up with a workaround by removing the existing line from the scene, ...

Explaining the jQuery $.post method

After a thorough search, I finally discovered the importance of adding return false; at the end of my JQuery code for posting data. Without it, my code wasn't functioning as expected. As a beginner in JQuery, I would appreciate some insights into the ...

Displaying a specific division solely on mobile devices with jQuery

I need to implement a feature where clicking on a phone number div triggers a call. However, I only want this div to be displayed on mobile devices. To achieve this, I initially set the div to "display:none" and tried using jQuery to show it on mobile devi ...

I added an onClick event to an SVG image, however, I am struggling to get the event to execute a jQuery if/else statement

I've been working on creating an SVG map of the US, and I'm almost finished. The final step involves implementing a simple if-else statement to prompt users for the name of the state when they click on it. My goal is to fill the state green if th ...

I am looking to enhance my JSON output using JavaScript

My JSON output appears as follows: {"intent":"P&P_Purchase","value1":{"date1":"30-Dec-19","prd_desc":"NEEM UREA OMIFCO (45 KG)","qty":"18MT","inv_no":"NRKT07003160"},"value2":{"date1":"25-Dec-19","prd_desc":"NEEM UREA IMP (45 KG)","qty":"18MT","inv_no ...

What is the best way to retrieve data from a jQuery AJAX call when it returns?

function isNewUsername(str){ var result; $.post('/api/isnewusername', {username:str}, function(data) { result = data.result; }, "json"); return result; } I am encounte ...

Troubleshooting Problem with JQuery in the YouTube Player API

Having some difficulty parsing YouTube video ids to a function that plays them in an embedded player using a combination of JavaScript and jQuery with the YouTube Data and Player APIs. Below is my code: <html> <head> <script src="//aj ...

Exploring the power of asynchronous Mongoose queries using the async await

I have a system where authenticated users can create new user accounts and assign them to specific groups. The middleware flow for this process is outlined in the following route: router.post('/account/add-users', userController.validateRegist ...

Is there a method to retrieve the subdirectories and files currently present on a given URL?

Picture having a file on your system with the following URL: I am curious if there is any software, command, or script that can retrieve subfolders/files based on a given URL. For instance: Input parameter: http://my.page/folder_1/ Output: http://my.p ...

What could be the reason for my inability to retrieve data when using the input type "number"?

One issue I encountered while using ejs to extract data into an HTML file is that when the input type is set to "number", the data extraction does not work. However, changing the input type to "text" allows for successful data extraction. Can someone pleas ...

Is there a way for me to duplicate a complex element multiple times within the same page?

As an illustration, let's say I have a social tab located in my header that I would like to duplicate in the footer. This tab is comprised of various buttons with SVG images on top, event listeners linked to button IDs, and CSS styling. One option is ...

Combining iDangerous Swiper with jquery .click() for Ultimate User Interaction

Need some help with this issue: I'm currently utilizing the iDangerous Swiper plugin, which is functioning properly. However, I also want to implement jQuery's click function on that same iDangerous swiper. For instance: <div id="swiper-con ...

Setting the current date of a jQuery datepicker to the value of an input tag

I am looking to assign a value to an input tag that has a datepicker attached to it. Upon initialization of the datepicker, I want to set this value. Below is a hypothetical example of what I am trying to achieve: HTML: <input id="test" value="">&l ...