Designing a layout that adapts to different screen sizes, utilizing nested div elements

I'm currently working on creating a responsive layout for my website, but I started the project without one and now my basic template has major flaws. How can I create a template like this picture where the divs adjust as the browser viewport size changes? The divs in the image seem to be moving independently rather than following a set command.

I want the layout in the picture to behave exactly as shown when the browser window is resized. Any guidance on how to achieve this would be greatly appreciated :)

Here is the image for reference: https://i.sstatic.net/fzHHO.jpg

Answer №1

Introducing PocketGrid, a compact CSS grid designed for responsive layouts.

Following Luca's recommendation, PocketGrid can be utilized to structure your layout.

A demonstration has been prepared for you on JSFiddle: http://jsfiddle.net/arleray/5Mvph/

The HTML code is straightforward:

<div id="LAYOUT" class="block-group">
    <div id="HEADER" class="block">
        <div class="box">HEADER</div>
    </div>
    <div id="WORK_AREA" class="block-group">
        <div id="LEFT_BAR" class="block">
            <div class="box">LEFT_BAR</div>
        </div>
        <div id="CONTENT" class="block">
            <div class="box">CONTENT</div>
        </div>
    </div>
    <div id="TOOLBOX" class="block-group">
        <div class="TOOLBOX_ITEM block">
            <div class="box">TOOLBOX ITEM</div>
        </div>
        <div class="TOOLBOX_ITEM block">
            <div class="box">TOOLBOX ITEM</div>
        </div>
        <div class="TOOLBOX_ITEM block">
            <div class="box">TOOLBOX ITEM</div>
        </div>
    </div>
</div>

To style your CSS effectively, I recommend employing the "mobile-first" approach:

1 - Begin with the "mobile" version (the smallest):

#LAYOUT { min-width: 800px; }
#HEADER { height: 30px; }
#WORK_AREA { width: 100%; }
#LEFT_BAR { width: 300px; }
#CONTENT {
    overflow: hidden; /* Trick to fill the remaining space */
    float: none;
    width: auto;
}
#TOOLBOX {
    min-width: 300px;
    width: 100%;
}

2 - Subsequently, incorporate media queries for larger versions (> 1100px) to implement changes from the mobile version only:

@media (min-width: 1100px) {
    #WORK_AREA { width: calc(100% - 300px); }
    #TOOLBOX { width: 300px; }
}

To achieve a fluid content width within the WORK_AREA, the "overflow:hidden" technique was utilized to occupy the remaining space after the LEFT_BAR.
However, due to the fixed toolbar on the right, the calc() function was necessary to compute the WORK_AREA width.

Note: The calc() function may have compatibility issues limited to Android 4.4+. For more details, refer to this link: http://caniuse.com/calc. It remains the optimal method (pure CSS-wise) for ensuring a fluid WORK_AREA next to the fixed sidebar, despite TOOLBOX being declared post-WORK_AREA.
Should you desire an alternative to the calc() function using the "overflow:hidden" trick, ensure the toolbox precedes the WORK_AREA, as demonstrated in this alternate JSFiddle: http://jsfiddle.net/arleray/5Mvph/11/

For additional insights on PocketGrid, explore various examples available here:

Trust this information proves beneficial!

Answer №2

Utilize media queries in CSS to customize the design based on different devices, check out media queries here, or alternatively, consider using frameworks such as Bootstrap. These tools can assist in managing div elements according to varying screen sizes.

Answer №3

Consider using bootstrap for a simpler life. You can find more information here. I hope this suggestion proves helpful.

Answer №4

Check out this extremely simple and basic Grid system I recommend: PocketGrid. It's only 1 KB when minified. Take a look at the demos and description. ;-)

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

Utilizing combined selectors in styled-components: A comprehensive guide

My existing CSS code is structured like this: .btn_1 { color: #fff; background: #004dda; display: inline-block; } .btn_1:hover { background-color: #FFC107; color: #222 !important; } .btn_1.full-width { display: block; width: 100%; } I ...

recover the identifier from the cryptic hash reply

Is it possible to decrypt the ID from the encrypted hash response in a Java Android function? I am facing an issue where I am unable to retrieve the ID when I take the username string instead of the result string displayed as a toast message. This is caus ...

Align numbers vertically inside scrollbar center

I have created a program where you can input a number, and it will count from 0 to the specified number. Currently, the numbers are aligned horizontally up to 12 before starting on a new line. However, I would like the numbers to be arranged vertically fr ...

Android application utilizing asp.net web services

Looking for assistance as a newcomer to android development. I am trying to run my asp.net web service in an android application but have encountered issues using the SOAP OBJECT. The web service is basic, simply taking my name and returning HELLO + "my ...

What is the best way to set a single column to have a fixed position when scrolling horizontally?

There is an inline form that extends both horizontally and vertically. Within this form, there is a final column containing an add button which I would like to have a fixed position only when scrolling horizontally, not vertically. <!doctype html> &l ...

Iframe form submissions

I am interested in accomplishing the following task. My objective is to submit a form to my booking engine and show the results on either a new page or within a Div element. Ideally, when the user clicks the submit button, it would display an Iframe or re ...

The hover effect is not resizing the button as expected

I attempted to create a similar effect like this: https://i.sstatic.net/hbiOS.gif As you can see, when the mouse hovers over the button, it has a scaling and fading white effect. I tried to achieve the same effect on my Bootstrap button by customizing m ...

Is it possible in JavaScript to have three different input values contribute to a sum without using HTML event attributes?

I've been working my way through the 57 programming exercises book by Brian P. Hogan. For most of these exercises, I've been attempting to create a GUI. In this particular exercise, the goal is to calculate the Simple Interest on a Principal am ...

Tips on removing HTML tags that are empty and contain white spaces or their corresponding HTML codes

Looking for a regex solution to use with preg_replace. This specific query was not addressed in previous discussions, as the tags I need to remove are not always empty. In addition to eliminating empty tags from an HTML structure, I also need to handle t ...

My approach to using this style in React involves utilizing the class attribute as follows: "class[attribute]" [Updated version 2]

When trying to customize an element based on its attribute, the process is a bit different in React compared to pure CSS. Here's an example: <div class='something' data-test='4'></div> ... .something[data-test] { bac ...

What to do when the image-sprite is too large for the container and needs to be resized to fit within a smaller

Is there a way to resize the sprite image to fit the container without using the tag? The sprite image dimensions are 562px (x5) width x 562px height main-desktop.png .container_outer{ height:200px; width:100%; } .chrome-part, .ie-part, .firefox- ...

Tips for creating a dynamic sticky footer

Seeking out a solution for a sticky footer that adjusts its height based on the width of the browser. Creating sticky footers in flexible designs can be tricky. I've come across various suggestions, discussions, and fixes for implementing sticky foot ...

What is the method for retrieving a PHP DOMXPath object?

Is it possible to return a string if the '$NotXP->query' equals query? How can I make the code below work correctly? $xp = new \DOMXPath(@\DOMDocument::loadHTMLFile($url)); $list = $xp->query('//table[@class="table ...

Display information from a .js file onto an HTML webpage

I'm completely new to the world of server-side development and I'm attempting to navigate it on my own. Currently, I have a server written in JavaScript using Express and I'm trying to display some content on my HTML page that was sent from ...

How to Keep Button Highlighted After Clicking

I've experimented with various methods like using :active, :focus, adding a class, and modifying CSS rules through jQuery to maintain the button highlight, but none of them have been successful. Please take a look at my code and point out any mistakes ...

how to change class on vue function result

I need a way to display the content stored in requestData as li elements. Each list item should have an onclick function that, when clicked (selected), adds a specific value from a reference to an array. If clicked again (unselected), it should remove the ...

Discovering if a location is en route using the Android Google Maps Directions API

Having both a starting point (A) and destination (B), I can successfully generate direction lines on the map. However, with the addition of location (C), is there an API available that can determine if (C) falls along the route from A to B? ...

Choosing the ID and class name through jQuery among numerous classes

I am looking to create a Modal Pop Up Box using CSS and JS, but I need help getting the id (mymodal , mybtn) and the Class (close) from the span Element in JavaScript. Please refer to the attached link for an explanation of the code. div class="head ...

Tips for implementing linear-gradient text in Android applications developed with React Native without using Expo

How can I implement linear-gradient text in react native mobile apps without using Expo? I want to apply a linear-gradient to change the text color. Is it possible to achieve this effect? If yes, please provide guidance on how to do it. If not, how do po ...

Having some trouble utilizing URLconnection as errors keep popping up

I have been working on a project involving sending and receiving data to a PHP page using the post method. Despite finding this code snippet online, I am encountering some errors that I'm struggling to resolve. If you'd like to see the errors I& ...