3

I'm designing a simple top-menu with a logo on the left, and inline-links on the right:

* { margin:0; padding: 0; }
#header { background-color: grey; }
#right { background-color: blue; float:right; }
#left { background-color: green; float:left; }
li { display:inline-block; padding: 0 30px; }
 <div id="header">
   <div id="left">LOGOLOGOLOGOLOGOLOGOLOGO</div> 
   <div id="right">
     <ul>
       <li>Link 1</li>
       <li>Link 2</li>
       <li>Link 3</li>
       <li>Link 4</li>
      </ul>
   </div>
 </div>

I would like that :

  • when the browser width is enough for everything to be inline, the links-list should be aligned on the right (float: right)

  • when the browser width is not enough for everything to be inline, the links-list should be aligned on the left

enter image description here

How to do it with my code mentioned before?


Note: I would like the "wrap effect" to appear exactly when

width(#left)+width(#right)>width(browser)

and not with something static like width(browser)<300px.

Basj
  • 41,386
  • 99
  • 383
  • 673

2 Answers2

2

$(document).ready(function(){
 $( window ).resize(function(){
var lwidth=parseInt($('#left').css('width'));
var rwidth=parseInt($('#right').css('width'));
var fwidth=lwidth+rwidth;
var swidth=$(window).width()
if (fwidth > swidth){
    $('#right').css('float','left');}
else{$('#right').css('float','right');;}
});
 
});
* { margin:0; padding: 0; }
#header { background-color: grey; }
#right { background-color: blue; float:right; }
#left { background-color: green; float:left; }
li { display:inline-block; padding: 0 30px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div id="header">
   <div id="left">LOGOLOGOLOGOLOGOLOGOLOGO</div> 
   <div id="right">
     <ul>
       <li>Link 1</li>
       <li>Link 2</li>
       <li>Link 3</li>
       <li>Link 4</li>
      </ul>
   </div>
 </div>

check on load, if width(#left)+width(#right)>width(browser) then change div#right float from right to left.

     $( window ).resize(function(){
    var lwidth=parseInt($('#left').css('width'));
    var rwidth=parseInt($('#right').css('width'));
    var fwidth=lwidth+rwidth;
    var swidth=$(window).width()
    if (fwidth > swidth){
        $('#right').css('float','left');}
else{$('#right').css('float','right');;}
    });
Jayababu
  • 1,641
  • 1
  • 14
  • 30
1

Media queries don't support individual elements yet, so you're going to have to use Javascript to do this.

I would suggest using css-element-queries. It allows you to write element based media queries so you don't have to write any javascript.

If you want to do it yourself, when the doc has loaded, use something like

  if (document.getElementById('left').offsetWidth + document.getElementById('right').offsetWidth > window.innerWidth) {
      //apply styles using javascript
  }

To do this when the window resizes:

  window.onresize = function(event) {
      if (document.getElementById('left').offsetWidth + document.getElementById('right').offsetWidth > window.innerWidth) {
        //apply styles using javascript
      }
  };
Community
  • 1
  • 1
Josh Durham
  • 1,632
  • 1
  • 17
  • 28
  • 2
    I used media queries before, but here I would like the wrap to appear **exactly** when `width(#left)+width(#right)>width(browser)` and **not** with something static like `300px`. How to do that? – Basj May 12 '15 at 12:24
  • I think you're going to have to use javascript to do this. – Josh Durham May 12 '15 at 12:29
  • Are you sure @JPDurham ? I hoped there would be a way without JS – Basj May 12 '15 at 12:42
  • I wanted to find a solution for the top-menu of http://www.samplerbox.org when we resize the browser width. Any idea @JPDurham? – Basj May 12 '15 at 12:45
  • You could try using [a library to simulate this](https://github.com/marcj/css-element-queries). You'll be able to use css, although it does use javascript to achieve this. – Josh Durham May 12 '15 at 12:49
  • @Basj did either of the suggestions in my answer work for you or are you still looking for another solution? – Josh Durham May 13 '15 at 15:44