The sandbox attribute is a small instruction attached to an HTML element. Its job is to restrict what embedded iframe content may do. It is easier to remember when you can see it in a real piece of markup.
What sandbox is used for
Restrict what embedded iframe content may do. The attribute appears inside an element’s opening tag. Depending on its definition, it may take a specific string, a number, an enumerated keyword, or be a boolean attribute.
The example below uses it in a iframe context. Other elements may support it too, or may give the same attribute a different meaning.
Example you can try
<section>
<iframe title="Embedded lesson" src="/lesson-preview" sandbox="allow-scripts"></iframe>
</section>
How the example works
Look for sandbox in the snippet. Here the example shows the value allow-scripts. This is a concrete input the browser can interpret; it is not just text shown to the reader.
Avoid combining allow-scripts and allow-same-origin for a same-origin iframe; it can undermine the sandbox.
Values and important behavior
An empty sandbox applies broad restrictions. Tokens such as allow-scripts, allow-forms, and allow-same-origin selectively relax them.
This detail matters most when the page receives user input, navigates to another document, or loads an external resource. In those cases, check the browser behavior and any server-side requirements separately.
Small practice task
Copy the code and change sandbox to another sensible value, or remove it entirely if it is optional. Test the difference in the browser and inspect any submitted value or network request.
Common Beginner Mistakes
- Avoid combining allow-scripts and allow-same-origin for a same-origin iframe; it can undermine the sandbox.
- Using
sandboxon an element that does not support it. - Believing that an HTML hint or client-side check replaces real server validation or a security policy.
Key Takeaways
sandboxis used to restrict what embedded iframe content may do.- Avoid combining allow-scripts and allow-same-origin for a same-origin iframe; it can undermine the sandbox.
- Choose valid values and test the result instead of guessing what the browser will do.