Ways to align a particular flex item horizontally while keeping the others centered using CSS

Take a look at this CSS Code Screenshot with an example. In my setup, I have a container styled with the following CSS properties:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.item-3 {
  align-self: flex-end;
}

By using the align-self property, I can move .item-3 to the bottom vertically. However, what I also want is to shift it horizontally, for instance, all the way to the right, while still keeping the other two items centered. Is there a specific property similar to align-self that would help achieve this horizontal alignment for .item-3?

I attempted to use margin-left: auto but unfortunately, it ended up displacing my centered items (item 1 and item 2).

Answer №1

There is a possibility, but I believe you may not actually require it. You can achieve the desired result by utilizing the margin property with the value of auto on flex elements:

.items {
  width: 180px;
  height: 180px;
  display: flex;
  gap: 1px;
  border: solid 1px;
  align-items: center;
}

.item {
  width: 40px;
  height: 40px;
  border: solid 1px;
}

.item-1 {
  margin-left: auto;
}

.item-3 {
  margin: auto 0 0 auto;
}
<div class="items">
  <div class="item item-1"></div>
  <div class="item item-2"></div>
  <div class="item item-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

Guide to integrating a Custom Font into live data on a PDF file with the help of jsPDF

I recently successfully converted a dynamic webpage to PDF using jsPDF and now I'm looking to customize the font family of the PDF document. Is there an option for this in jsPDF? Please advise, thank you! Here is my code snippet: <div id="#p ...

What is the best way to align the navigation with a maximum of three elements (lists) in a row using responsive design?

I've been trying various methods but haven't been able to center it while keeping the padding intact. It's a mobile navigation bar. Here is what my code is producing currently: [1]: https://i.sstatic.net/sHEjj.png This is how I want it to ...

Place a floating button directly in the middle of the image

This specific tag showcases an image with centered text beneath it. Could someone provide instructions on how to insert a floating button labeled 'Test Me' directly in the center of the image? Any help would be greatly appreciated! ...

What is the best way to restrict users from accessing more than 5 Bootstrap Tab-Panes at once?

Users are restricted to opening only a maximum of 5 tab panes, even if there are multiple tab buttons available. If the user tries to click on the 6th tab, an alert message will be displayed. function addTab(title, url){ var tabs=$(".tabs"), tab ...

Shadows appear distorted in Internet Explorer 9

After searching online for how to add shadows to my containers, I came across this code that I applied to my divs: .boxShadow { -webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); -ms-filter: " ...

Turn off hover functionality for mobile and tablet devices

Is there a way to prevent hover effects on mobile and tablet devices for an SVG file used in the img tag? scss file: .menu-link:hover { img { filter: invert(40%) sepia(90%) saturate(2460%) hue-rotate(204deg) brightness(93%) contrast(93%); } .side ...

What could be causing my button to pass the event to its parent element?

I've implemented a clever trick within a div, where I have included my own custom "data" indicating what action to take when the user clicks on it. My JavaScript code snippet is as follows (with some code omitted for clarity): $('[data-hqf-swit ...

Phonegap - Retaining text data in a checklist app beyond app shutdown

This is my first time developing an app with Phonegap. I am looking to create a checklist feature where users can input items into an input field. However, I am struggling with figuring out how to save these items so that they remain in the checklist even ...

Creating a Mobile-friendly Sidebar: A Guide to Angular 5

I've been seeing a lot of recommendations online about using Angular Material for creating a sidebar, but I'm hesitant to install an entire library just for one component. After following the instructions provided here, I managed to develop a si ...

Scrolling Vertically using WinJS ListView

Is it possible to achieve vertical scrolling in a ListView using HTML/JS for Windows 8 applications with WinJS? I successfully created a Windows 8 app with XAML/C# that utilizes a ListView for vertical scrolling. However, when attempting to replicate the ...

Is there a way to create automatic line breaks in this text?

Is there a way to automatically ensure the span spans multiple lines? I am working with HTML and CSS. Below is a modified version of my code, as some parts are in PHP: <div style='border: 1px solid; padding: 10px; margin-top: 5px;'> < ...

Set the div to display inline

Is there a way to align the second div next to the first div? I want the flash swf file to be displayed beside the table cells. <html> <div style="display: inline"> <table style="table-layout:fixed;width:100%;"> <tr> ...

Is there a method to achieve a similar effect as col-md and col-sm but for adjusting height instead?

I'm looking for a way to adjust the height based on the device size like how sm, md, and lg classes work. I'm designing a login form that looks great on cell phones, but appears too tall with excessive spacing when viewed on an iPad. There are la ...

Experiencing challenges during the creation of a NUXT build

Trying to build a Nuxt SSR app, but encountering an error related to the css-loader during the build command execution. The issue seems to be with Invalid options object. ERROR in ./node_modules/vue2-google-maps/dist/components/streetViewPanorama.vue (./no ...

Create a customized menu with jQuery that disappears when hovered over

Check out this menu I've created: http://jsbin.com/useqa4/3 The hover effect seems to be working fine, but I'm looking to hide the div #submenuSolutions when the user's cursor is not on the "Solution" item or the submenu. Is there a way to ...

Struggling to properly position content with Bootstrap and CSS

I have been tasked by my mentor to create a website based on a wireframe that he sent me. However, I am facing difficulty in positioning the text on these "hero" images. https://i.sstatic.net/Xw19o.jpg https://i.sstatic.net/uBdaa.jpg The text seems to be ...

Can an image be designated for the "over scrolled" (bounced) section of a mobile webpage?

When users scroll to the top or bottom of a mobile browser page, they usually encounter a default behavior that leaves white space and causes the whole page to bounce back. In iOS applications, it's easy to add images and interactive elements to these ...

What is the correct method for asynchronously loading CSS files in web development?

On a mission to enhance my website's performance, I set out to achieve the perfect score on PageSpeed Insights. Everything was going smoothly until I encountered an issue with CSS delivery. I initially achieved a flawless result by utilizing the prel ...

CSS bundling may cause rendering issues, but it functions properly when written directly on the page

When I attempt to link the CSS file within the head or put it in a bundle, it won't render properly. However, if I use inline styles, everything works fine. The application is running locally with debug set to true on Web.config - <compilation debu ...

Angular: Display an element above and in relation to a button

Before I dive in, let me just mention that I have searched extensively for a solution to my problem without much luck. The issue is describing exactly what I'm trying to accomplish in a way that yields relevant search results. If you're interest ...