Inline Critical CSS: Cut Your FCP in Half

1 1 12
calendar_today agoschedule2 min read
— Originally published at built2winweb.com

<!-- ========== SCHEMAS ========== --> <!-- FAQPage schema (generated from translation data) --> <!-- WebSite schema --> <!-- LocalBusiness schema --> ?> <!-- Organization schema --> <!-- BreadcrumbList schema (headline from translation) --> <!-- CommunicateAction schema --> <!-- ========== ARTICLE CONTENT ========== -->

Inline Critical CSS: Cut Your First Contentful Paint in Half

I’m Jacob Campbell, and if I could only make one speed fix on a slow site, it would be inlining critical CSS. External stylesheets are render-blocking: the browser refuses to paint anything until they download. Inline the little bit of CSS the first screen needs, and the page can start painting almost immediately. Here’s how it works and why it’s the biggest First Contentful Paint win available.

<style>
  /* critical.css contents here */
  body { font-family: 'Inter', sans-serif; background: #0a0a0a; }
  .hero { min-height: 100vh; display: flex; align-items: center; }
  .btn { background: #3b82f6; padding: 12px 28px; border-radius: 40px; }
</style>

<link rel="preload" href="full-styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="full-styles.css"></noscript>

<?php
// generate_critical_css.php – run after CSS changes
require 'vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;

$html = file_get_contents('https://yourdomain.com/');
$crawler = new Crawler($html);

// Find all style and link tags
$critical = '';
$crawler->filter('style, link[rel="stylesheet"]')->each(function ($node) use (&$critical) {
    if ($node->nodeName() === 'style') {
        $critical .= $node->text();
    } else {
        $href = $node->attr('href');
        $css = file_get_contents($href);
        $critical .= $css;
    }
});

// Simple heuristic: only keep rules used on the first 1000px height
// (For a real solution, use a library like `peterjmit/critical-css`)
file_put_contents('critical.css', $critical);
?>

composer require stripe/stripe-php

<?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_live_your_secret_key');

$checkout = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [[
        'price_data' => [
            'currency' => 'usd',
            'product_data' => ['name' => 'Custom PHP Website – Business Pro'],
            'unit_amount' => 175000, // $1,750
        ],
        'quantity' => 1,
    ]],
    'mode' => 'payment',
    'success_url' => 'https://built2winweb.com/success?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => 'https://built2winweb.com/cancel',
    'metadata' => ['package' => 'business']
]);
header("Location: " . $checkout->url);
exit;
?>

<?php
$payload = file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$webhook_secret = 'whsec_your_webhook_secret';

try {
    $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $webhook_secret);
} catch (\UnexpectedValueException $e) {
    http_response_code(400);
    exit();
}

if ($event->type === 'checkout.session.completed') {
    $session = $event->data->object;
    // Send email to client, create account, etc.
    mail('client@example.com', 'Payment Received', 'Package: ' . $session->metadata->package);
}
http_response_code(200);
?>

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

10 Proven Ways to Cut Your AWS Bill

rogo032 - Jan 16

Your Tech Stack Isn’t Your Ceiling. Your Story Is

Karol Modelskiverified - Apr 9

Your AI Doesn't Just Write Tests. It Runs Them Too.

Kevin Martinez - May 12

Why “Building in Public” Is Hollowing Out Your Developer Career

Karol Modelskiverified - Jun 18

AI Reliability Gap: Why Large Language Models are not for Safety-Critical Systems

praneeth - Mar 31
chevron_left
368 Points14 Badges
Worldwidebuilt2winweb.com
20Posts
2Comments
1Connections
100% custom PHP code from scratch. One-time flat fee — no monthly fees, no plugin bloat. Lightning-f... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!