Ways to achieve a layout with 2 fixed columns and 1 dynamic column using CSS

For the past 2 days, I've been struggling with this problem. I've experimented with "display: flex" and various combinations, but none have given me the desired outcome.

I require CSS code to achieve this layout (refer to the image).

I have two potential variations of HTML:

<div class="container">
    <div class="item">Left Button</div>
    <div class="item">Right button</div>
</div>

or

<div class="container">
    <div class="item">Left Button</div>
    <div class="item">Center button</div>
    <div class="item">Right button</div>
</div>

What should be included in the CSS to create this layout: https://i.sstatic.net/M2zTw.png

You can see both examples in the image provided. The first one corresponds to the first HTML structure, while the second example is for the second HTML version.

Essentially, I am looking for:

  • All items must have equal width at all times.

  • Each item should occupy approximately 33% of the container's width consistently.

  • If there are 3 items, the middle one should have a margin of 10px on both sides. However, it is crucial to remember the first point - maintaining uniform width among all items. This means that using 20px from the width as margin isn't feasible, as it would result in a narrower middle item.

One of the unsuccessful attempts involved:

.container {
    display: flex;
}

.items {
    flex: 1 0 0;
}

.items:nth-child(2) {
    margin: 0 10px;
}

This approach is incorrect.

I also tried the following, which again did not yield the desired results:

.container {
    display: block;
}

.items {
    width: 33%;
}

.items:nth-child(2) {
    margin: 0 10px;
}

Answer №1

Using flexbox will help achieve that layout.

.item {
  background: #f00;
  flex: 0 0 33.333%;
  border: 1px solid green;
}

* {
  box-sizing: border-box;
}
.container {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}
.item {
  background: #f00;
  flex: 0 0 33.333%;
  border: 1px solid green;
}
<div class="container">
  <div class="item">Left Button</div>
  <div class="item">RIght button</div>
</div>
<div class="container">
  <div class="item">Left Button</div>
  <div class="item">Center button</div>
  <div class="item">RIght button</div>
</div>

To ensure the middle margin is maintained, you can utilize max-width and calc as suggested by Pangloss.

.item {
  background: #f00;
  flex: 1;
  max-width: calc((100% - 20px)/3);
  border: 1px solid green;
}

* {
  box-sizing: border-box;
}
.container {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}
.item {
  background: #f00;
  flex: 1;
  max-width: calc((100% - 20px)/3);
  border: 1px solid green;
}
.multi .item:nth-child(2) {
  margin: 0 10px;
}
<div class="container">
  <div class="item">Left Button</div>
  <div class="item">RIght button</div>
</div>
<div class="container multi">
  <div class="item">Left Button</div>
  <div class="item">Center button</div>
  <div class="item">RIght button</div>
</div>

Answer №2

My recommendation is to use the CSS table layout, although you could achieve a similar result with flexbox as well. Each markup might require its own set of CSS styles.

body {
    background: gold;
    margin: 0;
}
.container {
    display: table;
    width: calc(100% + 20px);
    margin: 0 -10px;
    table-layout: fixed;
    border-collapse: separate;
    border-spacing: 10px 0;
}
.item {
    display: table-cell;
    background: aqua;
}
.item:nth-child(2) {
    /* visibility: hidden; */
}
<div class="container">
    <div class="item">Left Button</div>
    <div class="item">Center button</div>
    <div class="item">Right button</div>
</div>

Check out the jsfiddle example here!

Answer №3

Are you attempting to achieve something similar to this?

.test{
  display:table;
  width:100%
}

.test > div{
    width: calc(100% - 66.66% - 20px);
    margin-left: 10px;
    height:20px;
    float:left;
    background-color:#e8e8e8;
}

.test > div:first-child,
.test > div:last-child {
  background-color:#C1C1C1;
  width:33.33%;
  margin: 0;
}

.test > div:last-child{
  float:right
}
<div class='test'>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  
</div>

<div class='test'>
  <div>1</div> 
  <div>3</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

Tips on causing JavaScript execution to halt until an element has finished rendering

test.view.js timeDBox = new sap.ui.commons.DropdownBox({layoutData: new sap.ui.layout.GridData({linebreak: true}), change: function(oEvent){ oController.getKeyEqChart(); }, }), new sap ...

How to resolve HTML errors when uploading images in Dropzone and remove them from display

Removing HTML Error Display After Uploading Image in Dropzone I am looking for a solution to remove the HTML data displayed in the popup after uploading an image or other document using Dropzone. https://i.sstatic.net/kl0BM.png <form action="/ ...

Maintain button activation status even upon reloading the page (utilizing PHP and HTML)

Is it possible to maintain the active state of a button even after reloading the page without using custom CSS, only Bootstrap? I am using PHP and the buttons function as a switch for ON or OFF in an if condition. HTML <div style="margin-top: 20px ...

Django and its compatibility with modal windows

I have developed a Django website that includes multiple items in a "for" loop. I need to delete a specific item by opening a modal window and passing the post ID (referred to as "get_post_id") to the modal window. However, I want the modal window to exist ...

Automatically Switch Font Colors to Red and Green

I am looking to automate the process of changing multiple textbox colors based on the class property. <input type="text" Class="ChangeColor" /> <input type="text" Class="ChangeColor" /> <input type=& ...

Tips to position two shortcode elements next to each other horizontally

I have some code here and I am looking to align the two elements at the bottom, "php echo do_shortcode(wcas-search-form);" and "php do_action('hamzahshop_custom_min_cart');" on the same line horizontally. I am new to this and struggling to figure ...

The embedded CartoDB image header is set to have a style of "display:none"

I'm currently working on a CartoDB map showcasing my readers' summer fishing photos. Each row in the table has an image url, which I've placed at the top of the infowindow display list. I've enabled it and set the image window type to ...

The reason behind the clickable issue with my duplicate <ion-select>

I've been working on a form using HTML, CSS, and JavaScript where I implemented an "Add" button to duplicate the form. The issue arises with the ion-select element within the duplicated form. While the original form displays all predefined options upo ...

Understanding the Importance of CSS Specificity in Resolving Selector Conflicts

I need help with a specific pseudo-class selector issue. Even with !important, the text in the li:first-child element is not appearing in blue. The selector specificity for p is: (0, 0, 1) The selector specificity for li:first-child is: (0, 1, 1) I want ...

Utilize Python to enclose specific strings from a list within HTML tags if found within another string

A unique function has been created to encapsulate a search term with a specified HTML element along with its attributes. The intention is to highlight the search term in the resulting text that is later written to a log file. def inject_html(needle, hayst ...

Tips for avoiding a button reverting to its original state upon page refresh

I have a button with the ID #first that, when clicked, is replaced by another button with the ID #second. However, if I refresh the page after clicking on the second button, it goes back to displaying the first button. Is there a way to make sure that th ...

Steps to trigger a JavaScript popup from an iframe window to the parent window

Currently I am developing a web application using JSP with JavaScript. Within the parent window, an iframe is being utilized. The issue arises when a button in the iframe page is clicked, as it opens a popup window within the iframe itself. However, my obj ...

Various HTML tables display varying column widths even when utilizing the same colgroup settings

I'm facing an issue with multiple HTML tables on a single page, each following this structure: H2 Header Table H2 Header Table H2 Header Table My requirement is to have all tables occupy 100% width, with columns set at widths of 50%, 30%, and 20%. I ...

How can I incorporate custom text when hovering over dates on fullcalendar?

I'm looking to customize the mouseover text inside fullcalendar. Currently, the script only displays the event title on mouseover. You can see a working example here: JS Fiddle Example. How can I modify the code to show my own personalized text on mo ...

Utilize Cordova to download files and access them within the Cordova framework

Apologies for my lack of knowledge, any guidance or assistance in the right direction would be highly valued. I am working on a small application that retrieves data from an API using json requests. One of the functionalities required is to download a spe ...

Addressing the delay of "Rasterize Paint" on mobile devices while implementing css3 opacity transitions

I'm currently working on a project that involves users navigating back and forth between modals. To achieve this, I decided to use CSS transitions to change the opacity from 0 to 1. However, I've encountered some issues with slow transitions. So ...

What is the best way to retrieve the inner HTML or text content of a tag?

How can I retrieve the text value of an anchor tag in HTML using only golang.org/x/net/html, without external libraries? In my code sample below, I am able to extract the values of href and title with html.ElementNode. However, I need a way to specifically ...

Setting the position of the center to be relative inside a table cell

I need help with centering a +/- animation within my table rows to create an accordion toggle effect for revealing more information. Despite finding a nice looking animation, I'm struggling to position it correctly within the td element in my code. He ...

Encountered a MultiValueDictKeyError in Django at /employeeUpdate/ after making changes to the data

Encountering a django error "MultiValueDictKeyError at /employeeUpdate/ 'id'" after making changes to the data. Here is the code snippet from my views.py: def employeeUpdate(request): id = request.POST['id'] emp_username = requ ...

Github not recognizing CSS styles

Currently working on a website to showcase the games I've developed, but encountering issues with applying my CSS. The code can be found here. As a novice in HTML and CSS coding, I've tried numerous codes and tutorials without success. ...