Is there a way to have the width of divs fluctuate within a specified range before they move to a new line

I have a main container with two nested elements.

<div id="middleHolder">
  <a href="#">
     <img>
  </a>
  <div id="sidebar">
     <p>content</p>
  </div>

I am looking to achieve a layout where the link is floated to the left and covers the full size of the image (655px X 506px). The sidebar should also be floated left, aligning with the link when its width is between 33.5% and 27%. If the width becomes smaller than this range, I want the sidebar to move onto a new line and occupy the full width.
Is there a way to dynamically adjust the width of a div within a specific range before allowing it to break onto a new line without using multiple media queries?

Here is the CSS code provided:

#middleHolder > a {
  float: left;
  margin: 0 2% 2% 0;
  max-width: 655px;
  width: 100%;
  }

#socialMediaSidebar {
  float: left
  min-height: 506px;
  min-width: 270px;
  width: 27-33.5%;  /* Not accurate, but demonstrates desired behavior */
  }

  @media width… /*specific breakpoint would trigger once socialMediaSidebar falls below 270px */

#socialMediaSidebar {
  width: 100%;
  }

Answer №1

By combining max-width and min-width values, you can ensure that the content will always stay within a specified range. If the content cannot fit within this range, it will display as a block and drop down to the next line in the document flow.

Here's an example:

#test {
    min-width: 25%;
    max-width: 37%;
    background: #CBA;
}

Check out how it works here:

http://jsfiddle.net/e4j78ek6/1/

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

Is the User Agent Stylesheet giving h1 elements a default bold setting?

My h1 is appearing bold due to the user agent stylesheet, but I want it to be normal. How can I override this? h1 { display: block; font-size: 2em; -webkit-margin-before: 0.67em; -webkit-margin-after: 0.67em; -webkit-margin-start: 0px; -webkit-margin-end: ...

"PHP: Are you sure you want to delete? Confirm your

I'm trying to add a delete account button on my website, but I'm struggling with the implementation. Here's what I have so far: Javascript: <script language = "JavaScript" > function deleteUser(id) { if (confirm ...

Traverse an array to generate a horizontal chart

My goal is to dynamically create a horizontal table by mapping through an array, similar to the design shown in this image. https://i.sstatic.net/YfElb.png This is how I am currently mapping through the array: const glanceGames = this.state.gameData.map ...

Display only distinct items in a v-for loop in Vue.js

I am attempting to show icons based on specific v-for and v-if conditions. However, the icons are appearing multiple times when I only want them to display once. I attempted using v-if = 'index === 0', but this did not resolve the issue. <di ...

Implementing a different background image for every menu item when hovering over its parent

I am trying to figure out how to achieve this. My menu is placed in a container that is 500px high, with a background image. I would like the background image to change when hovering over a parent menu item. I believed I could do this using something like: ...

I'm looking for a skilled CSS spinner creator. Can anyone recommend one?

Can anyone recommend a good online application for creating CSS spinners? I've been searching and only found one on David Walsh's blog, but it only has one available. Any suggestions? ...

Repainting can be an issue if the child element has not been transformed

My parent map has a large number of children pins, and I am experiencing significant slowdown while panning the map due to continuous repainting. Strangely, this issue is resolved when tapping on a child's pin and transforming the thumbnail inside it. ...

What are the steps to create two frames using Twitter Bootstrap?

I'm just starting to work with Twitter Bootstrap and I need some help. Can someone assist me in creating a two-column layout inside the HTML, where the menu in the header stays visible even when scrolled down? I've included my HTML code below. Th ...

Adding padding to navigation items in Bootstrap 5 when the collapsible navbar is expanded

How can I modify the CSS rules for nav items in a Bootstrap 5 collapsible navbar to have proper padding and margins only when they are expanded and the buttons are on the right side? <!DOCTYPE html> <html lang="en"> <head> <meta ...

Tips for ensuring a form element remains at the bottom of the screen even when scrolling

Hey there, I have a question for you. If you check out my website and scroll all the way down, you will come across an email marketing search box. What I want is to have this box always visible on the screen, fixed at the bottom so that it stays in view ...

The issue of HTML5 audio not working when played multiple times on an Android 4.0.4 device's Native Browser

Currently engaged in a project utilizing HTML5 technology. Encountering an issue with playing the same audio file multiple times on the same page using the Android native browser. The problem is specifically observed on Android ICS 4.0.4. In this version, ...

Translucent tonal backdrop hue

Is there a creative solution to dynamically increase the width of a border up to a certain point, using the thickness of the border as a progress bar indicator while reusing defined colors in stylesheets? Currently, I have the element with the border nest ...

When using IE8 with pie.htc, implementing html5shiv.js causes rounded corners to disappear once more

While everything is running smoothly in most browsers, there seems to be an issue with IE8. After connecting pie.htc to address the rounded corners problem, everything seemed fine until html5 issues cropped up in IE8. To combat this, I implemented html5shi ...

Steps to create a div and render all its child elements unselectable

I attempted to create a div along with all its child elements as non-focusable. I initially used a tabIndex of -1 on the main div, but this only resulted in the focus shifting to its first focusable child (as per the default behavior). Is there a workaroun ...

Adjusting the Scroll on the Slider Container

I'm currently developing a dynamic news feed feature that incorporates slides to display a brief summary along with a "Read More" button. Clicking on the "Read More" button will expand the content, pausing the scrolling mechanism until minimized by cl ...

Divide a compound element of TextNodes and elements by utilizing jQuery

Looking for a way to split an HTML element based on user selection using jQuery? Check out the example below with square brackets indicating the selection: Lor[em <a>ips]um <span>dolor</span></a> The desired output should be: Lor ...

Can data be transferred between browsers, such as from Google Chrome to Mozilla Firefox?

I have a dilemma with two separate pages—one for Admin and the other for User. The Admin page is named index.html <!DOCTYPE html> <html lang="en"> <head> <style> * { padding: 0; ...

Creating a dynamic CSS height for a div in Angular CLI V12 with variables

Exploring Angular development is a new venture for me, and I could use some guidance on how to achieve a variable CSS height in Angular CLI V12. Let me simplify my query by presenting it as follows: I have three boxes displayed below. Visual representatio ...

What is the most efficient way to access a cell within an HTML table using jQuery or the Document Object Model (

I have an unchangeable HTML table styled with CSS. My goal is to extract the value from the first column for filtering purposes. I've been struggling to locate a suitable jQuery or DOM function to accomplish this task. Unable to find a way to access ...

What is the best way to conceal a portion of a child element that exceeds the size of its parent container?

My <div> contains a <p>, but sometimes the text in the <p> exceeds the boundaries of the div. <div style="width: 100px'; height='100px';"> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit ...