HTML Explanation
html
<!DOCTYPE html>
<html lang="en">
- <!DOCTYPE html>: Declares the document type
as HTML5.
- <html
lang="en">: Starts the HTML document, specifying the
language as English (lang="en").
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Button Example</title>
</head>
- <head>: Contains metadata about
the document.
- <meta
charset="UTF-8">: Specifies the character encoding, ensuring
correct rendering of text.
- <meta
name="viewport" content="width=device-width,
initial-scale=1.0">: Makes the page responsive, ensuring proper
scaling on all devices.
- <link
rel="stylesheet" href="styles.css">: Links to an external CSS
file for styling the page.
- <title>Button
Example</title>: Sets the title of the webpage, shown in the
browser tab.
<body>
<div class="button-container">
<button id="myButton" class="custom-button">Click
Me</button>
</div>
<script src="script.js"></script>
</body>
- <body>: Contains the main content
of the webpage.
- <div
class="button-container">: A container for the button, useful for
styling or positioning.
- <button
id="myButton" class="custom-button">Click
Me</button>:
- <button>: Defines a clickable
button.
- id="myButton": A unique identifier for
the button, used to target it in JavaScript.
- class="custom-button": Adds a CSS class for
styling.
- Click Me: The text displayed on the
button.
- <script
src="script.js"></script>: Links to an external JavaScript file to add
interactivity.
CSS Explanation
css
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
font-family: Arial, sans-serif;
}
- body: Styles the entire webpage.
- display: flex;: Makes the body a flex
container for easy alignment.
- justify-content: center;: Centers content
horizontally.
- align-items: center;: Centers content
vertically.
- height: 100vh;: Sets the height to 100%
of the viewport.
- margin: 0;: Removes default browser
margins.
- background-color: #f4f4f4;: Sets a light gray
background.
- font-family: Arial,
sans-serif;:
Sets the font.
css
.button-container {
text-align: center;
}
- .button-container: Styles the container
around the button.
- text-align: center;: Centers text within the
container.
css
.custom-button {
padding: 12px 24px;
font-size: 16px;
color: #fff;
background-color: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
}
- .custom-button: Styles the button.
- padding: 12px 24px;: Adds space inside the
button (12px top/bottom, 24px left/right).
- font-size: 16px;: Sets the font size.
- color: #fff;: Sets the text color to
white.
- background-color: #007bff;: Sets the background color
to blue.
- border: none;: Removes the default
border.
- border-radius: 5px;: Rounds the corners
slightly.
- cursor: pointer;: Changes the cursor to a
pointer when hovering.
- transition:
background-color 0.3s ease, transform 0.2s ease;: Smoothly animates
background color and button scaling.
css
.custom-button:hover {
background-color: #0056b3;
}
- .custom-button:hover: Styles the button when
hovered.
- background-color: #0056b3;: Changes the background to
a darker blue on hover.
css
.custom-button:active {
transform: scale(0.95);
}
- .custom-button:active: Styles the button when
clicked.
- transform: scale(0.95);: Shrinks the button slightly.
JavaScript Explanation
document.getElementById("myButton").addEventListener("click",
function () {
alert("Button clicked!");
});
- document.getElementById("myButton"): Selects the button with
the id myButton.
- .addEventListener("click",
function () { ... }):
Attaches an event listener to the button for a click event.
- alert("Button
clicked!");:
Displays a popup message when the button is clicked.
Comments
Post a Comment
If you have any Doubts plz let me know