| Start |
|---|
| Using Flexbox HTML: CSS: View Result (opens new window) HTML: CSS: View Result (opens new window) See Dynamic Vertical and Horizontal Centering (opens new window) under the Flexbox (opens new window) documentation for more details on flexbox and what the styles mean. Browser Support Flexbox is supported by all major browsers, except IE versions before 10 (opens new window). Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes (opens new window). For a quick way to generate prefixes there is Autoprefixer (opens new window), a third-party tool. For older browsers (like IE 8 & 9) a Polyfill is available (opens new window). For a more detailed look at flexbox browser support, see this answer (opens new window). |
| Using CSS transform CSS transforms (opens new window) are based on the size of the elements so if you don't know how tall or wide your element is, you can position it absolutely 50% from the top and left of a relative container and translate it by 50% left and upwards to center it vertically and horizontally. Keep in mind that with this technique, the element could end being rendered at a non-integer pixel boundary, making it look blurry. See this answer in SO (opens new window) for a workaround. HTML CSS View example in JSFiddle (opens new window) CROSS BROWSER COMPATIBILITYThe transform property needs prefixes to be supported by older browsers. Prefixes are needed for Chrome<=35, Safari<=8, Opera<=22, Android Browser<=4.4.4, and IE9. CSS transforms are not supported by IE8 and older versions. Here is a common transform declaration for the previous example: For more information see canIuse (opens new window). MORE INFORMATION
|
| Using text-align The most common and easiest type of centering is that of lines of text in an element. CSS has the rule HTML CSS This does not work for centering entire block elements. See more about |
| Using margin: 0 auto; Objects can be centered by using HTML CSS Result: JSFiddle example: Centering objects with margin: 0 auto; (opens new window) |
| Using position: absolute Working in old browsers (IE >= 8) Automatic margins, paired with values of zero for the View Result (opens new window) HTML CSS Elements that don't have their own implicit width and height like images do, will need those values defined. Other resources: Absolute Centering in CSS (opens new window) |
| Using calc() The calc() function is the part of a new syntax in CSS3 in which you can calculate (mathematically) what size/position your element occupies by using a variety of values like pixels, percentages, etc. Note:- Whenever you use this function, always take care of the space between two values CSS HTML |
| Vertical align anything with 3 lines of code Supported by IE11+ (opens new window) View Result (opens new window) Use these 3 lines to vertical align practically everything. Just make sure the div/image you apply the code to has a parent with a height. CSS HTML |
| Using line-height You can also use CSS That's quite ugly, but can be useful inside an |
| Centering in relation to another item We will see how to center content based on the height of a near element. Compatibility: IE8+, all other modern browsers. HTML CSS Link to JSFiddle (opens new window) The main points are the 3 |
| Vertically align an image inside div HTML CSS |
| Centering vertically and horizontally without worrying about height or width The following technique allows you to add your content to an HTML element and center it both horizontally and vertically without worrying about its height or width. The outer container
The inner container
The content box
DemoHTML CSS See also this Fiddle (opens new window)! |
| Centering with fixed size If the size of your content is fixed, you can use absolute positioning to 50% with HTML CSS Horizontal centering with only fixed widthYou can center the element horizontally even if you don't know the height of the content: HTML CSS Vertical centering with fixed heightYou can center the element vertically if you know the element's height: HTML CSS |
| Vertically align dynamic height elements Applying css intuitively doesn't produce the desired results because For widest browser support, a workaround with helper elements: HTML CSS jsfiddle (opens new window) from original question (opens new window). This approach
|
| Horizontal and Vertical centering using table layout One could easily center a child element using HTML CSS |
Read Article
https://develop.horje.com/learn/1434/reference