Inline Critical CSS: Cut Your FCP in Half

●1 ●15 ●67
calendar_today • schedule2 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);
?>

Want more #’s & #critical deep dives?

Join 6,024+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

10 Proven Ways to Cut Your AWS Bill

rogo032 - Jan 16

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

praneeth - Mar 31

Is Your ‘Scalable’ Backend a Ticking Time Bomb?

PrabashanaDev - Jun 18

Tailwind CSS for Beginners: From Zero to Your First Component

muhammadfarhan.dev - Jul 21

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

Karol Modelski - Apr 9
chevron_left
3.5k Points • 83 Badges
Worldwide • built2winweb.com
35Posts
12Comments
53Connections
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)

3 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!