11 Frontend tricks that most Frontend Developers don’t know about | by Daniel Anderson | JavaScript In Plain English | Oct, 2020 | Medium

11 Frontend tricks that most Frontend Developers don’t know about

Interesting tricks you can do with HTML/JS/CSS

Below are some tricks which a lot of frontend developers don’t know about. To do with HTML/CSS/JavaScript.

1. Datalist element

Image for post

<datalist>
<input list="animals" name="animal" id="animal"><datalist id="animals">
    <option value="Cat">
    <option value="Dog">
    <option value="Chicken">
    <option value="Cow">
    <option value="Pig">
  </datalist>

2. Clickable label with a checkbox

<input type="checkbox" name="checkbox" id="checkbox_id" value="value">
<label for="checkbox_id">I agree</label>
<label><input type="checkbox" name="checkbox" id="checkbox_id" value="value">I agree</label>

3. Child selectors

 /* 1st <li> element */
 li:first-child { 
    color: red;
 }/* Last <li> element */
 li:last-child { 
     color: green;
 }// Select All <li> elements but The First Three */
li:nth-child(n+4) {     
      color: yellow;    
}/* Select only the first 3 <li> elemets */
li:nth-child(-n+3) {     
    color: green;    
}/* Styles are elements that are not a <p> */
.my-class:not(p) { 
    display: none;
}

4. Writing mode

Image for post

Vertical text
writing-mode: vertical-rl;
<style>
.sideway { 
   writing-mode: vertical-rl;
}
.normal {
   width: 5%;
   float: left;
}
</style>
<p class="normal">
     Hi some paragraph text
</p>
<p class="sideway"> 
     Hey I'm some sidway text 
</p>
  writing-mode: horizontal-tb;
  writing-mode: vertical-rl;
  writing-mode: vertical-lr;
  writing-mode: sideways-rl;
  writing-mode: sideways-lr;

5. calc() function

width: calc(5px + 100px);
width: calc(6em * 8);
width: calc(100% - 50px);

6. Math.round & Math.floor alternatives

0|743.4343 // returns 743
Math.floor(743.4343) // returns 743
812.777+.5|0 // returns 813
Math.round(812.777) // returns 813

7. Console.table

let car1 = { name : "Audi", model : "A4" }
let car2 = { name : "Volvo", model : "XC90" }
let car3 = { name : "Ford", model : "Fusion" }console.table([car1, car2, car3]);
Image for post

Image for post

console.table()

8. Console.time

Image for post

Image for post

console.time()
// Starts the timer
console.time("MyTimer");// Ends the timer and outputs the time in milliseconds
console.timeEnd("MyTimer");

9. In operator

let cars = ['Audi', 'BMW', 'Mini', 'Bentley', 'Porsche'];0 in cars        // returns true
3 in cars        // returns true
6 in cars        // returns false
const person = { firstName : "Dave", surname: "Smith", age: 34 };'firstName' in person  // returns true
'surname' in person    // returns true
'age' in person        // returns true
'gendar' in person     // returns false

10. Make Chrome a text editor

data:text/html, <html contenteditable>

11. Multiple statements in if block without curly brackets

if (1 === 1)
 alert("Alert 1"), alert("Alert 2");

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.