# Class Components Marko makes it easy to create user interface components to use as building blocks for web pages and applications of any complexity. Marko promotes self-contained components that: - Are independently testable - Encapsulate the view, client-side behavior (like event handling) and styling - Can easily be combined to create composite UI components. Marko components compile into small, efficient JavaScript modules that hide implementation details from consumers. Components can be published to [npm](https://www.npmjs.com) for reuse across applications. ## UI component diagram  In Marko, the DOM output of a UI component is based on _input properties_ and optional _internal state_ used to control the view. If Marko detects changes to `input` or the internal `state`, then the view (that is, the DOM) will automatically update to reflect the new input and state. Internally, Marko uses virtual DOM diffing/patching to update the view, but that’s an implementation detail that could change at any time. ## Component structure Marko makes it easy to keep your component’s class and styles next to the HTML view that they correspond to. The following are the key parts of any UI component: - **View** - The HTML template for your UI component. Receives input properties and states, and renders to either server-side HTML or browser-side virtual DOM nodes. - **Client-side behavior** - A JavaScript `class` with methods and properties for initialization, event handling (including DOM events, custom events and lifecycle events), and state management. - **Styles** - Cascading StyleSheets, including support for CSS preprocessors like [Less](https://lesscss.org/) or [Sass](https://sass-lang.com/). ## Server-side rendering A UI component can be rendered on the server or in the browser, but stateful component instances will be automatically mounted to the DOM in the browser for both. If a UI component tree is rendered on the server, then Marko will recreate the UI component tree in the browser with no extra code required. For more details, please see [Rendering](/docs/rendering/). ## Single-file components Marko lets you define a `class` for a component right in the `.marko` file, and call that class’s methods with `on-*` attributes: ```marko class { onCreate() { this.state = { count: 0 }; } increment() { this.state.count++; } }
``` ### Styles Adding styles in your view is also made easy: ```marko style { .primary { background: #09c; } }