HTML Compiler Online
Javascript Online Compiler
index.html
Change Theme
Compress code
Format code
Download Code
Clear Code
Copy Code!
RunĀ
ctrl+s or click
Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Article - center horizontally using position absolute</title> <style> .parent { position: relative; border: 2px solid #4B5681; background: #7E89B4; height: 100px; } /* center element using position absolute and margin auto */ .child { position: absolute; left: 0; right: 0; margin: 0 auto; height: 100px; width: 200px; background: #353C5A; font-family: sans-serif; text-align: center; color: white; } </style> </head> <body> <h2>To align a element to the center horizontally use position absolute and margin auto.</h2> <br> <div class="parent"> <div class="child">Child</div> </div> </body> </html>
RunĀ