HTML CSS JavaScript Coding Tips
HTML CSS JavaScript Coding Tips
<header>
<h1>Header</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<style>
h1 {
font-size: 36px;
color: blue;
text-align: center;
}
p {
font-size: 18px;
color: green;
text-align: justify;
}
</style>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>John Doe</td>
<td>30</td>
<td>New York</td>
</tr>
<tr>
<td>Jane Doe</td>
<td>25</td>
<td>London</td>
</tr>
</table>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<br>
<input type="submit" value="Submit">
</form>
<style>
/* Define a CSS class */
<style>
/* CSS Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Your custom styles */
body {
font-family: Arial, sans-serif;
font-size: 16px;
background-color: #f2f2f2;
}
</style>
<style>
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
}
.item {
background-color: lightblue;
padding: 20px;
text-align: center;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
<style>
/* Target elements with a specific class */
.highlight {
background-color: yellow;
font-weight: bold;
}
/* Target elements with a specific ID */
#header {
background-color: lightgray;
padding: 20px;
text-align: center;
}
</style>
<body>
<div id="header">
<h1>Welcome to my website</h1>
</div>
<p>This is some example text.</p>
<style>
/* Define a CSS class for the parent element */
.container {
font-family: Arial, sans-serif;
font-size: 16px;
background-color: #f2f2f2;
}
/* Child elements will inherit the styles from the
parent */
.item {
background-color: lightblue;
padding: 20px;
text-align: center;
}
</style>
<div class="container">
<style>
.container {
padding: 20px;
background-color: lightgray;
}
.item {
width: 200px;
height: 100px;
margin: 10px;
background-color: lightblue;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
In this example, the padding, width, height, and margin values all
use appropriate units.
<style>
.container {
padding: 20px;
background-color: lightgray;
}
.item {
margin: 10px 20px;
background-color: lightblue;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
<style>
/* Use a CSS reset stylesheet */
/* http://meyerweb.com/eric/tools/css/reset/ */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
<style>
<style>
/* Use a simple selector */
In this example, the selector for the .box class is simple, using
only the class name. The selector for the style that changes the
background color of every third box uses the nth-child
pseudo-class, which is less complex than using multiple class
names or multiple selectors.
<style>
/* Default styles */
In this example, the default styles for the .box class set its width
to 100% and its background color to lightblue. The media query
checks if the screen width is at least 600px wide, and if so, sets
the width of the .box to 50%. This means that the box will take
up half of the screen on devices with a screen width of 600px or
more, and will take up the full screen on smaller devices.
In this example, the .container class is set to use the display: flex
property to activate Flexbox. The flex-direction property is set to
row to arrange the elements in a row. The justify-content
property is set to space-between to distribute the space between
the elements evenly. The align-items property is set to center to
align the elements vertically in the center of the container. The
.box class sets the width of each box to 30% and the height to
100px. This results in the three boxes being arranged in a row,
with equal space between them, and centered vertically.
<style>
.box {
width: 100px;
height: 100px;
background-color: lightblue;
transition: width 2s, height 2s;
}
.box:hover {
width: 200px;
height: 200px;
}
</style>
<body>
<div class="box">Hover over me</div>
</body>
In this example, the .box class sets the width and height of a box
to 100px and the background color to lightblue. The transition
property is set to width 2s, height 2s to animate changes to the
width and height over 2 seconds. The .box:hover selector targets
the box when the user hovers over it, and changes the width and
height to 200px. As a result, when the user hovers over the box,
it will smoothly animate from 100px to 200px in both width and
height over 2 seconds.
'use strict';
// Use let
let name = 'John Doe';
// Use const
const PI = 3.14;
// Function expression
const multiply = (a, b) => a * b;
// Implicit return
const square = x => x * x;
// Destructuring arrays
const colors = ['red', 'green', 'blue'];
const [first, second, third] = colors;
// Use filter
const even = numbers.filter(number => number % 2 ===
0);
// Use reduce
const sum = numbers.reduce((acc, number) => acc +
number, 0);
// math.js
// main.js
import { PI, add } from './math.js';
console.log(PI); // 3.14
console.log(add(1, 2)); // 3
// Default value
let name = 'John Doe';