Build a Simple HTML Page Using Kotlin HTML Builder
📖 Scenario: You want to create a simple HTML page using Kotlin's HTML builder pattern. This helps you write HTML code in Kotlin in a clear and structured way.
🎯 Goal: Build a small HTML page with a header, a paragraph, and a list using Kotlin's HTML builder pattern.
📋 What You'll Learn
Create an HTML document with a head and body
Add a title inside the head
Add a h1 header inside the body
Add a p paragraph inside the body
Add an unordered list ul with three list items li
💡 Why This Matters
🌍 Real World
Kotlin HTML builder is used to generate HTML content programmatically, useful in web servers, templates, or static site generation.
💼 Career
Understanding Kotlin DSLs and HTML generation is valuable for backend developers working with Kotlin frameworks like Ktor or building web tools.
Progress0 / 4 steps
1
Create the HTML builder setup
Create a variable called htmlContent and assign it the result of kotlinx.html.stream.createHTML().html with an empty lambda block {}.
Kotlin
Hint
Use val htmlContent = createHTML().html { } to start building your HTML.
2
Add the head section with a title
Inside the html builder block, add a head block. Inside head, add a title with the text "My Kotlin Page".
Kotlin
Hint
Use head { title { +"My Kotlin Page" } } inside the html block.
3
Add body with header and paragraph
Inside the html builder block, after head, add a body block. Inside body, add a h1 header with text "Welcome to Kotlin HTML Builder" and a p paragraph with text "This page is built using Kotlin.".
Kotlin
Hint
Use body { h1 { ... } p { ... } } inside the html block after head.
4
Add an unordered list and print the HTML
Inside the body block, add an unordered list ul with three list items li containing the texts "Item 1", "Item 2", and "Item 3". Then, print the htmlContent variable.
Kotlin
Hint
Use ul { li { ... } } inside body and then println(htmlContent) to show the HTML.
Practice
(1/5)
1. What is the main purpose of the HTML builder pattern in Kotlin?
easy
A. To write HTML code clearly and safely using Kotlin syntax
B. To convert HTML files into Kotlin source code automatically
C. To create CSS stylesheets within Kotlin code
D. To execute HTML code as Kotlin functions
Solution
Step 1: Understand the pattern's goal
The HTML builder pattern is designed to help write HTML code inside Kotlin programs clearly and safely.
Step 2: Compare options with the pattern's purpose
Only To write HTML code clearly and safely using Kotlin syntax correctly states that it helps write HTML code clearly and safely using Kotlin syntax.
Final Answer:
To write HTML code clearly and safely using Kotlin syntax -> Option A
Quick Check:
HTML builder pattern = clear, safe HTML in Kotlin [OK]
Hint: Focus on clarity and safety in HTML code generation [OK]
Common Mistakes:
Confusing HTML builder with HTML parsing
Thinking it generates CSS styles
Assuming it runs HTML as Kotlin code
2. Which Kotlin syntax correctly starts an HTML builder block for a div element?
easy
A. html.div { }
B. html { div { } }
C. html.div() { }
D. html.div[] { }
Solution
Step 1: Recall Kotlin DSL syntax for HTML builder
The HTML builder uses nested lambdas like html { div { } } to create tags.
Step 2: Check each option's syntax correctness
html { div { } } uses the correct lambda block syntax. Options B, C, and D use invalid Kotlin syntax for this pattern.
Final Answer:
html { div { } } -> Option B
Quick Check:
Correct lambda block syntax = html { div { } } [OK]
Hint: Remember: use curly braces {} for nested tags in Kotlin DSL [OK]
Common Mistakes:
Using dot notation without lambda braces
Adding parentheses incorrectly
Using square brackets instead of braces
3. What is the output of this Kotlin HTML builder code?
val result = html {
body {
p { +"Hello" }
p { +"World" }
}
}
println(result)
medium
A.
Hello
World
B.
HelloWorld
C.
Hello
World
D.
Hello
World
Solution
Step 1: Understand the structure built by the code
The code creates an html tag containing a body tag, which contains two p tags with text "Hello" and "World".
Step 2: Match the output string with the structure
<html><body><p>Hello</p><p>World</p></body></html> correctly shows the full HTML with nested tags. Other options miss tags or combine paragraphs incorrectly.
Final Answer:
<html><body><p>Hello</p><p>World</p></body></html> -> Option A
Quick Check:
Nested tags output = full html with body and paragraphs [OK]
Hint: Remember each tag creates nested HTML elements [OK]
Common Mistakes:
Forgetting the outer <html> tag
Combining text inside one <p> tag
Omitting the <body> tag
4. Identify the error in this Kotlin HTML builder code:
html {
body {
p { +"Welcome" }
div
p { +"Nested" }
}
}
medium
A. body tag cannot contain div tag
B. Incorrect use of + operator inside p tag
C. html block must be assigned to a variable
D. Missing braces {} after div tag
Solution
Step 1: Check syntax for nested tags
In Kotlin HTML builder, tags like div must be followed by braces to define their content.
Step 2: Identify the missing braces
The div tag is written without braces, so the nested p tag is not properly inside it.
Final Answer:
Missing braces {} after div tag -> Option D
Quick Check:
Every tag needs braces for children [OK]
Hint: Always add {} after tags to nest children [OK]
Common Mistakes:
Omitting braces for nested tags
Misusing + operator for text
Thinking body can't contain div
5. How would you modify this Kotlin HTML builder code to add a class attribute to the div tag?
html {
body {
div {
p { +"Content" }
}
}
}
hard
A. div("class" to "container") { p { +"Content" } }
B. div(class = "container") { p { +"Content" } }
C. div { attributes["class"] = "container"; p { +"Content" } }
D. div { setAttribute("class", "container"); p { +"Content" } }
Solution
Step 1: Recall how to set attributes in Kotlin HTML builder
Attributes are set inside the tag block using attributes["name"] = "value".
Step 2: Check each option for correct attribute setting
div { attributes["class"] = "container"; p { +"Content" } } uses attributes["class"] = "container" inside the div block, which is the correct way. Options A, C, and D use invalid syntax or a method not available in this pattern.
Final Answer:
div { attributes["class"] = "container"; p { +"Content" } } -> Option C
Quick Check:
Set attributes inside block with attributes["name"] = value [OK]
Hint: Use attributes map inside tag block to set HTML attributes [OK]
Common Mistakes:
Trying to pass attributes as function parameters
Using non-existent setAttribute method
Setting attributes outside tag block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.