<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login Page</title>
<!-- Import Google Material Web Components CSS -->
<link
rel="stylesheet"
href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css"
/>
<!-- Import Roboto font from Google Fonts -->
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f5f5f5;
font-family: 'Roboto', sans-serif;
}
.login-card {
width: 400px;
padding: 40px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.login-title {
text-align: center;
margin-bottom: 30px;
font-size: 24px;
font-weight: 500;
}
.mdc-text-field {
width: 100%;
margin-bottom: 20px;
}
.mdc-button {
width: 100%;
}
</style>
</head>
<body>
<div class="login-card">
<h2 class="login-title">Login</h2>
<form>
<div class="mdc-text-field mdc-text-field--outlined">
<input
class="mdc-text-field__input"
id="username"
type="text"
required
/>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<label for="username" class="mdc-floating-label">Username</label>
</div>
<div class="mdc-notched-outline__trailing"></div>
</div>
</div>
<div class="mdc-text-field mdc-text-field--outlined">
<input
class="mdc-text-field__input"
id="password"
type="password"
required
/>
<div class="mdc-notched-outline">
<div class="mdc-notched-outline__leading"></div>
<div class="mdc-notched-outline__notch">
<label for="password" class="mdc-floating-label">Password</label>
</div>
<div class="mdc-notched-outline__trailing"></div>
</div>
</div>
<button class="mdc-button mdc-button--raised" type="submit">
<span class="mdc-button__label">Login</span>
</button>
</form>
</div>
<!-- Import Google Material Web Components JavaScript -->
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
<script>
// Initialize MDC components after page load
window.addEventListener('load', function () {
// Initialize text fields
const textFields = document.querySelectorAll('.mdc-text-field');
for (const textField of textFields) {
mdc.textField.MDCTextField.attachTo(textField);
}
// Initialize button
const button = document.querySelector('.mdc-button');
mdc.ripple.MDCRipple.attachTo(button);
});
</script>
</body>
</html>
Explanation:
- Include Material Web Components:
- Include the Material Web Components CSS and JavaScript files from the CDN.
- Include the Roboto font from Google Fonts for the Material Design look.
- Basic HTML Structure:
- Create a div with class login-card to hold the login form.
- Inside the card, add a title (h2) and the login form (form).
- Text Fields:
- Use
mdc-text-field
and related classes to create outlined text fields:mdc-text-field
: The main container for the text field.mdc-text-field--outlined
: Style it as an outlined text field.mdc-text-field__input
: The actual input element.mdc-notched-outline
: Creates the outline and floating label effect.mdc-floating-label
: The floating label for the input.
- Button:
- Use
mdc-button
andmdc-button--raised
to create a raised button. mdc-button__label
wraps the button text.
- JavaScript Initialization:
- After the page loads, initialize the Material Web Components using JavaScript:
- Select all text fields using querySelectorAll(
'.mdc-text-field'
). - Loop through each text field and attach
mdc.textField.MDCTextField
to it. - Select the button and attach
mdc.ripple.MDCRipple
for the ripple effect.
- Select all text fields using querySelectorAll(
- To use this code:
- Save the code as an HTML file (e.g.,
login.html
). - Open the HTML file in your web browser.
The result: