Generate, Verify & Improve AI-Generated Markup
2025-10-12
Important
New Approach: This isn’t a comprehensive HTML/CSS tutorial. Instead, we’ll focus on the essential knowledge you need to verify and improve AI-generated code.
You already have AI tools! Use them to generate HTML/CSS, but you must understand what they create.
By the end of this session, you will be able to:
AI often generates <div>
soup — functionally correct but semantically wrong.
❌ What AI Might Generate
Problems:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
</head>
<body>
<header> <!-- Page/section header -->
<nav> <!-- Navigation links -->
<!-- navigation links go here -->
</nav>
</header>
<main> <!-- Main content (ONE per page) -->
<article> <!-- Self-contained content -->
<section> <!-- Thematic grouping -->
<!-- section content -->
</section>
</article>
<aside> <!-- Sidebar/related content -->
<!-- aside content -->
</aside>
</main>
<footer> <!-- Page/section footer -->
<!-- footer content -->
</body>
</html>
Common AI Mistakes
<main>
elements — Only ONE allowed per page!<div>
everywhere — Use semantic elements first, divs for styling onlyWhen AI generates HTML, verify these critical points:
✅ Structure
✅ Accessibility
✅ SEO & Meta Tags
Scenario: AI generates a product card. Let’s review and improve it.
AI’s First Attempt
What’s wrong? (Identify with class)
Improvements:
<article>
(self-contained product)<h2>
(semantic heading)<data>
element (machine-readable price)<button>
(keyboard accessible)Screen-reader helper (CSS)
Add a utility class for visually-hidden text used for assistive technologies (used above as .sr-only
).
.sr-only {
position: absolute !important;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
Use .sr-only
for labels or helper text that should be read by screen readers but not shown visually.
Prompting Tip
Instead of: “Create a product card”
Try: “Create a semantic HTML product card with proper accessibility. Include article element, heading, alt text for images, and a real button element.”
Landmarks and ARIA help screen readers navigate your page. Prefer native semantic elements (nav, main, header, footer, aside) first — use ARIA only when semantics can’t be expressed with native HTML.
<!-- Skip link for keyboard users -->
<a class="skip-link" href="#main">Skip to content</a>
<!-- Nav with accessible name -->
<nav aria-label="Primary navigation">
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
<!-- Use aria-hidden for decorative elements -->
<svg aria-hidden="true" focusable="false">...</svg>
Notes:
aria-label
or a visible heading inside landmarks to give assistive tech a name.role="navigation"
to a <nav>
unless you need to override semantics).aria-hidden="true"
for purely decorative content so screen readers ignore it.Prompting tip for LLMs: “Return semantic HTML with ARIA where necessary. Include a skip-link and explicit aria-labels on landmarks. Explain any ARIA you add and why native semantics couldn’t cover it.”
When AI generates image markup it often forgets responsive sources, width/height, and lazy-loading : this harms performance and causes layout shifts.
Basic responsive img
with srcset
/sizes
and lazy loading:
Use <picture>
for format selection or art-direction (e.g., WebP fallback or different crops):
<picture>
<source type="image/webp" srcset="/images/phone-800.webp 800w, /images/phone-1200.webp 1200w" sizes="(max-width:600px) 100vw, 50vw">
<img src="/images/phone-800.jpg" srcset="/images/phone-800.jpg 800w, /images/phone-1200.jpg 1200w" sizes="(max-width:600px) 100vw, 50vw" loading="lazy" width="800" height="600" alt="iPhone 15 in blue color">
</picture>
Best practices to include in verification:
alt
(or alt=""
for purely decorative images) and a sensible width
/height
or CSS aspect-ratio to avoid layout shift.loading="lazy"
for non-critical images, decoding="async"
for faster paint.srcset
/sizes
or <picture>
for large images so browsers select appropriate resources.Prompting tip for LLMs: “Generate image markup with srcset, sizes, width/height, loading=”lazy”, and a brief note about expected image file names and sizes. If the image is decorative, set alt=“” and aria-hidden as needed.”
You must understand these to verify AI-generated CSS:
Specificity determines which style wins when multiple rules target the same element.
Specificity Hierarchy (from weakest to strongest):
CSS rules:
/* Specificity: (1 element) */
p { color: blue; }
/* Specificity: (1 class) - WINS! */
.text { color: red; }
/* Specificity: (1 class + 1 element) - WINS OVER .text! */
p.text { color: green; }
/* Specificity: (1 ID) - WINS OVER EVERYTHING! */
#intro { color: purple; }
HTML example:
Every element is a box with 4 layers:
┌─────────────────────────────────┐
│ MARGIN (transparent) │
│ ┌───────────────────────────┐ │
│ │ BORDER │ │
│ │ ┌─────────────────────┐ │ │
│ │ │ PADDING │ │ │
│ │ │ ┌───────────────┐ │ │ │
│ │ │ │ CONTENT │ │ │ │
│ │ │ │ (text, img) │ │ │ │
│ │ │ └───────────────┘ │ │ │
│ │ └─────────────────────┘ │ │
│ └───────────────────────────┘ │
└─────────────────────────────────┘
.box {
/* Content size */
width: 200px;
height: 100px;
/* Spacing inside the border */
padding: 20px; /* All sides */
padding: 10px 20px; /* vertical | horizontal */
padding: 10px 20px 30px; /* top | horizontal | bottom */
/* Border */
border: 2px solid black;
/* Spacing outside the border */
margin: 20px;
margin: 0 auto; /* Center horizontally */
}
/* Default behavior (confusing!) */
.box {
width: 200px;
padding: 20px;
border: 2px solid;
/* Total width = 200 + 20*2 + 2*2 = 244px! */
}
/* Modern best practice (include padding/border in width) */
* {
box-sizing: border-box;
}
.box {
width: 200px;
padding: 20px;
border: 2px solid;
/* Total width = 200px (padding and border included) */
}
When AI generates CSS, check:
✅ Selectors
✅ Layout
✅ Accessibility
Modern standard: Design for mobile, enhance for desktop.
Mobile-First Media Queries
Important
Ask AI to rewrite as mobile-first!
Use Flexbox for one-dimensional layouts (rows or columns).
/* Parent container */
.flex-container {
display: flex;
flex-direction: row; /* or column */
justify-content: center; /* align on main axis */
align-items: center; /* align on cross axis */
gap: 20px; /* spacing between items */
}
/* Child items */
.flex-item {
flex: 1; /* Grow to fill space equally */
}
/* Horizontal navigation */
nav {
display: flex;
gap: 20px;
}
/* Centered content */
.hero {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Card layout (wrap on small screens) */
.cards {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px; /* grow | shrink | basis */
}
Use Grid for complex two-dimensional layouts.
Basic Grid Pattern
When to Use What?
Traditional CSS
Pros:
Cons:
Spacing (m=margin, p=padding, numbers = 4px units)
<div class="m-4"> margin: 1rem (16px)
<div class="p-6"> padding: 1.5rem (24px)
<div class="mx-auto"> margin-left/right: auto (center)
<div class="mt-2"> margin-top: 0.5rem
Layout
<div class="flex items-center justify-between">
<div class="grid grid-cols-3 gap-4">
<div class="w-full md:w-1/2"> width: 100% on mobile, 50% on md+
Typography
When prompting AI for Tailwind:
"Create a product card using Tailwind CSS with:
- White background with shadow
- Padding and rounded corners
- Responsive (full width on mobile, 1/3 on desktop)
- Hover effect on image"
AI will generate Tailwind classes correctly most of the time!
🚩 Problem 1: Inline Styles Everywhere
<!-- ❌ AI might generate -->
<div style="background: blue; padding: 20px; margin: 10px;">
<p style="color: white; font-size: 16px;">Text</p>
</div>
Why bad? Not reusable, hard to maintain, poor separation of concerns
Fix: Use classes
🚩 Problem 2: No Alt Text or Generic Alt
<!-- ❌ AI might generate -->
<img src="product.jpg">
<img src="product.jpg" alt="image">
<img src="product.jpg" alt="product image">
Why bad? Screen readers can’t describe the image
Fix: Descriptive alt text
Prompt fix: “Ensure all images have descriptive alt text explaining what’s shown”
🚩 Problem 3: Fake Buttons (Divs with Click Handlers)
Why bad? - Not keyboard accessible (can’t Tab to it) - No focus indicator - Screen readers don’t announce it as a button - No Enter/Space key support
Fix: Real button elements
🚩 Problem 4: CSS Without box-sizing
/* ❌ AI often forgets */
.card {
width: 300px;
padding: 20px;
border: 2px solid;
/* Actual width = 344px! */
}
Fix: Add box-sizing globally
🚩 Problem 5: No Focus Styles
Why bad? Keyboard users can’t see where they are
Fix: Always include focus styles
🚩 Problem 6: Hardcoded Pixel Values Everywhere
Why bad? Not responsive, doesn’t scale with user’s font preferences
Fix: Use relative units
Unit guide:
rem
— Relative to root font size (best for spacing)em
— Relative to parent font size (good for padding inside text)%
— Relative to parent element (good for widths)vw/vh
— Relative to viewport (good for full-screen sections)Every time AI generates HTML/CSS:
Step 1: Read & Understand (Don’t just copy!)
Step 2: Check Semantics
Step 3: Check Accessibility
Step 4: Check CSS Quality
Step 5: Test in Browser
Remember These Always
Now you try! Use these prompts with your AI tool of choice.
For HTML Generation
Create semantic HTML for a product review card with:
- Article element as container
- Product image with descriptive alt text
- Heading for reviewer name
- Star rating (5 stars, accessible)
- Review text paragraph
- Date element with machine-readable format
- Helpful/Not helpful buttons (real button elements)
Ensure proper accessibility with ARIA labels where needed.
For Tailwind CSS Styling
Style the review card using Tailwind CSS with:
- White background with subtle shadow
- Rounded corners and padding
- Responsive layout (full width on mobile, 1/3 on desktop)
- Hover effect (slight shadow increase)
- Focus states for buttons
- Mobile-first approach with sm:, md:, lg: breakpoints
For Verification
Review this HTML/CSS code for:
- Semantic HTML correctness
- Accessibility issues (alt text, labels, focus states)
- CSS best practices (box-sizing, relative units)
- Responsive design patterns
- Any potential problems
Suggest specific improvements with code examples.
You now have the knowledge to:
E. Bruno - HTML & CSS Essentials for AI-Assisted Development