Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

HTML Tutorial

HTML HOME HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Styles HTML Project I HTML Formatting HTML Quotations HTML Comments HTML Colors HTML CSS HTML Project II HTML Links HTML Images HTML Favicon HTML Page Title HTML Tables HTML Lists HTML Project III HTML Block & Inline HTML Div HTML Classes HTML Id HTML Project IV HTML Global Attributes HTML Buttons HTML Iframes HTML JavaScript HTML File Paths HTML Head HTML Layout HTML Responsive HTML Project V HTML Computercode HTML Semantics HTML Project VI HTML Dialog HTML Details HTML Style Guide HTML Entities HTML Symbols HTML Emojis HTML Character Sets HTML Languages HTML URL Encoding HTML Performance HTML SEO

HTML Forms

HTML Forms HTML Form Attributes HTML Form Elements HTML Input Types HTML Input Attributes Input Form Attributes HTML Form Validation HTML Progress and Meter HTML Project VII

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Video HTML Audio HTML YouTube HTML Project VIII

Web Accessibility

Web Accessibility ARIA Attributes Keyboard Focus and tabindex Landmarks Skip Links Color Contrast

Web APIs

Web APIs Geolocation API Drag and Drop API Popover API Web Storage API Web Workers API Server-Sent Events API

HTML Advanced

HTML and the DOM Web Components HTML Security Advanced Forms

HTML Cert

HTML Certificate

HTML Examples

HTML Examples HTML Videos HTML Editor HTML Quiz HTML Exercises HTML Challenges HTML Projects HTML Website HTML Syllabus HTML Study Plan HTML Interview Prep HTML Summary

HTML References

HTML Tag List HTML Attributes HTML Global Attributes HTML Browser Support HTML Event Attributes HTML Colors HTML Canvas HTML Audio/Video HTML Doctypes HTML Character Sets HTML URL Encode HTML Language Codes HTML Country Codes HTTP Status Codes HTTP Request Methods PX to EM Converter Keyboard Shortcuts

HTML Security


Share

HTML and Security

Most of the security of a website is handled on the server.

But HTML also has a few simple tools that make your pages safer.

This chapter shows the most important ones.


Use HTTPS

A page that starts with https:// is sent in a secret code between the server and the browser.

Nobody on the way can read or change it.

A page that starts with http:// is sent as plain text.

Always use HTTPS, and use https:// in your links, images, and scripts.

If an HTTPS page loads a file over HTTP, the browser may block the file or show a warning.


Show User Text Safely

Never trust text that a user has typed.

If you put the text on the page with innerHTML, the browser reads it as HTML.

A user could then add their own elements, or even their own scripts, to your web page.

Use textContent instead, so the text is shown as plain text:

Example

<input type="text" id="name" value="<b>Maria</b>">
<button onclick="show()">Show</button>

<p>With innerHTML:</p>
<p id="html"></p>

<p>With textContent:</p>
<p id="text"></p>

<script>
function show() {
  const name = document.getElementById("name").value;
  document.getElementById("html").innerHTML = name;
  document.getElementById("text").textContent = name;
}
</script>
Try it Yourself »

When you click Show, the page shows:

With innerHTML:
Maria

With textContent:
<b>Maria</b>

With innerHTML, the browser made the name bold, because it read <b> as a tag.

With textContent, the tags are shown as plain text, and nothing can run.



Links to Other Sites

A link with target="_blank" opens a new tab.

Add rel="noopener", so the new page cannot control your page:

<a href="https://example.com" target="_blank" rel="noopener">Visit example.com</a>

New browsers do this by default, but it is still good practice for older browsers.

Read more about the rel attribute.


Sandbox Iframes

An iframe shows another page inside your web page.

The sandbox attribute stops the page in the iframe from running scripts, sending forms, and opening popups:

Example

<p>A normal iframe:</p>
<iframe srcdoc="<p id='msg'>No script</p>
<script>document.getElementById('msg').innerHTML = 'The script ran';</script>">
</iframe>

<p>An iframe with sandbox:</p>
<iframe sandbox srcdoc="<p id='msg'>No script</p>
<script>document.getElementById('msg').innerHTML = 'The script ran';</script>">
</iframe>
Try it Yourself »

The srcdoc attribute holds the page that the iframe shows.

Both iframes hold the same small page with a script.

The script changes the text from "No script" to "The script ran".

The page shows:

A normal iframe:
The script ran

An iframe with sandbox:
No script

Use sandbox when you show a page that you do not control.

Read more about the sandbox attribute.


Content Security Policy

A Content Security Policy tells the browser which scripts are allowed to run on your page.

You can add it with a <meta> tag in the <head> section:

Example

<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'self'">
</head>
<body>

<p id="msg">The script did not run.</p>

<script>
document.getElementById("msg").innerHTML = "The script ran.";
</script>

</body>
Try it Yourself »

The page shows:

The script did not run.

script-src 'self' means that only script files from your own site can run.

Scripts written directly in the page are blocked.

So even if someone adds a script to your page, the browser will not run it.


Forms and Passwords

  • Always use type="password" for password fields, so the password is hidden while the user types
  • Use method="post", so the password is not shown in the address bar
  • Only send forms to pages that start with https://
<form action="/login" method="post">
  <label for="pwd">Password:</label>
  <input type="password" id="pwd" name="pwd">
  <input type="submit" value="Log in">
</form>

Note: Checks in HTML, like required, help the user, but they do not protect your site.

The server must always check the data again!


Summary

  • Always use HTTPS
  • Use textContent, not innerHTML, for text that users type
  • Add rel="noopener" to links that open a new tab
  • Use sandbox on iframes that show pages you do not control
  • A Content Security Policy decides which scripts can run
  • The server must always check form data again

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.