InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS

    CSS selectors

    • CSS selectors

    CSS properties

    • CSS properties

    CSS cookbooks

    • !important annotation
    • Align div in center horizontally
    • Align div in center vertically
    • Align multiple divs horizontally
    • Align text in center horizontally
    • CSS - drop cap effect
    • CSS - enlarge image on hover
    • CSS - round toggle switch using checkbox and label
    • CSS automatic table row numbering
    • CSS circle and oval
    • CSS clearfix - clear float automatically using ::after
    • CSS counters
    • CSS nested counters
    • CSS3 gradients
    • Create CSS triangles
    • First letter capital and bigger
    • How to display text on image using css
    • Install sass on Mac or Linux using ruby gem
    • Place a div in bottom right corner of browser
    • Test site css, javascript, html in old IEs
    • csslint
    • inherit value
    • initial value
    • inline-block and baseline alignment
    • margin collapsing
    • node-sass quick start tutorial on Linux and Mac
    • unset value
    • web typography
     
    • Home
    • > Tutorials
    • > CSS

    CSS – align div in center horizontally

    on Feb 22, 2016

    Div (when a block level element) or any other block level element can be aligned in center of its container using property margin-left:auto and margin-right:auto. Note that When div is inline-block this approach won’t work. Here are some examples.

    Align div in middle horizontally

    Here inner div is aligned in center using margin:auto. Not that text within center div is not aligned in center.

    <style type="text/css">
    .container {
      width: 300px;
      height:150px;
      background-color:lightgreen;
    }
    .inner {
      margin:auto;
      background-color:lightgray;
      width: 200px; height: 100px;
    }
    </style>
    
    <div class="container">
      <div class="inner">Hello world</div>
    </div>
    refresh done
    try it online

    Align h1 in middle horizontally

    Here inner h1 is aligned in center using margin:10px auto. Not that text within h1 is not aligned in center.

    <style type="text/css">
    .container {
      width: 300px;
      height:150px;
      background-color:lightgreen;
    }
    .inner {
      margin:10px auto;
      background-color:lightgray;
      width: 200px;
    }
    </style>
    
    <div class="container">
      <h1 class="inner">Hello world</h1>
    </div>
    refresh done
    try it online

    Suggested posts:

    1. CSS – align text in center horizontally
    2. CSS – align multiple divs horizontally
    3. CSS – how to align image and text in center vertically
    4. CSS – align div in center vertically
    5. CSS text-align – align text left, right or justify
    6. CSS – inline-block and baseline alignment
    7. How to display a div in browser window center
    8. CSS – margin collapsing
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS cookbook, CSS layout, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2023 InfoHeap.

    Powered by WordPress