What is the best way to position a box in the center using CSS?

I have constructed a container using div and included 4 additional div elements inside:

HTML:

<div className='main__container'>
        <div className='item'>Image</div>
        <div className='item'>Name</div>
        <div className='item'>Price</div>
        <div className='item'>Quantity</div>
</div>

CSS:

.main__container {
    position: relative;
    text-align: center;
    margin: 0px auto;
    display: flex;
    background-color: white;
}

.item:not(:last-child) {
    margin-right: 100px;
}

The resulting layout is depicted here:

https://i.sstatic.net/1om76.png

I am attempting to align the entire box in the middle of the page. Although I attempted to use margin: 0px auto, it did not produce the desired outcome.

Please share your thoughts if more details are necessary.

Answer №1

maybe

.main__container {
    // additional styling 
    width: max-content;
    margin-left: auto;
    margin-right: auto;
    // more styling
}

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 switching between 3 classes with mouseover using JavaScript

Currently, I am dealing with an unordered list that contains 4 items. The goal is to have the list grow to 100% of its width when hovered over, while all 'noun hovered li' items should shrink to a width of 0%. Once the cursor leaves, everything s ...

Pull data from one array of objects using the id from another array to display in a list using AngularJS ng-repeat

After retrieving a list of options, I make an API call to validate each option. The goal is to display whether each option is valid or not. My starting array looks like: $scope.preValidationArray = [ { id: 1, description: 'Item 1' }, { id: 2 ...

Struggling to transition from the `useSwr` hook to the `useMutation` hook in `react-query`?

I attempted to modify the useSwr hook in this way: import { useMutation } from 'react-query'; const { data: user, mutate: mutateUser } = useMutation<User>('/api/user'); However, I encountered an error that says: No mutationFn ...

Activate Bootstrap button functionality with JavaScript

Hey team, I've got a Bootstrap button on my HTML page: ..... <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> ... <div class="accept-btn"> <button type="button" class="bt ...

Is there a way to customize the icon in the material-ui date picker component for React?

Is it possible to customize the icon for the material UI Date picker? I've searched through the code and API documentation but can't find any mention of how to change the icon. You can check out their official documentation here: https://materi ...

React route with custom webpack configuration is disrupted by subdirectories in the URL

In my user interface, I have set up a page to display a list of items along with a form for adding new ones. However, when I try to access the "New Company" link, I encounter a 404 error and see a blank screen indicating that it cannot find the main.js fil ...

React, facing the challenge of preserving stored data in localStorage while accounting for the impact of useEffect upon

Seeking some assistance with the useEffect() hook in my Taking Notes App. I've set up two separate useEffects to ensure data persistence: one for when the page first loads or refreshes, and another for when a new note is added. I've added some l ...

The React Slide API for Mui is kicking off the listener at the beginning instead of the end

I am trying to make an object slide smoothly from the right side of the screen to the left. Below is the code I am using: import rock1 from "src/demos/MyDemo/assets/rock1.png" import Slide from '@mui/material/Slide'; const Test = () =& ...

What is the best way to create a drop-down menu that exports data directly to an Excel spreadsheet?

I have been struggling with a seemingly simple issue - I can't seem to get my drop-down box to display the chosen option. The code I'm using is quite similar to this generic one, but for some reason, it's not reporting the selected option to ...

The grid is intended to be positioned horizontally, but is currently being shown vertically

I need help fixing the layout of my items. They are currently displaying vertically instead of in a horizontal row. Any suggestions on how to correct this? https://i.stack.imgur.com/CQ4yG.jpg <div class="col-lg-12 col-sm-12 portfolio-item"> <d ...

Is it possible to integrate React Bootstrap with Next.js?

Is it possible to integrate react-bootstrap with Next.js? I have the latest versions of both, but none of my react-bootstrap components are showing up in my app. I've searched online for solutions, but haven't found anything helpful. If needed, I ...

Ember's distinctive feature: Named Block Helpers

Can we create "named blocks" in a different way? For instance, {{#customBlock "repeatableBlock"}} {{!-- block containing numerous properties and data that may become messy if hardcoded multiple times --}} {{/customBlock}} Then, elsewhere in the code, {{ ...

Preserve unencoded HTML content as a string

Here is an example string: text = '&lt;div class="blue"&gt; &lt;p&gt;This is a sample text string' When I use <%= text.html_safe %> in my webpage, it displays correctly, but not when I try it in the console. If I attempt: ...

When navigating to a new page in a PDF using react-pdf, the document automatically scrolls to the top

I have integrated react-pdf (github link) into my React application to display PDF files. Below is the code snippet: import { useColor } from '@/hooks'; import { ArrowLeftIcon, ArrowRightIcon } from '@chakra-ui/icons'; import { HStack, ...

Ajax - Trouble with Updating DIV Content

As a beginner in AJAX, I am encountering difficulties creating a simple AJAX program. My goal is to have a button that, when clicked, changes the text of the div below it. Despite numerous attempts, I have not been able to identify the issue. Below is the ...

How can I use jQuery to target elements other than the vertical scrollbar when

Here is how I am utilizing the mouseleave jquery event $(document).ready(function(){ $(document).mouseleave(function(event) { //perform a task }); }); Is there any method to prevent this event from triggering when a user scrolls ...

Locate the coordinates on an HTML5 canvas that correspond to a specified color

Is there a more efficient method to identify the coordinates of pixels with a particular color without looping through every pixel in the image? ...

Guide to inserting an HTML file into an article's content

After downloading an extension from freefrontedit and uploading it to my server in the directory /accordeon, I successfully accessed it by pointing my browser to . However, I am now faced with the challenge of loading the index.html into my Joomla article ...

Creating a paragraph from text inputs using React

I'm currently working on code that retrieves input from two textboxes - one for a string value and one for a number value. I want this data to be displayed in real-time within a paragraph without the need for a submit button. I've been struggling ...

What are the best practices for implementing a Redux store in large-scale React applications?

Currently in the process of developing a react e-commerce application that allows users to buy and sell pre-owned items. The typical user workflow for creating and managing ads involves creating an ad, uploading it to a database, fetching the ad data from ...