CSS - Separate grid items and expand to the maximum width within the column

Is it possible to have two buttons in one row, where one is wider than the other? If the viewport size becomes too small, can the buttons automatically switch to sit in a single column with the smaller button stretching its width to match the wider one?

I've provided a codepen example to illustrate my current setup:

.outer {
  display: flex;
  justify-content: center;
}

.inner {
  display: inline-grid;
  grid-template-columns: repeat(2, minmax(min-content, max-content));
  grid-gap: 16px;
}

button {
  border: none;
  background: transparent;
  padding: 10px 48px;
}

.stroke {
  border-bottom: 2px solid black;
}

.flat {
  background: black;
  color: white;
}
<div class="outer">
  <div class="inner">
    <button class="stroke">
      <span>WIDER BUTTON</span>
    </button>
    <button class="flat">
      <span>BUTTON</span>
    </button>
  </div>
</div>

Take a look at the CodePen demonstration here.

The content within the buttons is dynamic, so manually setting a fixed width won't work. Can this layout adjustment be achieved using only (s)css?

Thank you for any assistance in advance.

Answer №1

Consider using flexbox for your layout instead of inline-grid.

By including the property flex-wrap: wrap;, the content will automatically move to the next line if it doesn't fit in the container.

Add flex-grow: 1; to the smaller button to make it expand and fill the available space within the container.

.outer {
  display: flex;
  justify-content: center;
}

.inner {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -16px;
}

button {
  border: none;
  background: transparent;
  padding: 10px 48px;
  margin: 0 16px;
}

.stroke {
  border-bottom: 2px solid black;
  width: 300px;
}

.flat {
  background: black;
  color: white;
  width: 200px;
  flex-grow: 1;
}
<div class="outer">
  <div class="inner">
    <button class="stroke">
      <span>WIDER BUTTON</span>
    </button>
    <button class="flat">
      <span>BUTTON</span>
    </button>
  </div>
</div>

Answer №2

To enhance your design, you can utilize the flex-wrap: wrap property by changing the display attribute to flex within the .inner class and applying flex: 1 1 auto; to your button.

If you desire more customization options for your layout, consider implementing @media queries.

.outer {
    display: flex;
    justify-content: center;
}

.inner {
    /*display: inline-grid;
    grid-template-columns: repeat(2, minmax(min-content, max-content));*/
    display: flex;
    flex-wrap: wrap;
    /*grid-gap: 16px;*/
}
button {
    border: none;
    margin: 8px;
    flex: 1 1 auto;
    background: transparent;
    padding: 10px 48px;
}

.stroke {
    border-bottom: 2px solid black;
}

.flat {
    background: black;
    color: white;
}
<div class="outer">
    <div class="inner">
        <button class="stroke">
            <span>WIDER BUTTON</span>
        </button>
        <button class="flat">
            <span>BUTTON</span>
        </button>
    </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

Ways to prevent decreasing the value below zero in ReactJS?

I have created two buttons, one for increasing and another for decreasing a counter value. However, when I click on the minus button, it should not display negative values. But in my case, when I click on the minus button (initially at zero), it shows -1, ...

Step-by-step guide for making a CSS triangle design that is compatible across different browsers

Here's a common CSS code for creating a CSS triangle: display: inline-block; vertical-align: middle; content: " "; border-right: 4px solid transparent; border-left: 4px solid transparent; border-top: 6px solid black; width: 0; height: 0; Click here ...

Unveil the stunning left-to-right SVG animation

I have two SVG images and I want to create an animation with them. The animation consists of revealing the text "Full Screen" from left to right using the first SVG, and then covering the word "Screen" with the second SVG to reveal the entire second image. ...

What steps can be taken to ensure an animation does not continually reset to its original state when run infinitely?

I am looking to create an animation that continues infinitely without returning to its original state after completion, similar to a sun moving animation. Below is a sample project: .animator3{ -webkit-animation-name:j3; -webkit-animation-delay:0s; -web ...

Tips on maintaining the original text hues that were established using setForeground() when a QTableWidgetItem is picked

Issue at Hand: Currently, when the first row is selected as shown in Figure 2 below, all text colors are changed to black, instead of maintaining their original style displayed in Figure 1. Is there a way to retain the original colors when a row is selec ...

Unique arrangement of hexagons in a honeycomb layout with precise centering

Currently, I am having trouble creating hexagonal blocks with content in a specific layout. My goal is to have four blocks starting with one at the top, followed by a row of two, and finishing with a row of one as shown in the image below. Unfortunately, a ...

What is the best way to position an image in the center of a browser regardless of the screen resolution?

Is there a way to have an image centered horizontally regardless of its size or the browser window resolution using HTML and CSS? This would be specifically for the website's logo, ensuring it consistently appears at the center of the header. ...

JavaScript: Creating Custom IDs for Element Generation

I've been developing a jeopardy-style web application and I have a feature where users can create multiple teams with custom names. HTML <!--Score Boards--> <div id="teamBoards"> <div id="teams"> ...

The vanishing sticky navigation bar

My navigation bar is no longer sticky, and now the text overlaps the video that should start below it. https://github.com/shanegibney/stackoverflowStickynavbarIssue You can also find a link to gh-pages there. The issue lies with a video occupying the en ...

I am having difficulty retrieving all the cssRules for certain pages

Attempting to retrieve all CSS rules using JavaScript has yielded varying results. For instance, on StackOverflow, the code used is: console.log( document.styleSheets[1].href,'rules', document.styleSheets[1].cssRules,'Should be css rules, ...

Function exhibiting varying behavior based on the form from which it is called

Currently, I'm utilizing AJAX to execute a server call, and I've noticed that the behavior of my function varies depending on its origin. Below is an excerpt of the code that's causing confusion: <html> <body> <script> ...

Position a single div on the left-hand side and arrange two divs on the right using display:flex

Is there a way to achieve this layout without adding another div, using only parents and 3 child elements? I had tried using height: max-content, but it didn't work as expected. .container { display: flex; flex-wrap: wrap; } .item { flex-bas ...

Tips for creating zoomed-in background images on small screens

I am currently implementing this code for a website project .homepage{ background-image: url(/wp-content/themes/ep/img/_.png); -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background ...

What is the best way to pinpoint the origin of the element.style being injected by JavaScript?

I've been tasked with updating a client's WordPress website, but I'm still getting acquainted with the overall structure of the site. When looking at the blog page (), I noticed that posts are displayed within a wrapper labeled #blogleft, a ...

Tips for filling in the values for the options in a select dropdown menu

Currently, I am facing a strange bug related to the select element. Whenever I open the dropdown, there is always a mysterious black option at the top. This is how my HTML looks like: This particular element is part of my test controller. <select ng- ...

Struggling with a JQuery function that fails to perform addition

I'm experiencing an issue with my code where the math doesn't add up when trying to sum values under the venting selection. Instead of adding up, it just displays as text. I have an event set up that should populate the values upon selecting valu ...

Issue with z-index not applying to a fixed header

I've been attempting to recreate the problem using this plunker. .demo-blog.mdl-layout .mdl-layout__content { padding-top: 0px; position: relative; margin-top: -80px; z-index: 100; } header.main-header{ z-index: -50; } My goal is ...

The CSS @media query is failing to function properly

I'm currently developing a web application similar to Spotify for my personal use, but I'm running into some issues with the @media query as it doesn't seem to be working correctly. Here is the CSS code: /* Screen Adapting */ @media (height ...

Unlocking the Potential: Launching the Excel Application Using HTML or JavaScript

Is there a way to launch the Excel application using HTML or JavaScript? Can I open an Excel file using HTML or JavaScript simply by calling the Excel application? ...

Achieve full height for nested children within a container of varying height without using flexbox

Inside a container of variable height, I have multiple nested lists that I want to take up all the available space. I attempted to use flexbox for this purpose, but performance issues arose in Chrome, FF, and Safari when dynamically adding more nested lis ...