js css code example

Example 1: jquery css

$('#element').css('display', 'block'); /* Single style */
$('#element').css({'display': 'block', 'background-color' : '#2ECC40'}); /* Multiple style */

Example 2: javascript modify css

//Pure JavaScript DOM
var el = document.getElementById("elementID");
el.style.css-property = "cssattribute";

//When doing A CSS property that have multiple words, its typed differently
//Instead of spaces or dashes, use camelCase
//Example:
el.style.backgroundColor = "blue";

//Make sure before using jQuery, link the jQuery library to your code
//JavaScript with jQuery
//jQuery can use CSS property to fid=nd an element
$("#elementID").css("css-property", "css-attribute");

//On jQuery, the CSS property is typed like normal CSS property
//Example: 
$("#elementID").css("background-color", "blue");

//If you want multiple property for jQuery, you can stack them on one code
//instead of typing each attribute
//Example: 
$("#elementID").css({"css-property": "css-attribute", "css-property": "css-attribute"});

//you can also make them nice by adding line breaks
//Example: 
$("#elementID").css({
  "css-property": "css-attribute",
  "css-property": "css-attribute"});
//You can add as much CSS property and attribute as you want
//just make sure, always end it with a comma before adding another one
//the last property doesn't need a comma

Example 3: how to change style of an element using javascript

<html>
<body>

<p id="p2">Hello World!</p>

<script>
document.getElementById("p2").style.color = "blue";
</script>

<p>The paragraph above was changed by a script.</p>

</body>
</html>

Example 4: add css in javascript

document.getElementById("demo").style.display = "none";

Example 5: js style

const title = document.querySelector('h1')

//title.setAttribute('style', 'margin: 50px');

console.log(title.style); // Show all style propertyes
console.log(title.style.color); // Log a color in console

title.style.margin = '50px'; // Set a margin of title
title.style.color='crimson'; // Set a color of title
title.style.fontSize = '60px'; // Set font size of title

Example 6: html css javascript

<!-- Answer to: "html css javascript" -->

<!--
  To keep things simple:
  HTML provides the basic structure of sites, which is enhanced and
  modified by other technologies like CSS and JavaScript. CSS
  is used to control presentation, formatting, and layout.
  JavaScript is used to control the behavior of different elements.
-->

For example:
<!--HTML-->
<div id="me">Click me!</div>
<!--CSS-->
<style>
  div {
    height: 128px; /* Makes the div 128px in height */
    width: 128px; /* Makes the div 128px in width */
    background: red; /* Makes the div's background, red */
  }
</style>
<!--JS-->
<script>
  document.getElementById("me".addEventListener("click", function(){
      document.getElementById("me").style.background = 'blue';
  });
</script>
<!-- If you want to test the code above, go to:https://www.w3schools.com/code/tryit.asp?filename=GDZ0PJDLYQ1V -->

<!--
  For more information, go to:
  https://blog.hubspot.com/marketing/web-design-html-css-javascript
-->