Friday, February 6, 2015

Working with CSS

1) Before CSS, the styling was inline with the tag, e.g.
     <p style="font-size: 4px; ...">text goes here</p>
2)  but there is another way of styling by including the <style> tag in <head> here </head>, e.g.
<head>
      <style>
              p{
                     font-size: 4px;
                     ...
                   }
      </style>
</head>
3) The third way being the separate .css file
     p{
           font-size: 4px;
           ...
        }
As for the span, it's for exceptions within tags
     span{
           font-size: 6px;
           ...              }
Anyway, this is the generic form:
selector1, selector2, ...{
          property1:...;  /*a selector is actually a tag like <p> but without the < and >*/
          property2:...;
          ...
             }

Some selectors and their properties:
body{
 background-image: url('http://bit.ly/UpQgJ6');
}

a{                                /*this is for the text links*/
  color: #cc000;
  text-decoration: none;
}

img{
    height: 100px;
    width: 300px;
    border: 1px solid #4682b4;

    display: block;/*this makes it in a new line as opposed to inline-block;*/                           
    margin: auto;/*this makes the image go to the center from left and right sides*/
    /*margin: 10px 20px 30px 40px; this is the order of margins: top right bottom left*/

    /*or we can do*/
    /* margin-top: ...*/
    /* margin-rigth: ...*/
    /* margin-bottom: ...*/
    /* margin-left: ...*/
    /*The margin is part of the box or block and is different from the concept of the position!*/
    /*It keeps the box away from other boxes in case of inline-block e.g.*/
}/*this is written of course in the css file, but in the html file if we want to apply this special style to a particular image we would do like the following*/
/*<img src="..." height="100px" width="300px" border="1px solid #4682b4"/> */
/*notice the = instead of the : inside the img tag*/

/*here the div makes a button shape*/
/*A div can have a border like a table or a cell. A div can be centered as well.*/
div{
     margin: auto; /*this is to center the div block; its content can be placed to the right maybe within the div*/
     /*margin-left: 5px;*/
     background-color: #BCD2EE;/*the height, width, and background-color should be specified */
     height: 50px;                            /*to let the div appear*/
     width: 120px; /*this is simply the width of the div block*/
     /*the % could be used to the width, e.g. width: 45%;*/
     /*or even to left: 45%;*/

     border-color: #6495ED;
     border-style: dashed;
     border-width:3px;
     /*border: 3px dashed #6495ED;   using the previous 3 may be better than this one alone*/
     border-radius: 15px; /*this round the edges of the div*/
     /*border-radius: ...%; like 50% is a circle*/
     /*border-bottom-left-radius: 15px;*/
     text-align: center; /*just like in a p tag, this is for the text only*/


     /*divs have by default display: block; notice:*/
     /in the case of  a <p> inside a <div>, the width of the paragraph made by the p tag is that of the block and doesn't take the width of the page*/
     /*if 2 successive divs have display: inline-block; then they are on the same line, but the element is still a block box meaning it is affected by the allocated by the allocated height and width*/
/*but the text or paragraph inside the div e.g. will control the positioning of the divs, so we need to add     vertical-align: middle;*/
     /* display: inline; on the same line but  not a block*/
     /*display: none;    don't display it (even its children _if any_ will disappear).*/

     margin-left: 5px; /*a margin might have a negative value*/
}
When we want to obtain a horizontal menu, we can use an unordered list and display it as inline.


/*amazingly this selector * selects everything!*/
*{
    border: 1px solid black; /*every element is bordered*/
}
so everything has a border, including p, img, ... as stated here.
In fact, any html element is made of a content, surrounded by a padding, surrounded by a border, surrounded by a margin.
So padding is the distance between the border and the actual content e.g. the paragraph text.
padding: ...px; is a padding on all sides

In positioning we make use of:
float: ...; /*The value can be right e.g.*/
Floating elements will not overlap one on top of the other, but a floating element may overlap a non-floating element.
I noticed that in order for the element to be floating it has to have a specified width and height or else it won't work because it already covers the whole page width (in case of a div e.g.).
Suppose only the height is specified, we could use
clear: left;  /*this puts the block for example below any floating block on the left*/
clear: right;/*or*/
clear: both;

More on positioning:
Positions of elements is static by default.
Position: absolute; relative; or fixed; /*fixed means to the screen*/
When position: static; the Left: 50px; e.g. has no effect. I think static positioning is either below or on the same line and can flow (float: right e.g.) if we want to.
When position: absolute; then Left: 50px; would work. It is positioned relative to the first non-static-positioned parent, I think the parent might be absolute-, relative-, or fixed-positioned. In case that parent didn't exist then it is positioned relative to the <html> page.
When position: fixed; then n Left: 50px; would work. It is positioned relative to the browser window. It is fixed even while scrolling the window.
When position: relative; it is positioned relative to the container of the div e.g..

It's worthy to note that float may sometimes contradict with the positioning, so float wins as it seems.

/*to only affect e.g. the p tags that are inside e.g. the div tag we write:*/
div p{
        ...
}
/*but simply a p will affect all the p tags everywhere*/
p{
   ...
}

so div p{ ...} affects all the ps that are descendants of the higher div; but if we want to affect only the ps that are directly children of a top-level div (I mean div directly nested in the body) then we use:
div > p {
            ...
}

A programmer defined selector can be:
#id_name{
              ...
}
this id name is typical to be in a particular tag in the html file, e.g.:
<h2 id="id_name">...</h2>

To apply a styling to multiple different tags, we use the programmer defined class, e.g.
.class_name{
                  ...
}
this class name is probably in many tags in the html file, e.g.
<h1 class="class_name">...</h1>
<p class="class_name">...</p>

By the way, one tag can have an id and belong to a certain class at the same time, e.g.
<p class="class_name" id="id_name">...</p>

 The id has a higher priority than a class in case of contradiction.

And now comes the turn of the pseudo-class; the general form is:
selector:pseudo-class_selector {
    property: value;
}
It seems like the pseudo-class_selector is a predefined value, as well as the selector, the following examples are all about links:
a:hover {
    color: #cc0000;
    font-weight: bold;
    text-decoration: none;
}
a:visited{ /*a link that was clicked on*/
          ...
}
a:link{ /*a link that was not visited*/
          ...
}

Another example of the pseudo-class selector is:
p:first-child{ /*the first <p> child in any parent will be colored in red; the parent can be div, li, or ...*/
    color: red;
}
this works just like a class would but easier.
In this example instead of the p tag being the selector we could had have any other tag even a programmer defined class is ok.
But consider the following html snippet:
  <div>
       <p>I'm the first child!</p>
       <p class="class-name">I'm the second child</p>
       <p class="class-name">I'm the third child</p>
       <p>I'm the fourth child</p>
  </div>
and this css snippet:
.class-name:first-child{
                               ...
}
well the first child is considered the absolute one which corresponds to the paragraph "I'm the first child!" and not to "I'm the second child", so the html snippet won't be affected by the css pseudo-class snippet.
As if saying: "if the class-name affected tag was the first child do ..."

Likewise for the second child, it would be for example
p:nth-child(2){
                  ...
}
As if saying: "if the p tag was the second child do ...", thus <h3> e.g. can be the first child and <p> is the second one so it will be affected, but say <h2> was the second child then it won't be affected.

And the 5th child
p:nth-child(5){
                  ...
}

In my programming terms I would say that the pseudo selector related to the link a are somehow working at runtime, but the first-child and nth-child(...) are working at code-time.

The magnificent z-index property, the higher the value the more it will be showing on the webpage.

opacity: 0.5; this interesting feature is to how much brightness maybe an element would have.



To hide the remaining of an image within the parent container div element, we might use:
overflow: hidden;
















No comments:

Post a Comment