Reduce the distance between child items when utilizing the display flex with the space-between property

I have a container (100% width) with 3 buttons inside. I want to keep them evenly spaced with a small margin between each button.

My attempt with Bootstrap and display flex with space-between didn't give me the desired result.

https://i.sstatic.net/9eWqc.png

Here is the code I used:

.container {
    display: flex;
    flex-direction: row;
    text-align: center;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
    align-items: center;
    justify-content: space-between;
}

.child-element {
    background: #FFFFFF;
    box-sizing: border-box;
    width: 100px;
    height: 60px;
    border: solid 1px #C4C4C4;
}

.child-element .selected {
    background: #FFED00;
}
<div class="container">
    <button class="child-element" data-option="1"><span style="color:#000">First</span></button>
    <button class="child-element" data-option="2"><span style="color:#000">Second</span></button>
    <button class="child-element" data-option="3"><span style="color:#000">Third</span></button>
</div>

This is the arrangement I would like to achieve:

https://i.sstatic.net/8UgXA.png

What changes can I make to achieve this layout? Is there a better alternative to using margin-left and margin-right?

Answer №1

To improve the layout, consider removing justify-content: space-between and replacing it with gap: 8px:

.container {
    display: flex;
    flex-direction: row;
    text-align: center;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
    align-items: center;
    
    /* add gap */
    gap: 8px;
}

.child-element {
    background: #FFFFFF;
    box-sizing: border-box;
    width: 100px;
    height: 60px;
    border: solid 1px #C4C4C4;
}

.child-element .selected {
    background: #FFED00;
}
<div class="container">
    <button class="child-element" data-option="1"><span style="color:#000">First</span></button>
    <button class="child-element" data-option="2"><span style="color:#000">Second</span></button>
    <button class="child-element" data-option="3"><span style="color:#000">Third</span></button>
</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

Challenges encountered while making CSS adjustments on the Wordpress Twentyfourteen theme

I'm currently assisting a friend with making some final adjustments to their website. The site is built on Wordpress using the Twentyfourteen theme and can be found at centromariposa.no. Most of the modifications have been completed, but I'm fac ...

Every time I attempt to input text into the text area, the page automatically refreshes, preventing me from completing any entries

Currently, I am working on developing a webpage that can dynamically update text inputted by users in a database. However, I have encountered an issue where the page refreshes automatically whenever I click on the textarea. If I type quickly enough, some o ...

Tips for manipulating rows in HTML using jQuery when the id attribute is added

Apologies in advance for any language errors in my explanation I am working on an input table where each row has a unique ID, and the input in each row affects the next row. As new rows are added, I have implemented an incremental numbering system for the ...

Tips on saving the background image URL value into a variable

How can I save the URL value of a background image in a variable? For example: image: url(images/9XkFhM8tRiuHXZRCKSdm_ny-2.jpg); I store this value as value = images/9XkFhM8tRiuHXZRCKSdm_ny-2.jpg in a variable and then use this variable in an href tag. ...

`vertical navigation on the left side with expanding submenus`

Trying to create a navigation system on the left side of the page with submenus appearing on the right without impacting the main content. If anyone has any code snippets or resources that can help achieve this, I would really appreciate it. The objectiv ...

Is it possible to use jQuery to apply CSS styling to a div that is dynamically called by Javascript

I currently have the following HTML code: <div> <script type="text/javascript" src="someUrl"></script> </div> Once this script runs, it generates a nested div with links inside like this: <div> <div> <a hre ...

Load the iframe element only when the specified class becomes visible within the container div, without relying on jQuery

I am facing a unique challenge where the performance of my page is significantly impacted by loading multiple iframes. These iframes are contained within popup modals, and I would like to delay their loading until a user clicks on the modal. When the mod ...

Determining User Consent: A Straightforward Guide to Identifying Previously Accepted Cookie Consent

I've been tasked with adding a cookie to a website. The site only tracks the number of visits, so there is no user-specific data available for cookie assignment or acknowledgment tracking. Is it possible to show the cookie notification only to users ...

Tips for ensuring only one submenu is open at a time

I have successfully created a responsive menu that is working well. However, I am facing an issue. I want only one menu to be open at a time. When a submenu is opened, the other menus should be hidden. How can I achieve this? Below is my Script code $( ...

How can the front design of Material-UI's Card header be customized?

Currently, I am facing an issue with the Material-UI card header as the background color is affecting the readability of the default font. My aim is to use the typography prop h4 for the header, but I am struggling to achieve this. https://i.stack.imgur.c ...

Struggling to successfully transmit my form information to my email

I've been trying to figure out how to get the information submitted through the form on my website sent to my email, but I can't seem to make it work... Here's what I have in my email.php file: <?php $EmailFrom = "<a href="/cdn-cgi/ ...

What makes text-decoration a crucial element of the foreground design?

<span>Educating children on unlocking their creative and intellectual potential through the world of Computer Science.</span> Here is the HTML code provided above along with the corresponding CSS: span { text-decoration: underline; color: red ...

The CSS selector functions as expected when used in a web browser, however, it

While conducting test automation using Selenium, I typically rely on css selectors to find elements. However, I recently came across a peculiar issue. I observed that in certain cases, the css selector works perfectly when tested in the browser console. Fo ...

Using Javascript to dynamically add and remove elements on click

Whenever I click on a link, I am able to generate input, label, and text. However, I want to be able to delete these elements with the next click event on the same link: I've tried updating my code like this: var addAnswer = (function() { var la ...

A guide on utilizing buttons within ion list items to execute actions independently for both the button and the list item

Currently, I have a list item with a button that is displayed based on a certain condition. My issue is that when I click the button, a popup should appear, but instead, it also navigates to the next page in the background. Can someone help me figure out h ...

What are some possible applications for leveraging the HTML5 <link rel> attribute duo of stylesheet and next?

The specification for HTML5 emphasizes the handling of each link created within a link element as separate entities. This means that multiple link elements with `rel="stylesheet"` are treated independently, considering them as distinct external resources a ...

Transfer information to a different division using Ajax

I am attempting to transfer data to another div after an Ajax refresh. Each time it refreshes, new data is fetched from the database. My goal is to retain the old data displayed in the first div, store it, and then move it to the second div when Ajax refre ...

Converting CSS into jQuery

I am exploring ways to create a collapsible section with arrow icons (right and down arrows) in jQuery or JavaScript. Can anyone provide guidance on how to convert my CSS styling into jQuery code? Below is the jQuery approach I have attempted: $(document ...

What strategies can be used to accurately position a rotated image within a rotated div?

Below is the code snippet I am using: <div class="container"> <img class="img"/> <div class="underlay"></div> </div> The image is a picture of some rotated text by 10 degrees, although the ...

Creating a Modal Dialog with Justified Tab and Dropdown Using Bootstrap 4.1

I am struggling with Bootstrap 4.1 as I try to align content to the right side. Specifically, I have a Navigation Bar that triggers a Modal Dialog containing Tabs. The dropdown menu on the far right of the Tab is what I want to justify to the right. Here a ...