Demonstration of CSS and JavaScript.
HTML: The framing and structure of the house.
CSS: Paint, Wall-paper, visual finishings and appearance
JavaScript: The “Internet signal” between the NODES of the DOM to propogate information and events.
This code will use the local images kitten.jpg and tiger.jpg and modify the text under the images when the image changes upon button click. I'll also ensure to handle local resources correctly and dynamically update the text depending on which image is displayed.
Here's the revised HTML code:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Image Flip Example</title> <style> .image-container { text-align: center; /* Center align content */ margin-top: 50px; /* Margin at the top */ } #flipButton { padding: 10px 20px; /* Padding around the text */ font-size: 16px; /* Text size */ cursor: pointer; /* Cursor appears as a pointer */ background-color: #4CAF50; /* Background color */ color: white; /* Text color */ border: none; /* No border */ border-radius: 5px; /* Rounded corners */ } #imageCaption { font-size: 18px; color: #333; margin-top: 10px; } </style></head><body> <div class="image-container"> <img id="imageToFlip" src="kitten.jpg" alt="Kitten Image" width="200"> <br> <button id="flipButton" onclick="flipImage()">Flip Image</button> <h3 id="imageCaption">Your Wife before the Wedding</h3> </div>
<script> function flipImage() { var image = document.getElementById('imageToFlip'); var caption = document.getElementById('imageCaption'); if (image.src.includes('kitten.jpg')) { image.src = 'tiger.jpg'; caption.innerHTML = 'Your Wife after the Wedding'; } else { image.src = 'kitten.jpg'; caption.innerHTML = 'Your Wife before the Wedding'; } } </script></body></html>
Local Image Source: The image sources have been updated to kitten.jpg and tiger.jpg, which should be stored in the same directory as your HTML file. Dynamic Text: The caption under the image now changes dynamically when you click the button to flip the image. This is handled by checking the current source of the image and updating the innerHTML of the caption accordingly. JavaScript Function: The flipImage function now uses includes to check if the current image source contains 'kitten.jpg'. This method is more reliable when the full path to the image might change due to different deployment or development environments. Style and Layout: Minor improvements to the CSS for better readability and aesthetics. Ensure that both images (kitten.jpg and tiger.jpg) are in the same directory as this HTML file for this to work correctly when loaded in a browser.<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Flip Example</title>
<style>
/* Using class selector for styling the container */
.image-container {
text-align: center; /* Center align content */
margin-top: 50px; /* Margin at the top */
} /* Using ID selector for styling the button */
#flipButton {
padding: 10px 20px; /* Padding around the text */
font-size: 16px; /* Text size */
cursor: pointer; /* Cursor appears as a pointer */
background-color: #4CAF50; /* Background color */
color: white; /* Text color */
border: none; /* No border */
border-radius: 5px; /* Rounded corners */
}
</style>
</head>
<body>
<div class="image-container">
<img id="imageToFlip" src="kitten.jpg" alt="Kitten Image" width="200">
<br>
<button id="flipButton" onclick="flipImage()">Flip Image</button>
</div> <script>
function flipImage() {
var image = document.getElementById('imageToFlip');
// Use local filenames for comparison
if (image.src.includes('kitten.jpg')) {
image.src = 'tiger.jpg'; // Switch to the second local image
} else {
image.src = 'kitten.jpg'; // Switch back to the original local image
}
}
</script>
</body>
</html>
Explanation:
HTML Structure: The HTML contains a div element with the class image-container that centers the content within it and applies a margin at the top for spacing. Inside this div, there is an <img> tag with an id of imageToFlip and a <button> with an id of flipButton. The .image-container class selector is used to style the container, centering the content and applying a margin. The #flipButton ID selector specifically styles the button, setting its background color, text color, cursor appearance, and other properties for visual appeal and usability. The flipImage() function checks the current source of the image. If it's the initial kitten image, it changes to another kitten image upon clicking the button. If it's the second image, it switches back to the original. This function is called when the button is clicked, demonstrating a basic interaction on the web page. This page is an introductory example of how to interactively change an element on a web page using JavaScript along with applying specific CSS styles using class and ID selectors.
This is a practical demonstration suitable for beginners in web development to understand the dynamics of HTML, CSS, and JavaScript.