6

I'm wondering if there is a way to change the order of HTML content depending on screen size.

This is the main website header, it shows correctly on every media query.

<h1>title</h1>
<nav>inline list items</nav>
<h2>subtitle</h2>

Basicly it looks like this:

H1 TEXT
H2 TEXT-----------------LIST ITEMS

(or check out http://www.jeroenduijker.nl/test/ and resize the screen)

As soon as the media query steps in (@media only screen and (max-width: 480px) {}) , the h2 gets squished, and i'd prefer to put the nav bar above the h1.

Is there an easy way to make the html go:


-----------------LIST ITEMS
H1 TEXT
H2 TEXT

Hopefully i've explained it well enough, i hope you can help me out, i'd appreciate it!

0

1 Answer 1

13

You could write two nav bars and have one hidden and the other visible, and then just switch that based on the media query.

HTML:

<nav class="show-mobile">
      ...
</nav>

<h1>title</h1>
<nav class="hide-mobile">
      ...
</nav>
<h2>subtitle</h2>

CSS:

@media only screen and (max-device-width: 480px) {
      .show-mobile {
        display: block;
      }
      .hide-mobile {
        display: none;
      }
    }

 @media only screen and (min-device-width: 481px) {
      .show-mobile {
        display: none;
      }
      .hide-mobile {
        display: block;
      }
    }

Or two H1 elements, one before the nav and one after and have them switch visibility based on the media query.

<h1 class="hide-mobile"></h1>
<nav>
  ...
  ...
</nav>
<h1 class="show-mobile">Title</h1>
<h2>subtitle</h2>

Or you could tell the nav to have absolute positioning at the top when the mobile media query is active.

@media only screen and (max-device-width: 480px) {
  nav {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 44px
  }
}
3
  • I've tried all three options, but hiding the element doesn't "remove" it, so it stays there, and still squishes the h2 to the side unfortunately :( edit; the third option might work, but i'll have to edit a bit of the location, because it doesn't stay inline with the other stuff anymore
    – Redavac
    Oct 24, 2013 at 18:34
  • I used the absolute option, as it moves the element around, and just positioned it correctly, and changed some other css for the media query, thanks!
    – Redavac
    Oct 24, 2013 at 18:45
  • @Jeroen the class isn't going to just work on it's own you'd actually need to create the class in your CSS as well. See my edit. You might also want to spend sometime reading on some CSS Media Query fundamentals. Oct 24, 2013 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.