Asked  6 Months ago    Answers:  5   Viewed   37 times

I would like to use media queries to resize elements based on the size of a div element they are in. I cannot use the screen size as the div is just used like a widget within the webpage, and its size can vary.

Update

Looks like there is work being done on this now: https://github.com/ResponsiveImagesCG/cq-demos

 Answers

85

After nearly a decade of work — with proposals, proofs-of-concept, discussions and other contributions by the broader web developer community — the CSS Working Group has finally laid some of the groundwork needed for container queries to be written into a future edition of the CSS Containment spec! For more details on how such a feature might work and be used, check out Miriam Suzanne's extensive explainer.

Hopefully it won't be much longer before we see a robust cross-browser implementation of such a system. It's been a grueling wait, but I'm glad that it's no longer something we simply have to accept as an insurmountable limitation of CSS due to cyclic dependencies or infinite loops or what have you (these are still a potential issue in some aspects of the proposed design, but I have faith that the CSSWG will find a way).


Media queries aren't designed to work based on elements in a page. They are designed to work based on devices or media types (hence why they are called media queries). width, height, and other dimension-based media features all refer to the dimensions of either the viewport or the device's screen in screen-based media. They cannot be used to refer to a certain element on a page.

If you need to apply styles depending on the size of a certain div element on your page, you'll have to use JavaScript to observe changes in the size of that div element instead of media queries.

Alternatively, with more modern layout techniques introduced since the original publication of this answer such as flexbox and standards such as custom properties, you may not need media or element queries after all. Djave provides an example.

Tuesday, June 1, 2021
 
redrom
answered 6 Months ago
18

Unless you have more style sheets than that, you've messed up your break points:

#1 (max-width: 700px)
#2 (min-width: 701px) and (max-width: 900px)
#3 (max-width: 901px)

The 3rd media query is probably meant to be min-width: 901px. Right now, it overlaps #1 and #2, and only controls the page layout by itself when the screen is exactly 901px wide.

Edit for updated question:

(max-width: 640px)
(max-width: 800px)
(max-width: 1024px)
(max-width: 1280px)

Media queries aren't like catch or if/else statements. If any of the conditions match, then it will apply all of the styles from each media query it matched. If you only specify a min-width for all of your media queries, it's possible that some or all of the media queries are matched. In your case, a device that's 640px wide matches all 4 of your media queries, so all for style sheets are loaded. What you are most likely looking for is this:

(max-width: 640px)
(min-width: 641px) and (max-width: 800px)
(min-width: 801px) and (max-width: 1024px)
(min-width: 1025px)

Now there's no overlap. The styles will only apply if the device's width falls between the widths specified.

Wednesday, June 2, 2021
 
Corsair
answered 6 Months ago
61

You can have a large number of specific files modularized quite nicely and extremely specifically depending on what device is being targeted, and only the files you need will be retrieved.

This (the emphasized portion) is a very common misconception about stylesheets loaded via media queries in <link> elements. (What you said about modularization of files still holds true.)

All files will be loaded regardless of the media queries being applied to <link> elements. A file is not loaded only when a media query is met, because there is no guarantee that the browser won't meet it (or even stop meeting it) when some of its properties change while viewing the same page.

For instance if you rotate a mobile device from portrait to landscape, it would stop matching (orientation: portrait) and start matching (orientation: landscape). It needs to make sure the styles in the latter are ready to apply as it switches to that format, so the styles have to be loaded beforehand.

The HTML5 spec for the <link> element does not make any mention of how the media attribute should determine whether or not a resource should be loaded. The Media Queries spec doesn't define how style sheets should be loaded with respect to media queries, however it does state this in a note:

User agents are expected, but not required, to re-evaluate and re-layout the page in response to changes in the user environment, for example if the device is tilted from landscape to portrait mode.

Sunday, August 15, 2021
 
mikelovelyuk
answered 4 Months ago
19

Happy !!! I got it :D

The only problem is that for a new resizing the browser move. It will be necessary to refresh the page, because window.onload = zoomIn;

Solution! I removed queryes screen of the css and:

widthscrencan = (window.innerWidth > 0) ? window.innerWidth : screen.width; // capture width screen onload
canvasScale = 1; //global  

   if (widthscrencan <=360){ 

      function zoomIn() {
          var SCALE_FACTOR = .37;
          canvasScale = canvasScale * SCALE_FACTOR;

      canvas.setHeight(canvas.getHeight() * SCALE_FACTOR);
      canvas.setWidth(canvas.getWidth() * SCALE_FACTOR);

      var objects = canvas.getObjects();
      for (var i in objects) {
          var scaleX = objects[i].scaleX;
          var scaleY = objects[i].scaleY;
          var left = objects[i].left;
          var top = objects[i].top;

          var tempScaleX = scaleX * SCALE_FACTOR;
          var tempScaleY = scaleY * SCALE_FACTOR;
          var tempLeft = left * SCALE_FACTOR;
          var tempTop = top * SCALE_FACTOR;

          objects[i].scaleX = tempScaleX;
          objects[i].scaleY = tempScaleY;
          objects[i].left = tempLeft;
          objects[i].top = tempTop;

          objects[i].setCoords();
      }


      canvas.renderAll();
      alert(widthscrencan+' function 360'); 

  }

   window.onload = zoomIn;
 }






   if ((widthscrencan >=361) || (widthscrencan ==768)){ 

            function zoomIn768() {
          var SCALE_FACTOR = .45;
          canvasScale = canvasScale * SCALE_FACTOR;

          canvas.setHeight(canvas.getHeight() * SCALE_FACTOR);
          canvas.setWidth(canvas.getWidth() * SCALE_FACTOR);

          var objects = canvas.getObjects();
          for (var i in objects) {
              var scaleX = objects[i].scaleX;
              var scaleY = objects[i].scaleY;
              var left = objects[i].left;
              var top = objects[i].top;

              var tempScaleX = scaleX * SCALE_FACTOR;
              var tempScaleY = scaleY * SCALE_FACTOR;
              var tempLeft = left * SCALE_FACTOR;
              var tempTop = top * SCALE_FACTOR;

              objects[i].scaleX = tempScaleX;
              objects[i].scaleY = tempScaleY;
              objects[i].left = tempLeft;
              objects[i].top = tempTop;

              objects[i].setCoords();
          }


          canvas.renderAll();
          alert(widthscrencan+' function 768'); 


        }
          window.onload = zoomIn768;
    }

   if ((widthscrencan >=769) || (widthscrencan ==992)){ 

            function zoomIn992() {
          var SCALE_FACTOR = .5;
          canvasScale = canvasScale * SCALE_FACTOR;

          canvas.setHeight(canvas.getHeight() * SCALE_FACTOR);
          canvas.setWidth(canvas.getWidth() * SCALE_FACTOR);

          var objects = canvas.getObjects();
          for (var i in objects) {
              var scaleX = objects[i].scaleX;
              var scaleY = objects[i].scaleY;
              var left = objects[i].left;
              var top = objects[i].top;

              var tempScaleX = scaleX * SCALE_FACTOR;
              var tempScaleY = scaleY * SCALE_FACTOR;
              var tempLeft = left * SCALE_FACTOR;
              var tempTop = top * SCALE_FACTOR;

              objects[i].scaleX = tempScaleX;
              objects[i].scaleY = tempScaleY;
              objects[i].left = tempLeft;
              objects[i].top = tempTop;

              objects[i].setCoords();
          }


          canvas.renderAll();
          alert(widthscrencan+' function 992'); 


        }
          window.onload = zoomIn992;
    }

      if ((widthscrencan >=993) || (widthscrencan ==1200)){ 

            function zoomIn1200() {
          var SCALE_FACTOR = .9;
          canvasScale = canvasScale * SCALE_FACTOR;

          canvas.setHeight(canvas.getHeight() * SCALE_FACTOR);
          canvas.setWidth(canvas.getWidth() * SCALE_FACTOR);

          var objects = canvas.getObjects();
          for (var i in objects) {
              var scaleX = objects[i].scaleX;
              var scaleY = objects[i].scaleY;
              var left = objects[i].left;
              var top = objects[i].top;

              var tempScaleX = scaleX * SCALE_FACTOR;
              var tempScaleY = scaleY * SCALE_FACTOR;
              var tempLeft = left * SCALE_FACTOR;
              var tempTop = top * SCALE_FACTOR;

              objects[i].scaleX = tempScaleX;
              objects[i].scaleY = tempScaleY;
              objects[i].left = tempLeft;
              objects[i].top = tempTop;

              objects[i].setCoords();
          }


          canvas.renderAll();
          alert(widthscrencan+' function 1200'); 


        }
          window.onload = zoomIn1200;
    }


      if (widthscrencan >=1300){ 

            function zoomIn1300() {
          var SCALE_FACTOR = 1.01;
          canvasScale = canvasScale * SCALE_FACTOR;

          canvas.setHeight(canvas.getHeight() * SCALE_FACTOR);
          canvas.setWidth(canvas.getWidth() * SCALE_FACTOR);

          var objects = canvas.getObjects();
          for (var i in objects) {
              var scaleX = objects[i].scaleX;
              var scaleY = objects[i].scaleY;
              var left = objects[i].left;
              var top = objects[i].top;

              var tempScaleX = scaleX * SCALE_FACTOR;
              var tempScaleY = scaleY * SCALE_FACTOR;
              var tempLeft = left * SCALE_FACTOR;
              var tempTop = top * SCALE_FACTOR;

              objects[i].scaleX = tempScaleX;
              objects[i].scaleY = tempScaleY;
              objects[i].left = tempLeft;
              objects[i].top = tempTop;

              objects[i].setCoords();
          }


          canvas.renderAll();
          alert(widthscrencan+' function 1300'); 


        }
          window.onload = zoomIn1300;
    }
Thursday, August 19, 2021
 
Gabriele Mariotti
answered 4 Months ago
84

it's bug in ionic, once you focus on any input the keyboard will show up and will add padding-bottom for the scroll-content class to lift the for above the keyboard and it doesn't remove the padding-bottom after you close the keyboard. I tried to check if I have any JS event on the mobile keyboard but we don't so my work around is to set a fixed padding-bottom for the scroll-content class to prevent changing it on the runtime.

.scroll-content {
    padding-bottom: 0 !important;
 }
Saturday, August 21, 2021
 
mgierw
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share