Day 21 : #100DaysofCode - Cascading Style Sheets 101
Tags: css, 100daysofcode, womenintech, codenewbie
Published: 2020-06-22 02:34:30

While building my sinatra website I realized I need to freshen up on my CSS and decided to take the <a href="https://www.linkedin.com/learning/instructors/christina-truong"> Christina Truong's Essential CSS course </a>.

She began with explaining ways to connect your css to your HTML document.There are many ways to add css to your HTML document:

<h1> 1. External CSS File: </h1>
In order to add your css to your file externally, you could create a css file:

``main.css``

Then within your ``index.html`` file you would add the following to your ``<head>`` tag.

```
<head>
  <link rel="stylesheet" href="/css/main.css">
</head>
```

Please note, that sometimes you may see the following ``type`` inside of you link rel: 

```
<head>
  <link rel="stylesheet" href="/css/main.css" type="text/css">
</head>
```

The type was required in previous HTML versions but is no longer required in HTML5, so while you may see it, it is no longer necessary in the most recent version of HTML. 

<h1> Using Multiple External CSS files: </h1> 
There will be times when you need to reference multiple CSS files. When that happens it is better to import your css files.

For example, lets say you have the following css files.

``styles/layouts.css``
``styles/fonts.css``
``styles/buttons.css``

Then you would create a css file that imports all of those files like this:

``styles.css`` :

```
@import url('styles/layouts.css')
@import url('styles/fonts.css')
@import url('styles/buttons.css')
```

Lastly, within your HTML file you would add the following:

```
<head>
  <style>
    @import url("css/styles.css");
  </style>
</head>
```

You may see ``@import`` is used with CSS Preprocessors like sass. However, it is worth noting that importing your css files effect page loading. It is important that you research how importing css files will effect your personal project. 

<h1> Inline Method </h1>

The inline method uses a style attribute which is added to the opening HTML tag like this:

```
<p style="color:red;"> red Paragraph </p>
```

Inline styling should be used sparingly because it makes it hard to read. Also, inline overrides your ``css`` files.

<h1> Internal CSS: </h1>

CSS is added between the style tag in the head of the document. Like this: 

```
<head>
<style>
    html {
      background: #018DED url(https://unsplash.it/1500/1000?image=881&blur=5);
      background-size: cover;
      font-family: 'helvetica neue';
      text-align: center;
      font-size: 10px;
    }

    body {
      margin: 0;
      font-size: 2rem;
      display: flex;
      flex: 1;
      min-height: 100vh;
      align-items: center;
    }
</style>
</head>
```

Though it is better than inline, it is still inefficient. You would have to copy this block into each of your HTML files for it to work.


Homepage always called index.html

<h1> Images in CSS and RETINA DISPLAY</h1>

Apple created the <i>Retina Display</i> which stands for <i>high pixel density</i> screens. 

<i>Pixel density</i> refers to the number of pixels within a given space and the space is measured in <i>pixels per inch (PPI)</i> or <i>dots per inch (DPI)</i>.

<i>Retina displays</i> have double the number of <i>PPI/DPI</i>. Meaning that Retina displays can fit two pixels within the same width and height of a non-Retina display.

The more pixels there are within the same area, the smaller the pixels are, which is how text and images appear smoother, clearer and show more detail.

A simple way to support Retina in non-Retina screens is to use an image twice the size.

For example, if you use an image file that is 300 pixels wide then resize it to 150 pixels with css, you are essentially adding double the amount of pixels within the same dimension, just like Retina. For both Retina and non-retina screens, the image will display as 300 pixels wide.

There are other ways to optimize images on your webpage. Here is an article that provides<a href="https://www.shopify.com/blog/7412852-10-must-know-image-optimization-tips"> 10 great tips to optimize images for the web: </a>

<ol>
<li> Name your images descriptively and in plain language. </li>
<li> Optimize your alt attributes carefully. </li>
<li> Choose your image dimensions and product angles wisely. </li>
<li> Reduce the file size of your images. </li>
<li> Choose the right file type. </li>
<li> Optimize your thumbnails. </li>
<li> Use image sitemaps. </li>
<li> Beware of decorative images. </li>
<li> Use caution when using content delivery networks (CDNs). </li>
<li> Test your images. </li>
</ol>

<h1> Absolute versus Relative Path </h1>

An <b> Absolute Path</b> refers to a resource located on a server. The entire url including the ``http`` must be referenced. An absolute path is usually used for linking to pages outside your website.

A <b>Route Relative Path </b> is referenced by using a forward slash which will assume that the url is the same as your current url. This is important when you need to view your website on a local server during development.

<i> Avoid Hotlinks- hotlinks is the act of linking specific files such as images and embedding them into your own website. </i> 

Check <a href="https://www.w3.org/"> W3C </a> for information on CSS.

Song of the day: 

{% soundcloud https://soundcloud.com/aloysius-chay/l-o-v-e-nat-king-cole %}