Adjust the size of CSS boxes based on the window dimensions

I am facing an issue with two divs placed side by side.

They appear fine when the page is viewed in full-screen mode.

However, upon resizing the window, the right div overlaps the left one and causes a messy layout.

.

-I want to prevent these boxes from stacking on top of each other when the window size changes.

I attempted to use max-width; and min-width; properties but was unsuccessful in fixing the problem.

.

-I also tried adjusting the font sizes to be smaller or larger based on the window size using values like font-size: 70%;, but it did not work as intended.

CSS:

.leftbox {
    top: 15%;
    position: absolute;
    color: white;
    padding: 10px;
    font-size: 2em;
    max-width: 700px;
    overflow: hidden;
    background: rgba(121, 183, 0, 0.9);
    box-shadow: 0 4px 10px 4px rgba(43, 141, 6, 0.85);
}

#rightbox {
    top: 15%;
    right: 5%;
    position: absolute;
    background: rgba(121, 183, 0, 0.9);
    padding: 40px;
    max-width: 500px;
    border-radius: 4px;
    box-shadow: 0 4px 10px 4px rgba(43, 141, 6, 0.85);
    font-size: 2em;
    color: white;
}

HTML

<div class="leftbox">
    [Lorem Ipsum text for leftbox goes here]
</div>

<div id="rightbox">
    [Lorem Ipsum text for rightbox goes here]
</div> 

JSFiddle: https://jsfiddle.net/p6mttf9q/

Answer №1

I am a fan of @JiFus method, however there is an additional solution available to you. You can implement Media Queries and adjust

position: absolute;

to be

position: relative;

for smaller screens or devices.

For instance, you could utilize the following code:

@media screen and (max-width: 800px) {
.leftbox { position: relative !important; }
#rightbox { position: relative !important; }
}

Feel free to check out the jsfiddle link for more details: https://jsfiddle.net/p6mttf9q/4/

Answer №2

Take a look at this JSFiddle link

This technique showcases the optimal way to use absolute elements effectively. The key code snippet responsible for this is:

.leftbox {
  max-width: calc(50% - 28px);
}

as well as

#rightbox {
  max-width: calc(45% - 88px);
}

In essence, I am calculating the maximum width for both divs to prevent any overlap. The calculation is based on ensuring that both containers occupy approximately 50% of the space available:

For the left box: 50% - padding (10px on each side) - box shadow (4px on each side).

For the right box: 50% - margin-right (5%) - padding (40px on each side) - box shadow (4px on each side)

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

Adjust the style of an element when hovering over a different element

Below is the HTML code in question: <div> class="module-title" <h2 class="title" style="visibility: visible;"> <span>Spantext</span> Nonspantext </h2> </div> I am looking to change th ...

Mobile Website Blog Page With Dual Sidebars Issue

I have created a blog page using bootstrap 4 that features both left and right sidebars along with the main content section. Everything looks great on Desktop view. However, when viewed on a mobile phone, the order changes to: left-sidebar>content> ...

If an AngularJS Form Item is hidden with jQuery's hide() method, will it still be validated?

Even though we're in 2020, I'm still working with AngularJS 1.x instead of the newer version due to work requirements. Despite this limitation, I am facing a challenge with setting up a form that requires exclusive-or validation between two field ...

Fixing the CSS 404 error caused by using variables in Flask routes: A step-by-step guide

I have created a basic web application to showcase book reviews. Upon a successful search, users are provided with links to book titles - when a link is clicked, the goal is to pass the ISBN of the selected book to the url_for method and then present the c ...

Adaptable Text Title

Is there a way to make text headings responsive? When looking at the example provided here, we can see the heading is "The Swift List". How can I ensure that the words "THE" and "LIST" remain aligned with the edges of the word "SWIFT"? While I have managed ...

What’s the best method for enclosing a table and text within a div?

The following code snippet generates the following output: However, my desired outcome is this: When I remove <div>hello world</div>, the table fits perfectly within the outer div. But when I add text to the div along with the table, it doesn ...

Displaying an HTML file in a Node and Express application using JavaScript File handling

Starting my first Node, Express & Angular project has been a bit tricky due to issues with the asset pipeline. I'm not entirely sure if it's related to my folder structure, which I tried setting up based on online tutorials but am open to suggest ...

Selecting the initial item of every nested tag repeatedly for a total of n times

Is there a way to stop coloring elements after 'tue 7-1-1' using CSS? I am only allowed to manipulate the first child of every element up until a certain point through CSS, without modifying the existing code. Note: Span elements are nested wit ...

Converting JSON data into a JavaScript array and retrieving the array through an AJAX request from PHP

Currently, I am working on a project where I have created a function named AjaxRequest to manage all my AJAX requests. While making the requests is not an issue, receiving and placing the data back onto the page has proven to be quite challenging. Within ...

Tips for combining two htmlelements together

I have two HTML table classes that I refer to as htmlelement and I would like to combine them. This is similar to the following code snippet: var first = document.getElementsByClassName("firstclass") as HTMLCollectionOf<HTMLElement>; var se ...

The displayed database results will appear in the opposite order of what was originally assigned for both inline and block elements

I have a database with information that I am trying to display using a while loop. My goal is to show the results in this format... Firstname Lastname - Firstname Lastname - Firstname Lastname player1 ---------------player1-----------------------pla ...

Enhance your product listings in Opencart 2.0 by adding supplementary images alongside the main product image

Can someone assist me with moving additional product images next to the main product image in the default opencart 2.0.1.1 theme? Currently, they are appearing below the product image and I would like them to be displayed alongside it instead. ...

ng-model to interact with dynamically loaded elements

My dynamic list contains various items, such as cars, and is not of fixed length. Upon loading, the list appears as follows: itemList = [ { id: 0, name: 'bmw' }, { id: 1, name: 'audi' }, { id: 3, name: 'vw' }, ...

Issue with IE11: Flexbox with absolute positioning not sizing correctly, fills entire width instead of individual content

Here's a simple case to reproduce the issue: <div style="display: inline-block; position: relative; border: 1px solid black;"> short <div style="display: flex; position: absolute; border: 1px solid black; top: 100%; lef ...

A fading transparency box on the border

Looking for a box with varying opacity from left to right (See image for reference - the red marked box has decreasing opacity from right to left). Here is my attempt: .waterMark { background-color: #FFFFFF; float: right; opacity: 0.6; position: ...

Tips for aligning ticks to the left on a d3 bar chart

i recently finished creating a stacked bar graph with varying tick lengths on the y-axis side. my goal is to align the text within the ticks to the left, similar to this example: http://jsfiddle.net/2khbceut/2/ here is the HTML: <title>Diverging Sta ...

Using Ajax to update a webpage by invoking a PDO function within a PHP class

Looking for assistance with filtering a list of files using a dropdown box and categories. I have a PHP class ready to handle the SQL query and results, but I'm eager to implement this through AJAX to avoid page refresh. Feeling stuck and seeking gui ...

Open the link and input the text into the text box?

Suppose I have the following JavaScript code: <script language="javascript" type="text/javascript"> function addText() { var newText = document.myForm.inputText.value; document.myForm.description.value += newText; } </script> I want t ...

Encountering a problem when transitioning Bootstrap 3 code to version 5

After finding this template on github, I noticed it was a bit outdated with the use of Bootstrap 3. I decided to update it to Bootstrap 5 and replaced the Bootstrap 3 CDN accordingly. However, upon doing so, I encountered some issues. Can anyone help me ...

How can I add content using HTML or JavaScript?

How can I append a .txt file using HTML or Java without ActiveX prompts getting in the way? It's becoming quite annoying! Is there a simple way to script this task without having to deal with ActiveX? The current script snippet looks something like ...