Elton Cassas

LinkedIn LogoGithub LogoBehance LogoFigma Logo
CSS 3 logo

Updated on: Sat Apr 23 2022 00:00:00 GMT+0000 (Coordinated Universal Time)

Estimated reading time : 10 min read

css

ui design

CSS Fundamentals

Title : Mi primer artículo MDX

TOC

Selectors

Used for the selection of HTML DOM elements

Selectors CheatSheet

Types of CSS Selectors

A way to define types of selector we have the following:

Tag Selector

Uses the typo of the HTML tag.

html {    
	//styling
}

div {
}

h1 {
}

span, p {} 

Class and Id Selector

Giving the following markup

section.main
    div.article
    div#aside
        div#card
footer.footer

The class selectors would look

.main {    
	/*Styles here*/
}

.article {    
	/*Styles here*/
}

.footer {    
	/*Styles here*/
}

#aside {    
	/*Styles here*/
}

#card {    
	/*Styles here*/
}

Global selector

/* This will select will affect all DOM elements.
It could be used in the process of normalize predetermined styles, 
though it is the best approach  */
* {    
	padding: 0;    
	margin: 0;
}

Direct Child Selector

Example:

ul > li{    
	//code
}

.father .child {    
	//code
}

ul .father {    
	//code
}

From HTML:

<ul>
    <li>The CSS aplies here</li>
    <li></li>
    <li></li>
    <li class="father">
        <ul class="child">
            <li></li>
        </ul>
    </li>
</ul>

Next Brother Selector

Instead of using the > symbol, you need to use the + symbol, it only applies to the next brother element

<h1>
</h1>
<p>
    <!-->The code applies here if it is used h1 + p<-->
</p>
<h2>
</h2>
<p>
    <!-->The code applies here if it is used h2 + p<-->
</p>
h1 + p {
}

h2 +p {
}

Next Brothers Selectors

You can use with the following code

.class1 ~ .class2 {
}

p ~ div {    
	/*This will select all brothers of p and div that comes after div*/
}

The selected HTML is all brothers of .class1 that has the .class2, the condition is that the elements must be children of the same father

Other attribute selectors

Some examples:

[type="email"]{    
	//code here
}

[required]{    
}

/*You can also use RegEx with this attribute*/

/*Starts With*/
[href^=""]{    }
/*End with*/
[href$=""]{    }
/*Contains in*/
[href*=""]{    }

Bases of CSS

Specificity

What style rule is the most specific.

Cascade

What comes after overrides what is before it.

Inheritance

An element takes the styles from the tag father, if no style is specified to it.

Makes a tag to use the default styles (initial value)

Makes a tag inherit the styles from the father

Architecture

Layout and CSS modules

We have the next CSS modules

Display

Box Model

.example {
    box-sizing:border-box;
             /*content-box
               padding-box*/
}

Other useful content:

.example {
    /*To center horizontally*/
    margin-left:auto;
    margin-right:auto;
}

*, *::before, *::after {
    box-sizing: border-box;
}

Dimensions

Margin and Padding

Margin.- Separation between outer elements and the border.

Padding.- Separation between the border and the content of a element.

/*Margin*/
h1 {
    /*Shorthand Up, Right, Down, Left*/
    margin: 10px 5px 8px 4px;
}

/*Trick To use vertical margin in a child component you must add padding,
 otherwise it will be ignored */
article {
    display:block;
}

.card {
    padding-top: 1px;
    margin-top: 10%;   
}

/*Padding*/
h1 {
    /*Shorthand Up, Right, Down, Left*/
    padding: 10px 5px 8px 4px;
}

Box-sizing

Property to declare the way to calculate the width and height

/*trick, it is normal to set the box-sizing to border box,
 using a global pseudo-element */

*, *::before, *:: after {
    box-sizing: border-box;
}

Borders

Some useful example of how to use order property:

/**some shorthands*/
.example {
    border-style: solid dashed dotted double;
    border-color: black red blue purple;
    /**border-width:;*/
    border: 1px solid black;
}
.example{
    border-radius: 50px;
    /*The order of border is top-left, top-right, bot-right, bot-left*/
    border-top-left-radious: coord-x coord-y;
    /*Shorthand*/
    border-radius: x x x x / y y y y;
}

https://codepen.io/eltoncass/pen/KKzpVMG

Border

Border of an element.

Outline

Exterior Border

Box Shadow

body {
    /*box shadow shorthand*/
    box-shadow: h-offset v-offset blur spread color | inset
}
/*example chess table*/
.chess {
    width: 200px;
    height: 200px;
    background : black;
    box-shadow: 400px 0, 800px 0,
                200px 400px, 600px 200px,
                0 400px, 400px 400px,
                800px 400px;
}

Background

/*background is a shorthand*/
body {
    background-color: yellow;
    background-color: url(myawesomeimage.png);
    background-repeat: no-repeat;
}

body {
    background-size: 800px;
    /*background-size: contain|cover;*/
    /*background-position: left center right | top center bottom;*/
    background-position: center center;
    /*To add a padding to the position of an image*/
    background-position: right 1rem bottom 1rem;
}

background-color

Sets a color as the background

background-image

Sets an image as the background, the value can be a relative path or absolute

background-repeat

Indicates if the image will repeat itself. Values (repeat || no-repeat || pos-x || pos-y)

background-size

It only works with background image

background-size can be contain, cover or auto.

By default is auto

background-position

It only works with background image

it takes two values, the first its position-x and the second is position-y

background-clip & background-origin

It is not very useful.

.box {
    width: 500px;
    height: 300px;
    background-image: url(example.png);
    background-size: cover;
    margin: 100px auto;
    border: 24px solid;
    border-color: black brown;
    
	/*Using the box model specifies, what would the background cover. */
    background-clip: content-box;
    padding: 50px;
    box-shadow 0 10px 30px 0 rgba(black, 0.4);

    /*Specifies the starting point of the background, using the box model*/
    background-origin: content-box;
}

background-attachment

Maintains the background in its position, it can be useful for cool effects like parallax

.box {
    width: 600px;
    height: 400px;
    background-image: url(example.png);
    margin: 100px auto;
    background-attachment: fixed;
    background-size: 1500px;
}

background shorthand

Background can have multiple images, but just one color.

body {
    background-image: url(playa.jpg), url(carretera.jpg), url(faro.jpg);
    background-size: 20%;
    background-position: 0 0, center center, right bottom;
    background-repeat: no-repeat;
}

body {
    /*shorthand
    background: image position / size repeat atachment origin clip;
    background: color;
    */
    background: url(playa.jpg) 0 0 / 20% no-repeat,
							  url(carretera.jpg) center center / 20% no-repeat,
					      url(faro.jpg) right bottom / 20% no-repeat, yellow;
}

Tip: the last background should always be the last specified. When you want to have a color in your backgrounds always put it on last.

Colors

Concepts

Current Color

Css now supports this value-variable currentColor, which it will take the last declared color in the element, father-element and so on.

Models

Modes

RBG & RBGA

body {
    background-color: rgb(255,0,0); /*R*/
    background-color: rgb(0,255,0); /*G*/
    background-color: rgb(0,0,255); /*B*/

    background-color: rgb(0,255,255);   /*C*/
    background-color: rgb(255,0,255);   /*M*/
    background-color: rgb(255,255,0);   /*Y*/
    background-color: rgb(0,0,0);       /*K*/

    background-color: rgb(255,255,255); /*W*/
    background-color: #8082
    /*The statement before means: with 22 as alpha*/
    background-color: #88008822;
    background-color: rgba(#880088, 0.2)
}

You can also add transparency (alpha) to the color statements.

Hsl & Hsla

HSL is not HSB

        - 0 or 360 is red
        - 60 yellow
        - 120 green
        - 180 cyan
        - 240 blue
        - 300 magenta

image-20200802183322678

body {
    /*pure color*/
    background-color: hsl(0, 100%, 50%);
    /*with more light*/
    background-color: hsl(0, 100%, 70%);
    /*darker*/
    background-color: hsl(0, 100%, 30%);
    /*less saturation*/
    background-color: hsl(0, 60%, 30%);
}

Gradient

Gradient are background images, that’s why it needs a element, height or width to be specified.

All gradients are background images

By default the bg-repeat its declared as repeat.

Two types of gradient

body {
    background-image: linear-gradient(yellow, red);
    background-repeat: no-repeat;

    /*background-image: linear-gradient(color1 [stop]) //stop is optional*/
    background-image: linear-gradient(yellow 50px, red 70%);

    /*background-image: linear-gradient([angle], color1 [stop]) 
    //stop and angle are optional. 180deg is the default value*/
    background-image: linear-gradient(90deg, yellow 50px, red 70%);
    background-image: linear-gradient(to top left, yellow 50px, red 70%);
}

Text & Fonts

https://www.youtube.com/watch?v=Rrl_QxPX248

Basic concepts

em & rem

p {
    font-size: 2rem;
    font-style: italic;
    text-align: left;
    line-height: 2| 200% |4rem;
    /*text-align: justify exists but is not recommended*/
    /*line-height: should be at least 1.3 times of the font size,
    it will depend if it is a title or content*/
    text-decoration: line-through;
    text-transform: uppercase;
    letter-spacing: -0.02m;
    word-spacing: 0.5em;
    font-family: Oswald 'Open Sans';
    font-weight: 500;
    text-shadow: .25px.25px 1px hsl(200,20%,40%);
}

vertical-align

white-spacing & overflow

text-transform

uppercase | lowercase | capitalize | line-throught

letter-spacing & word-spacing

Pseudo-class

Pseudo-Class are a type of selectors are more related to the position and display of elements, they are in charge of the state.

They are used with : character

:root

It refers to the HMTL element (tag), it used for declare CSS variables.

:hover, :active, :visited

hover: when the pointer enters a determined element, it hovers over it, this state represents that.

actived: Indicates an element that has been clicked on.

visited: tags already visited.

:target

Use to represent an element with an unique id in the url page, this element is the destiny of a redirection ()

:not

Excludes an element from a css rule ( e.g. :not([type=""]) )

:empty

Applies to elements without any child.

:nth-child(): nth-last-child(): & :first-child & :last-child

Select the indicated child element. The hierarchy does matter

nth-last-child will start counting from the last child

You can stack this pseudo classes.

/*Find the first five**/
p:nth-child(-n+5) {
    /**styles*/
}

/*Last five**/
p:nth-last-child(-n+5) {
    /**styles*/
}

/*Numbers from x**/
p:nth-child(n + x) {
    /**styles*/
}

/*Number 'til x**/
p:nth-last-child(n + x) {
    /**styles*/
}

/*Multiple of 3*/
p:nth-child(odd):nth-child(3n){
    /**styles*/
}

/*First Child*/
div :first-child {
    /*styles*/
}

/*Last Child*/
div :last-child {
    /*styles*/
}

:nth-of-type & first-of-type: & :last-of-type

Select the element of its type, the hierarchy does not matter. It is used for tags, does not work with classes.

/**Selects the 3rd parragraph*/
p:nth-of-type(3) {
    /*styles*/
}

/*find the first p*/
p:first-of-type {
    /*styles*/
}
Go Back Home