center item in div code example

Example 1: center a div in css

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

Example 2: css center image

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

Example 3: center div horizontally and vertically

.parent {
  position: relative;
}
.child {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

Example 4: css align center

//HTML
Hello World
//CSS .parent { display: flex; justify-content: center; align-items: center; }

Example 5: how to center a div element

/*ADD MARGIN auto to left and right*/
.box1{
    width:80%;
    margin:0 auto;
}

Example 6: center element in div

/* html */

Centering with CSS

Text-Align Method

Margin Auto Method

Absolute Positioning Method

/* css */ h1, h3 { text-align: center; } .blue-square-container { text-align: center; } .blue-square { background-color: #0074D9; width: 100px; height: 100px; display: inline-block; }

Tags:

Misc Example