center center 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 align center

.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}

Example 3: css center div

<!-- 
	 Simple CSS CENTER DIV Example
 	 
	 * See Fiddle in link for PoC *
-->

<div class="parent">
  <div class="child">
    <p>
      Eh Up Me Duck!
    </p>
  </div>
</div>

<style>
.parent {
  /* Just for aesthetics: see fiddle */
  border: 1px solid black;
  padding: 2px;
}

.child {
  border: 1px solid red;
  width: 50%;
  
  /* This is where the magic happens */
  margin-left: auto;
  margin-right: auto;
  /**********************************/
}

p {
  /* As you do */
  text-align: center;
}
</style>

Example 4: center align css

text-align: center;

Example 5: css center position

/**************** 1º method to center all elements ********************/
body {                   
  text-align: center;
}

/* If all of your elements have the property display equal to "inline", 
"block" or "inline-block", then you can use the text-align: center in
the <body> tag */

tag_name {
    display: inline; /* block or inline-block*/
}

/*...but if we have an element of type "block" with a width diferente
from the default (maximum width of the page), then this will no 
longer work!*/
  
tag_name {
    display: block;
  	width: 170px;
}


/**************** 2º method to center all elements ********************/
/* Another method, is to use the margins to center the element 
horizontally and/or vertically */

tag_name {
    display: block;
  	width: 100px;
  	margin: 0 auto 0 auto;  /* top, right, bottom, left */
}