Below is an example of HTML code for a Celsius to Fahrenheit converter app that is visually appealing and includes several functionalities. This example uses HTML for structure, CSS for styling, and JavaScript for functionality.
To make it work, you'll need to create three files: index.html, style.css, and script.js. The HTML code goes into index.html, CSS styles into style.css, and JavaScript into script.js.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>C to F Converter</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Celsius to Fahrenheit Converter</h1>
<div class="converter">
<input type="number" id="celsius" placeholder="Enter temperature in Celsius">
<button id="convertBtn">Convert to Fahrenheit</button>
<p id="result"></p>
</div>
<div class="options">
<button id="clearBtn">Clear</button>
<button id="swapBtn">Swap to F to C</button>
</div>
<div id="conversionHistory">
<h2>Conversion History</h2>
<ul id="historyList"></ul>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
.container {
max-width: 400px;
margin: auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.converter {
margin-bottom: 20px;
}
input[type="number"] {
width: 100%;
height: 40px;
margin-bottom: 10px;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
width: 100%;
height: 40px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
#result {
margin-top: 10px;
font-size: 18px;
font-weight: bold;
}
.options {
margin-bottom: 20px;
}
#clearBtn, #swapBtn {
width: 48%;
display: inline-block;
margin: 1%;
}
#conversionHistory {
margin-top: 20px;
}
#historyList {
list-style: none;
padding: 0;
}
#historyList li {
padding: 5px;
border-bottom: 1px solid #ccc;
}
#historyList li:last-child {
border-bottom: none;
}
script.js
document.addEventListener('DOMContentLoaded', function() {
const celsiusInput = document.getElementById('celsius');
const convertBtn = document.getElementById('convertBtn');
const resultElement = document.getElementById('result');
const clearBtn = document.getElementById('clearBtn');
const swapBtn = document.getElementById('swapBtn');
const historyList = document.getElementById('historyList');
let isCtoF = true;
convertBtn.addEventListener('click', convertTemperature);
clearBtn.addEventListener('click', clearInput);
swapBtn.addEventListener('click', swapConversion);
function convertTemperature() {
const temperature = parseFloat(celsiusInput.value);
if (isNaN(temperature)) {
resultElement.textContent = 'Please enter a valid temperature.';
return;
}
let result;
if (isCtoF) {
result = (temperature * 9/5) + 32;
resultElement.textContent = `${temperature}°C is equal to ${result.toFixed(2)}°F`;
addHistoryItem(`${temperature}°C to ${result.toFixed(2)}°F`);
} else {
result = (temperature - 32) * 5/9;
resultElement.textContent = `${temperature}°F is equal to ${result.toFixed(2)}°C`;
addHistoryItem(`${temperature}°F to ${result.toFixed(2)}°C`);
}
}
function clearInput() {
celsiusInput.value = '';
resultElement.textContent = '';
}
function swapConversion() {
isCtoF = !isCtoF;
if (isCtoF) {
celsiusInput.placeholder = 'Enter temperature in Celsius';
convertBtn.textContent = 'Convert to Fahrenheit';
swapBtn.textContent = 'Swap to F to C';
} else {
celsiusInput.placeholder = 'Enter temperature in Fahrenheit';
convertBtn.textContent = 'Convert to Celsius';
swapBtn.textContent = 'Swap to C to F';
}
clearInput();
}
function addHistoryItem(item) {
const li = document.createElement('li');
li.textContent = item;
historyList.appendChild(li);
}
});
This code creates a simple, visually appealing temperature conversion app. It allows users to convert Celsius to Fahrenheit and vice versa, with the option to swap the conversion type. The app also keeps a history of conversions and provides a clear button to reset the input field and result display.