Search code examples
htmlbrowsercenter

How can I center any html content in the center of a browser?


I am new to many html code concepts and have a question in regards to centering html content.

In regards to html code, how is the best way to center any content in the center of a webbrowser?

I am specifically needing to do this in a C# webBrowser control, and would like to know if I need to add some styles to my html code, add some attributes, add a specific container or is there another approach.

I am needing to center both horizontally and vertically.

Thanks.


Solution

  • You have to specify a fixed height to your conatiner and add the same height as line-height, then add your content to another container and place it inside the main container.

    Try the following code

    <!DOCTYPE html>
    <html>
    <style>
    .content{
        text-align: center; 
        height:800px;
        line-height: 800px;
    }
    .inner-content {
      display: inline-block;
      vertical-align: middle;
      line-height: normal;      
    }
    </style>
    <body>
        <div class="content"><span class="inner-content">test content<br>test content<br>test content<br>test content<br>test content<br>test content<br>test content<br>test content<br>test content<br>test content<br></span></div>
    </body>
    </html>