Bootstrap 3 div居中问题求助:三行三张图片无法居中
Hey there! Let's work through why your three rows of three images aren't centering on your page. You mentioned trying both a custom center class and the text-center class without success—here are some common fixes and checks to resolve this:
Check for conflicting CSS styles
Often, existing styles (likefloat: leftapplied directly to the images, or a parent container withdisplay: flexbut missingjustify-content: center) can override your centering attempts. Open your browser's dev tools (press F12), inspect the row container holding your images, and look for any styles that might be forcing elements to align left. For example, if your images are usingfloat: left, add aclearfixclass to the parent row or switch to a flexbox layout instead.Use Flexbox for consistent centering
Flexbox is one of the most reliable ways to center elements in a row. Try adding this CSS to your custom center class:.custom-center-row { display: flex; justify-content: center; flex-wrap: wrap; /* Ensures images wrap to a new row on smaller screens */ gap: 1rem; /* Optional: adds spacing between images for better layout */ }Apply this class to each of your row containers (the elements holding the three images per row). Just make sure your images don't have a fixed width that's forcing the row to take up the full screen width (unless that's what you want).
Confirm
text-centeris applied correctly
If you're using Bootstrap'stext-centerclass (or a similar utility), it needs to be added to the parent container of the images—not the images themselves. Also, note thattext-centeronly works on inline or inline-block elements. If your images are set todisplay: block, switch them todisplay: inline-blockor addmargin: 0 autodirectly to each image to center them individually.Check container width constraints
If your row container haswidth: 100%but includes padding or a max-width that's smaller than the total width of your three images, the centering effect might not be visible. Double-check that the container has enough space to let the image group sit in the middle.
If none of these fix the issue, sharing a snippet of your HTML for one of the image rows would help narrow down the problem further!
内容的提问来源于stack exchange,提问作者SJM

