Positioning divs to float on either side of the screen without breaking their alignment on wide

Displayed here is an example that showcases floating divs on the left and right with varying container widths.

When the container is large, the divs floating to the left cling to the left edge, while the div floating to the right clings to the right edge. Is there a method to ensure that these divs stay together (horizontally centered within the container) when it's large? Note that fixing the width of the container or any div is not an option.

My preference would be to find a solution without relying on javascript.

.container {
  display: inline-block;
  border: 1px solid black;
}
.inner1 {
  float: left;
  background-color: #aff;
}
.inner2 {
  float: right;
  background-color: #ffa;
}
.inner3 {
  float: left;
  background-color: #faf;
}
First example with wide container<br />
<div class="container" style="width:400px">
  <div class="inner1"><---------- First</div>
  <div class="inner2">Second
    <br />--------
    <br />------</div>
  <div class="inner3">Third ----------</div>
</div>
<br />

Second example with small container<br />
<div class="container" style="width:100px">
  <div class="inner1"><---------- First</div>
  <div class="inner2">Second
    <br />--------
    <br />------</div>
  <div class="inner3">Third ----------</div>
</div>

EDIT : I am utilizing floats in order to prevent the default arrangement of divs. Non-floating divs are typically ordered either from left-to-right or top-to-bottom, creating unused space at times. For instance, if div1 is wide but not tall, and div2 is narrow but tall, on a large screen, div2 will be positioned to the right of div1 leaving vacant space below div1 and to the left of div2. I intend to use this space for div3, but due to the ordering, div3 ends up below or to the right of div2.

To combat this issue, I opted for floating divs, which led to the problem of divs separating on larger screens as outlined initially.

If there is a way to solve this problem without using floats, that would be ideal.

Answer №1

As a newcomer, I am unable to leave comments at the moment. However, I can certainly provide some input. Based on my interpretation of the inquiry, it seems like you may be in search of the

vertical-align: middle;

I trust that this is the information you were seeking.

Answer №2

Are you looking for something similar to this example? Try removing the float property from the .inner3 class.

.container {
  display: inline-block;
  border: 1px solid black;
}
.inner1 {
  float: left;
  background-color: #abc;
}
.inner2 {
  float: right;
  background-color: #cba;
}
.inner3 {
  background-color: #fac;
}

First scenario with a wide container<br />
<div class="container" style="width:400px">
  <div class="inner1"><---------- First</div>
  <div class="inner2">Second
    <br />--------
    <br />------</div>
  <div class="inner3">Third ----------</div>
</div>
<br />
Second scenario with a small container<br />
<div class="container" style="width:100px">
  <div class="inner1"><---------- First</div>
  <div class="inner2">Second
    <br />--------
    <br />------</div>
  <div class="inner3">Third ----------</div>
</div>

Answer №3

To achieve a responsive layout, you can utilize CSS3 @media queries. Simply position the divs on the screen with a width of 400px and above.

.container {
  border: 1px solid black;
}
.inner1 {
  float: left;
  background-color: #aff;
}
.inner2 {
  float: right;
  background-color: #ffa;
}
.inner3 {
  float: left;
  background-color: #faf;
}

@media (min-width: 400px) {
  .container {
    margin: 0 auto;
  }
  .inner1 {
    float: left;
  }
  .inner2 {
    float: left;
  }
  .inner3 {
    float: left;
  }
}

When the screen width is equal to or greater than 400px, the container will be centered on the screen and the inner divs will float to the left. By default, inner1, inner2, and inner3 will float to the left, right, and left respectively.

Example on JSFiddle

Answer №4

My suggestion for this scenario is to eliminate display: inline-block; from the .container class and instead include overflow: hidden; in both the .container and .inner3 classes:

.container {
  border: 1px solid black;
  overflow: hidden;
}
.inner1 {
  float: left;
  background-color: #aff;
}
.inner2 {
  float: right;
  background-color: #ffa;
}
.inner3 {
  overflow: hidden;
  background-color: #faf;
  float: left;
}

UPDATE Check out the instructed changes on jsfiddle:
jsfiddle-link

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

Acquire the information for PHP data updating

I'm currently working on a login system and data update feature that allows users to update their account information. To begin, please enter the login name of the account you wish to modify: UPDATEINPUT.HTML <html> <form method = "get" a ...

Tips for customizing the appearance of a React-Table header when sorting data

How can I change the header's background color in react-table based on a selected item? For example, if I click on ID, the ID header should change its background color to red. I have tried various methods to update the background color upon selection ...

Is the scroll navigation bar transparent on the right edge?

Below is an image attachment showing a navigation bar with list items. The overflow-y property allows scrolling to the right and left, and the desired effect is to make only one side transparent at a time based on the scroll direction. Is it possible to ac ...

Position a button to be aligned with the bottom of its container

My goal is to have the button positioned at the bottom of the red div, nested inside it. .grand-parent { display: flex; flex-direction: row; flex-wrap: wrap; height: 300px; background-color: green; } .pa ...

Using object-fit with the value of "cover" expands the size of the containing

Currently, I am setting up a grid for a blog where I have cropped the images using object-fill: cover; to ensure uniformity in appearance regardless of the image aspect ratio. However, this approach is causing an issue by stretching the parent element (a d ...

What is the best way to make an HTML number input display more than two decimal places?

If a user inputs 1.00, will it be automatically formatted to 1.0000? Is this feature included in the HTML 5 numeric input? ...

Strategies for increasing the height of a child element relative to its parent container

<body> <section class="container"> <div class="child"> //content goeshere </div> </section> <body> I have a HTML code snippet above. I am trying to set the height of the 'child' class div ba ...

Utilizing Shiny app as a functional tool requires knowing the appropriate placement for the www/ directory and managing CSS and

I am currently in the process of transforming a relatively intricate shiny application (consisting of 3-4 modules and 8 CSS/JS files) into a package with a single external function that constructs an instance of the app based on specific input (build_myApp ...

Tips for generating a JSON string with nested structure

Can someone assist me in generating a JSON based on the HTML elements provided below? { "AppName": "ERP", "ModuleDesc": [ { "Name": "Payroll", "AcCESSRIGHTS": [ { "Create": "Y", "Retrive": "Y", "U ...

Issues with Windows font rendering when using @font-face in CSS

Encountering issues with @font-face on Windows computers, regardless of the browser used. The fonts display properly on Linux and OSX. Below is the CSS code I'm currently using (generated with font-squirel) Review the screenshots for the identified ...

How can I use jQuery to vertically align an element?

Here's my website: Take a look here! I have a menu on the left column that I want to center. Take a look at the image below for a better understanding of what I'm trying to achieve. https://i.stack.imgur.com/ZzprT.png Here is the JavaScript c ...

Jquery and CSS behaving in an unexpected manner

Here's a piece of code that I need some help with. The issue occurs when clicking on a box to expand it and display more information. Most of the time, it works fine, but occasionally if an image is not cached, clicking again to minimize the box cause ...

Can JQuery be used to specifically target attributes with vendor prefixes?

Is there a way to change the Thumb color for my slider without needing to select the thumb with a vendor prefix? I want to be able to dynamically pick the color without simply appending a class. $('.button').on('click', function (e) { ...

Using a local variable within quotes in Angular 4 (HTML) and utilizing ngFor

In my current project, I am utilizing Angular2-Collapsible. The objective is to display the details upon clicking a table row. Below is the code snippet provided. Specifically, if I add [detail]= "detail1", the collapsible-table-row-detail will appear when ...

Why can't JQuery/javascript's dynamic gallery's images be used as buttons instead of links?

I've been struggling with my online portfolio for several nights as I build my website. Despite exhaustive internet searches, I couldn't quite articulate my problem in words and tried multiple workarounds. To see the issue, please visit the beta ...

Retrieve element attributes and context inside a function invoked by an Angular directive

When working with a directive definition, you have access to the $element and $attrs APIs. These allow you to refer back to the element that called the directive. However, I'm curious how to access $element and $attrs when using a standard directive l ...

Adjusting screen size does not update container padding

My attempt to remove the padding of a container on smaller screens using a media breakpoint is not yielding the desired result: @media screen and (max-width: 576px) { .content-wrapper > .content { padding: 0; } .container, .containe ...

Creating Divs in a Row with Bootstrap 5

I want to display these two images next to each other Check out my code:- code <div class="container-md"> <div class="row"> <div class="col-md-4"> <p> Image Video Live Streamin ...

Conceal any zero values from an empty numerical input

Currently, I have a form that retrieves data from a database and includes a number input type field. When the field is empty, it defaults to displaying "0." However, I would like to hide the "0" and only show the value if it is different from 0. Despite a ...

Using Javascript to delete an HTML list

Currently, I am working on a webpage that includes notifications along with options to mark them as read individually or all at once. However, when attempting to use my loop function to mark all notifications as read, I noticed an issue. let markAllAsRead ...