Note: Before you run through the examples below, open the Beginner's example site that we built during the Getting started with the Web article series. You should have this open as you follow the steps below.
How to open the devtools in your browser
The devtools live inside your browser in a subwindow that looks roughly like this:
How do you pull it up? Three ways:
- Keyboard. Ctrl + Shift + I, except
- Internet Explorer. F12
- Mac OS X. ⌘ + ⌥ + I
- Menu bar.
- Firefox. Menu ➤ ➤ Toggle Tools, or Tools ➤ Web Developer ➤ Toggle Tools
- Chrome. View ➤ Developer ➤ Developer Tools
- Safari. Develop ➤ Show Web Inspector. If you can't see the Develop menu, go to Safari ➤ Preferences ➤ Advanced, and check the Show Develop menu in menu bar checkbox.
- Opera. Developer ➤ Web Inspector
- Context menu. Press-and-hold/right-click an item on a webpage (Ctrl-click on the Mac), and choose Inspect Element from the context menu that appears. (An added bonus: this method straight-away highlights the code of the element you right-clicked.)
The Inspector: DOM explorer and CSS editor
The devtools usually open by default to the inspector, which looks something like the below screenshot. This tool allows you to see what the HTML on your page looks like at runtime, as well as what CSS is applied to each element on the page. It also allows you to instantly modify the HTML and CSS and see the results of your changes reflected live in the browser viewport.
If you don't see the inspector,
- Tap/click the Inspector tab.
- In Internet Explorer, tap/click DOM Explorer, or press Ctrl + 1.
- In Safari, the controls are not so clearly presented, but you should see the HTML if you haven't selected something else to appear in the window. Press the Style button to see the CSS.
Exploring the DOM inspector
For a start, try right-clicking (Ctrl-clicking) an HTML element in the DOM inspector and look at the context menu. The menu options vary among browsers, but the important ones are mostly the same:
- Delete Node (sometimes Delete Element). Deletes the current element.
- Edit as HTML (sometimes Add attribute/Edit text). Lets you change the HTML and see the results on the fly. Very useful for debugging and testing.
- :hover/:active/:focus. Forces element states to be toggled on, so you can see what their styling would look like.
- Copy/Copy as HTML. Copy the currently selected HTML.
- Some browsers also have Copy CSS Path and Copy XPath available, to allow you to copy the CSS selector or XPath expression that would select the current HTML element.
Try editing some of your DOM now. Double-click an element, or right-click it and choose Edit as HTML from the context menu. You can make any changes you'd like, but you cannot save your changes.
Exploring the CSS editor
By default, the CSS editor displays the CSS rules applied to the currently selected element:
These features are especially handy:
- The rules applied to the current element are shown in order of most-to-least-specific.
- Click the checkboxes next to each declaration to see what would happen if you removed the declaration.
- Click the little arrow next to each shorthand property to show the property's longhand equivalents.
- Click a property name or value to bring up a text box, where you can key in a new value to get a live preview of a style change.
- Next to each rule is the file name and line number the rule is defined in. Clicking that rule causes the dev tools to jump to show it in its own view, where it can generally be edited and saved.
- You can also click the closing curly brace of any rule to bring up a text box on a new line, where you can write a completely new declaration for your page.
You'll notice a number of clickable tabs at the top of the CSS Viewer:
- Computed: This shows the computed styles for the currently selected element (the final, normalized values that the browser applies).
- Box model: This represents visually the current element's box model, so you can see at a glance what padding, border and margin is applied to it, and how big its content is.
- Fonts: In Firefox, the Fonts tab shows the fonts applied to the current element.
Find out more
Find more out about the Inspector in different browsers:
- Firefox Page inspector
- IE DOM Explorer
- Chrome DOM inspector (Opera's inspector works the same as this)
- Safari DOM inspector and style explorer
To see what happens, try entering the following snippets of code into the console one by one (and then pressing Enter):
document.querySelector('html').style.backgroundColor = 'purple';
var myImage = document.createElement('img'); myImage.setAttribute('src','https://farm4.staticflickr.com/3455/3372925208_e1f2aae4e3_b.jpg'); document.querySelector('h1').appendChild(myImage);
Now try entering the following incorrect versions of the code and see what you get.
document.cheeseSelector('html').style.backgroundColor = 'purple';
var myImage = document.createElement('img'); myBanana.setAttribute('src','https://farm4.staticflickr.com/3455/3372925208_e1f2aae4e3_b.jpg'); document.querySelector('h1').appendChild(myImage);
You'll start to see the kind of errors that the browser returns. Often these errors are fairly cryptic, but it should be pretty simple to figure these problems out!
Find out more
- Firefox Web Console
- Safari Console