Placing two items at opposite extremes of a full-width display with a header

Despite the abundance of resources on CSS positioning, I constantly find myself revisiting this topic because it continues to perplex me with no clear solution in sight.

Let's simplify things. Imagine a header div with a width of 100%, like this:

<div style="width: 100%; height: 100px;">

</div>

Now, within this div on the left side of the screen are 4 links enclosed in a container div while on the right side, there are two links within another container div. These containers have set widths, leaving empty space in the middle.

<div style="width: 100%; height: 100px;">

<div style="width: 400px">
<div style="float: left; margin-left: 5px;">link1</div>
<div style="float: left; margin-left: 5px;">link2</div>
<div style="float: left; margin-left: 5px;">link3</div>
<div style="float: left; margin-left: 5px;">link4</div>
</div>

<div style="width: 200px">
<div style="float: left; margin-left: 5px;">link1</div>
<div style="float: left; margin-left: 5px;">link2</div>
</div>

</div>

The dilemma is how to make these fixed-sized containers align at each end of the fluid 100% container without overlapping or dropping below when the browser window is resized. The goal is to avoid scrollbars or JavaScript solutions altogether.

Is there a way for these fixed-size containers to remain snugly positioned as the page shrinks?

Floats and inline-block display properties haven't provided a resolution. Creating a large fixed-size container is not an option as the width must be maintained at 100%. Currently relying on Javascript to hide the right links upon shrinking the page size, which is less than ideal.

Is there truly no workaround for this predicament?

Answer №1

Embracing the power of flexible boxes!

#container {
  display: flex; /* Unlocking the magic! */
  height: 100px;
  border: 3px solid red;
}
#left-column {
  width: 400px; /* Ideal width, adjusts for narrow windows */
  border: 3px solid blue;
}
#right-column {
  width: 200px; /* Ideal width, adjusts for narrow windows */
  margin-left: auto; /* Move it to the right */
  border: 3px solid green;
}
#container > div > div { /* Additional styles */
  float: left;
  margin-left: 5px;
}
<div id="wrapper">
  <div id="left">
    <div>link1</div>
    <div>link2</div>
    <div>link3</div>
    <div>link4</div>
  </div>
  <div id="right">
    <div>link1</div>
    <div>link2</div>
  </div>
</div>

Answer №2

To ensure proper layout, it is recommended to set a minimum width of 600px along with using the width:100% on the container div. Without this setup, the options left are for elements to either overlap (using position: absolute) or stack below each other (floats alone). Depending on the browser used, it will respect the min-width specified, or at the very least keep the right/left links in place.

If needed, CSS media queries can be implemented to show/hide elements at different screen widths as desired.

It's advisable not to utilize inline styles like this example demonstrates, as it limits flexibility and makes updating more complicated.

.outer {
  width: 100%;
  min-width: 600px;
  height: 100px;
  background-color: #555;
}

.leftLinks {
  width: 400px;
  position: absolute;
  float: left;
  background-color: #999;
}

.rightLinks {
  width: 200px;
  position: relative;
  float:right;
  background-color: #999;
}

li {
  float: left;
  display: inline-block;
  margin-left: 5px;
}
<div class="outer">

<div class="leftLinks">
  <ol>
    <li>link1</li>
    <li>link2</li>
    <li>link3</li>
    <li>link4</li>
  </ol>
</div>

<div class="rightLinks">
  <ol>
    <li>link1</li>
    <li>link2</li>
  </ol>
</div>

</div>

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

When working with basic shapes such as "rect" or "circle," the inline SVG may not be displayed

This is the code we are using to style our checkboxes: .checkbox-default { display: inline-block; *display: inline; vertical-align: middle; margin: 0; padding: 0; width: 22px; height: 22px; background: url('data:image/ ...

What is the reason that setTimeout does not delay calling a function?

I am looking to develop a straightforward game where a div is duplicated recursively after a short interval. Each new div should have a unique ID (ID+i). The concept is to continuously generate divs that the user must click on to remove before reaching a ...

How to deactivate or modify the href attribute of an anchor element using jQuery

My HTML code looks something like this: <div id="StoreLocatorPlaceHolder_ctl07_ctl00_ctl00_pager_ctl00_ctl00_numeric" class="sf_pagerNumeric"> <a href="http://www.domain.com/store-list">1</a> <a href="http://www.domain.com/sto ...

How to create a captivating image effect using CSS on hover

I am attempting to create an animated image on hover. The desired outcome is similar to the one showcased here: Check it out (scroll to view the image "Our Team") Essentially, I want a background where I can showcase information such as names and links ju ...

Experiencing difficulties with utilizing height percentages for CSS divs

Despite encountering similar issues with others, I am still unable to resolve the problem on my own. Your assistance is greatly appreciated. Currently, I am developing a guestbook in PHP using an HTML template. The issue I am facing is that the div elemen ...

Using jQuery to move a div to a specific location on the page

Is there a way to translate the position of div x and y using Jquery that is compatible with all browsers, such as IE 7, IE8, IE9, and IE10? I have attempted the following: <div id="s1" style="-ms-transform:translate(159,430)"> hello ...

Mastering the art of combining images and text harmoniously

I am having trouble aligning my lion image and h1 tag side by side. There seems to be an issue but I can't figure out what it is. h2 { width:50%; float:right; padding:30px 0px 0px 0px; margin:0 auto; } .lion { width:10%; float: left; paddin ...

What is the best way to send HTML tag content to mark.js and delimit them with a space or comma?

I have been utilizing the mark.js library to highlight keywords on a webpage, and it's been working well. However, I now need to insert an extra space or a comma after each tag such as h1, h2, etc. Initially, I thought about using a loop like the one ...

Spin a three-dimensional picture

Looking for some assistance! I have an image on my website that is rotated and needs to be turned back to face us correctly. I've tried using the code snippets below but they don't seem to be working: transform: rotateY(10deg); transform: rotate ...

Using jQuery's toggle function with a double click event to change the display to none

A div I created has the ability to expand to full screen upon double click, and I now wish to implement a toggle function so it can return to its original size when double clicked again. Initially, the code successfully increased the size of the div. Howe ...

An unexpected error has occurred in the browser console: The character '@' is not valid

I recently made the decision to dive into learning about Unit Testing with JavaScript. To aid in this process, I started using both Mocha.js and Chai.js frameworks. I downloaded the latest versions of these frameworks onto my index.html from cdnjs.com. How ...

Transforming an HTML file into a Vue.js component

I'm working on a login.vue file and I need to incorporate styling along with the necessary js and css files. Unfortunately, I'm clueless about how to achieve this within the login.vue file. Below is a snippet from the html file: <!DOCTYPE ht ...

Looking for a drum set with clickable buttons? Having trouble changing the background color in CSS/HTML? Wondering how to map keyboard keys to HTML buttons?

Behold my HTML creation: <H1> <center> EPIC GUITAR JAM </center> </H1> <img class="guitar" src="guitar.jpg" /> <button class="strum" onclick="Strum()"> Strum Chord </button> <button class="pluck" o ...

Avoiding the appearance of a scrollbar when content extends beyond its containing block

I am struggling with writing markup and CSS to prevent a background image from creating a scrollbar unless the viewport is narrower than the inner content wrapper: The solution provided in this post didn't work for me: Absolutely positioned div on ri ...

Eliminate the div element using jQuery if it contains a line break tag but no text

I am faced with a situation on a page where some div elements contain content while others only have a BR tag. I am attempting to remove the div elements that contain only the BR tag. Below is the HTML format in question: Here is an example of one type: ...

Switch between including 'light' or 'dark' styles using Javascript?

I am currently working on a project where I aim to incorporate a dark mode toggle feature. To maintain organization and scalability, I have chosen to implement the SASS (.scss) architecture rather than plain CSS. My plan is to utilize variables within my ...

Animated div or fieldset featuring a multi-step form

I have recently put together a detailed step-by-step guide. Each step is wrapped in its own "fieldset" within a single .html file. To navigate back and forth, I have incorporated javascript into the process. However, as time goes on, I am noticing that th ...

Issues occur with Google visualization when the screen is resized

When I resize the browser, an error code google-visualization-errors-8 appears in my Google chart's CSS. https://i.stack.imgur.com/4LX1C.png What could be causing this error to occur? Note: I am redrawing the graph every time the screen is resized. ...

Issue encountered while determining the specificity of a CSS selector involving pseudo-classes

As I delved into a tutorial on MDN, an intriguing code snippet caught my attention: /* weight: 0015 */ div div li:nth-child(2) a:hover { border: 10px solid black; } An unanswered question lingers in my mind: Why does this rule's specificity displa ...

Difficulties encountered when adjusting the size or reducing the images in a Cycle2 slideshow

Greetings to all fellow coders! Thank you for taking the time to read this post. I am currently facing a challenge in my web development project where I am trying to make my Cycle 2 slideshow images resize and reposition alongside other divs when the wind ...