0

I have two DIV elements with style="float:left;". I set the margin, width, etc. based on the size of the window. When the window is smaller I lower the width and margin CSS attribute values of the DIVs to make them appear next to each other (not below).

This looks nice in Firefox, however in Chrome and Opera the DIVs jump below each other for a moment before my JavaScript runs. After a moment the screen looks nice again.

The question: Is there a way to catch a window.onresize event and manipulate CSS before the effects of the resize event are visible on the browser?

4
  • 1
    Without seeing your particular code, it's likely you can accomplish all your styling with media queries and without using javascript at all. There are lots of tutorials on the web, including [this one][1] by Nick La. [1]: webdesignerwall.com/tutorials/… Commented Jul 29, 2013 at 19:24
  • What about width: 50% ( lower this based on your paddings/margins ) use float left or display inline-block depending what you like. Commented Jul 29, 2013 at 19:25
  • I second @AdamHart, a concrete example and use case would reduce a great deal of ambiguity in your question. Commented Jul 29, 2013 at 19:38
  • The demo site that Adam Hart sent is pretty cool. However, when I resize the window in chrome it also blinks. As much as I saw the demo uses percentage divs. What I am implementing is a kind of grid where there widths and spaces based on the available space can be calculated with a bit special equotations. Commented Jul 29, 2013 at 20:08

2 Answers 2

2

You could use media-queries. These are the prefered method for responsive design.

CSS

 div {
        margin-left: 20px;
    }

    @media (max-width: 600px) {
      div {
        margin-left: 5px;
      }
    }

In this example the div will have a margin-left of 5px on screens that are 600px in width or lower. This is part of your CSS so no waiting for things to load, it loads with the rest of your CSS.

Sign up to request clarification or add additional context in comments.

1 Comment

His question explicitly mentions modifying the width, which means that the variable event he is looking for must be change in height.
0

This is because Firefox waits for javascript to load before rendering the page. Chrome and Opera display the page, regardless of whether javascript has loaded or not. Try writing javascript to set the effected body to visibility: hidden; initially, and changing it to "visibility: visible;" after the javascript has executed.

Update:

Set visibility: hidden; in the css.

And add this inside of your <head></head>:

<noscript>
    body { visibility: visible; }
</noscript>

This will ensure the body is invisible until the javascript has loaded. In the case that javascript isn't enabled for the device, it defaults to visibility: visible;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.