Notes-2
Notes-2
CSS is the technology responsible for defining the visual presentation of a website's HTML content. It
allows developers to control the layout, design, and presentation of web pages. CSS is fundamental for
separating content (HTML) from style (CSS), which brings many benefits like easier maintenance and
faster page rendering. Here's a breakdown of what CSS does:
- Layout: CSS controls the position of elements on a page (e.g., using properties like `display`, `position`,
`margin`, `padding`).
- Colors and Visual Design: CSS allows you to define background colors, text colors, and even gradient
backgrounds (using properties like `color`, `background-color`).
- Fonts: With properties like `font-family` and `font-size`, CSS gives you complete control over the
typography of your page.
- Media Queries: CSS can be used to adapt the layout to different screen sizes using media queries,
ensuring that a site looks good on devices from desktop computers to mobile phones.
- Cross-media styling: CSS allows styling for different media types (screen, print, etc.), so it adapts
accordingly. For instance, you can define print-specific styles that hide navigation bars or adjust layout.
JavaScript's object system is not as complex as other object-oriented languages like Java or C++, but it
does allow for:
- Object Creation: JavaScript allows you to define objects using literal notation or the `new Object()`
syntax.
- Inheritance: JavaScript supports inheritance through prototypes, which allows objects to share
properties and methods.
- Encapsulation: You can create private and public data within objects by using closures.
Though JavaScript doesn't strictly follow object-oriented principles like classes and inheritance (though
classes were introduced in ECMAScript 6), it still provides a mechanism to work with objects effectively.
For example:
```css
h1 {
font-size: 24px;
color: blue;
```
- `border`: Defines the border around an element (width, style, and color).
- `margin` and `padding`: `margin` is used to control the space outside an element’s border, while
`padding` controls the space inside the element, between the content and the border.
JavaScript Basics
- Client-Side Execution: JavaScript runs directly in the user's browser, meaning that it doesn't need a
round-trip to the server for execution. This makes it fast and responsive.
- `var` is used to declare variables in JavaScript. However, it has some scope issues and can lead to
unexpected behavior, so modern JavaScript prefers `let` (block-scoped) and `const` (for constants).
- `const` is used for values that should not be reassigned after their initial value is set. This ensures that
the variable's value is constant throughout the execution of the program.
- `!==` is the strict inequality operator, used to check if two values are not equal in both type and value.
This is more reliable than `!=`, which can perform type coercion.
- Strings in JavaScript are enclosed in either single or double quotes. Example: `'Hello World'` or `"Hello
World"`.
- Arrays are collections of values, and the values are separated by commas. Example: `let fruits = ['apple',
'banana', 'cherry'];`.
Scope of Variables
- Local vs. Global Variables: A local variable takes precedence over a global variable if both have the
same name. Local variables are confined to the function or block where they're declared, while global
variables are accessible throughout the entire script.
- Padding defines the space inside the element's border, around its content. It can be set uniformly for
all sides, or individually for each side (`padding-top`, `padding-right`, etc.).
- Margin is the space outside the element's border. It’s useful for controlling the spacing between
elements.
- Negative values are not allowed for `padding`, but they are allowed for `margin` in CSS.
- Functions in JavaScript are blocks of code designed to perform a particular task. You call functions by
their name, passing arguments if necessary. Example:
```javascript
function greet(name) {
```
Opacity in CSS
- The `opacity` property controls the transparency of an element. A value of `1` means fully opaque,
while `0` means fully transparent. You can use this to create various visual effects, such as fading in or
out elements.
- Dynamic Pages: JavaScript is primarily used for making web pages dynamic. It can respond to user
events (clicks, keypresses, etc.), modify the HTML structure (DOM manipulation), validate forms, and
much more.
- `background-color` in CSS: The syntax for changing the background color of elements is as follows:
```css
p{
background-color: yellow;
```
This would change the background color of all `<p>` (paragraph) elements to yellow.
JavaScript Interactivity
JavaScript was originally designed for client-side scripting, meaning it runs in the user's browser. This
allows developers to create interactive web pages without needing to constantly communicate with the
server.