What it's for
The Code Sandbox is a scratch pad for web pages. Use it to:
- try an idea before putting it on a client's site, such as a new button style or a menu that folds up on phones
- check how a piece of a page behaves at phone, tablet and desktop widths
- work out why some code isn't doing what you expect, with the browser's error messages right underneath
- paste in code from a tutorial or another tool and see what it does
- build a small page from scratch and download it, ready to upload.
Your code runs only in your browser. Nothing is uploaded, and the page you're testing is kept apart from the rest of the Studio, so a mistake in your code can't break anything else.
Quick start
The first time you open the Code Sandbox, it starts you off with a project called My first project: a heading, some text and a button that counts clicks.
- Start a project. Click New and pick a starting point, or start Blank.
- Write code. Use the HTML, CSS and JS tabs. The preview on the right updates as you type.
- Check the sizes. Click Phone, Tablet and Desktop above the preview to see the page at each width.
- Watch the console. Any errors appear in red under the preview, with a link to the line that caused them.
- Take it with you. Use Download to save it as a web page, or Copy share link to send it to someone.
The screen at a glance
- Project bar. Switch projects, start, rename, copy, delete or open them, change the layout, and share or download.
- Code tabs. HTML, CSS and JS, plus Page for the page's settings. A dot on a tab means it has something in it; a red dot on JS means there's an error.
- Run. Whether the preview updates as you type, and a button to run it yourself.
- Status line. Where the cursor is, how many lines there are, and whether your work is saved.
- Preview. Your page, with buttons to try it at different screen widths.
- Console. Messages from your JavaScript and any errors.
Side by side and Stacked in the project bar switch between the code beside the preview and the code above it. Stacked gives the preview the full width, which is handy for desktop-sized pages. On narrower screens the two always stack.
Projects
Each thing you work on is a project, with its own HTML, CSS, JS and page settings. Switch between them with the Project menu. Everything saves automatically as you type: the status line says Saving… and then Saved.
Starting a project
Click New and pick a starting point. Each one is a small working example you can learn from or build on:
| Starter | What's in it |
|---|---|
| Simple page | A heading, some text and a button that counts clicks. |
| Blank | Empty tabs, nothing to clear out first. |
| Card grid | Cards that sit three, two or one across depending on the screen, with no media queries needed. |
| Phone menu | A header whose links fold into a Menu button on small screens. |
| Form with checks | Name, email and message fields, checked in the browser before sending. |
| Gentle animation | Cards that fade up one after another, and a loading spinner. |
Managing projects
- Rename gives the project a new name.
- Make a copy duplicates it, so you can try a different idea without losing the one that works.
- Delete removes it, after asking you to confirm. If you delete your last project, a new blank one called "Untitled" takes its place.
Download the project first if you might want it again.
Writing code
The HTML tab is for what goes inside the page's body: headings, text, images, buttons. The CSS tab is for styles, and the JS tab for JavaScript. You don't need to type the <html>, <head> or <body> tags, or link the CSS and JS: the sandbox puts it all together for you.
If you paste a complete page into the HTML tab, with its own <html> and <head>, that works too. Your CSS and JS tabs are added to it.
Helpers in the code box
- Colours: tags, attributes, text, numbers and comments each get their own colour, so mistakes like a missing quote stand out.
- Line numbers down the left. A line with an error turns red.
- Closing tags: when you type
<p>in the HTML tab, the sandbox adds</p>after the cursor for you. - Indenting: press Enter and the new line lines up with the one above. After an opening tag or
{, it indents one step further. - Tab indents and Shift+Tab takes the indent away. Select several lines first to indent them all at once.
- Ctrl+/ turns the line you're on into a comment, or back again. Use it to switch a line off for a moment without deleting it.
- Ctrl+Z undoes typing, as in any text box.
Inside the code box, Tab indents instead of moving to the next button. To move on, press Esc and then Tab.
When the preview updates
With Run as I type ticked, the preview updates by itself:
- CSS changes appear almost instantly, without reloading the page. So a menu you've opened stays open while you adjust its colours.
- HTML changes appear when you pause typing for a moment.
- JavaScript runs again after a slightly longer pause, so half-typed code doesn't fill the console with errors.
Untick it if you'd rather run the page yourself, for example while writing JavaScript that makes something happen on load. Then click Run or press Ctrl+Enter.
When the page runs again, the preview keeps your scroll position, so you don't have to keep scrolling back down to the part you're working on.
The Page tab
Settings for the page as a whole. The tab gets a dot when any of them are in use.
- Page title: the name on the browser tab when you download the page or open it in a new tab.
- Language code: the language of the page, such as
en,es,frorde. Screen readers use it to pronounce the text correctly. - Extra code for the <head>: for things that belong in the page's head, such as a Google Fonts link, a library from a CDN, or meta tags. It goes in before your CSS, so your CSS can use the fonts it loads.
- Add a small reset: three common starting rules, so sizes include padding and borders, the page has no gap around its edge, and images shrink to fit their space.
The preview
Screen sizes
The buttons above the preview show the page at different widths:
| Button | Width | Like |
|---|---|---|
| Fit | Whatever space the preview has | A browser window of that size. |
| Desktop | 1280 px | A laptop or desktop screen. |
| Tablet | 820 px | A tablet held upright. |
| Phone | 390 px | A typical modern phone. |
| Small | 320 px | The smallest phones still in use. If it works here, it works everywhere. |
If a size doesn't fit in the preview, it's shrunk to fit, and the label beside the buttons says so, such as "1280 px · shown at 52%". The page itself still behaves exactly as it would at the full width.
Reload and New tab
- Reload starts the page again from scratch and scrolls back to the top. Use it to replay animations or reset a page your JavaScript has changed.
- New tab opens the page on its own in a new browser tab, so you can try it at full size or use the browser's own developer tools on it.
The console
The console under the preview shows:
- anything your JavaScript writes with
console.log(),console.warn()orconsole.error() - errors in your JavaScript, in red
- files the page couldn't load, such as an image with the wrong address.
The counts at the top show how many messages, errors and warnings there are. Click Console to fold it away when you need more room for the preview.
Going straight to an error
An error in your JavaScript comes with a link such as JS line 12. Click it to jump to that line, which is highlighted. The JS tab's dot also turns red until the error is fixed.
Trying out a line of JavaScript
The box at the bottom of the console runs a line of JavaScript inside your page. Type something like document.title or document.querySelectorAll('img').length and press Enter to see the answer. Use ↑ and ↓ to bring back lines you typed earlier.
Clear and Keep old messages
Clear empties the console. Normally it also empties itself each time the page runs again. Tick Keep old messages to keep them, with a line marking each new run. That's useful when you're comparing what happens before and after a change.
Opening files
Click Open files…, or drag files onto the code box. You can open several at once.
- An HTML page, on its own or together with its CSS and JS files, opens as a new project. The sandbox sorts it out for you: the styles go into the CSS tab, the scripts into the JS tab, the title and language into the Page tab, and the page content into the HTML tab. If you select the page's own CSS and JS files along with it, they're brought in too.
- CSS or JS files on their own go into the project you have open, replacing what's in that tab.
Opening a CSS or JS file on its own replaces that tab in the current project. Click Make a copy first if you want to keep what's there.
Files can be up to 3 MB each.
Downloading and sharing
Download
The Download menu has three choices:
- One HTML file: the whole page, with its CSS and JS inside it, in a single file. Double-click it to open it in a browser, or upload it to a website.
- Zip with separate files:
index.html,style.cssandscript.js, linked together the way most websites are set up. - Copy as one HTML file: copies the whole page so you can paste it into another editor or tool.
Copy share link
Copy share link copies a link with the whole project packed into it. When someone opens the link, the project is added to their Code Sandbox as a new project, with "(shared)" after its name. Nothing is uploaded: the code travels inside the link itself.
Big projects make long links. If a link is very long, the sandbox warns you, since some apps cut long links short. For big projects, send a download instead.
Projects live in this browser on this computer. To move one to another computer, download it as one HTML file and open it there with Open files…, or send yourself a share link.
Keyboard shortcuts
| Press | To |
|---|---|
| Ctrl+Enter | Run the page. |
| Ctrl+S | Save straight away. Saving happens automatically anyway; this just does it now. |
| Ctrl+/ | Comment out the line you're on, or bring it back. |
| Tab / Shift+Tab | Indent or un-indent the line, or all the selected lines. |
| Esc then Tab | Leave the code box and move to the next button. |
| ← / → on the tabs | Move between the HTML, CSS, JS and Page tabs. |
| ↑ / ↓ in the console box | Bring back lines you typed earlier. |
On a Mac, use ⌘ instead of Ctrl.
Questions
My code uses localStorage or cookies and it doesn't work in the preview.
The preview is deliberately walled off from the rest of the Studio for safety, so it can't store anything. Click New tab to run the page on its own, where storage works normally.
An image or file isn't showing up.
The console says which file couldn't be loaded. The preview can't see files on your computer, so use full web addresses (starting with https://) for images and other files while you're testing.
The page went blank or froze.
Usually a JavaScript loop that never ends. Untick "Run as I type", fix or comment out the loop, then click Run. If the whole tab is stuck, reload it: your code is saved.
A message says the browser's storage is full.
The browser has run out of room for your projects, and the status line says "Not saved". Download the projects you want to keep, then delete old ones.
The error points to a "page line" instead of a JS line.
That error came from code in the HTML tab or the head (such as a script you pasted into the HTML), not from the JS tab. The line number counts from the top of the whole page.
Can I use a library like jQuery or a Google Font?
Yes. Paste its <script> or <link> tag into "Extra code for the <head>" on the Page tab.