center inside a with CSS

Giving the div text-align: center should work. (You may have to add align=’center’ as a property for it to work in IE6, though.) Note: As pointed out by @streetpc, this method will not work properly if the image is wider than the container. Alternatively, you could also have the image as a background image: background-image: … Read more

How to horizontally center a floating element of a variable width?

Assuming the element which is floated and will be centered is a div with an id=”content” … <body> <div id=”wrap”> <div id=”content”> This will be centered </div> </div> </body> And apply the following CSS: #wrap { float: left; position: relative; left: 50%; } #content { float: left; position: relative; left: -50%; } Here is a … Read more

CSS Centering with Transform

Because translateX(-50%) moves something back to the left 50% (because of the – negative value), which means it pairs with left: 50%; to center something. If you want to use right: 50%; then use that with translateX(50%) to center. * {margin:0;} span { position: absolute; top: 50%; right: 50%; transform: translate(50%,-50%); background: black; color: white; … Read more

Why centering with margin 0 auto works with display:block but does not work with display:inline-block ?

My understanding is as follows (though I am happy to be corrected). Inline elements do not have a width property, and so the “auto” cannot be calculated. Block elements have a width property, so the width of the “auto” can be calculated. Inline-block elements have an outside which acts inline, but an inside which acts … Read more

CSS horizontal centering of a fixed div?

The answers here are outdated. Now you can easily use a CSS3 transform without hardcoding a margin. This works on all elements, including elements with no width or dynamic width. Horizontal center: left: 50%; transform: translateX(-50%); Vertical center: top: 50%; transform: translateY(-50%); Both horizontal and vertical: left: 50%; top: 50%; transform: translate(-50%, -50%); Compatibility is … Read more