The CSS float property is used to set or return the horizontal alignment of elements. But this property allows an element to float only right or left side of the parent body with rest of the elements wrapped around it. There is no way to float center in CSS layout. So, we can center the elements by using position property.
Example 1: This example set the position of elements exactly at the center of the screen.
<!DOCTYPE html> <html> <head> <title> Make float to center to element </title> <!-- Style to set element float to center --> <style> .Center { width:200px; height:200px; position: fixed; background-color: blue; top: 50%; left: 50%; margin-top: -100px; margin-left: -100px; } </style> </head> <body> <div class="Center"></div> </body> </html> |
Output:

Example: This example set the position of text float elements exactly at the center of the screen.
<!DOCTYPE html> <html> <head> <!-- Style to set text-element to center --> <style> .center { text-align-last: center; border: 2px solid black; } </style> </head> <body> <h2 style = "text-align:center"> Text is centered: </h2> <div class="center"> <p> <font color="green"> GeeksForGeeks A Computer Science Portal for Geeks </font> </p> </div> </body> </html> |
Output:

Recommended Posts:
- Differences between HTML <center> Tag and CSS "text-align: center;" Property
- How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS?
- Graph measurements: length, distance, diameter, eccentricity, radius, center
- How to horizontally center a div using CSS?
- How to vertically center text with CSS?
- How to center a popup window on screen?
- p5.js | center() Function
- How to vertically and horizontally align flexbox to center ?
- How to center a div within another div?
- How to Align modal content box to center of any screen?
- How to Align Navbar Items to Center using Bootstrap 4 ?
- How to Align <legend> Tag Text to Center ?
- How to center absolutely positioned element in div using CSS ?
- How to target all Font Awesome icons and align them center?
- How place a checkbox into the center of a table cell?
- How to place table text into center using Bootstrap?
- How to set the dropdown button in the center?
- How to set column in center using Bootstrap 5?
- D3.js bisector.center() Method
- HTML | center Tag
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


