---
title: HtmlElement
summary: API reference for HtmlElement.
status: draft
owner: SAIF Documentation
last_reviewed: 2026-09-17
library: SAIF.Framework.Web.Mvc
reference_type: package
---

# HtmlElement

Concrete, in-memory composable HTML element implementing `IHtmlNode`; provides a general-purpose fluent way to build arbitrary markup, though it is not consumed by any other type in this library — a public building block for consuming applications.*

## Purpose
- Concrete, in-memory composable HTML element implementing `IHtmlNode`; provides a general-purpose fluent way to build arbitrary markup, though it is not consumed by any other type in this library — a public building block for consuming applications.*

## Context
- Namespace: `SAIF.Web.Mvc.UI`
- Assembly/package: `SAIF.Framework.Web.Mvc (source-shipping NuGet package)`
- Kind: class

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| HtmlElement(string) | `public HtmlElement(string tagName)` | Constructs the element for the given tag name using `TagRenderMode.Normal`, delegating to the two-argument constructor. |
| HtmlElement(string, TagRenderMode) | `public HtmlElement(string tagName, TagRenderMode renderMode)` | Constructs the element, wrapping an internal `TagBuilder` for the given tag, storing `renderMode`, and initializing `Children` to an empty list. |
| Children | `public IList<IHtmlNode> Children { get; }` | The nested child nodes appended to this element; when non-empty, `WriteTo` and `InnerHtml` render these instead of any raw HTML previously set via `Html(string)`. |
| InnerHtml | `public string InnerHtml { get; }` | Read-only. Returns the concatenated `ToString()` of every child in `Children` when children exist; otherwise returns the raw inner HTML string previously set via `Html(string)`. |
| RenderMode | `public TagRenderMode RenderMode { get; }` | Read-only. The tag rendering mode captured at construction (`Normal`, `StartTag`, `EndTag`, or `SelfClosing`), controlling how `WriteTo` emits the tag. |
| TagName | `public string TagName { get; }` | Read-only. The HTML tag name supplied to the constructor, exposed from the underlying `TagBuilder`. |
| AddClass(string[]) | `public IHtmlNode AddClass(params string[] classes)` | Appends each given class name to the existing `class` attribute value (space-separated), creating the attribute if absent, and returns this element for chaining. |
| PrependClass(string[]) | `public IHtmlNode PrependClass(params string[] classes)` | Adds each given class name via `TagBuilder.AddCssClass`, which inserts it ahead of classes already present, and returns this element for chaining. |
| AppendTo(IHtmlNode) | `public IHtmlNode AppendTo(IHtmlNode parent)` | Adds this element to `parent.Children` and returns this element for chaining. |
| Attributes() | `public IDictionary<string, string> Attributes()` | Returns the live attribute dictionary backing the underlying `TagBuilder`, allowing direct reads or mutation. |
| Attribute(string) | `public string Attribute(string key)` | Returns the current value of the named attribute from the attribute dictionary; throws if the key is not present. |
| Attribute(string, string) | `public IHtmlNode Attribute(string key, string value)` | Sets the named attribute, replacing any existing value, by delegating to the three-argument overload with `replaceExisting: true`. |
| Attribute(string, string, bool) | `public IHtmlNode Attribute(string key, string value, bool replaceExisting)` | Merges the named attribute into the underlying `TagBuilder`, either replacing or preserving an existing value depending on `replaceExisting`, and returns this element for chaining. |
| Attributes(object) | `public IHtmlNode Attributes(object attributes)` | Converts the anonymous object's properties to a string/string dictionary and merges them in, replacing existing values, then returns this element for chaining. |
| Attributes&lt;TKey, TValue&gt;(IDictionary&lt;TKey, TValue&gt;) | `public IHtmlNode Attributes<TKey, TValue>(IDictionary<TKey, TValue> attributes)` | Merges the supplied attribute dictionary in, replacing existing values, by delegating to the overload that accepts `replaceExisting`. |
| Attributes&lt;TKey, TValue&gt;(IDictionary&lt;TKey, TValue&gt;, bool) | `public IHtmlNode Attributes<TKey, TValue>(IDictionary<TKey, TValue> attributes, bool replaceExisting)` | Merges each entry of the supplied dictionary into the underlying `TagBuilder`'s attributes, replacing or preserving existing values per `replaceExisting`, and returns this element for chaining. |
| Css(string, string) | `public IHtmlNode Css(string key, string value)` | Appends a `key:value` pair to the `style` attribute (creating it if absent), skipping the append when the existing style already contains `display:none`, and returns this element for chaining. |
| Html(string) | `public IHtmlNode Html(string value)` | Clears `Children` and sets the underlying `TagBuilder.InnerHtml` to the given raw HTML string, then returns this element for chaining. |
| Template() | `public Action<TextWriter> Template()` | Returns the callback previously set via `Template(Action<TextWriter>)`, or `null` if none was set. |
| Template(Action&lt;TextWriter&gt;) | `public IHtmlNode Template(Action<TextWriter> value)` | Stores a callback that, when present, takes over rendering of the element's inner content in `WriteTo` (overriding `Children` and `InnerHtml`), and returns this element for chaining. |
| Text(string) | `public IHtmlNode Text(string value)` | Sets the underlying `TagBuilder`'s inner text (HTML-encoded via `SetInnerText`) and clears `Children`, then returns this element for chaining. |
| WriteTo(TextWriter) | `public void WriteTo(TextWriter output)` | Writes the composed markup: for `SelfClosing` mode, writes the self-closing tag only; otherwise writes the start tag, then the template callback if set, else the rendered children if any exist, else the raw inner HTML, and finally the end tag. |

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or
> contextual evidence where direct confirmation was not available in source artifacts. Statements
> marked with ** could not be confirmed; treat as unknown until verified.

## Sources
- `src/Web.Mvc/UI/HtmlElement.cs`
