Adjust the dimensions of a div element based on the screen size

I'm struggling with adjusting the size of a div responsively based on the surrounding divs. My layout includes buttons on both sides of the page and text in the middle that varies from 0 to 140 characters.

What I aim to achieve is for the buttons to move inward as the page size changes, while the amount of visible text in the middle div decreases. Setting the width with a % didn't give the desired result, as it constrained the size rather than letting it expand or shrink according to the available space.

I need a solution to dynamically adjust the size of the middle item while keeping the outer elements at their respective edges until the middle item reaches a certain width (e.g. don't allow the text to occupy less than 15% of the page).

.leftSide {
  float: left;
}

.middle {
  display: inline-block;
  padding-left: 5px;
  min-width: 15%;
  background: #ccc;
}

.rightSide {
  float: right;
}
<div class="header">

  <div class="leftSide">
    <button>Save</button>
    <button>Next</button>
    <button>Back</button>
  </div>

  <div class="middle">Some text here - min char 0, max 140</div>

  <!--Comment out the class: middle above and uncomment the class: middle below to see what I mean with the length being a problem when resizing the page-->

  <!--<div class = "middle">Text could also be really long like this one is. The cow jumped over the moon bla bla bla.... </div> -->

  <div class="rightSide">
    <button>Cancel</button>
    <button>Other Button</button>
  </div>

</div>

I've shared my code on jsfiddle: https://jsfiddle.net/KR8T3/8vywe1zr/14/ (In the example, I used plain text in the middle div for simplicity. In reality, the content can range from 0 to 140 characters)

Answer №1

It seems like the best approach would be to utilize CSS flexbox and eliminate the need for floats. In the snippet below, I utilized display: flex; for the header section and utilized flex-shrink: 0; for the button items to prevent them from breaking into multiple lines. For the middle text, I used flex-shrink: 1; so it can shrink to fit the content when resized. Additionally, I included flex-grow: 1; for the final item to fill any remaining space and aligned it to the right using text-align: right;.

Does this solution align with what you were envisioning?

.header {
   display: flex;
}

.leftSide {
  flex-shrink: 0;
}

.middle {
  display: inline-block;
  padding-left: 5px;
  min-width: 15%;
  background: #ccc;
  flex-shrink: 1;
}

.rightSide {
  flex-shrink: 0;
  flex-grow: 1;
  text-align: right;
}
<div class="header">

  <div class="leftSide">
    <button>Save</button>
    <button>Next</button>
    <button>Back</button>
  </div>

  <div class="middle">Some text here - minimum of 0 characters, maximum of 140 characters</div>

  <div class="rightSide">
    <button>Cancel</button>
    <button>Other Button</button>
  </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

The reason for variation in the width setting for input elements

I've been scratching my head for a while now trying to figure out why the width of input elements isn't setting correctly. In this example, I set the width to 100px for the submit button But in this one, I also set it to 100px for the text fie ...

Having difficulty automatically adjusting the width of HTML columns according to their content

I have a HTML table that I want to automatically set the width of the table columns, adjusting based on the content within. Despite setting the width to auto, as shown in the image below, the content in the ID column does not stay confined to a single line ...

The jQuery code stops functioning once the identical HTML content is loaded through AJAX

Before you mark my question as a duplicate, I have already attempted the solutions provided by similar issues, but unfortunately none of them have resolved my problem. Here is my specific issue: I am working with a fragment that includes the following inpu ...

Pop-up window for uploading files

Looking for assistance with my file uploading system. I am utilizing a unique purple button from http://tympanus.net/Development/CreativeButtons/ (Scroll down to find the purple buttons). Additionally, I am using a wide, short content popup feature availa ...

Changing color of the collapsible icon and content when hovered over

Currently, I am working on creating collapsible content for a FAQ dropdown. The button has a 'plus' icon which changes to a 'minus' icon when clicked. My goal is to change the color of the pre-clicked 'plus' icon to white when ...

How to disable event.preventDefault on a hyperlink with a delay using jQuery or JavaScript

I need to disable event.preventDefault after a certain period of time on a hyperlink using jQuery or JavaScript. When I click on the link, it should redirect me to the specified href link after a delay because I need to send an AJAX request during that tim ...

How can I customize the color of Chrome's default date and time picker to a different shade of blue?

Is there a way to customize the color of the blue buttons, text, and date indicator in the native datetime picker for Chrome using CSS? https://i.stack.imgur.com/eZSaE.png ...

Vue is currently operating in development mode

I keep receiving this notification each time I visit the site: Vue is currently running in development mode. Remember to switch to production mode when deploying for production. Find more helpful tips at .... I came across a solution from someone suggest ...

Pressing the button inside the iframe will cause the div to appear in the parent

I have a button in my iframe that opens a div on the same page: <li> <input type='button' id='hideshow2' class='radiobutton' value='Core' onclick="showDiv('#content2')"> </li> <di ...

Tips on transmitting form information from client-side JavaScript to server-side JavaScript with Node.js

Having created an HTML page with a form, my goal is to capture user input and store it in a JSON file. However, I've run into some confusion... In the process, I utilized the express module to set up a server. My mind is juggling concepts such as AJA ...

Setting up VSCode for THREE.js development

Struggling with implementing my JavaScript code in VSCode, trying to move away from CodePen and use an actual code editor. Transition seemed simple but can't get it to work. Using the following files: test.html <!DOCTYPE html> <html> .. ...

Fetching the most recent 5 entries from the database

Hey everyone, Currently, I'm working on a project that involves displaying job data in a table based on department and week of the year. I need to only show the last 5 weeks from the current date. For example, if it's currently week 23, the tabl ...

Press the button to copy the content to the clipboard with JavaScript

I am struggling with copying email text in HTML within an href tag. I want the email value to be copied when a user clicks on the icon next to it, but for some reason it is not working as expected. Instead of just copying the email address, my code seems ...

Utilize Angular Material to assign the value of a mat-select component based on the FormControlName property

I am using Angular version 7 and have encountered an issue with a form I am creating. The form includes a dropdown select to choose an option, but when the page loads, the pre-defined value is not showing up. This page is specifically designed for editing ...

How can you conceal nested elements within a layout that adjusts to various screen sizes or is percentage-based

Contemplate this HTML: <body> <div id="parent"> <div id="child"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras dapibus sapien congue, fringilla justo quis, aliquam tellus. Maecenas semper consectetur metus ege ...

Performing various JavaScript functions within a single hyperlink

I've implemented the Tab Content Script by dynamicdrive.com on my website. The script creates tabbed navigation, and I find it to be quite useful. Currently, I am trying to figure out how to make a single link select two tabs at once. Essentially, I ...

Struggling to adjust the width of a recurring div element in PHP

I'm encountering an issue on my PHP project where I have multiple comment box divs that are repeating. Each time a user clicks on one of these divs, I want the width to change to 100%. However, my problem is that when I try to implement this functiona ...

Generate dynamic Bootstrap Carousel slides with unique hashtag URLs

I've been attempting to connect to various slides within a bootstrap carousel from a different page but have had no success. Here is an example of what I'm trying to achieve: <a href="services#slide2">Link to Slide 2</a> For refere ...

What methods can I use to obtain negative numbers through swipe detection?

In my code, I am using three variables. The first one is x which serves as the starting point, followed by myCount which counts the number of swipes a user performs, and finally, dist which calculates the distance from the initial point. I want to set myC ...

Tips for ending a page prematurely

I am currently facing an issue with hosting images on my webpage where the bottom of the images are uneven, creating a messy look at the bottom of the page. https://i.sstatic.net/N3XvJ.png Normally I would attempt to align them in a perfect square layout ...