centering elements css code example

Example 1: css align center

//HTML
<div class="parent">
	<span>Hello World</span>
</div>

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

Example 2: css center

/* this will center all children within the parent element. */
.parent {
  display: flex;
  justify-content: center; /* horizontal */
  align-items: center; /* vertical */
}

Example 3: centering css elements

// add to the parent element
.parent {
	display: grid;
    place-items: center;
}

Example 4: vertical align css

.container{
  display: table;
}

.div-inside-container{
  display: table-cell;
  vertical-align: middle;
}

Example 5: css center div vertically

/* center vertically */
/* No Flexbox */
.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}



/* With Flexbox */

.parent {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

Example 6: how to center a div element

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