Ensure the widest possible width between two elements

There are 3 elements involved:

<button class="a"></button>
<div class="b"></div>
<button class="c"></button>

The style defined for these elements is as follows:

.a {
    width: 30px;
}
.c {
    width: 50px;
    float:right;
}

I am looking to ensure that the div .b appears inline without line breaks, with a width that maximizes the available space between .a and .c.
I prefer not using absolute positioning for this. Any suggestions?

Answer №1

To arrange the .b element, place it between two other elements and float them to the left and right respectively. Ensure that the .b element has overflow:hidden property to fill the available width and clear previously floated elements:

It's important to note that there are invalid HTML elements in the code provided.

HTML Markup

<button class="a"></button>
<button class="c"></button>
<div class="b">A</div>

CSS Styles

.a { width: 30px; float:left; }
.c { width: 50px; float:right; }
.b { overflow:hidden; }

JSFiddle Link

Answer №2

Test out this CSS code snippet for the b class.

.b {max-width:100%;}

Answer №3

Try removing the float property and implement it in this manner:

.x {
    width: 25px;
    display:inline-block;
}
.y {
    width:calc(100% - 75px); /* important */
    display:inline-block;
}
.z {
    width: 40px;
    display:inline-block;
}

check out the demo here

Answer №4

What do you think of trying out this style?

button.a, div.b, button.c
{
    display: block;
    vertical-align: middle;
    float: left;
}

button.a, button.c
{
  width: 25%;
}

div.b
{
  width: 50%;
  text-align: center;
}

Check it out here!

You might want to consider incorporating a grid system.

Answer №5

To achieve the desired layout, you can float both buttons and let the div automatically fill the remaining space within its container.

<button class="primary"></button>
<button class="secondary"></button>
<div class="content">Lorem ipsum</div>

Apply the following CSS styles:

.primary {
    width: 30px;
    float: left;
}
.secondary {
    width: 50px;
    float: right;
}

For the div to consistently stay between the buttons, even if they wrap onto multiple lines, use this additional styling:

.content { 
    margin-left: 30px;
    margin-right: 50px;
}

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

Populating a list in Nextjs with elements retrieved from a JSON API response

I'm currently immersed in a project built on Nextjs. It involves retrieving data from an API and converting it into a JSON response. Let's consider the following API endpoint: https://jsonplaceholder.typicode.com/users The data is fetched using ...

The landscape orientation media query does not adhere to the specified maximum width

I am currently working on creating a mobile landscape design for a website specifically tailored for iPhone SE and iPhone 12. In the process, I encountered an issue that caught my attention: Within two different breakpoints, I made adjustments to the top ...

Organize columns on mobile devices using Materialize

I'm currently working with the materialize framework and am attempting to change the order of columns on mobile view. Specifically, I want the BUY BUTTON to appear before the CONTENT section on phones. I've been experimenting with pull and push b ...

One Background Image Serving Multiple Divs

Can you use one image (PNG or SVG) as the background for multiple divs? Take a look at the images below to see how it could work. And if the screen width gets smaller and the divs stack up vertically, is there a way to change the background accordingly? D ...

Creating interactive animations triggered by scrolling when using JQuery in combination with Bootstrap 3 modals

I am struggling to grasp the concept of Bootstrap modal div visibility, especially when they appear visible even when hidden. The normal divs on the page, outside of the modal, function properly with my code. ... I created some JS and CSS logic that dic ...

Kendo MVC Spreadsheet - Modifying cell text orientation to a 90-degree angle

My issue may seem trivial, but I'm struggling to achieve the desired behavior. Working with the Telerik MVC framework, my current task is to create a Spreadsheet that matches a specific template. However, in this template, there are cells in the head ...

Tips for styling inside containers with margins between elements without affecting their position on the page

I have three items enclosed in a container with a background color and padding applied. However, when I try to add margin between them, the elements shift to the next row. .div-category{ background-color: #2274A5; padding-left: 50px; padding-right: ...

Is there a way to prevent reset.css from affecting a specific element?

I am facing this issue because I am receiving stylized text from "Portable Text to React". However, the global style in my index.css file includes a css reset that eliminates all default styling applied to elements of the portable text. Is there a way to ...

The data fail to load correctly with Ajax

I'm having trouble displaying the data from each link in my div. I suspect it may be related to an external .js file that controls the slide up jquery function. Every time a link is clicked, the box slides up but doesn't show any new data. Additi ...

Using jQuery to add an element at the current caret position

Within my contentEditable div, there is a table nested. Here is an example of the HTML structure: <div id="divId" contenteditable="true"> <table> <tbody> <tr> <td><br></td> ...

Is there anyone who can take a shot at creating this calendar widget using HTML/CSS? I'm struggling to code it myself

I have been assigned a task to create a calendar widget using HTML and CSS, but I'm struggling to figure out the structure with tables or divs. Here is my design for the calendar: Here is how I've attempted to design it so far: You can view the ...

Hover effect that transforms every element of the same kind except for the one that is currently being

Is it possible to create a hover effect that changes not only the hovered "a" tag but also other "a" tags? Currently, I can only achieve the desired effect on the selected hover. ul li{ list-style: none; } ul li a { color: black; } ul li a:hover{ ...

The significance of tabindex in CSS

Is it possible to manipulate tabindex using CSS? If so, which browsers support this feature and on what types of elements? UPDATE I am interested in capturing keydown events on a div element. After researching keyboard events on http://www.quirksmode.org ...

What is the reason my bootstrap carousel isn't functioning properly?

My carousel is only displaying the first image and seems to be stuck. The navigation buttons are also not functioning. Here is the code for reference: <main> <div id="slider" class="carousel slide" data-mdb-ride="carousel"> <div cl ...

What's causing the appearance of a horizontal scrollbar?

I'm puzzled by the appearance of a horizontal scroll bar on my webpage and I can't seem to pinpoint the exact cause. I've tried simplifying the page as much as possible, but there still seems to be an issue. Here's all the information I ...

How to adjust the font size in the Angular Material Select Panel?

One way to customize the appearance of a panel in CSS is by using the panelClass attribute. <mat-form-field appearance="fill"> <mat-label>Toppings</mat-label> <mat-select [formControl]="toppings" panelClass=&quo ...

Why does the browser keep converting my single quotation marks to double, causing issues with my JSON in the data attribute?

This task should be straightforward, but I seem to be facing a roadblock. I am trying to insert some JSON data into an input's data attribute, but the quotes in the first key are causing issues by prematurely closing the attribute. Here is the code ...

When collapsing on mobile, display all Bootstrap sub menus as expanded

I am currently utilizing wp-bootstrap-navwalker.php to generate bootstrap navigation menus on my Wordpress site. I am working on a project where the client requires the parent of a submenu in the navigation to be clickable. After modifying the code in wp-b ...

Retrieve information from a table by utilizing just two specific columns through the power of JavaScript

I am completely new to the world of web development and have been struggling to find a solution. I have a table structured like this: A B C D FullName ABC pqr xyz TelephoneNo 123 RST GHI My goal is to extract data ...

What is the correct way to obtain an element (specifically a canvas) by its ID in a C# within an ASPX webpage?

I have a requirement to access a canvas element in an aspx file. The goal is to make the canvas drawable, allow users to save what they draw on it, and provide an option to clear it back to white. Here is the HTML code: <div> <canvas id="can ...