When the browser width is insufficient to display everything side by side, items will wrap to the next

I'm working on a sleek top menu design that features 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 want to ensure that:

  • when the browser width allows for everything to be inline, the list of links should align to the right (float: right)

  • when the browser width does not allow for everything to be inline, the list of links should align to the left

How can I achieve this using the code mentioned above?


Note: The "wrap effect" should only occur precisely when

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

and not based on a static value like width(browser)<300px.

Answer №1

$(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>

Inspecting on page load, in case

width(#left) + width(#right) is greater than width of browser
, then modify the CSS property float of div#right 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');;}
    });

Answer №2

At the moment, media queries do not have support for individual elements, so you will need to rely on Javascript to accomplish this.

An alternative solution is to use a library like css-element-queries. This allows you to create element-specific media queries without the need for writing additional javascript code.

If you prefer to do it manually, you can utilize the following code snippet when the document loads:

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

To ensure this functionality is maintained when the window is resized, include the following code:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with JavaScript Variable Not Reflecting Changes in Progress Bar

I'm in the midst of developing a game and have incorporated a progress bar to facilitate the incrementation of certain values on my HTML page. However, once the progress bar hits 100%, it updates the cost and level only once. After that initial update ...

Having trouble getting my local website to load the CSS stylesheet through Express and Node.js in my browser

https://i.stack.imgur.com/qpsQI.png https://i.stack.imgur.com/l3wAJ.png Here is the app.js screenshot: https://i.stack.imgur.com/l3wAJ.png I have experimented with different combinations of href and express.static(""); addresses. However, I am ...

What is the best way to implement Bootstrap in Coda2?

I am new to web design and I am trying to incorporate Bootstrap into my HTML code using Coda2. Since I don't have access to a server, I am referencing the directory address of CSS files on my hard drive instead of a web address. Currently, the beginni ...

What is the functionality of intro.js?

Currently, I am utilizing an onboarding tour at my.bonify.de that provides a similar experience to introjs. The way we have implemented this feature is quite unattractive, using a cutout div with a very large box-shadow. We are looking to enhance it by in ...

`finding difficulty in presenting components in react`

I am encountering issues with an outdated version of CRUD where some codes are no longer functioning as expected. Visual Studio Code does not flag them as errors, so the page continues to load but my content box remains empty. Can someone assist me with th ...

The HTML elements in my JSX code seem to constantly shift around whenever I resize my webpage

Using react.js, I'm currently developing a website that appears like this before resizing: pre-resize_screenshot_website However, upon vertical or diagonal resizing, the layout becomes distorted especially in the 'availability search bar' ...

Empty array returned when using wrapper.classes() with Vue-test-utils and CSS modules

Recently I started using Vue Test Utils along with CSS Modules. My Vue component is all set up with CSS Modules and it functions perfectly in the app as well as in Storybook. Take my BasicButton for example: <template> <button :class="[ ...

The color of hyperlinks in firefox varies based on the specific URL being visited

I am a beginner in the world of html and css. I have two links placed consecutively. In my css file, I have defined classes for a:link and a:hover. However, I noticed that the second link is displaying a purple color instead of silver. Both links correctly ...

Adjust the color palette size when the zoom level is modified in the responsive mode within Angular

I am currently working with Angular 15 and attempting to adjust the color palette size depending on the zoom level. <input #primaryColor (change)="colorChange($event,'primary')" formControlName="primaryColor" class="co ...

Using an Icon Button within a Material Card in React

I am attempting to place the edit icon button on the right side of my card, similar to the image shown below. Please refer to this sandbox link for the current output: VIEW IMAGE HERE SEE SANDBOX LINK HERE <CardHeader classes={{ ...

Encountered an error in Angular1: TypeError - promise.catch does not exist as a

Upon using angular-ui-router, I encountered the following error while clicking on the links view1 and view2 in index.html. The same example worked with the regular angular router. Is there something missing in the code? Thanks. TypeError: promise.catch i ...

Tips for overlaying an image on a div regardless of its height

(!) Although this question may seem repetitive, I have not been able to find a suitable solution in any of the previous 10 topics. I apologize for the inconvenience and am actively seeking a resolution to this unique situation; Allow me to outline the iss ...

Disabled button in Bootstrap4 displaying the wrong color as white instead of the intended color

On one of my pages, I have a bootstrap button that is disabled but the styling doesn't seem to be working correctly. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.mi ...

What is the best way to showcase images along with their captions in a horizontal layout

I am facing an issue with displaying the last four images at the end of the page horizontally. When there is no caption, I can use the display:flex property to achieve this layout, but as soon as I add a caption, the images are displayed vertically instead ...

Damaged background in Bootstrap interface modal

https://i.stack.imgur.com/nTAnK.png Is there anyone who can assist me in identifying the issue causing the broken or irregular backdrop in the background of overlays or pop-ups? $scope.Modal = $modal.open({ animation: true, ...

Dragging an image within a div using JQueryUI

Something strange is happening with my code. I am utilizing JQueryUI to enable dragging of div elements that contain an image tag. The issue arises when I start dragging the div - the image, which was perfectly centered within the div while stationary, now ...

Focus border radius for anchor tag image

I am trying to implement the following CSS: .sky:focus {border-radius: 25px; border: #000 solid 1px; outline: none} And my HTML looks like this: <a class='sky' href='#'><img src='cloud.jpg'></a> I want a 25 ...

All hyperlinks are displayed as a single entity

I have 5 image links displayed side by side, but when I hover over them, they act as one collective link. Can someone please assist me with resolving this issue? After updating my code to include the entire div, it seems that there is something within the ...

Reposition the element at the bottom of its parent element

I am facing an issue with the HTML structure in my application: <div class="work-item"> <div class="image-container"> </div> <div class="text-container"> </div> </div ...

two divs side by side with adjustable sizes

Is there a way to achieve a typical forum post layout with user info on the left and the post itself using CSS? I tried wrapping two divs around another div and using float, but encountered an issue where the floated div remains on the left instead of next ...