Search code examples
csshtmlmobilemedia-queriespage-layout

Repositioning page layout with CSS


I have a problem where I wish to alter the positioning of webpage using CSS for a mobile view. The current set up for desktop is a sidebar floated to the left and a textbox floated to the right. The HTML is as follows:

<div id="container">
   <div id="sidebar">...</div>
   <div id="textbox">...</div>
</div>

My dilemma is that on mobile devices I wish for the sidebar content to appear under the textbox and I am struggling to produce the CSS to do this. I am using media queries to target smart phones eg:

@media screen and (max-width:479px) {
  /* Target portrait smartphones */
}

Can anyone provide me with the CSS to do this? Thanks in advance.


Solution

  • It's pretty simple, in order to do that you should use it like this

    <div id="container">
       <div id="textbox">...</div>
       <div id="sidebar">...</div>
    </div>
    

    The textbox first because you'll remove float for mobile. So the textbox goes first then the sidebar.

    The css:

    .textbox {
        width: ***px;
        float right;
    }
    .sidebar {
        width: ***px;
        float left;
    }
    @media screen and (max-width:479px) {
      /* Target portrait smartphones */
      .textbox, .sidebar {
         float: none;
      }
    }
    

    Hope this helps ! :)