How do I put an HTML page to be displayed when opening a website from hosts file?
One has nothing to do with the other.

A hosts file is just a "directory" of host names and their IP addresses.  It's not limited to websites.  You can think of it as a "flat file DNS lookup."

# Sample hosts file
#

123.456.789.001      foo.bar


With this address, you can...


Go to a website - http://foo.bar
SMB share :  smb://foo.bar
SSH connection:  ssh user@foo.bar
Copy files securely from one host to another - scp stuff.ext user@foo.bar:/path/stuff
and much more


In order for your browser to display a web page, it must be "served" that page.  Since, by default, your clients aren't running a web server, there's nothing to serve.

I'm assuming that you want to display this "web page" when someone goes to a site you "blocked" by using a custom hosts file.

You need to do this at the (your) network level where the DNS lookup redirects (or firewall intercepts/redirects) to a server hosting your error page.