What is the best way to ensure that the width of a div fills its parent container?

I need my divs(.give, .sep, .take) to completely fill the width of its parent container (.trade).

This is the HTML code:

<div class="trade">
  <div class="give"></div>
  <div class="sep"></div>
  <div class="take"></div>
</div>

Here is the CSS code:

.trade {
  position: relative;
  width: 90%;
  height: 80px;
  border: 1px solid black;
}

.give,
.take {
  height: 100%;
  display: inline-block;
}
.sep {
  width: 20px;
  height: 100%;
  border-left: 1px solid black;
  border-right: 1px solid black;
  display: inline-block;
}

Despite trying .give,.take{width:auto;}, it did not achieve the desired result.

I also attempted this solution:

.give,
.take {
  position:absolute;
  width:50%;
}
.sep {
  position:absolute;
  left:50%;
  transform:translate(-50%,0);
}
.give {
  left: 0;
}
.take {
  right: 0;
}

However, .give and .take ended up overlapping with .sep.

What would be the best way to make this layout work without relying on Javascript?

Additionally, trying

.give{margin-right:10px;} .take{margin-left:10px;}
or
.give{padding-right:10px;} .take{padding-left:10px;}
only increased their widths instead of filling the parent container.

Answer №1

Does this achieve the desired outcome? Using flexbox can simplify the process... The borders and padding on .trade are added for visual guidance

.business{
  display:flex;
  flex-direction:row;
  justify-content:space-around;
  align-content:center;
  border:1px solid red;
  padding:1rem;
  margin:0 auto;
  float:none;
  
  width:80%;
  height:10rem;
}


.business div{
  border:1px solid black;
  flex:1
}

.business div:before{
  content:attr(class)
}
<div class="business">
  <div class="offer"></div>
  <div class="divider"></div>
  <div class="accept"></div>
</div>

Answer №2

In the words of Z-Cyril, attempting to manipulate using float is not advisable. Additionally, incorporating inline-block is overly complex and unnecessary for this particular situation.

The key tool to utilize here is Flexbox. Apply .trade { display: flex; } in the CSS to leverage flexbox.

You can then employ .give, .take { flex-grow: 1; } to evenly expand them to fill the parent's width. Rather than relying on margins to separate your three child elements, consider utilizing .trade { column-gap: 20px; } instead.

.trade {
  position: absolute;
  width: 95%;
  height: 90px;
  border: 2px solid black;
  display: flex;
  column-gap: 25px;
}

.separator {
  width: 25px;
}

.give,
.take {
  flex-grow: 1;
}

/* included for illustration */

.trade {
  background-color: green;
}

.give,
.separator,
.take {
  background-color: purple;
}
<div class="trade">
  <div class="give"></div>
  <div class="separator"></div>
  <div class="take"></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

The issue arises when I try to retrieve information from MySQL using Ajax, as it appears

I am currently working on developing an ecommerce website. In order to display products, I am fetching data from a database. My goal is to allow users to add products to their cart without having to refresh the page. I have attempted to use AJAX for this ...

Show angular variable data in a table based on their index

I have an array of angular variables with values like [ 170, 1, 130, 3, 134, 4.... and so on]. I would like to present these values in a table format where the even positioned values are shown in one column and the odd positioned values in another column. ...

What is the best way to make one element match the height of another element?

I am facing an issue with the height of my two columns as I have another column separating them. The challenge is to make the left and right columns extend in height based on the content added to the center column. One important point to consider is that ...

What is the best way to display my index.html file while utilizing PHP Slim REST Api?

Recently I began diving into PHP Slim to create a REST API, and now I'm looking to integrate a client interface with it. One issue I've encountered is not being able to serve my index.html (the client's homepage) properly. Despite following ...

Unable to utilize the rupee font within a select element on Chrome and IE8 browsers

Issues with displaying rupee font in select element on Chrome and IE8 I have implemented WebRupee for showing the Indian currency symbol. The font appears correctly when used within a <p> tag, but when placed inside a select option element, it only ...

Viewing an image from a local file on a web browser

I am currently working on a project where I want the user to be able to select a local image that will then be displayed on the page. As someone who is new to web development, I did a lot of research and found some helpful information on StackOverflow. I t ...

Leveraging the @font-face feature along with fonts sourced from fontsquirrel

As I embark on creating my very first website, I find myself delving into the world of fonts from Font Squirrel. However, a challenge arises as I realize that not all the fonts I downloaded from the site are easy to use. The real struggle comes when deal ...

Difficulty with Horizontal Mousewheel Scrolling

Struggling to implement a horizontal scrolling feature (via mousewheel) for both containers in the code below. I want this feature to be easily applied to any future container creations as well. <body> <style> #container { display: flex ...

Eliminate the cushioning on a single item

I'm currently working on a website and running into a small issue. The main page has a background with a white body in the center, containing padding for the text to prevent it from being right at the border. #content { width: 900px; overflow: h ...

The grid flex end is behaving differently than I anticipated

I am struggling to align two buttons vertically on the right side. Here is my code snippet: const WalletsContainer = () => { return ( <Grid style={{ background: 'red' }} direction={'column'} alignItems={'flex-end'} ...

Assign a class to an element depending on its position using JavaScript/jQuery

<ul> <li>Apple</li> <li>Banana</li> <li>Orange</li> </ul> How can I use jQuery to add a class to the second li element in the list without using an existing class or id? ...

The element remains stationary and does not shift positions

My form is not centralizing properly, it seems to be stuck to the left side of #wrapper. I've tried adjusting margin-top but it doesn't seem to affect its horizontal position at all. I've played around with different values for margin and ev ...

Update the Kendo window scrolling feature to include fixed update and cancel buttons

I've encountered an issue while using ASP MVC and the latest version of Kendo. When I add too much content to a Kendo window, it starts scrolling vertically, which ends up hiding the cancel/update buttons at the bottom. This is not ideal as the user s ...

Why isn't Bootstrap validation functioning properly in my Modal?

I am attempting to implement client-side validation using bootstrap, as outlined here: https://getbootstrap.com/docs/4.0/components/forms/#custom-styles The challenge I'm facing is that my form is within a modal. When I click the submit button, nothi ...

What is the best way to remove horizontal scrolling and reduce element size on mobile devices?

My current project is utilizing the Material-ui framework, and I have a situation where numerous anchor elements are being displayed as a table within a Paper component from mui. However, due to the length of this table, it causes a horizontal scroll bar t ...

How can I retrieve the reference number of an item by clicking a button within a list item in an unordered

Presented here is a collection of data points and a button nested within an ul <ul> <li class="mix" category-1="" data-value="600.35" style="display:block;"> <figure> <figcaption> <h3> ...

Customize Text Area's Font Based on Selected Option in Dropdown List

I'm currently working on an HTML file that includes a dropdown list and a text area. Here's the code snippet: <select id='ddlViewBy' name='ddlViewBy' onchange='applyfonttotextarea()'> <option value = ' ...

Achieving the ideal HTML layout for small screens that differs from larger screens is a common challenge faced by web developers

After implementing the markup code, I achieved the desired layout on large screens. However, when reducing the screen size to a phone level, I require a layout similar to image-3, and on larger screens like laptops, it should resemble image-1. Image-1 and ...

What could be causing the border-collapse property to be ineffective on my HTML table?

My HTML table has nested tables, but the border collapse property is not working. Despite adding the border-collapse property to both the main table and nested tables, only the outer borders are visible, with no inside borders. <table style="bord ...

What is the best way to showcase the information from this API on an HTML page using Vanilla JavaScript?

I am currently working on implementing an AJAX call to fetch data from a movie API online. Below is the HTML code I have written for this: <html> <head> <meta charset=utf-8> <title>Movie Database</title> ...