Is it possible to vertically resize just one of three divs to match the size of the window?

I'm facing a challenge in creating a responsive webpage with multiple divs. Here is the structure I am trying to achieve:

<div id="container"> maximum width of 1200 and height of 1000
    <div id="vertically-static-top">20 pixels high</div>
    <div id="vertically-resizeable-middle">height can be adjusted</div>
    <div id="vertically-static-bottom">50 pixels high</div>
</div>

The maximum width and height for the overall container are set to 1200 and 1000 respectively.

The vertically-static divs should resize only horizontally when the browser window is resized, maintaining a fixed height vertically.

On the other hand, the vertically-resizable div needs to adjust both its width and height based on the dimensions of the window.

I am struggling to make the middle div resize vertically while keeping all content within the browser window. Any help would be greatly appreciated!

Thank you!

Answer №1

In my opinion, the solution involves utilizing the position property with a value of absolute. Below is a snippet demonstrating this approach.

html,
body {
  font: 13px Verdana;
  margin: 0;
  height: 100%;
}

#container {
  max-height: 1000px;
  max-width: 1200px;
  background: red;
  color: #fff;
  height: 100%;
  position: relative;
}

#vertically-static-top,
#vertically-static-bottom,
#vertically-resizeable-middle {
  position: absolute;
  left: 0;
  right: 0;
}

#vertically-static-top {
  height: 20px;
  top: 0;
  background: black;
}

#vertically-static-bottom {
  height: 50px;
  bottom: 0;
  background: black;
}

#vertically-resizeable-middle {
  top: 20px;
  bottom: 50px;
}
<div id="container">
  <div id="vertically-static-top">20 pixels high</div>
  <div id="vertically-resizeable-middle">adjustable height</div>
  <div id="vertically-static-bottom">50 pixels high</div>
</div>

This method should be effective in achieving the desired outcome.

Answer №2

To achieve this layout, you can utilize flexbox. Below is a demonstration of the code:

#container {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical!important;
  -webkit-box-direction: normal!important;
  -ms-flex-direction: column!important;
  flex-direction: column!important;
  height: 240px;
  margin: auto;
  max-height: 1000px;
  max-width: 1200px;
}

#vertically-static-top,
#vertically-static-bottom {
  background-color: #80bdff;
  border-radius: .25rem;
  color: #fff;
  padding: 1rem;
}

#vertically-resizeable-middle {
  background-color: #957bbe;
  border-radius: .25rem;
  color: #fff;
  flex-grow: 1;
  padding: 1rem;
}
<div id="container"> maximum width of 1200 and height of 1000
    <div id="vertically-static-top">20 pixels high</div>
    <div id="vertically-resizeable-middle">adjustable height</div>
    <div id="vertically-static-bottom">50 pixels high</div>
</div>

Adjust the height of #container (height: 240px;) or change it to 100% to fit the height of the parent element

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 ensure that text highlighting appears consistent on all browsers?

I am facing an issue with the appearance of text highlighting between Chrome and Firefox browsers. Is there a way to ensure consistency in the appearance across all browsers? a { text-decoration: none; font-weight: 900; font-size: 4vw !impo ...

Inject a <div> element into a table row upon clicking a button using jQuery and PHP

I am faced with an issue regarding displaying pdf reports within specific table rows. The scenario involves a table containing folder paths and buttons in each row. Upon clicking the button, the path is sent to a sequence of PHP files (ajax.php, ajax2.php, ...

PHP/HTML failing to upload extra large files

Allowing users to upload large files is causing an issue for me. I tested uploading a 22 MB file and it was successful. However, when I tried uploading a 200 MB file, it seems that the file does not enter the if block and instead returns the output from th ...

Customize items within a bootstrap5 navbar by utilizing traditional CSS styling techniques

Hello everyone, I'm currently working on creating a navigation bar using Bootstrap 5. I am looking to customize the colors of certain elements such as the navbar, tags, navbar background, and burger menu. I've attempted to adjust the tags' c ...

Is there a reason why using margin:auto doesn't work for vertically centering items?

I'm having trouble understanding how margin auto functions. In the scenario I'm dealing with, I have a parent div and a child div nested within it, both with specified widths and heights. I attempted to use margin auto in order to center the inne ...

Place the emoji where the cursor is located

My query was already posted on stack overflow but unfortunately, I did not receive a response. The issue revolves around 2 links named "add emoji 1" and "add emoji 2". As mentioned earlier, my question can be accessed here: Insert smiley at cursor positio ...

Swap the content of one div with another div using client-side code only

Currently, I am in the process of developing my personal website. To enhance user experience, I have implemented a vertical navigation bar on the left side of the page. My goal is to replace the content of a specific div with content from other HTML files ...

Tips for customizing the appearance of path elements within an external SVG file being utilized as a background image

How can I change the color of the paths in an SVG background image assigned to a div using CSS? ...

Tips on showcasing the elements within a div container using flexbox

Seeking advice on positioning items/cards using flexbox in my initial react app. The issue lies with the div within my card component where applying display: flex; turns every card into a block (column-like) structure, flexing only the content within each ...

How can I incorporate dynamic data to draw and showcase graphs on my website using PHP?

I am currently working on a project that requires displaying a graph showing the profit of users per day. Currently, I have implemented code to display a static graph on my page. <script type="text/javascript" src="https://www.google.com/jsapi">< ...

Hover over parts of an image to bring attention to them

I am interested in developing a webpage featuring a black and white image of 5 individuals. When hovering over each person, I would like them to illuminate and display relevant information in a dialog box next to them. Do you have any tips on how I can ac ...

The row and col classes will only expand to the full width if they are used within form or p elements

I am facing some unusual behaviors while using Bootstrap in my current project. It seems to be the first time I have encountered such issues. Below is the HTML code snippet: <div class="row" style="background-color: blue; padding: 2rem; ...

Using AJAX to dynamically edit and update PHP data

The information displayed on my index.php page is fetched from a database using the mysqli_fetch_array function. The data is presented in a table format with fields like Name, Age, Email, and Update. An edit button allows users to modify the data. Here is ...

Tips on implementing SCSS styles in my create-react-app application along with Material UI components?

I recently developed a React app using Create-React-App, and now I am looking to incorporate Material UI with styling through SCSS. https://i.sstatic.net/JLEjX.png To manage my styles, I created a main.css file using node-sass. While attempting to apply ...

Creating a JavaScript function triggered by a click event that will redirect to

I'm attempting to create an onclick function that will redirect you to a new page when you click on a div element, much like the A + href attribute in HTML. Any suggestions on how to achieve this? Should I just add an A tag inside the div? I'm un ...

Mastering Bootstrap: Achieving flawless column stacking in succession

I am currently integrating bootstrap into my existing HTML code, but I only require it in two specific sections to avoid rewriting the entire code. Everything looks fine on my 1080p screen as intended. However, when I resize the screen slightly The titl ...

What is the best way to create a @mixin incorporating variables to easily reference breakpoints using custom aliases?

Currently in the process of constructing a website using SASS coding, I have created several @mixins for Media Queries that will be utilized later with the @include directive. The structure of these mixins is as follows: _standards.sass // Media queries @ ...

The rendered typeface may vary from the specified font family

I've encountered an issue with a font on my website. I'm using DIN Regular True Type Font and declaring it as follows: @font-face { font-family: 'DINRegular'; src: url('../fonts/DINBek-Regular.ttf') format('truetype ...

JavaScript Empty Input Field when Duplicating Node

I am seeking advice on how to clear the textboxes when an HTML form is cleared. The following JS code runs onclick: var counter = 0; function moreField() { counter++; var newFields = document.getElementById('readroot').cloneN ...

Creating a logo that scales seamlessly across different web browsers while maintaining responsiveness through

I am working on making this logo align perfectly with all screen resolutions and browsers, while ensuring it stays at the exact center (both vertically and horizontally) of the #container. If the resolution drops below 320px, I want the company name to dis ...