HTML CSS JS Examples
HTML CSS JS Examples
CSS Styling: 11
<!DOCTYPE html>
<html>
<head>
<title>My First Webpage</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first webpage.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My First Webpage</title>
<style>
h1 {
color: red;
font-size: 36px;
}
p {
color: blue;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first webpage.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My First Webpage</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first webpage.</p>
<img src="myimage.jpg" alt="My Image">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My First Webpage</title>
<style>
nav {
background-color: #333;
overflow: hidden;
}
nav a {
float: left;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
color: black;
}
</style>
</head>
<body>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
<h1>Welcome to my website!</h1>
<p>This is my first webpage.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My First Webpage</title>
<style>
nav {
background-color: #333;
overflow: hidden;
}
nav a {
float: left;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
color: black;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px
rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<nav>
<a href="#">Home</a>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
<h1>Welcome to my website!</h1>
<p>This is my first webpage.</p>
<script>
// JavaScript code to toggle the dropdown menu
let dropdown =
document.getElementsByClassName("dropdown");
let i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click",
function() {
this.classList.toggle("active");
let dropdownContent =
this.nextElementSibling;
if (dropdownContent.style.display ===
"block") {
dropdownContent.style.display =
"none";
} else {
dropdownContent.style.display =
"block";
}
});
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My First Web Page</title>
</head>
<body>
<h1>Hello World!</h1>
CSS Styling:
<!DOCTYPE html>
<html>
<head>
<title>CSS Example</title>
<style>
h1 {
color: red;
font-size: 24px;
}
p {
color: blue;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
This is an HTML page with CSS styling applied to the heading and
paragraph elements. The CSS is defined in the <style> element
in the <head> section of the document. The h1 selector targets
the heading element and sets its color to red and font size to 24
pixels. The p selector targets the paragraph element and sets its
color to blue and font size to 16 pixels.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
alert("Hello World!");
</script>
</head>
<body>
<h1>Hello World!</h1>
<p>This is my first web page.</p>
</body>
</html>
the page is loaded, the alert box displays the message "Hello
World!".
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
function showMessage() {
alert("Button clicked!");
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<button onclick="showMessage()">Click me</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Example</title>
<style>
div {
border: 1px solid black;
padding: 10px;
margin: 10px;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div>This is a box</div>
</body>
</html>
This is an HTML page with a div element styled using CSS box
model properties. The div selector sets a black border around the
div element with a thickness of 1 pixel, adds 10 pixels of padding
inside the border, and 10 pixels of margin outside the border.
<!DOCTYPE html>
<html>
<head>
<title>My First Web Page</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p>This is my first web page.</p>
</body>
</html>
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
h1 {
color: #333;
font-size: 2em;
text-align: center;
}
p {
color: #666;
font-size: 1.2em;
}
This CSS code adds some styling to the previous HTML example.
It sets the background color of the page to light gray (#f0f0f0)
and the font family to Arial or sans-serif. It also styles the header
(<h1>) to be centered, with a font size of 2em and a color of
dark gray (#333), while the paragraph (<p>) has a font size of
1.2em and a color of gray (#666).
alert("Hello, World!");
<img src="https://www.example.com/image.jpg"
alt="Example Image">
This HTML code adds an image to the web page. The src attribute
specifies the URL of the image file, while the alt attribute provides
alternative text for the image in case it cannot be displayed.
nav {
background-color: #333;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav li {
float: left;
}
nav a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
This JavaScript code prompts the user to enter their name and
stores the result in a variable called name. It then displays an
alert box with a personalized greeting that includes the user's
name.
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>Welcome to my webpage!</h1>
<p>Here's some content for my page.</p>
</body>
</html>
p {
color: red;
}
alert("Hello, world!");
body {
background-color: #f0f0f0;
}
let x = 5;
p {
font-size: 18px;
}
This example demonstrates how to change the font size of text
using CSS. In this case, we're targeting all <p> elements and
setting their font size to 18px.
let x = 5;
if (x > 10) {
alert("x is greater than 10");
} else {
alert("x is less than or equal to 10");
}
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first website. I hope you enjoy
it!</p>
</body>
</html>
h1 {
color: blue;
font-size: 36px;
}
This is a CSS style rule that targets all <h1> elements and sets
their color to blue and font size to 36 pixels. The color property
sets the color of the text, while the font-size property sets the
size of the font.
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label>
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
}
This is a CSS style rule that creates a grid layout with three
columns that are each one-third of the available width, and a gap
of 10 pixels between each cell. The display: grid property
specifies that this element should use the grid layout.
function addNumbers(a, b) {
return a + b;
}
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first webpage using HTML.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
</style>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first webpage using HTML and CSS.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is my first webpage using HTML, CSS, and
JavaScript.</p>
<button onclick="alert('Hello World!')">Click
me</button>
</body>
</html>
Explanation: This code creates a button that, when clicked,
displays an alert message with the text "Hello World!"
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<img src="image.jpg" alt="A beautiful image">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
img {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Welcome to my website!</h1>
<img src="image.jpg" alt="A beautiful image">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<script>
function changeText() {
document.getElementById("text").innerHTML =
"New text!";
}
</script>
</head>
<body>
<h1>Welcome to my website!</h1>
<p id="text">This is the original text.</p>
<button onclick="changeText()">Change text</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
}
</style>
<script>
// JavaScript for responsive navigation bar
function toggleNavbar() {
let navbar = document.getElementById("navbar");
if (navbar.className === "navbar") {
navbar.className += " responsive";
} else {
navbar.className = "navbar";
}
}
</script>
</head>
<body>
<!-- HTML for navigation bar -->
<div class="navbar" id="navbar">
<a href="#" class="active">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
<a href="javascript:void(0);" class="icon"
onclick="toggleNavbar()">
☰
</a>
</div>
<h1>Welcome to my website!</h1>
<p>This is a responsive navigation bar using HTML,
CSS, and JavaScript.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
/* CSS for card layout */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
width: 300px;
margin: 20px;
display: inline-block;
border-radius: 5px;
}
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
}
.card img {
width: 100%;
border-radius: 5px 5px 0 0;
}
.card-container {
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
/* CSS for countdown timer */
#timer {
font-size: 48px;
font-weight: bold;
}
</style>
<script>
// JavaScript for countdown timer
let countDownDate = new Date("Mar 1, 2023
00:00:00").getTime();
let x = setInterval(function() {
let now = new Date().getTime();
let distance = countDownDate - now;
let days = Math.floor(distance / (1000 * 60 *
60 * 24));
let hours = Math.floor((distance % (1000 * 60 *
60 * 24)) / (1000 * 60 * 60));
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
/* CSS for modal window */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 600px;
border-radius: 5px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
<script>
// JavaScript for modal window
let modal = document.getElementById("myModal");
let btn = document.getElementById("myBtn");
let span =
document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</head>
<body>
<!-- HTML for modal window -->
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Modal Header</h2>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
/* CSS for modal window */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 600px;
border-radius: 5px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
<script>
// JavaScript for modal window
let modal = document.getElementById("myModal");
let btn = document.getElementById("myBtn");
let span =
document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</head>
<body>
<!-- HTML for modal window -->
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Modal Header</h2>
<p>Modal content goes here.</p>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
/* CSS for navigation bar */
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
</style>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<title>My Slideshow</title>
<style>
/* CSS for slideshow */
.slideshow {
width: 100%;
height: 400px;
position: relative;
}
.slideshow img {
width: 100%;
height: 400px;
object-fit: cover;
}
.slideshow .prev, .slideshow .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 30px;
font-weight: bold;
padding: 10px;
background-color: rgba(255, 255, 255, 0.5);
color: black;
cursor: pointer;
transition: background-color 0.3s ease;
}
.slideshow .prev:hover, .slideshow .next:hover {
background-color: rgba(0, 0, 0, 0.5);
color: white;
}
.slideshow .prev {
left: 0;
}
.slideshow .next {
right: 0;
}
/* CSS for slideshow indicators */
.slideshow-indicators {
text-align: center;
}
.slideshow-indicators button {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
border: none;
margin: 0 5px;
background-color: #bbb;
cursor: pointer;
transition: background-color 0.3s ease;
}
.slideshow-indicators button.active {
background-color: #555;
}
</style>
</head>
<body>
<!-- HTML for slideshow -->
<div class="slideshow">
<img src="slide1.jpg" alt="Slide 1">
<img src="slide2.jpg" alt="Slide 2">
<img src="slide3.jpg" alt="Slide 3">
<img src="slide4.jpg" alt="Slide 4">
<div class="prev"
onclick="plusSlides(-1)">❮</div>
<div class="next"
onclick="plusSlides(1)">❯</div>
<div class="slideshow-indicators">
<button class="active"
onclick="currentSlide(1)"></button>
<button onclick="currentSlide(2)"></button>
<button onclick="currentSlide(3)"></button>
<button onclick="currentSlide(4)"></button>
</div>
</div>
<script>
// JavaScript for slideshow
let slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
function showSlides(n) {
let i;
let slides =
document.getElementsByClassName("slideshow")[0].getElem
entsByTagName("img");
let dots =
document.getElementsByClassName("slideshow-indicators")
[0].getElementsByTagName("button");
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace("
active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>My Pricing Table</title>
<style>
/* CSS for pricing table */
.pricing-table {
width: 100%;
max-width: 800px;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
}
.pricing-table .column {
width: 100%;
max-width: 300px;
margin: 0 20px;
background-color: #fff;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
text-align: center;
transition: box-shadow 0.3s ease;
}
.pricing-table .column:hover {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
.pricing-table .header {
background-color: #333;
color: #fff;
font-size: 24px;
font-weight: bold;
padding: 20px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.pricing-table .price {
font-size: 48px;
font-weight: bold;
margin: 20px 0;
}
.pricing-table .description {
font-size: 16px;
color: #888;
margin-bottom: 20px;
}
.pricing-table .features {
list-style: none;
padding: 0;
margin: 20px 0;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
.pricing-table .features li {
font-size: 16px;
padding: 10px;
border-bottom: 1px solid #ccc;
transition: background-color 0.3s ease;
}
.pricing-table .features li:last-child {
border-bottom: none;
}
.pricing-table .features li:hover {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<!-- HTML for pricing table -->
<div class="pricing-table">
<div class="column">
<div class="header">Basic</div>
<div class="price">$9.99/month</div>
<div class="description">Lorem ipsum dolor sit
amet, consectetur adipiscing elit.</div>
<ul class="features">
<li>10 GB storage</li>
<li>50 GB bandwidth</li>
<li>5 email accounts</li>
<li>24/7 support</li>
</ul>
</div>
<div class="column">
<div class="header">Pro</div>
<div class="price">$19.99/month</div>
<div class="description">Lorem ipsum dolor sit
amet consectetur adipiscing elit.</div>
<ul class="features">
<li>50 GB storage</li>
<li>200 GB bandwidth</li>
<li>20 email accounts</li>
<li>24/7 support</li>
</ul>
</div>
<div class="column">
<div class="header">Premium</div>
<div class="price">$49.99/month</div>
<div class="description">Lorem ipsum dolor sit amet,
consectetur adipiscing elit.</div>
<ul class="features">
<li>100 GB storage</li>
<li>500 GB bandwidth</li>
<li>50 email accounts</li>
<li>24/7 support</li>
</ul>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Simon Game</title>
<style>
/* CSS for Simon game */
#simon-board {
width: 100%;
max-width: 500px;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.simon-button {
width: 100%;
max-width: 150px;
height: 150px;
margin: 10px;
background-color: #eee;
border: 5px solid #333;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
cursor: pointer;
transition: transform 0.3s ease;
}
.simon-button:hover {
transform: scale(1.1);
}
.simon-button.green {
background-color: #6ab04c;
border-color: #4e8b39;
}
.simon-button.red {
background-color: #e74c3c;
border-color: #c0392b;
}
.simon-button.yellow {
background-color: #f1c40f;
border-color: #d4ac0d;
}
.simon-button.blue {
background-color: #3498db;
border-color: #2980b9;
}
.simon-button.disabled {
opacity: 0.5;
cursor: default;
}
#simon-message {
text-align: center;
font-size: 24px;
font-weight: bold;
margin-top: 20px;
}
#simon-score {
text-align: center;
font-size: 16px;
margin-top: 10px;
}
</style>
</head>
<body>
<!-- HTML for Simon game -->
<div id="simon-board">
<div class="simon-button green"></div>
<div class="simon-button red"></div>
<div class="simon-button yellow"></div>
<div class="simon-button blue"></div>
</div>
<div id="simon-message"></div>
<div id="simon-score">Score: <span
id="score">0</span></div>
scss
Copy code
<script>
// JavaScript for Simon game
const buttons =
document.querySelectorAll(".simon-button");
const message =
document.getElementById("simon-message");
const score = document.getElementById("score");
// Highlights button
function highlightButton(button) {
button.classList.add("active");
setTimeout(() => {
button.classList.remove("active");
}, 500);
}
return false;
}
}
return true;
}
// Resets game
function resetGame() {
pattern = [];
playerPattern = [];
level = 1;
score.textContent = "0";
}
buttons.forEach((button) => {
button.addEventListener("click", () => {
if (message.textContent === "Your turn!") {
playerPattern.push(Array.from(buttons).indexOf(button))
;
highlightButton(button);
if (playerPattern.length === pattern.length)
{
if (checkPattern()) {
level++;
score.textContent = level - 1;
playerPattern = [];
playGame();
} else {
message.textContent = "Game over! Click
any button to start again.";
buttons.forEach((button) => {
button.classList.add("disabled");
});
setTimeout(() => {
resetGame();
buttons.forEach((button) => {
button.classList.remove("disabled");
});
playGame();
}, 3000);
}
}
}
});
});
}
// Start game
playGame();
</script>
</body>
</html>