CSS Box Model : Auto (margin: 0 auto;)

The margin property also lets you center content. However, you must follow a few syntax requirements. Take a look at the following example:

div.headline {
  width: 400px;
  margin:auto;
}

In the example above, margin: 0 auto; will center the divs in their containing elements. The 0 sets the top and bottom margins to 0 pixels. The auto value instructs the browser to adjust the left and right margins until the element is centered within its containing element.

In order to center an element, a width must be set for that element. Otherwise, the width of the div will be automatically set to the full width of its containing element, like the <body>, for example. It’s not possible to center an element that takes up the full width of the page, since the width of the page can change due to display and/or browser window size.

In the example above, the width of the div is set to 400 pixels, which is less than the width of most screens. This will cause the div to center within a containing element that is greater than 400 pixels wide.


Instructions:

1 .Set the width of the .pull-quote class elements to 350 pixels.

Hint:

Setting the width of an element is done with the width property followed by the value. Remember, pixels is written as px!


2.In one line, set the vertical (top and bottom) margins of the .pull-quote class to 0 and the horizontal (left and right) margins to auto.

Hint:

Here’s a refresher for how to create a ruleset that will horizontally center a <div>:

div {
  margin:auto;
}

For the margin ruleset above, the first value 0 sets the vertical (top and bottom) margin, and the second value auto sets the horizontal (left and right) margin.


3.Set the vertical margins of the #main element to 0, and the horizontal margins to auto.


Hint:

Use the same syntax as the above. Remember to add a space between the 0 and auto values!



Question & Answers

Comments

Popular posts from this blog

The Box Model in DevTools( With YT Video)

CSS Box Model : box-sizing_ border-box