How to Center Something in a DIV in CSS

104 7
    • 1). Open your HTML file in a code editor or Notepad. Locate the DIV tags in your HTML code where you want to center the content. Look for the ID attribute of the opening DIV tag or add one yourself. Set the ID value to a unique, memorable name. Here is an opening DIV tag with an ID attribute:

      <div>

    • 2). Find the <style> tags at the top of your HTML file or add them yourself. The <style> tags should go between the <head> tags.

    • 3). Write your CSS selector to "select" the content within your DIV. This is the content you want to center, and you can select it by its wrapping tags. For an <img> tag, for example, write the selector like this:

      img {}

      Write the selector for tags containing the class name "centerme" like this:

      .centerme {}

      Replace the period with a hash symbol to select an ID instead.

    • 4). Set the horizontal margins of the centered content to "auto," and give the content a width. You can set the width to pixels for a fixed value or set it to a percentage to let it change depending on browser size. Here is the code:

      .centerme {

      width: 80%;

      margin: 0 auto;

      }

      The "margin: 0 auto;" is shorthand for zero top and bottom margins and automatically calculated left and right margins.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.