Home > Article > Web Front-end > js css settings
JavaScript and CSS are two core technologies for Internet development. JavaScript is a scripting language used to dynamically add interactive effects and user experience to web pages; CSS is a style language used to beautify web pages. and define its display effect. In this article, we will discuss how to set up JavaScript and CSS for the best web design and user experience.
1. JavaScript settings
1.1 Introduction of JavaScript
When introducing JavaScript into an HTML document, you usually need to use the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag. The best way to include JavaScript in the 93f0f5c25f18dab9d176bd4f6de5d30e tag is to use the async or defer option. The difference is that the async option indicates that the script will be executed when downloading (that is, asynchronous execution), while the defer option indicates that the script will not be executed until the document is downloaded. For example:
<head> <script src="example.js" async></script> <script src="example.js" defer></script> </head>
1.2 Using external JavaScript
The advantage of using external JavaScript files is to reuse code across multiple documents and ensure consistency. By referencing external JavaScript files, you can separate the code from the HTML document, making the HTML document more concise. For example:
<head> <script src="example.js"></script> </head>
1.3 Page redirection
In JavaScript, you can use the location attribute of the window object to redirect the page to another page. The method is as follows:
window.location.href = "https://www.example.com";
This code redirects the page to "https://www.example.com".
1.4 Add event listener
Event listener is used to perform certain operations when an event occurs. In JavaScript, use the addEventListener() method to add an event listener. For example:
document.addEventListener("click", function() { // 执行一些操作 });
This code executes a function when the user clicks on the document.
1.5 Manipulating DOM elements
DOM (Document Object Model) can be used to manipulate elements in HTML and XML documents. In JavaScript, DOM elements can be obtained through getElementById() method, getElementsByClassName() method, getElementsByTagName() method, etc. For example:
document.getElementById("example").innerHTML = "Hello, world!";
This code sets the innerHTML attribute of the DOM element with the ID "example" to "Hello, world!".
1.6 Adding elements to an array
In JavaScript, you can use the push() method to add elements to an array. For example:
var fruits = ["apple", "banana", "orange"]; fruits.push("pear");
This code adds "Pear" to the end of the array named fruits.
2. CSS Settings
2.1 External Style Sheet
External style sheet can be used to allow multiple documents to share the same style. You can make your HTML document cleaner by putting your stylesheet into a separate CSS file and then referencing that file in your HTML document. For example:
<head> <link rel="stylesheet" href="styles.css"> </head>
This code places the stylesheet in a file called styles.css.
2.2 Internal style sheet
A style sheet can be placed in an HTML document by adding a c9ccee2e6ea535a969eb3f532ad9fe89 tag in the 93f0f5c25f18dab9d176bd4f6de5d30e tag of the HTML document. For example:
<head> <style> body { background-color: yellow; } </style> </head>
This code sets the background color of the document to yellow.
2.3 Selectors
Selectors can be used to select elements to which styles should be applied. In CSS, selectors are used to specify the scope of styles. CSS has several different selector types, including class selectors, ID selectors, element selectors, and attribute selectors. For example:
h1 { font-size: 24px; } .example { background-color: blue; } #example { color: green; } [example="true"] { border: 2px solid red; }
This code sets the font size of the h1 element to 24 pixels, the background color of the element with class name .example to blue, and the color of the element with ID example to green, with example Elements with a property value of true have their borders set to red.
2.4 Layout and positioning
By using the position attribute and the top, bottom, left, and right attributes, elements can be laid out and positioned in CSS. For example:
#example { position: absolute; top: 100px; left: 50px; }
This code absolutely positions the element with ID example 100 pixels from the top of the document and 50 pixels from the left.
2.5 Animation effects
By using the animation properties of CSS, you can create animation effects for elements. For example:
#example { animation-name: example-animation; animation-duration: 2s; animation-iteration-count: infinite; } @keyframes example-animation { from { transform: translateX(0); } to { transform: translateX(100px); } }
This code applies an animation effect to the element with ID example that moves 100 pixels to the left to its position. This animation effect is named example-animation and repeats infinite times within 2 seconds.
Conclusion
JavaScript and CSS are key technologies necessary for creating modern Internet applications. Correct JavaScript and CSS settings can greatly improve the usability and user experience of your page. In this article, we discussed how to set up JavaScript and CSS for optimal web design and user experience.
The above is the detailed content of js css settings. For more information, please follow other related articles on the PHP Chinese website!