Styling your div elements in CSS to shrink in width relative to their parent container

.container {
    max-width: 750px;
    height: 100px;
    background-color: red;
    margin: 0;
    padding: 0;
}
.box1 {
    width: 100px;
    height: 100%;
    float: left;
    background-color: black;
}
.box2 {
    width: 650px;
    height: 100%;
    float: left;
    background-color: black;
}

<div class="container">
    <div class="box1"></div><div class="box2"></div>
</div>

I am trying to ensure that .box2 shrinks proportionally as .container decreases in size. Unfortunately, setting max-width: 650px; does not seem to be effective. Instead of wrapping onto a new line when the browser window narrows, I want .box2 to maintain its maximum dimensions.

Answer №1

Strengthen those muscles like this:

.strengthen {
    display: flex;
}
.strengthen-block {
    flex: 1 1;
}

Check out the code snippet below:

.strengthen {
    display: flex;
}
.strengthen-block {
    flex: 1 1;
}
br {
    clear: both;
}
.container {
    max-width: 750px;
    height: 100px;
    background-color: red;
    margin: 0;
    padding: 0;
}
.elem1 {
    width: 100px;
    height: 100%;
    float: left;
    background-color: black;
}
.elem2 {
    width: 650px;
    height: 100%;
    float: left;
    background-color: black;
}
<div class="container">
    <div class="elem1"></div><div class="elem2"></div>
</div>
<br>
<strong>with strength...</strong>
<div class="container strengthen">
    <div class="elem1 .strengthen-block"></div><div class="elem2 .strengthen-block"></div>
</div>

Cross browser support for flex:

Answer №2

The issue at hand arises from the fixed width set on the second block, causing a break when both blocks can no longer fit in a single row.

To address this using floats, one solution is to utilize the calc() CSS function to determine the width of the parent minus 100px from the first block. This results in the following property for the second block:

width: calc(100% - 100px);

View jsfiddle example


Sample Code:

.parent {
  //max-width: 750px;
  height: 100px;
  background-color: tomato;
  margin: 0;
  padding: 0;
}
.block1 {
  width: 100px;
  height: 100%;
  float: left;
  background-color: purple;
}
.block2 {
  width: calc(100% - 100px);
  max-width: 650px;
  height: 100%;
  float: left;
  background-color: dodgerblue;
}
<div class="parent">
  <div class="block1"></div>
  <div class="block2"></div>
</div>


Alternatively, another approach is to eliminate floats and implement flexbox.


Check out the flexbox example here


.parent {
  //max-width: 750px;
  height: 100px;
  background-color: tomato;
  margin: 0;
  padding: 0;
  display: flex;
}
.block1 {
  flex: 0 0 100px;
  height: 100%;
  background-color: purple;
}
.block2 {
  flex-grow: 1;
  max-width: 650px;
  height: 100%;
  background-color: dodgerblue;
}
<div class="parent">
  <div class="block1"></div>
  <div class="block2"></div>
</div>

For further information on flexbox, refer to this resource.

Answer №3

1.) To maintain a consistent relationship between the two widths, consider utilizing percentage values for their width properties:

  • 100px = 100/750*100 percent of 750px = 13.3333%
  • 650px = 650/750*100 percent of 750px = 86.6666%

2.) Alternatively, if you want the 100px to remain fixed while allowing the second div to shrink, apply this rule to the second DIV:

width: calc(100% - 100px);

Answer №4

If you're looking to avoid using 'flexbox' and 'calc', there is another technique that involves utilizing margins to address your issue. This method will work effectively if you only need block2 and the parent block to be flexible.


Check out the example below:

.parent {
    max-width: 500px;
    height: 100px;
    background-color: red;
    margin: 0;
    padding: 0;
}
.block1 {
    width: 100px;
    height: 100%;
    float: left;
    background-color: blue;
}
.block2 {
    margin-left: 100px;
    height: 100%;
    background-color: red;
}
<div class="parent">
    <div class="block1"></div><div class="block2"></div>
</div>

In the code for block 2, I've included a margin that corresponds to the width of block 1. Additionally, I removed the 'float' property from block 2.

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

I encountered a problem in Javascript where I was unable to get the video.getCurrentTime() function to display the current time of

While everything else in my code is running smoothly with setInterval, there seems to be an issue with video.getCurrentTime() not displaying in the div id = "test" when the video is playing. I've been trying to figure out why this is happening. Here&a ...

Creating a flexible Tic Tac Toe board using only HTML and CSS without any JavaScript

I'm currently tackling an HTML layout challenge that involves creating a responsive Tic Tac Toe board using only HTML and CSS, without any JavaScript. This is how I have structured the layout of the board: <div class="board"> <div class= ...

Create an HTML table with a line separating two table cells

I need help with creating a table row and td's dynamically using basic HTML and CSS. I would like to draw lines between the td's similar to the example image shown below. Can someone please assist me in achieving this? https://i.sstatic.net/GPd ...

Identifying periods of inactivity using an embedded iframe

I have developed a website that showcases a three.js model within an iframe. My goal is to redirect users back to the homepage (index.html) after they have been inactive for a specified amount of time. While I have managed to achieve this using JavaScript, ...

Semantic UI table with rowspan feature

I am a beginner with semantic ui and I have been trying to replicate this specific layout. While going through semantic ui's documentation, I found something similar but not quite identical. Below is the HTML code: <div class="ui celled grid cont ...

Struggling to place the sans-serif font right at the edge of the H1 tag

Encountering a strange issue that suggests I might be overlooking something simple. Any thoughts on the following: I have a heading tag styled with a sans-serif font (loaded from Google fonts) and when I apply a background-color, the text does not align t ...

What is the best way to arrange my crimson blocks to sit next to each other?

Is there a way to align the red boxes side by side with some space in between them for separation, and how can I ensure that the signin/signup button appears against the blue background of the header? I attempted using float but encountered issues. The d ...

Simple Way to Modify Color of Active Page Link - HTML, CSS, and JavaScript

I found a helpful example on how to change link colors for the current page here. Here is the script I added: <script> // current page highlight $(document).ready(function() { $("[href]").each(function() { if (this.href == window.l ...

Error encountered using Meteor 1.3 autoform/quickform integration

Having recently transitioned to using Meteor 1.3, I've encountered a few challenges related to debugging due to missing imports or exports in tutorials. This particular issue seems to be in the same vein. I wanted to incorporate the autoform package ...

I'm currently facing an issue with toggling the visibility of a div using JavaScript

In my attempt to create a unique custom select menu using html, css, and javascript, I encountered an issue with toggling the display style of the div containing the options when clicking on the button (in this case, an arrow). HTML: <ul class="de ...

What changes should I make to my code in order to incorporate an additional level of submenu to my CSS-based dropdown menu?

Hello and thank you for your help, I currently have some CSS code that is working well for 3 levels of dropdown menus, but I am now in need of it to also support a 4th level. When I try to add the extra level by copying the 3rd level code and making adjus ...

What methods can I use to extract particular information from HTML when working with React?

I am currently working with an API that returns a preformatted HTML file containing CSS styles and ID tags that indicate specific information I need to extract. The challenge is that the HTML code is not very visually appealing, and I want to render it usi ...

Making an element sticky within a container while the parent element is set to overflow hidden

Here is a case where I have generated HTML and CSS code that includes classes for overflow, container height, and sticky positioning: .overflow-hidden { overflow: hidden; } .overflow-x-auto { overflow-x: auto; } .container { max-height: 100px; ...

Order of stacked divs with absolute positioning

I'm struggling to make a photo expand when hovered over without it expanding under the existing content. My current HTML and CSS code is as follows: .userCard {width:360px;height:180px;border:1px solid black;float:left;margin:10px;position:relative ...

Issues with IE9's CSS hover functionality are causing problems

This particular css style functions well on most browsers, but seems to have compatibility issues with Explorer 9 specifically when it comes to the :hover effect. There are instances where it works perfectly fine and other times when it doesn't work a ...

Using iScroll without animation by using scrolltoelement on an iPhone PhoneGap application

I've been working on an iOS PhoneGap app that uses Backbone and some other libraries. To handle scrolling, I implemented iScroll which has been functioning properly. However, I encountered a problem with the following code snippet: events: { &apo ...

Contrasting the utilization of percentage width in relative vs fixed contexts

I am facing an issue with the sizing of two div elements on my page, one with relative positioning and the other with fixed positioning. <div class="outer1"> </div> <div class="outer2"> </div> Here is the CSS: .outer1{ width: ...

"Google Chrome v84 does not render elements with zero height, line-height, and display: flex correctly

In Google Chrome v84, there seems to be an issue with line-height and display: flex (Chrome v83 and FireFox are not affected). The height of the element is not always enforced properly. <div class="outer-wrapper"> <div class="wrap ...

How can we consolidate an excess of menu items into a condensed, collapsed menu?

I have a navigation bar displaying category items, but if the menu items exceed the navbar width, how can I condense them into a drop-down menu using Bootstrap 3.0? Currently, my navbar only shows 10 items due to limited space. However, I have more items ...

Can an action be activated when the mouse comes to a halt?

Currently, I am having trouble triggering an event when a user stops moving their mouse over a div element. The current event is set up to show and follow another element along with the mouse movement, but I want it to only display when the mouse stops mov ...