Aligning a element at the center is not functioning properly when using margin: 0 auto;

I'm having trouble aligning elements to the center when applying margin: 0 auto. You can view the code here: https://jsfiddle.net/helloworld123/vhhx1o7n/

The goal is to align the elements in the center of the page (.entry-wrap should be centered) and have its children (.entry) floated to the left.

I've managed to achieve this to some extent in this fiddle https://jsfiddle.net/helloworld123/n8o18ges/, but I don't want the elements in the third row to be aligned to the center.

<div class="entry-wrap">
    <div class="entry">
        <img src="https://d13yacurqjgara.cloudfront.net/users/43342/screenshots/2772190/dribbble_2016_partb_001_teaser.jpg" alt="Shot title" />
    </div>
    <div class="entry">
        <img src="https://d13yacurqjgara.cloudfront.net/users/43342/screenshots/2772190/dribbble_2016_partb_001_teaser.jpg" alt="Shot title" />
    </div>
    <div class="entry">
        <img src="https://d13yacurqjgara.cloudfront.net/users/43342/screenshots/2772190/dribbble_2016_partb_001_teaser.jpg" alt="Shot title" />
    </div>
    <div class="entry">
        <img src="https://d13yacurqjgara.cloudfront.net/users/43342/screenshots/2772190/dribbble_2016_partb_001_teaser.jpg" alt="Shot title" />
    </div>
    <div class="entry">
        <img src="https://d13yacurqjgara.cloudfront.net/users/43342/screenshots/2772190/dribbble_2016_partb_001_teaser.jpg" alt="Shot title" />
    </div>
</div>

body {
    background: #f8f8f8;
}
.entry-wrap {
    width: 500px;
  height: auto;
    margin: 0 auto;
  background: #ccc;
  padding: 10px;
}
.entry {
    float: left;
    border: 1px solid #f1f1f1;
    margin: 2px;
}

This is the desired outcome that I am aiming for.

Answer №1

1.) Replace float: left; with display: inline-block; on the .entry element

2.) Set text-align: center; for the .entry-wrap element

https://example.com/jsfiddle123

Answer №2

In the beginning, all of the elements within entry-wrap are floated, causing entry-wrap to collapse. To address this issue, one solution is to add overflow:hidden to it, or implement a clearfix technique.

If the intention is to center the child elements, adjusting the width of the wrapping div can achieve that effect:

body {
    background: #f8f8f8;
}

.entry-wrap {
    width: 620px;
    margin: 0 auto;
  padding: 15px;
  background: #ccc;
  overflow: hidden;
}
.entry {
    float: left;
    border: 1px solid #f1f1f1;
  margin: 2px;
}

Live demonstration: https://jsfiddle.net/o8aj3hqL/

Answer №3

Your centering block with the CSS property margin: 0 auto is perfectly aligned within the <body> element. However, the issue lies with the child elements being floated, preventing them from being center aligned. To resolve this, consider using display: inline-block instead of float: left.

For reference, you can view the demonstration in this fiddle: https://jsfiddle.net/vhhx1o7n/1/

Answer №4

body {
  background: #f8f8f8;
}
.entry-wrap {
  width: 618px;
  height: 318px;
  margin: 0 auto;
  background: #ccc;
  padding: 10px;
}
.entry {
  float: left;
  border: 1px solid #f1f1f1;
  margin: 2px;
}
<div class="entry-wrap">
  <div class="entry">
    <img src="https://uniqueimageurl.com/image1.jpg" alt="Image 1" />
  </div>
  <div class="entry">
    <img src="https://uniqueimageurl.com/image2.jpg" alt="Image 2" />
  </div>
  <div class="entry">
    <img src="https://uniqueimageurl.com/image3.jpg" alt="Image 3" />
  </div>
  <div class="entry">
    <img src="https://uniqueimageurl.com/image4.jpg" alt="Image 4" />
  </div>
  <div class="entry">
    <img src="https://uniqueimageurl.com/image5.jpg" alt="Image 5" />
  </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

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

Content moves as you scroll, not within a lightbox

I have implemented lightbox style overlays for my 'Read More' content on my website. However, I am facing an issue where the content within the lightbox does not scroll; instead, the background page scrolls. Any assistance with resolving this p ...

After the initial iteration, the .length function ceases to function properly when

A validation method is set up to check the length of a user input field. It functions properly on the initial try, but does not update if I go back and modify the field. $("#user").blur(function () { if ($("#user").val().length < 3) { $("#userval ...

I'm looking to replicate this navigation layout using Vuetify's 'navigation drawer' component. How can I go about doing this?

I'm currently utilizing Vuetify and implementing the Navigation Drawer component. I am attempting to replicate a similar navigation layout found here. The fixed menu on the left should maintain its width even when resizing the browser window. Upon ...

"Enhance your browsing experience with Chrome by automatically changing the background when

I am currently using Chrome with a CSS3 background. <h4 class="title-name clearfix"><a href="****************">Fairy Tail - LUCY</a> </h4> My issue is that when I hover my mouse over the title, the ba ...

The conditional CSS file does not have precedence over the regular CSS file

Currently, my approach involves using conditional comments to connect to a CSS file named "IEonly.css" if the Internet Explorer version is not greater than 8. My goal is to modify certain properties in the standard CSS file. Oddly enough, IEonly.css effect ...

Pressing a button is a way to select a specific form

I'd like to create a clickable button that can direct users to the form section on the same page. <button type="button" class="btn btn-primary btn-sm"> Go to Form Section </button> ... <form id="form1"> <div class="form-g ...

Having trouble with loading base.css in React tutorial?

Struggling with the React tutorial here. Managed to get the server/API up and running thanks to stackoverflow, but now I'm facing an odd issue - base.css won't load. The base.css file is sitting right where it should be in the css folder, and we ...

Tips for aligning an HTML button with a hyperlink

<form method="get" action=https://www.wwf.de/spenden-helfen/allgemeine-spende> <button type="submit">Donate Now</button> I am facing an issue where the button is appearing randomly on my website, but I need it to ...

When I try to host my Jekyll site on GitHub Pages using my custom domain, the CSS doesn't seem to be rendering at all

Everything looks perfect when I test my site on local host, but once I push it to my GitHub repo, only the HTML appears with no CSS or JavaScript. I've attempted changing my URL in the config.yml file to https://chanvrequebec.com and modifying href="/ ...

Java and Selenium: Struggling to Find Element based on Attribute

Attempting to click a button on a webpage with the given HTML code: <html lang="en" webdriver="true"> <head> <body class="scbody" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAKCAYAAAB10jRKAAAAGXRFWHRTb2 ...

Achieving the desired results through the utilization of CSS3 rather than relying on images

I am currently exploring the use of CSS3 to create a menu instead of relying on images over at . You can view my progress and code (including images and styles) here: Although I attempted to use CSS3 for the border shadow and matching the background of ...

Protecting against overflow for text inside a container that changes when hovered over

There is text displayed on top of an image within a div container that functions as a link. <a href="https://link.com" style="color:white"> <div class="thumbnail"> <img src="image.jpg" clas ...

What is the best way to restrict user input to specific numbers only?

I have a text input field, how can I ensure that only numeric values from 1 to 31 are allowed? <input type="number" min="1" max="31"> ...

React Application - The division is positioned centrally when run on local host, however, it does not retain its center alignment

I'm encountering an issue with deploying my create-react-app on github pages. The first image showcases my deployed app on github pages, but the image is not properly centered on the page as intended. https://i.stack.imgur.com/rOciT.jpg On the othe ...

The issue with grunt-contrib-compass not functioning properly within npm

I recently installed npm and grunt, followed by installing grunt-contrib-compass. However, when I try to run grunt, I encounter the following error: Running “compass:dist” (compass) task Warning: not found: compass Use –force to continue. Aborted du ...

What is the best way to center a div within another div without using the margin tag?

I am struggling to find a way to center a container (div) on my webpage. I've tried various methods like using margin:auto, margin:0 auto;, margin-left:auto;, and margin-right:auto;, but none of them seem to work. Additionally, I'm unsure about ...

Change the input field font style in AngularJS

Check out this Plunker link for validation of input field: http://plnkr.co/edit/iFnjcq?p=preview The validation only allows numbers to be entered and automatically adds commas. My query is, if a negative number is entered in the field, how can I change th ...

navigating directly to a particular slide within a Bootstrap carousel on a different page by clicking

Attempting to set up a bootstrap build, where clicking on certain states on one page will direct the user to a specific slide on another page. I'm struggling to grasp this concept. A friend of mine, who is a Javascript developer, provided some code f ...

Central alignment of div with cursor

I'm experimenting with creating a unique custom cursor using a <div> that trails the movement of the mouse pointer. While the current setup works smoothly, I've noticed that when scrolling down the page, the div lags behind until the scrol ...