Live Editing HTML and CSS With Chrome DevTools
Live Editing HTML and CSS With Chrome DevTools
We take a look at the handy live editing feature in Chrome DevTools and how it can help debug your HTML and CSS to make your front-end smoother.
Join the DZone community and get the full member experience.Join For Free
Code something amazing with the IBM library of open source blockchain patterns. Content provided by IBM.
Chrome DevTools is a powerful suite of web development tools built right into the Chrome browser. One of the most useful features of DevTools for web developers is the ability to live edit the HTML and CSS on a page. This functionality allows any developer, even those with weaker HTML and CSS knowledge, to quickly prototype and iterate on potential changes to a web page.
While working on Lucidchart, one of my recent projects was to add some check marks into the inputs where users fill in their billing information in order to convey immediate feedback when the users entered properly formatted and valid information. Even though I am more comfortable working in our backend code than I am with CSS and HTML, I was able to easily implement these check marks with the power of live editing.
Editing HTML Using the Inspector Tool
There are two quick ways to open the inspector. The first is to open DevTools with F12, select the “Elements” tab, and click the cursor icon in the top left. The second, and faster, way is to use the keyboard shortcut Ctrl+Shift+C. If you work regularly in a Linux environment, there’s a good chance you’ve used this shortcut on accident many times when you meant to copy text!
Once the inspector is active, you can locate the HTML of any element on the page by clicking on it. Chrome will also show you positioning and sizing information of elements as you hover over them.
You can locate the HTML of any element on the page by clicking on it
Once an element is selected, you can interact with it in all kinds of ways. By right-clicking on the HTML in the “Elements” tab and selecting “Edit as HTML,” you can make live edits to the markup of a webpage that Chrome will immediately render once you’re done editing.
Telling DevTools that you are done editing can be tricky. Your first instinct may be to hit the Escape key, but this will back you out of editing while discarding your changes. You can ensure your changes save by hitting Ctrl+Enter or simply clicking outside of the text box you are editing.
HTML edits can be undone or redone using the normal Ctrl+Z and Ctrl+Y hotkeys. They will also be lost upon page refresh unless you enable persistent editing.
To start my checkmark project, I began by creating a prototype element as a place to test the tweaks I wanted to make. Using Chrome DevTools’ live edit functionality, I added a placeholder div containing only the letter “X” as my temporary makeshift checkmark.
'Edit as HTML' lets you make live edits to a webpage in Chrome
Obviously, this is a very poor excuse for a check mark, but it’s enough to help me quickly identify some issues up front. For one thing, the check mark is in completely the wrong spot. Additionally, it messed up the alignment of the other elements on the page. Both of these issues can be resolved with some CSS. Luckily, Chrome also provides some great tools for quickly prototyping CSS changes using DevTools.
Editing CSS From the “Elements” Tab
To the right of the HTML view in the “Elements” tab, there is a view showing a breakdown of what CSS rules apply to the selected element. This view can be extremely helpful for debugging a plethora of CSS bugs. For example, because you can see what rules apply to the current element, you can identify if an element is getting CSS rules that you didn’t expect or is missing CSS rules you thought applied to it. The CSS view also shows when a style applied by a matching rule is being overridden by a more specific rule.
The CSS view provides some very helpful live editing functionality. Every style has a checkbox next to it, letting you enable or disable certain styles and see how they affect the elements on the page. You can also apply new styles to a specific element, an existing CSS rule, or a brand new CSS rule. All of these changes are reflected immediately on the page.
Chrome DevTools provides some convenient functionality to make live editing CSS easier to do. The most helpful for those less familiar with CSS is autocomplete. As you type in the name of your style, Chrome will suggest likely matches. Once you enter the name of the style, Chrome will also help you use the correct value for that style. For enumerated styles, like “position” or “display,” Chrome will show the legal values for you to choose from. For numerical inputs, you can use the up and down arrows to increment the value by 1 without having to re-type the units.
As with live editing HTML, you can undo or redo your live CSS changes with Ctrl+Z and Ctrl+Y.
Using DevTools, I was able to very easily figure out a combination of CSS rules that aligned my pseudo-checkbox where I wanted it. The process involved a bit of trial and error, but live edit made the iteration cycle very short, which was particularly helpful for a CSS novice like me.
Live CSS editing is a great way to rapidly iterate on minor CSS changes
We’ve only scratched the surface of what Chrome DevTools’ live edit functionality is capable of, especially in the CSS domain. These basic tips are always invaluable for me when I go to debug a CSS issue or quickly prototype a new CSS change.
Published at DZone with permission of Stephen Rollins , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.