HtmlElement.ClientRectangle Property

Definition

Gets the bounds of the client area of the element in the HTML document.

C#
public System.Drawing.Rectangle ClientRectangle { get; }

Property Value

The client area occupied by the element, minus any area taken by borders and scroll bars. To obtain the position and dimensions of the element inclusive of its adornments, use OffsetRectangle instead.

Examples

Assume you have loaded the following HTML page into a hosted instance of the WebBrowser control.

<HTML>  

    <BODY>  

        <DIV id="div1" style="position:absolute;top:100px;left:100px;border-      style:solid;border-width:1px;">  
            Edit this text.  
        </DIV>  

    </BODY>  

</HTML>  

The following code example demonstrates retrieving this element and expanding its dimensions if the client area is less than 400 pixels wide by 50 pixels high, and also sets the DIV to the contentEditable state so that the user can input text.

C#
private void EnableEditing()
{
    if (webBrowser1.Document != null)
    {
        HtmlElement elem = webBrowser1.Document.GetElementById("div1");
        if (elem != null)
        {
            if (elem.ClientRectangle.Width < 200)
            {
                elem.SetAttribute("width", "200px");
            }

            if (elem.ClientRectangle.Height < 50)
            {
                elem.SetAttribute("height", "50px");
            }

            elem.SetAttribute("contentEditable", "true");
            //elem.SetFocus();
        }
    }
}

Remarks

ClientRectangle will return position data only for elements that have been assigned an explicit height and width, or elements that use absolute positioning. A document is absolutely positioned if its position style is set to absolute, after which it can be positioned at any coordinate on the HTML page.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also