Creating a visually appealing Celsius to Fahrenheit converter app in HTML, CSS, and JavaScript can be a fun project! Below is a simple implementation that includes a clean design, responsive layout, and some additional functionalities like resetting the input and displaying the conversion history.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Celsius to Fahrenheit Converter</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Celsius to Fahrenheit Converter</h1>
<div class="converter">
<input type="number" id="celsius" placeholder="Enter Celsius" />
<button id="convertBtn">Convert</button>
<button id="resetBtn">Reset</button>
</div>
<div class="result">
<h2 id="resultText"></h2>
</div>
<div class="history">
<h3>Conversion History</h3>
<ul id="historyList"></ul>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code (styles.css)
body {
font-family: Arial, sans-serif;
background-color: #f0f4f8;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: white;
border-radius: 10px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 300px;
text-align: center;
}
h1 {
margin-bottom: 20px;
color: #4a90e2;
}
.converter {
margin-bottom: 20px;
}
input[type="number"] {
width: 80%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-right: 10px;
}
button {
padding: 10px 15px;
border: none;
border-radius: 5px;
background-color: #4a90e2;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #357ab8;
}
.result {
margin: 20px 0;
}
.history {
margin-top: 20px;
text-align: left;
}
.history ul {
list-style-type: none;
padding: 0;
}
.history li {
background: #e7f3fe;
margin: 5px 0;
padding: 10px;
border-radius: 5px;
}
JavaScript Code (script.js)
document.getElementById('convertBtn').addEventListener('click', function() {
const celsius = parseFloat(document.getElementById('celsius').value);
if (!isNaN(celsius)) {
const fahrenheit = (celsius * 9/5) + 32;
document.getElementById('resultText').innerText = `${celsius}°C = ${fahrenheit.toFixed(2)}°F`;
addToHistory(celsius, fahrenheit);
} else {
document.getElementById('resultText').innerText = 'Please enter a valid number.';
}
});
document.getElementById('resetBtn').addEventListener('click', function() {
document.getElementById('celsius').value = '';
document.getElementById('resultText').innerText = '';
});
function addToHistory(celsius, fahrenheit) {
const historyList = document.getElementById('historyList');
const listItem = document.createElement('li');
listItem.innerText = `${celsius}°C = ${fahrenheit.toFixed(2)}°F`;
historyList.appendChild(listItem);
}
Explanation
-
HTML Structure: The HTML code creates a simple structure with an input field for Celsius, buttons for conversion and reset, a result display area, and a history section.
-
CSS Styling: The CSS styles the app to make it visually appealing with a clean layout, rounded corners, and hover effects on buttons.
-
JavaScript Functionality: The JavaScript code handles the conversion logic, updates the result display, and maintains a history of conversions. It also includes input validation to ensure that the user enters a valid number.
How to Use
- Create three files:
index.html, styles.css, and script.js.
- Copy the respective code into each file.
- Open
index.html in a web browser to see the converter in action.
Feel free to customize the styles and functionalities further to suit your preferences!