Cause an element to extend beyond others when the viewport reaches its limit

My design dilemma involves two squares: a blue one that will expand to contain things totaling 800px, and a red one that must always remain fully visible. When narrowing the viewport, the red square should overlap the blue square, rather than disappearing off the screen.

How can I make this happen?

.container {
  display: flex;
}

div {
  height: 80px;
}

.should-be-overlapped {
  width: 100%;
  min-width: 800px;
  background: blue;
}

.always-full-width {
  width: 400px;
  background: red;
}
<div class="container">
  <div class="should-be-overlapped"></div>
  <div class="always-full-width"></div>
</div>

Answer №1

To achieve this, there are primarily two approaches.

You can either enclose the blue section in a wrapper (which I would recommend).

Check out the updated codepen

View the stack snippet below:

.container {
  position: relative;
  display: flex;
}

div {
  height: 80px;
}

.wrapper {
   flex: 1;
   overflow: hidden;
}

.should-be-overlapped {
   width: 100%;
   min-width: 800px;
   background: blue;
}

.always-full-width {
  flex: 0 0 400px;
  background: red;  
}
<div class="container">
  <div class="wrapper">
    <div class="should-be-overlapped">
    </div>
  </div>
  <div class="always-full-width">
  </div>
</div>


Alternatively, you can utilize position: absolute.

Check out the updated codepen

.container {
  display: flex;
  position: relative;
  overflow: hidden;
}

div {
  height: 80px;
}

.should-be-overlapped {
   width: 100%;
   min-width: 800px;
   background: blue;
}

.always-full-width {
  position: absolute;
  right: 0;
  width: 400px;
  background: red;  
}
.<div class="container">
  <div class="should-be-overlapped">
  </div>
  <div class="always-full-width">
  </div>
</div>

Answer №2

The blue square I have is designed to hold items totaling 800px.

If you want it to be able to shrink, use flex: 0 1 800px:

.container {
  display: flex;
}

div {
  height: 80px;
}

.should-be-overlapped {
  /*min-width: 800px;*/
  flex: 0 1 800px; /* doesn't grow but shrinks, initial width set to 800px (this is also its "max-width") */
  background: blue;
  word-break: break-all; /* for longer unbreakable strings, just for demo */
}

.always-full-width {
  /*width: 400px;*/
  flex: 0 0 400px; /* since you're using flexbox, doesn't grow nor shrink, initial width set to 400px (fixed) */
  background: red;
}
<div class="container">
  <div class="should-be-overlapped">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
  <div class="always-full-width"></div>
</div>

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

What is the best way to limit the top margin for a fixed element?

Recently, I came across this code snippet: jQuery(document).ready(function($){ $( window ).scroll(function() { var current_top = $(document).scrollTop(); var heights = window.innerHeight; document.getElementById("sHome").styl ...

What are some methods for displaying images retrieved from an API onto an HTML webpage?

Problem: The image is not appearing on my HTML page. How can I fix this issue? Please see below for details. https://i.sstatic.net/XYoHg.png Here are the code snippets I am using to display the image: <div class="col-md-7"> <div c ...

Can anyone help me with fixing the error message 'Cannot assign to read-only property 'exports' of the object' in React?

Recently, I decided to delve into the world of React and started building a simple app from scratch. However, I have run into an issue that is throwing the following error: Uncaught TypeError: Cannot assign to read-only property 'exports' of o ...

Struggling to have PHP process and store form data in MySQL database

I am attempting to process HTML form data into a MySQL database using PHP for the first time, and I am feeling overwhelmed. The form is POSTed to the formSubmit.php file, where the variables are created for the SQL command to query. I have tried adjusting ...

Why isn't my li:hover responding the way it should?

Check out this Fiddle When the mouse hovers over the element, an opaque "cloud" appears and I want to completely hide the underline that is normally displayed. The underline is created by using a border-bottom on the parent div. .element_style{ width ...

Show only half of the Google Charts

I have a code snippet that displays a chart with dimensions of 500x500. However, I only want to show half of the chart, like 500x250. But whenever I adjust the values in the div, it resizes the entire chart instead of just showing half. My goal is to hide ...

What is the best way to resize a Drawable to fit the width of a TextView?

When trying to render HTML with image tags within a TextView, I encountered issues with scaling the images using the getDrawable() code. Here is an example snippet of the code: myTextView.setText(Html.fromHtml(htmlSource, this, this)); @Override public Dr ...

What could be the reason for my application not utilizing its bootstrap classes effectively?

Just starting out with Bootstrap and have made some progress. However, the bootstrap classes I'm using are not rendering properly. What could be the issue? <div class="position-absolute bottom-0 end-0 border border-dark"> https://i.ss ...

What is the best way to choose a single li element?

I am working on creating a reservation system using HTML, CSS, and JS. I want to customize the color of the border for each list item (li). However, I want only one li to be selected at a time. When I click on a different li, it should remove the selection ...

Prevent touching the pseudo content of an element

My issue involves a toggle button component where I have utilized the ::before pseudo class to add text. The problem arises when clicking on the text within the toggle button causes the button's state to change. How can this be prevented? Here is the ...

Issue with Flex property not being supported in Safari browser

.rq-search-container { position: relative; width: 100%; background: white; display: flex; flex-direction: row; align-items: flex-start; border-radius: 2px; z-index: 30; -webkit-flex: 1 0 15em; } While this code functi ...

"After completing the survey form, the User Details form is displayed upon clicking the submit button

In my Quiz, each question loads on a separate page with clickable options. Some questions may have multiple answers, including an "Others" option. At the end of the quiz, users need to fill out a form. Although I've created a survey form, I'm fa ...

Assign a unique HTML attribute to a child element within a Material-UI component

Currently, I am trying to include the custom HTML5 attribute "data-metrics" in the span element within the ListItemText Material UI component. However, I am facing some difficulty achieving this as per the specifications outlined in the Component API Docum ...

Anchor tags are not visible to Jquery

My issue is with integrating JQUERY and PHP. In my external PHP file, I have the following echo statement - `echo ("<a href='#' id='bb'>hello</a>"); Similarly, in my external js file, I have this JQUERY code - $(' ...

Overflow of content within a rectangular element

One challenge I'm facing is creating HTML/CSS code that automatically adjusts the website layout (blocks and content) when I resize the browser window. For example: I encounter text overflow issues in a block I've created when I minimize the brow ...

Using a twig loop to display just a single table cell

The following twig loop is used to generate data: {% for reservationDate, rooms in data.pricingTable %} <tr> <td> {% for room in rooms %} <tr> <td ...

Is it possible to activate the identical drop-down or popover with multiple buttons?

Is it possible to activate the same drop-down menu with multiple buttons? For example, I want a button at the top of the page labeled special menu, another in the middle also labeled special menu, and one at the bottom as well labeled special menu. When a ...

Handling onMouseLeave event for Popover component in material ui library with hiderBackdrop parameter specified

<Popover key={element.id} className={classes.popoverItem} classes={{ paper: classes.paperElement }} open={isOpen} anchorEl={this.myRef.current} anchorOrigin={{ vertical: 'bottom&apo ...

The background color is failing to respond to the padding of the adjacent image

Check out this page to see the issue. The code snippet surrounded by pre tags has a background-color that is not aligning properly with the image, even though the image has a defined padding of 16 pixels on the right. The text alignment is correct, but the ...

Retrieving the CSS property values for a specific element using Selenium

Imagine a scenario where I locate an element by its XPath using: WebElement element = driver.findElement(By.xpath("specific XPath")); While I can retrieve the value of a single CSS property with element.getCssValue("specific property"), is there a way to ...