CSS does not reflect when using WebBrowser in Windows form

Asked 2 months ago, Updated 2 months ago, 4 views

The UI design on Windows form is simple, so I thought I would use webBrowser to display the button.
As a trial, we placed the following simple buttons as btn1.html in the same folder path as WindowsFormsApplication1.exe.

The contents of btn1.html are as follows:

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <style type="text/css">
                    background: #f44336;
                    cursor —pointer;
                    padding —7px;
                    opacity: 0.8;
                # btn:active {
                    background: #FFA500;
                    width —150px;
                    margin —5px;
            <script type="text/javascript">



If you open this btn1.html in IE or Chrome, the hover and active are working well.
However, if you run it on Winform with webBrowser, it won't work at all (buttons are drawn).
It seems that the CSS is not accurately reflected, so the hover and active are not loaded.

Below is the source of winform.

using System;
using System.Collections.General;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
    public partial class Form 1:Form
        public Form 1()

        private void Form1_Load(object sender, EventArgse)


        private void button1_Click(object sender, EventArgse)
            String local;
            String address;



        public bool Is HorizontalScrollbarPresent (WebBrowser web)
                var width of ScrollableArea=web.Document.Body.ScrollRectangle.Width;
                var width ofControl = web.Document.Window.Size.Width;

                return width of ScrollableArea> width of Control;
            catch (Exception ex)
                return false;

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgse)
            if (IsHorizontalScrollbarPresent (webBrowser1))

How can I fix it to reflect the CSS?

c# winforms

2022-09-30 14:24

1 Answers

The WebBrowser control uses Internet Explorer for drawing.And Internet Explorer has browser and document modes that you need to understand.

  • The WebBrowser control is in IE7 browser mode to ensure compatibility so that the display does not change.
  • View in IE5 document mode if no appropriate <!DOCTYPE> declaration has been made in browser mode below IE10.

The HTML in the questionnaire above does not contain the <!DOCTYPE> declaration, so the CSS is only displayed in IE5 mode, not reflected.For your information, open the HTML in IE11 and use the F12 Developer Tool to specify the document mode to IE5 to see the same results.

To change the browser mode of the WebBrowser control, set the registry value for Browser Emulation before loading the WebBrowser control.

2022-09-30 14:24

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.