"Trouble with CSS flex display aligning items with space between them

As a newcomer to flexbox and CSS, I must confess my amateur status. Please forgive any inexperienced blunders.

Below is the code snippet:

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
html,
body {
  height: 100%;
}

body {
  font-family: Roboto, sans-serif;
  margin: 0;
  background: #aaa;
  color: #333;
  /* I'll give you this one for free lol */
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal {
  background: white;
  width: 480px;
  border-radius: 10px;
  box-shadow: 2px 4px 16px rgba(0, 0, 0, .2);
  display: flex;
  padding: 16px;
  gap: 16px;
}

.icon-item {
  flex-shrink: 0;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
}

.header-item {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.header {
  font-weight: bold;
}

.icon {
  color: royalblue;
  font-size: 26px;
  font-weight: 700;
  background: lavender;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-button {
  background: #eee;
  border-radius: 50%;
  color: #888;
  font-weight: 400;
  font-size: 16px;
  height: 24px;
  width: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
  padding: 0;
}

button {
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 8px;
}

button.continue {
  background: royalblue;
  border: 1px solid royalblue;
  color: white;
}

button.cancel {
  background: white;
  border: 1px solid #ddd;
  color: royalblue;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Modal</title>
</head>

<body>
  <div class="modal">
    <div class="icon-item">
      <div class="icon">!</div>
    </div>
    <div class="info-item">
      <div class="header-item">
        <div class="header">Are you sure you want to do that?</div>
        <button class="close-button">✖</button>
      </div>
      <div class="text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Pariatur excepturi id soluta, numquam minima rerum doloremque eveniet aspernatur beatae commodi. Cupiditate recusandae ad repellendus quidem consectetur sequi amet aspernatur cumque!</div>
      <div>
        <button class="continue">Continue</button>
        <button class="cancel">Cancel</button>
      </div>
    </div>
  </div>
</body>

</html>

In the header-item class, I am aiming for a layout similar tohttps://i.sstatic.net/pEJtF.png utilizing flexbox and specifically the justify-content: space-between property. Despite my efforts, the outcome falls short like in this image https://i.sstatic.net/3wVia.png...

The issue might lie with the space-between value which lacks sufficient space for the items within the container to rearrange effectively. My assumption leads me to wonder how can I achieve a smart solution without resorting to hardcoding pixel values. Is it feasible for the .header-item class to expand across the entire parent width so its children have ample space to align?

I appreciate your insights!

Answer №1

By applying the CSS properties width:100%; to the .header-item class and margin-left: auto; to the .close-button class, you can ensure that the close-button aligns itself at the end of the row.

body {
  font-family: Roboto, sans-serif;
  margin: 0;
  background: #aaa;
  color: #333;
  /* I'll give you this one for free lol */
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal {
  background: white;
  width: 480px;
  border-radius: 10px;
  box-shadow: 2px 4px 16px rgba(0,0,0,.2);
  display: flex;
  padding: 16px;
  gap: 16px;
}

.icon-item {
  flex-shrink: 0;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
}

.header-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  width:100%;
}

.header {
  font-weight: bold;
}

.icon {
  color: royalblue;
  font-size: 26px;
  font-weight: 700;
  background: lavender;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-button {
  background: #eee;
  border-radius: 50%;
  color: #888;
  font-weight: 400;
  font-size: 16px;
  height: 24px;
  width: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
  padding: 0;
  margin-left: auto;
}

button {
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 8px;
}

button.continue {
  background: royalblue;
  border: 1px solid royalblue;
  color: white;
}

button.cancel {
  background: white;
  border: 1px solid #ddd;
  color: royalblue;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Modal</title>
  </head>
  <body>
    <div class="modal">
      <div class="icon-item">
        <div class="icon">!</div>
      </div>
      <div class="info-item">
        <div class="header-item">
          <div class="header">Are you sure you want to do that?</div>
          <button class="close-button">✖</button>
        </div>
        <div class="text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Pariatur excepturi id soluta, numquam minima rerum doloremque eveniet aspernatur beatae commodi. Cupiditate recusandae ad repellendus quidem consectetur sequi amet aspernatur cumque!</div>
        <div>
          <button class="continue">Continue</button>
          <button class="cancel">Cancel</button>
        </div>
      </div>
    </div>
  </body>
</html>

Answer №2

Almost there, just one adjustment:

.navbar-item {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  width: 100%; /* Ensures full width utilization by flex */
}

If omitted, your flex layout will attempt to cram all elements into the smallest available area.

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 dropdown menu in the bootstrap is not being properly filled with content on the webpage

In my header file, I am attempting to add a bootstrap dropdown that will display the user id along with two other options in the dropdown menu. Within the header file, I have created a new div element for the dropdown and linked it to a php file where the ...

What is the best way to showcase an endless amount of items in a

I'm facing an issue with the layout of items in my view. I have a list of items, and I can display up to 6 items at a time using class="col-md-2". However, when there are more than 6 items, they move to the next row automatically. I want them to be d ...

Struggling to grasp the concept of using z-index in CSS

As a programmer versed in Python, C, Java, and Delphi, I am not a web developer or designer by trade. However, when required to fill those roles, I do my best; please be patient with me. :-) I have created a map (with a background image as a div), where I ...

Positioning a div around a multi-layered canvas for a centered effect

I am looking to create a unique HTML5 canvas setup where multiple canvases are stacked on top of each other to allow for rendering with different layers using JavaScript. The challenge is to have these canvases centered both horizontally and vertically on ...

Is it possible to decrease the HTML font size when making the website responsive?

I made the entire website using rem units due to utilizing the bootstrap 4 alpha version. Now, I am ready to begin the responsive design of the site. My html {font-size: 16px} declaration is set, so I am wondering if I can adjust the size of html in the m ...

controlling link behavior programmatically in ASP.NET using C#

Is there a way to dynamically enable or disable an href link in a sidebar menu usercontrol based on the user's rights? I have tried using the Disabled property in the code-behind but it doesn't seem to work as expected. For instance, how can I c ...

Trouble assigning the 'data' attribute to the 'Object' tag using jQuery. [Limited to IE8]

I have encountered a problem when creating an object element dynamically in jQuery to display some content. The code functions perfectly in all browsers except for IE8. Here is the code snippet: j$(document).ready(function(){ j$('.ob ...

Combining PHP and MySQL with a join statement

I am trying to calculate the average rating for each store in a directory list using MySQL's AVG function. Whenever someone reviews a store, they assign a rating ranging from 1 to 5. This rating is then inserted into the rating column of the reviews ...

As the height is expanded, the background color gradually infiltrates the body of the page

I am currently working on an angular application that utilizes angular-pdf. The controller and view function perfectly, and the PDF is displayed correctly, except for one issue. The height of the PDF exceeds the min-height of the module, causing it to expa ...

Is there a bug with Kendo UI? The Kendo grid pager seems to be misaligned with the bottom

Recently, I've been experimenting with Kendo UI and encountered a specific issue: The pager for my Kendo grid is not aligning properly at the bottom of the grid. Although I've attempted to adjust the CSS styling for k-grid-pager, the issue persis ...

Constructing an Angular component with any content in the constructor can cause the entire page to malfunction

When attempting to modify the constructor of one of my Angular components, I encountered an issue where adding any code to the constructor caused the entire page to render blank. This resulted in all other components disappearing and only displaying the ba ...

Applying a transparent layer to all slider images, with the exception of the one that is

I am utilizing an IosSlider that is functioning properly, but I am looking to enhance it by adding opacity to all images except for the selected image. This adjustment will make the selected image stand out more and enable users to focus on one image at a ...

Show unique language text in the <noscript> element based on the user's browser language

I am just starting out with HTML, and I would like to show a message if JavaScript is disabled. To do this, I have placed the message within the <noscript> tag, which is working perfectly. <noscript> Need to enable Javascript. </noscript> ...

Does the language setting on a browser always stay consistent?

Using javascript, I am able to identify the language of my browser function detectLanguage(){ return navigator.language || navigator.userLanguage; } This code snippet returns 'en-EN' as the language. I'm curious if this i ...

Load css-background-images before executing jquery transitions in Sencha Touch for optimal performance

While working on my Sencha Touch app, I implemented a jQuery animation that transitions background images using CSS. The issue I encountered is that the transition to the next image only happens smoothly after all the images have been fully loaded once. Th ...

Is it feasible to place an image on top of a box?

Hello everyone, I have been trying to figure out how to achieve a specific layout in React. I am using MUI so there is no need for any hard-coded CSS. However, my attempts have not been successful so far. The layout I am aiming for consists of an image on ...

Using Django to stream H.264 video files with FileWrapper through HTML5

Encountering a peculiar problem while attempting to stream H.264 video with Django using the FileWrapper class. The view function being used is as follows: def mp4(request, path): wrapper = FileWrapper(open(path, 'rb')) content_type = mimety ...

Cannot play Apple Trailer URLs using the <video> tag

I'm struggling to embed an HTML5 video player on my website. Here is the code I am using: <video id="player" controls="controls" width="100%"> <source src="http://trailers.apple.com/movies/paramount/captainamerica/captainamerica-tlr1_h. ...

Alter the position of the anchor object in the center using JavaScript upon page load

When my page loads, I want to automatically jump to a specific anchor tag and ensure that the anchor object is centered in the window. Initially, I implemented a basic function to achieve this: <body onload="goToAnchor();"> <script type="text/ja ...

Unable to query MySQL database for filtering by month using PHP

As part of my Student work at the local college, I was tasked with creating a website on a specific topic. I chose Parking Car Lot as my topic, but now I'm stuck and not sure where to find the issue. My main goal is to filter data by year and month fr ...