Category: 1. CSS

https://cdn3d.iconscout.com/3d/free/thumb/free-css-3d-icon-png-download-7578024.png

  • CSS Getting Started

    Getting Started with CSS

    In this tutorial you’ll learn how easy it is to add style and formatting information to the web pages using CSS. But, before we begin, make sure that you have some working knowledge of HTML.

    If you’re just starting out in the world of web development, start learning from here »

    Without further ado, let’s get started with the Cascading Style Sheets (CSS).

    Including CSS in HTML Documents

    CSS can either be attached as a separate document or embedded in the HTML document itself. There are three methods of including CSS in an HTML document:

    • Inline styles — Using the style attribute in the HTML start tag.
    • Embedded styles — Using the <style> element in the head section of a document.
    • External style sheets — Using the <link> element, pointing to an external CSS file.

    In this tutorial we will cover all these three methods for inserting CSS one by one.

    Note: The inline styles have the highest priority, and the external style sheets have the lowest. It means if you specify styles for an element in both embedded and external style sheets, the conflicting style rules in the embedded style sheet would override the external style sheet.

    Inline Styles

    Inline styles are used to apply the unique style rules to an element by putting the CSS rules directly into the start tag. It can be attached to an element using the style attribute.

    The style attribute includes a series of CSS property and value pairs. Each "property: value" pair is separated by a semicolon (;), just as you would write into an embedded or external style sheets. But it needs to be all in one line i.e. no line break after the semicolon, as shown here:

    Example

    <h1 style="color:red; font-size:30px;">This is a heading</h1>
    <p style="color:green; font-size:22px;">This is a paragraph.</p>
    <div style="color:blue; font-size:14px;">This is some text content.</div>

    Using the inline styles are generally considered as a bad practice. As style rules are embedded directly inside the HTML tag, it causes the presentation to become mixed with the content of the document; which makes the code hard to maintain and negates the purpose of using CSS.

    Note: It’s become impossible to style pseudo-elements and pseudo-classes with inline styles. You should, therefore, avoid the use of style attributes in your code. Using external style sheets is the preferred way to add styles to the HTML documents.


    Embedded Style Sheets

    Embedded or internal style sheets only affect the document they are embedded in.

    Embedded style sheets are defined in the <head> section of an HTML document using the <style> element. You can define any number of <style> elements in an HTML document but they must appear between the <head> and </head> tags. Let’s take a look at an example:

    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;title&gt;My HTML Document&lt;/title&gt;
    &lt;style&gt;
        body { background-color: YellowGreen; }
        p { color: #fff; }
    &lt;/style&gt;
    </head> <body>
    &lt;h1&gt;This is a heading&lt;/h1&gt;
    &lt;p&gt;This is a paragraph of text.&lt;/p&gt;
    </body> </html>

    Tip: The type attribute of the <style> and <link> tag (i.e. type="text/css") defines the language of the style sheet. This attribute is purely informative. You can omit this since CSS is the standard and default style sheet language in HTML5.


    External Style Sheets

    An external style sheet is ideal when the style is applied to many pages of the website.

    An external style sheet holds all the style rules in a separate document that you can link from any HTML file on your site. External style sheets are the most flexible because with an external style sheet, you can change the look of an entire website by changing just one file.

    You can attach external style sheets in two ways — linking and importing.

    Linking External Style Sheets

    Before linking, we need to create a style sheet first. Let’s open your favorite code editor and create a new file. Now type the following CSS code inside this file and save it as “style.css”.

    Example

    body {
    
    background: lightyellow;
    font: 18px Arial, sans-serif;
    } h1 {
    color: orange;
    }

    An external style sheet can be linked to an HTML document using the <link> tag. The <link> tag goes inside the <head> section, as you can see in the following example:

    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;title&gt;My HTML Document&lt;/title&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;
    </head> <body>
    &lt;h1&gt;This is a heading&lt;/h1&gt;
    &lt;p&gt;This is a paragraph of text.&lt;/p&gt;
    </body> </html>

    Tip: Among all the three methods, using external style sheet is the best method for defining and applying styles to the HTML documents. As you can clearly see with external style sheets, the affected HTML file require minimal changes in the markup.

    Importing External Style Sheets

    The @import rule is another way of loading an external style sheet. The @import statement instructs the browser to load an external style sheet and use its styles.

    You can use it in two ways. The simplest is within the header of your document. Note that, other CSS rules may still be included in the <style> element. Here’s an example:

    Example

    <style>
    
    @import url("css/style.css");
    p {
        color: blue;
        font-size: 16px;
    }
    </style>

    Similarly, you can use the @import rule to import a style sheet within another style sheet.

    Example

    @import url("css/layout.css");
    @import url("css/color.css");
    body {
    
    color: blue;
    font-size: 14px;
    }
  • CSS Tutorial

    CSS stands for Cascading Style Sheets. CSS is a standard style sheet language used for describing the presentation (i.e. the layout and formatting) of the web pages.

    Prior to CSS, nearly all of the presentational attributes of HTML documents were contained within the HTML markup (specifically inside the HTML tags); all the font colors, background styles, element alignments, borders and sizes had to be explicitly described within the HTML.

    As a result, development of the large websites became a long and expensive process, since the style information were repeatedly added to every single page of the website.

    To solve this problem CSS was introduced in 1996 by the World Wide Web Consortium (W3C), which also maintains its standard. CSS was designed to enable the separation of presentation and content. Now web designers can move the formatting information of the web pages to a separate style sheet which results in considerably simpler HTML markup, and better maintainability.

    CSS3 is the latest version of the CSS specification. CSS3 adds several new styling features and improvements to enhance the web presentation capabilities.

    Note: Our CSS tutorial will help you to learn the fundamentals of the latest CSS3 language, from the basic to advanced topics step-by-step. If you’re a beginner, start with the basic section and gradually move forward by learning a little bit every day.


    What You Can Do with CSS

    There are lot more things you can do with CSS.

    • You can easily apply same style rules on multiple elements.
    • You can control the presentation of multiple pages of a website with a single style sheet.
    • You can present the same page differently on different devices.
    • You can style dynamic states of elements such as hover, focus, etc. that isn’t possible otherwise.
    • You can change the position of an element on a web page without changing the markup.
    • You can alter the display of existing HTML elements.
    • You can transform elements like scale, rotate, skew, etc. in 2D or 3D space.
    • You can create animations and transitions effects without using any JavaScript.
    • You can create print friendly version of your web pages.

    The list does not end here, there are many other interesting things that you can do with CSS. You will learn about all of them in detail in upcoming chapters.


    Advantages of Using CSS

    The biggest advantage of CSS is that it allows the separation of style and layout from the content of the document. Here are some more advantages, why one should start using CSS?

    • CSS Save Lots of Time — CSS gives lots of flexibility to set the style properties of an element. You can write CSS once; and then the same code can be applied to the groups of HTML elements, and can also be reused in multiple HTML pages.
    • Easy Maintenance — CSS provides an easy means to update the formatting of the documents, and to maintain the consistency across multiple documents. Because the content of the entire set of web pages can be easily controlled using one or more style sheets.
    • Pages Load Faster — CSS enables multiple pages to share the formatting information, which reduces complexity and repetition in the structural contents of the documents. It significantly reduces the file transfer size, which results in a faster page loading.
    • Superior Styles to HTML — CSS has much wider presentation capabilities than HTML and provide much better control over the layout of your web pages. So you can give far better look to your web pages in comparison to the HTML presentational elements and attributes.
    • Multiple Device Compatibility — CSS also allows web pages to be optimized for more than one type of device or media. Using CSS the same HTML document can be presented in different viewing styles for different rendering devices such as desktop, cell phones, etc.

    Tip: Now most of the HTML attributes are being deprecated and it’s not recommended to use. So it’s a good idea to use as much CSS as possible to increase the adaptability your website and make them compatible to future browsers, as well.


    What This Tutorial Covers

    This CSS tutorial series covers all the fundamentals of CSS, including the idea of selectors, methods of setting colors and backgrounds, way of formatting fonts and text, styling UI elements such as hyperlinks, lists, tables, etc. as well as the concept of CSS box model, and so on.

    Once you’re comfortable with the basics, you’ll move on to next level that explains the way of setting dimension and alignment of elements, methods for positioning elements on a web page, using image sprites, as well as the concept of relative and absolute units, visual formatting model, display and visibility, layers, pseudo classes and elements, media dependent style sheets, and so on.

    Finally, you’ll explore some advanced features introduced in CSS3 like gradient colors, drop shadow effect, 2D and 3D transforms, alpha transparency, as well as the method of creating transition and animation effect, flex layouts, filter effect, the concept of media queries, and more.

    Tip: Every chapter in this tutorial contains lots of real-world examples that you can try and test using an online editor. These examples will help you to better understand the concept or topic. It also contains smart workarounds as well as useful tips and important notes.