Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


CSS Functions

Css has functions that can be used to set values.

attr() function

The attr()function can be used with any CSS property, but support for properties other than content is experimental, and support for the type-or-unit parameter is sparse. The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. 

<div class="col-9">
   <p id="mySite"><a href="https://lovemesomecoding.com">Visit lovemesomecoding</a></p>
</div>

<style>
   #mySite > a:after {content: " (" attr(href) ")";}
</style>

calc() function

The calc() CSS function allows you to perform calculations when specifying CSS property values. The calc() function takes a single expression as its parameter, with the expression’s result used as the value. The expression can be any simple expression combining the following operators, using standard  operator precedence rule.

<div class="col-9">
    <p id="myMessage">Learning css functions</p>
</div>

<style>
       #myMessage{
            background-color: lightblue;
            width: calc(100% - 120px);
            text-align: center;
       }
 </style>

var() function

The var() CSS function  can be used to insert the value of a custom property (sometimes called a “CSS variable”) instead of any part of a value of another property.

    <div class="col-9">
        <p id="myVar">Learning css functions</p>
    </div>

<style>
            :root {
                --favoriteColor: coral;
            }
     
           #myVar{
                background-color: var(--favoriteColor);
                width: calc(100% - 120px);
                text-align: center;
           }
        </style>

repeat-linear-gradient() function

The repeating-linear-gradient() CSS function creates an image consisting of repeating linear gradients. It is similar to linear-gradient() and takes the same arguments, but it repeats the color stops infinitely in all directions so as to cover its entire container.

    <div class="col-9">
        <p id="myLinearGradient">Learning css functions</p>
    </div>

<style>
           #myLinearGradient{
                background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
                height: 300px;
                text-align: center;
           }
        </style>

 

Source code on Github

February 6, 2020

CSS Fonts

Having the right font has a huge impact on how users experience a website. The right font can create a strong identity for your brand. Using a font that is easy to read are important. The font adds value to your text. It is also important to choose the correct color and text size for the font.

Font Family

There are five generic font families:

  1. Serif fonts have a small stroke at the edges of each letter. They create a sense of formality and elegance.
  2. Sans-serif fonts have clean lines (no small strokes attached). They create a modern and minimalistic look.
  3. Monospace fonts – here all the letters have the same fixed width. They create a mechanical look. 
  4. Cursive fonts imitate human handwriting.
  5. Fantasy fonts are decorative/playful fonts.

We use the font-family property to specify the font of a text. The font-family property should hold several font names as a “fallback” font, to ensure maximum compatibility between browsers/operating systems. Start with the font you want, and end with a generic family (to let the browser pick a similar font in the generic family, if no other fonts are available). The font names should be separated with comma.

If the font name is more than one word, it must be in quotation marks, like: “Times New Roman”.

<div class="row">
    <div class="col-3">
        Font Family
    </div>
    <div class="col-9 fontFam">
        I am learning css.
    </div>
</div>

<style>
            .fontFam {
                font-family: "Lucida Console", "Courier New", monospace;
            }
</style>

Font Style

The font-style property is mostly used to specify italic text.

<div class="row">
    <div class="col-3">
        Font Style
    </div>
    <div class="col-9 fontStyleNormal">
        This is font style normal
    </div>
    <div class="col-3">
    </div>
    <div class="col-9 fontStyleItalic">
        This is font style italic
    </div>
</div>

 <style>
            .fontStyleNormal {
                font-style: normal;
            }

            .fontStyleItalic {
                font-style: italic;
            }
        </style>

 

Font Weight

The font-weight property specifies the weight of a font. Values can be normal, bold, lighter, bolder, <number> from 100-1000.

<div class="row">
    <div class="col-3">
        Font Weight
    </div>
    <div class="col-9 fontWeight">
        This is font weight
    </div>
</div>

<style>
            .fontWeight{
                font-weight: 900;
            }
        </style>

Font Size

The font-size property sets the size of the text. Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs. Always use the proper HTML tags, like <h1> – <h6> for headings and <p> for paragraphs.

The font-size value can be an absolute, or relative size.

Absolute size:

  • Sets the text to a specified size
  • Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
  • Absolute size is useful when the physical size of the output is known

Relative size:

  • Sets the size relative to surrounding elements
  • Allows a user to change the text size in browsers

 

Google Fonts

You can also use Google fonts if you want. They are free to use and many people use them.

 

February 6, 2020

CSS Pagination

Pagination is a very common thing to do when it comes to displaying data or displaying large amount of data. Pagination also gives you the flexibility to load data page by page which eases the load on your backend.

<div class="col-9" id="pag_1">
    <div class="pagination">
        <a href="#">&laquo;</a>
        <a href="#">1</a>
        <a href="#">2</a>
        <a href="#">3</a>
        <a href="#">4</a>
        <a href="#">5</a>
        <a href="#">6</a>
        <a href="#">&raquo;</a>
      </div>
</div>


   <style>
        #pag_1 > .pagination {
            display: inline-block;
        }

        #pag_1 > .pagination a {
            color: black;
            float: left;
            padding: 8px 16px;
            text-decoration: none;
        }
    </style>

Source code on Github

February 6, 2020

CSS Media Queries

@media can be used to apply part of a style sheet based on the result of one or more media queries . With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.

@media not|only mediatype and (expressions) {
  css-code;
}

You can use an external style sheet based on media query.

<link rel="stylesheet" media="mediatype and|not|only (expressions)" href="style.css">

Media Types

  • all – all media types
  • print – printers
  • screen – computer screens, tablets, smart-phones, etc.
  • speech – screenreaders that reads page out loud

Media features

  • min-width – minimum width of display area where rule is applied.
  • min-height – minimum height of display area where rule is applied.
  • max-width – maximum width of display area where rule is applied.
  • max-height – maximum height of display area where rule is applied.
  • orientation – portrait or landscape

Max-width

<div class="row">
    <div class="col-4">
        <h4>Screen with Max width</h4>
        <div>max-width: 600x</div>
    </div>
    <div class="col-8" id="maxWidth">
        max width
    </div>
</div>
<style>
            @media screen and (max-width: 600px) {
                #maxWidth {
                    background-color: lavender;
                }
            }
</style>

Min-width

<div class="row">
    <div class="col-4">
        <h4>Screen with Min width</h4>
        <div>min-width: 601px</div>
    </div>
    <div class="col-8" id="minWidth">
        min width
    </div>
</div> 
<style>
            @media screen and (min-width: 601px) {
                #minWidth {
                    background-color: yellow;
                }
            }
</style>

Min-width and max-width

<div class="row">
    <div class="col-4">
        <h4>Screen with Min to Max width</h4>
        <div>min-width: 600px to min-width: 800px</div>
    </div>
    <div class="col-8" id="mintomaxWidth">
        min to max width
    </div>
</div>
<style>
            @media screen and (max-width: 900px) and (min-width: 600px)   {
                #mintomaxWidth {
                    background-color: lightblue;
                }
            }
</style>

Min-width or max-width

<div class="row">
    <div class="col-4">
        <h4>Media query with or</h4>
    </div>
    <div class="col-8" id="mediaWithOr">
        media query with or
    </div>
</div>
<style>

            /* use , for or */

            @media screen and (max-width: 800px), screen and (min-width: 600px)   {
                #mediaWithOr {
                    background-color: greenyellow;
                }
            }
</style>

Orientation

<div class="row">
    <div class="col-4">
        <h4>Orientation Portrait</h4>
    </div>
    <div class="col-8" id="portrait">
        portrait
    </div>
</div>
<br>
<div class="row">
    <div class="col-4">
        <h4>Orientation Landscape</h4>
    </div>
    <div class="col-8" id="landscape">
        landscape
    </div>
</div>
         <style>
            @media screen and (orientation: landscape) {
                #landscape {
                    background-color: purple;
                }
            }

            @media screen and (orientation: portrait) {
                #portrait {
                    background-color: lightblue;
                }
            }
        </style>

Print

<div class="row">
    <div class="col-4">
        <h4>Print</h4>
    </div>
    <div class="col-8" id="print">
        print
    </div>
</div> 
         <style>
             @media print {
                #print {
                    color: blue;
                    font-size: xx-large;
                    font-weight: bold;
                }
            }
            
        </style>

 

Source code in Github

 

February 6, 2020

CSS Gradients

CSS gradients let you display smooth transitions between two or more specified colors.

Gradients can be used anywhere you would use an <image>, such as in backgrounds. Because gradients are dynamically generated, they can negate the need for the raster image files that traditionally were used to achieve similar effects. In addition, because gradients are generated by the browser, they look better than raster images when zoomed in, and can be resized on the fly.

Linear Gradient

To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

<div class="row">
    <div class="col-3">
        Linear Gradient
    </div>
    <div class="col-9 linearGrad">
        I am learning css.
    </div>
</div>

 <style>
            .linearGrad{
                background: linear-gradient(yellow, red);
                height: 300px;
                text-align: center;
            }
        </style>

Radial Gradient

A radial gradient is defined by its center. To create a radial gradient you must also define at least two color stops.

Radial gradients are similar to linear gradients, except that they radiate out from a central point. You can dictate where that central point is. You can also make them circular or elliptical.

<div class="row">
    <div class="col-3">
        Radial Gradient
    </div>
    <div class="col-9 radialGrad1">
        Default
    </div>
</div>

 <style>
            .radialGrad1{
                background-image: radial-gradient(red, yellow, green);
                height: 300px;
                text-align: center;
            }
        </style>

 

 

Source code on Github

 

February 6, 2020