Learn to Code HTML and CSS👨‍💻

Basics of Web Development

Hello Everyone!!!

In my previous article I explained you how to code HTML to create the structure of your website. In this article you will learn to code CSS to build the appearance of your website. Readability and Appearance are most important when you develop a website. Users get their first impression about the website by the appearance. So you have to design your website with user friendly design compact and stylish appearance.

If you are a beginner to web development, please read my first article on HTML before you learn CSS. I have attached the link of my first article at the end of this article.

Let’s start to learn CSS..

Part II
CSS: Providing the Skin for a Website

What is CSS?
In the previous blog I explained you what CSS is. Let’s memorize again.

CSS is a design language we use to style an HTML document. CSS stands for Cascading Style Sheets. CSS was designed to enable the separation of presentation and content. CSS describes how HTML elements are to be displayed on screen.

Let’s start to code with CSS..

There are three ways you can insert a CSS style sheet into your HTML document.

  1. External CSS
  2. Internal CSS
  3. Inline CSS

1.External CSS

You have to create a separate file with .css extension and open it with the text editor. The external .css file should not contain any HTML tags. You can change the look of an entire website by changing the external file. Each HTML page must include a reference to the external style sheet file inside the <link>element, inside the head section.

In the above example I have inserted a CSS file named first_file.css to my HTML document and all the CSS code are written in that CSS file.

2.Internal CSS

The internal style is defined inside the <style> element, inside the head section. An internal style sheet may be used if one single HTML page has a unique style.

output of the code

3.Inline CSS

To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property. An inline style may be used to apply a unique style for a single element.

output of the code

You can use any of these methods according to your need. In this blog I will use internal CSS to show you the examples.

A CSS rule consists of a selector and a declaration block.

A CSS Style rule is made up of 3 parts

  • Selector -The selector points to the HTML element you want to style.
  • Property -A property is a type of attribute of HTML tag.
  • Value -Values are assigned to properties

The declaration block contains one or more declarations separated by semicolons. Each declaration includes a CSS property name and a value, separated by a colon. Declaration blog is surrounded with curly braces.

Example

  • p is the selector that points to the HTML element you want to style
  • color is a property, and blue is the property value
  • text-align is a property, and center is the property value

what are CSS Selectors?

A CSS selector selects the HTML element(s) you want to style.

There are several categories of CSS selectors.

  • Simple selectors (select elements based on name, id, class)
  • Combinator selectors(select elements based on a specific relationship between them)
  • Pseudo-class selectors(select elements based on a certain state)
  • Pseudo-elements selectors(select and style a part of an element)
  • Attribute selectors(select elements based on an attribute or attribute value)

Here I will explain only the basic CSS selectors.

1.CSS element selector

The element selector selects HTML elements based on the element name.

output of the code

2.CSS Id selector

Id selector selects an element based on the value of its id attribute.
There should be only one element with a given Id in a document.
To select an element with a specific id, write a hash (#) character, followed by the id of the element.

output of the code

3.CSS Class selector

To select elements with a specific class, write a period (.) character, followed by the class name. The class selector selects HTML elements with a specific class attribute.

output of the code

4.CSS Universal selector

The universal selector (*) selects all HTML elements on the page.

output of the code

5.CSS Grouping selector

Grouping selector is used when multiple elements have the same style. To group selectors, separate each selector with a comma. It will be better to group the selectors, to minimize the code.

output of the code

Those are the basic selectors we use in CSS. I think now you have an understanding of what CSS is and how to code with CSS.

Now let’s get move into some common CSS styles..

CSS Colors

A color unit represents a color value that you can assign to a property such as heading, paragraph, border etc. Color values can be specified in various formats. Hex, Short Hex, RGB%, RGB Absolute and keyword are the color formats used in CSS. You can use any of these color formats to design your web page.

Web Safe Colors
There are 216 colors which are supposed to be most safe and computer independent colors. These colors vary from Hexa code 000000 to FFFFFF. These colors are safe to use because they ensure that all computers would display the colors consistently.

CSS Measurement Units

CSS supports a number of measurements including absolute units such as inches, centimeters, points as well as relative measures such as percentages and em units.

Absolute length units use an exact measurement to specify the size or placement of an element.
CSS absolute length units are:
1.cm(Centimeters)
2.in (inches)
3.mm (millimeters)
4.pc (picas)
5.pt (points)

Relative length units adjust properties according to screen size or user preferences.
CSS relative length units are :
1.em (em space)
2.ex (x-height)
3.px (pixels)
4.percentages(%)

CSS Fonts

You have to be very careful when choosing a font for your website. Font is a main factor that users use to decide the quality of your website. You have to use a font that is easy to read.

CSS has five generic font families. Those are:
1.Serif
2.Sans-serif
3.Monospace
4.Cursive
5.Fantasy

You can change the font by changing the value of font-family property.
You can change the size and the color of the font by changing the values of font-size and color properties respectively.

Always try to use fonts that are easy for users to read and see.

CSS backgrounds

CSS background properties are used to add backgrounds for elements. We can add backgrounds for each element such as body, paragraph, heading etc.

The background-color property specifies the background color of an element. This is a sample code to add background color to the body element(whole web page).

The background-image property specifies an image to use as the background of an element. This is a sample code to add back ground image to the body element(whole web page).

When you use the above code, the image is repeated by default and it covers the entire web page.

CSS Borders

The CSS border properties allow you to specify the style, width, and color of an element’s border.

The border-style property specifies what kind of border to display.
The border-width property specifies the width of the four borders.
The border-color property is used to set the color of the four borders.

CSS Margin

CSS margin is used to create space around an element, outside of any defined borders. You can create space around each side of an element using four CSS properties named margin-top, margin-bottom, margin-left and margin-right.

CSS padding

CSS padding is used to create space around an element, inside of any defined borders. You can create space around each side of an element using four CSS properties named padding-top, padding-bottom, padding-left and padding-right.

Now I will show you an example how to use fonts, colors, measurement units, background colors, borders, margins and padding in your codes. Go through this example code and the output of the code. Then you will be able to understand all theses CSS styles clearly.

Output of the code

I think now you can understand how a add a background color, how to use fonts, colors, borders, margins and padding correctly.

That’s all for CSS..

Now you can design your website using CSS knowledge you got from this article. Always try to use the proper designs to give your website the perfect look.

So if you think my article is important, please give me a clap and leave a comment…

Thanks for reading..

Link for the HTML article
https://dilkiiwijesinghe.medium.com/learn-to-code-html-and-css-7181b4f5006f

Undergraduate of University of Moratuwa, Faculty of Information Technology

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store