Styles in HTML

The style attribute in HTML defines styling information for an HTML document like color, font, size, etc. 


Syntax of HTML style attribute:

<tagname style="property:value;">

Styling of HTML elements is done by CSS properties which is totally a different topic to learn. (alert-passed)

Example: Background Color

The background-color is a CSS property that defines the background color of any HTML element. 

<body style="background-color: grey">
    <h1 style="background-color: blueviolet">This is a h1 heading</h1>
    <p style="background-color: yellow">This a simple paragraph</p>
</body>

Output:

Background Color in HTML

Example: Text Color and Font

  • The color property is used to define the text color of the HTML element. 
  • The font-family property is used to define different fonts of the HTML element.

<h1 style="color: blueviolet">This is a h1 heading with color</h1>
<p style="font-family: sans-serif">This is an example of font family.</p>

Output:

font family and color in HTML element

Whatever example we have seen so far is known as inline CSS in which we have used style attribute inside HTML elements but it is not the recommended way of using CSS style properties. 


Internal CSS Styles.

The <style> tag should be placed within the head section of the HTML document, and it can be used to apply CSS styles to specific elements in the document. It has two main attributes: type and media. The type attribute specifies the type of style sheet language being used, and it is typically set to "text/css". 


Here is an example of how the style tag is used:

<!DOCTYPE html>
<html>
  <head>
    <style type="text/css">
      body {
        background-color: blue;
      }
      h1 {
        color: white;
      }
    </style>
  </head>
  <body>
    <h1>This is a h1 heading</h1>
    <p>This is a paragraph.</p>
  </body>
</html>
Output:
Internal CSS in HTML

In this example, the <style> tag is used to set the background color of the <body> element to blue and the color of the <h1> element to white.

Paragraphs in HTML

Paragraphs in HTML are used to separate and organize blocks of text on a webpage. The primary tag used for creating paragraphs in HTML is the <p> tag.


When creating a paragraph, the text is placed between the opening <p> tag and the closing </p> tag. For example, the following code creates a paragraph with the text "This is a paragraph":

<p>This is a paragraph</p>


It's important to note that the <p> tag is a block-level element, which means that it creates a new block formatting context. This means that by default, a new line will be added before and after the paragraph, creating a clear visual separation from other elements on the webpage.

Example:

<h1>This is a Heading</h1>
<p>This is first paragraph.</p>
<p>This is second paragraph.</p>

Output:

paragraph example in HTML


White Space in HTML Paragraph.

Note: You cannot add extra spaces or extra newlines inside paragraph tags in HTML. While loading the page, the browser will automatically remove all extra spaces and lines from paragraphs. 

Example:

<p>
   This is the first paragraph 
   I am writing multiple lines 
   in this paragraph.
</p>
<p>
   This is the second paragraph.
   I am adding      extra white space
   in this       paragraph.
</p>

Output:

white space in Paragraph

If you want to add white spaces and line breaks to format the text based on your requirement then it is recommended to use the <pre> tag which is used to display preformatted text.

Example:

<pre>
     This is preformatted text. 
     I am adding      extra while 
     space in this paragraph.
</pre>

Output:

preformatted text in HTML

Use of <br> and <hr> tags in HTML.

In addition to the <p> tag, there are several other tags that can be used to format paragraphs, like:

  • <br /> tag, which creates a line break within a paragraph.
  • <hr /> tag, which is used to separate content with a horizontal line.

Example:

<p>
   This is a paragraph with a break <br />The next line of the paragraph is here.
</p>
<hr />
<p>
  This is another paragraph separated by a Horizontal line
</p>

Output:

Line break and HTML tag example

When creating paragraphs, it's also important to use proper formattings, such as bold, italics, and underlining, to make the text more readable and visually appealing.


It's also important to use headings and subheadings to create a clear hierarchy of content on the webpage. This makes it easier for users to understand the content and navigate the webpage.

Headings in HTML

Headings in HTML are used to create a hierarchical structure for the content on a webpage. They help users understand the organization of the content and can also be used by search engines to understand the content on a page.


HTML has six levels of headings, denoted by the <h1>, <h2>, <h3>, <h4>, <h5>, and <h6> tags. The <h1> tag represents the highest level of heading, while the <h6> tag represents the lowest level.

Example:

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>

Output:

html heading example h1 to h6

The <h1> tag is typically used for the main heading of a webpage, and it should be used sparingly. Subsequent headings, such as <h2>, <h3>, and so on, are used to break up the content and create a clear hierarchy.


Important of Heading Tag.

It's important to note that the heading tags should be used in a logical and consistent manner throughout the webpage. Using heading tags randomly or skipping heading levels can create confusion for users and search engines.


Search engines use headings to understand the content on a page, and they assign more importance to headings than to regular text. As a result, it's important to use headings appropriately and to include keywords in headings whenever possible.


In addition to the heading tags, it's also important to use proper formattings, such as bold, italics, and underlining, to distinguish headings from the rest of the text.

Example:

<h1 style="font-family: sans-serif">Heading 1</h1>
<h2 style="font-style: italic">Heading 2</h2>

Output:

Style HTML Headings

In conclusion, headings in HTML play an important role in organizing and structuring content on a webpage. They help users understand the content and make it more discoverable for search engines. Proper use of headings is essential for creating a clear, organized, and user-friendly webpage. 

Attributes in HTML

HTML (Hypertext Markup Language) is the standard language used to create web pages. One of the key components of HTML is attributes, which are used to provide additional information about an HTML element.


Attributes are added to the opening tag of an HTML element and have the following syntax: <tagname attribute="value">. For example, the "src" attribute is used to specify the source of an image in an "img" tag: <img src="image.jpg">.


There are many different attributes that can be used in HTML, and they serve different purposes. Some of the most commonly used attributes include:

The href attribute.

"href": This attribute is used to specify the URL (Uniform Resource Locator) of a link in an "a" tag. 

Example:

<a href="https://www.algolesson.com">AlgoLesson</a>
Output:
href attribute in HTML

The src attribute.

"src": This attribute is used to specify the source of an image or other media in an "img", "audio", or "video" tag. Here we have also used height and width attributes to specify the height and width of the media file.

Example:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <img src="WelcomeAlgoLesson.png" height="100" width="300" />
    <video src="AlgoLessonVideo.mp4" width="420" height="240" controls>
      Video File
    </video>
  </body>
</html>

Output:

src attribute in HTML

The alt attribute.

"alt": This attribute is used to provide alternative text for an image, which is displayed if the image cannot be loaded. This is important for accessibility, as it allows users who are visually impaired to understand the content of the image.

Example:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <img src="WelcomeAlgoLesson.png" height="100" width="300" alt="This is AlgoLesson Logo" />
  </body>
</html>

Output:

example of alt tag in HTML

The title attribute.

"title": This attribute is used to provide additional information about an HTML element, which is displayed as a tooltip when the user hovers over the element.

Example:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <p>
      <abbr title="HyperText Markup Language">HTML</abbr> is a markup language
    </p>
  </body>
</html>

Output:

title attribute in HTML

The class and Id attribute.

"class" and "id": These attributes are used to give an HTML element a specific class or id, which can then be used to style the element using CSS (Cascading Style Sheets).
Example:
<!DOCTYPE html>
<html>
  <head>
    <style>
      #header {
        background-color: rgb(18, 142, 184);
        color: rgb(255, 255, 255);
        padding: 30px;
        text-align: center;
      }
      .fruit {
        background-color: rgb(6, 128, 16);
        color: white;
        border: 2px solid black;
      }
    </style>
  </head>
  <body>
    <h1 id="header">Welcome to AlgoLesson</h1>
    <h2 class="fruit">Apple</h2>
    <h2 class="fruit">Mango</h2>
  </body>
</html>

Output:

class and id attribute

There are many other attributes that can be used in HTML, and the specific attributes available for a particular tag will depend on the type of element. For example, the "form" tag has attributes such as "action" and "method" that are used to specify where the form data is sent and how it is sent.


In summary, attributes are an essential part of HTML that provide additional information about an HTML element and can be used for a variety of purposes, including styling, linking, and accessibility.

C++ Programming Complete Tutorial.

C++ is a High-level, General-purpose, object-oriented programming language created by Bjarne Stroustrup. It is one of the most popular cross-platform programming languages for developing high-performance applications. 

C++ Complete Tutorial

In this tutorial, we will cover all the basics and advanced concepts of C++ programming language with lots of examples. As C++ is an Object Oriented Programming (OOPs) language so we will also cover concepts like Classes, Polymorphisms, Inheritance, Encapsulation, Friend functions, and more.


List of Topics: 

Introduction to Programming Languages.
C++ Program Basics
Introduction to C++ Programming.
Input/Output Stream in C++.
Using Namespace in C++.
Comments in C++ Programming.
Variables, Constant, and Literals in C++.
Escape Sequences in C++.
Data Types in C++.
Type Conversion in C++.
Signed and Unsigned Types in C++.
Operators in C++.
Flow Controls in C++.
if..else statement in C++.
For Loop in C++.
While Loop in C++.
do...while Loop in C++.
Break and Continue Statement in C++.
Switch Statement in C++.
Functions and Recursion in C++.
Functions in C++.
Types of Function in C++.
Call by Value and Call by Reference in C++.
Function Overloading in C++.
Recursion in C++.
Arrays and String in C++.
Introduction of Array in C++.
One-Dimensional Array in C++.
Two-Dimensional Array in C++.
Static and Dynamic Arrays.
How To Pass Array to Function in C++.
String in C++.
Character Array Vs String in C++.
String Member Functions in C++.
User Define Data Types in C++.
Structure in C++.
Union in C++.
The enumeration in C++.
Classes in C++.
Object Oriented Programming in C++.
Classes and Objects in C++.
Constructors and Destructors in C++.
Access Specifiers in C++.
Encapsulation in C++.
Inheritance in C++.
Polymorphism in C++.
Friend Function in C++.
Virtual Function in C++.

You can also learn C++ programming from our video lectures that are available on our YouTube channel AlgoLesson.

Pointers in C++

In C++ programming, a pointer is a variable that is used to store the address of another variable of the same type. They are very useful to pass the value as a reference in the function (also known as call by reference). It points to the memory location where the first byte of the object is stored.

Syntax for Declaration of Pointer Variable.

syntax: data_type * pointer_name;
example: int *ptr; 
//ptr can point to the address of a integer type variable


Working of Pointer in C++.

pointer in C++
Define a pointer variable and initialize the pointer variable with the address of variable using unary operator (& also called address of operator) which return the address of the variable.
int x = 5;
int *ptr;
ptr = &x; //assign address of variable x to the pointer variable ptr

The derefrence/indirected operator (*) is an operator that is used to access the value stored at the location pointed by the pointer. It return the value of the variable located at that address specified by the operand. Using * operator we can also change the value of the variable pointed by the pointer. 
int x = 5;
int *ptr = &x;

cout<<*ptr; //answer: 5

*ptr = 10;
cout<<*ptr; //answer: 10

Pointer Example Code:

//C++ Pointer Example
#include<iostream>
using namespace std;

int main(){

    //variable declaration
    int x = 10;
    //pointer vairbale declaration 
    int *ptr;
    //pointer variable initialize
    ptr = &x; 
    
    cout<<"Value of x: "<<x<<endl;
    cout<<"Value of *ptr: "<<*ptr<<endl;
    cout<<"Value of ptr: "<<ptr<<endl;

    return 0;
}
Output:
Value of x: 10
Value of *ptr: 10
Value of ptr: 0xd36fff8d4

Pointer Example in Function (Call by Reference).

//C++ Pointer Example for function
#include<iostream>
using namespace std;

//call by reference
void swapValue(int *x, int *y){
    *x = 30;
    *y = 10;
}
int main(){

    int m = 10, n = 30;
    //function call
    swapValue(&m, &n);

    cout<<"m = "<<m<<endl;
    cout<<"n = "<<n;

    return 0;
}
Output:
m = 30
n = 10

Pointer to Pointer in C++.

Pointer to pointer is a condition in which we store the address of one pointer into another pointer which point the address of actual variable. The pointer to pointer is declare using two derefrencing operator (**).
syntax: int **pptr; //pointer to pointer

Example:
//C++ Pointer to Pointer Example
#include<iostream>
using namespace std;

int main(){

   int value = 30;
   //taking address of value
   int *ptr = &value;
   //taking address of pointer ptr
   int **pptr = &ptr;

   cout<<"Value of value: "<<value<<endl;
   cout<<"Value of *ptr: "<<*ptr<<endl;
   cout<<"Value of **ptr: "<<**pptr<<endl;

   return 0;
}
Output:
Value of value: 30
Value of *ptr: 30
Value of **ptr: 30

Key points to note:
  • Never apply the indirected operator (*) to the uninitialized pointer. 
int *ptr;
cout<<*ptr; //undefined behaviour
  • Never assign value to an uninitialize pointer, it will lead to segmentation fault error.
int *ptr;
*ptr = 1; //Segmentation Fault (SIGSEGV)


Difference Between Call By Value and Call By Reference in C++

In C++ programming, there are two ways of passing value to the function: Call by Value or Call by Reference


Before going further into this topic, I will suggest you understand the difference between actual parameters and formal parameters. The actual parameters are those parameters that are passed to the function and formal parameters are those parameters that are received by the function.


Call by Value: Here the values of actual parameters will be copied to formal parameters and these two different parameters store values in different memory locations. Any changes made to the variable inside function are not going to reflect back to the actual parameter. 

Example:

//C++ Example of call by value
#include<iostream>
using namespace std;

//function definition
void update(int x, int y){

   x = 5;
   y = 40;
   cout<<"Values inside function call:"<<endl;
   cout<<"x = "<<x<<endl;
   cout<<"y = "<<y<<endl;
}
int main(){

    int x = 10, y = 20;    
    
    //function call
    update(x, y);
    cout<<"Values after function call:"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl; 

    return 0;
}
Output:
Values inside function call:
x = 5
y = 40
Values after function call:
x = 10
y = 20

call by value example

Here in the above code, we have updated the value of x and y inside the function but that is not affecting the value of our original variable x and y inside the main() function. It is happening because variables inside update() are local to the function and get destroyed later. 


Call by Reference: Here both actual and formal parameters refer to the same memory location. Therefore, any changes made to the formal parameters will get reflected in the actual parameters. 
In Call by Reference, instead of passing values to the function we are passing the memory addresses at which those values are stored using the address of operator (&). The parameters of function are pointers that is used to hold the address of another variable(alert-success)
Example:
//C++ Example of call by reference
#include<iostream>
using namespace std;

//function definition
void update(int *x, int *y){

   *x = 50;
   *y = 40;
   cout<<"Values inside function call:"<<endl;
   cout<<"x = "<<*x<<endl;
   cout<<"y = "<<*y<<endl;
}
int main(){

    int x = 10, y = 20;    
    
    //function call
    update(&x, &y);
    cout<<"Values after function call:"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl; 

    return 0;
}
Output:
Values inside function call:
x = 50
y = 40
Values after function call:
x = 50
y = 40

call by reference
Here in the above code, we are passing the address of the variable &x and &y to the function update() and the parameters of this function are pointers (*x and *y). A pointer is used to hold the address of another variable and you can fetch the value present at that particular address using dereference operator (*).  

Difference Between Call by Value and Call by Reference.

Call By Value Call By Reference
Copies of arguments are passed to the function. Changes to parameters inside the function do not affect the original values. References to the original arguments are passed to the function. Changes to parameters inside the function directly affect the original values.
Syntax: void functionName(int a, int b) Syntax: void functionName(int &a, int &b)
Changes inside the function do not affect the original variables. Changes inside the function directly affect the original variables.
May have a performance impact if large data structures are passed since copies are made. Typically more efficient as no copies are made, especially for large data structures.
// Call By Value Example
void swap(int x, int y) {
int temp = x; x = y;
y = temp;
}
int main() {
int a = 5, b = 10;
swap(a, b); cout << "a: " << a << ", b: " << b;
return 0;
}
// Call By Reference Example
void swap(int &x, int &y) {
int temp = x; x = y; y = temp;
}
int main() {
int a = 5, b = 10; swap(a, b); cout << "a: " << a << ", b: " << b;
return 0;
}


Read more:

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson