CSS Training
CSS Training
What is CSS?
Selectors
In order to to work with CSS, you first need to
devise a way of filtering the HTML elements,
and this is done via selectors.
The * (wildcard) selector: used for
selecting all elements on a page.
Ex. * {visibility:hidden;} will hide all elements
on a page
th.grid_scale_ng{font-size:20px;}
input#Q13_5{visibility:hidden;}
<tableclass="confirmit-grid"summary="Answers of Q2x1">
<thead>
<tbody>
<trid="yui_3_3_0_1_141033941277444">
<tr><td><input type = radio id = Q1_1_1 checked = checked></td></tr>
<tr></tr>
<tr></tr>
</tbody>
td:not([style="text-decoration:overline"])
If you are selecting elements based on the value of some attribute, you
are not restricted to matching whole attribute values only the attribute
name has to be specified entirely, not the value itself . Instead, you are
able to use pattern matching :
attribute value starting with some pattern (^= operator)
input[id^=Q1] selects all inputs with an attribute named id that
has a value starting with the string Q1 please be aware that the
^ symbol is reserved in confirmIT, so you cannot use this variant
there. Use the contains variant instead (described below).
Attribute value that contains the pattern anywhere within the full
string (*= operator)
fieldset td[headers*=header1] selects all table cells that have an
attribute called headers with a value containing the string
header1. If, for example, we have cells that have attribute values
like header11 or header111, those will be selected as well.
Attribute values that end with the pattern ($= operator)
fieldset td[headers$=_header1] selects all table cells that have
an attribute called headers with a value ending with the string
_header1.
Pseudo-class selectors
:first-child matches the first child element of another element
Link state selectors (:link, :visited, :hover, :active ) used for styling
links differently, according to their state. Very often used for
navigation menus
Dimension of elements can be specified in pixels, %, em or pt. The most used are
pixels and %.
width, max-width inline elements mostly ignore the width and height
properties, so its best to set them as block elements and then set the
width/height.
height, max-height (height: auto = flexible height, decided by height of
children; height: 100% = total height of its container)
coordinates of elements
top, left, right, bottom only for positioned elements (if element does
not have position (relative/absolute) property declared, then these
properties are useless on their own)
visibility: hidden or visible. hidden hides the element, but does not free its space
in the layout of the page (if you need this, use display:none;). Default is visible.
display: block, inline or none. You can set display: inline on a div to make it behave
like a span, or display:block on a span or an image to make them behave like divs.
background-color: this can be specified as a hex value (ex. #e0e0e0), as rgb ( ex.
rgb(255,255,255)) or as words (ex. blue);
General properties
containers, cont.
background-image: can use an image as background, the value must be
an url and must be specified like this: url(some URL string). To repeat the
background image throughout the whole containers space, use the
background-repeat property (values are repeat-x, repeat-y, repeat and
no-repeat).
overflow (overflow: hidden - causes all text that cant fit inside the parent
container to not be displayed at all; overflow: scroll creates a scroll
vertical and/or horizontal scrollbar for the text that cant fit in its container,
depending on the situation.
overflow-x or overflow-y can be used to enforce horizontal or vertical
scrolling only)
z-index a property which takes numeric value. Applying it on a positioned element (you can
read about those in the Positioning of elements section) causes the element that has the
higher z-index to be displayed in its entirety when overlapping with another element that has a
lower z-index. The usual default value is 0, and you can specify negative values in order to give
a lower priority to an element.
A very common problem with z-index and embedded Flash videos is that the Flash video is
given a higher stacking priority. To solve the issue, add this in the Flash object code:
<param name="wmode" value=transparent" />
Inheritance
Inheritance is the process by which elements inherit the values of
properties from their ancestors in the tree. Some properties, e.g. color, are
automatically inherited by the children of the element to which they are
applied. Each property defines whether it will be automatically inherited.
Normally, think about text properties when you think about inheritance.
Some properties like margin, borders, padding, width and so on do not
inherit (if they would, it would cause mayhem.)
The inherit value can be set for any property and will force a given
element to inherit its parent elements property value, even if the property
is not normally inherited.
Three main sources of style information form a cascade. They are:
The browser's default styles for the markup language.
Styles specified by a user who is reading the document.
The styles linked to the document by its author. These can be specified in
three places:
In an external file
In a definition at the beginning of the document: use this method only
for styles that are used only on that page.
On a specific element in the body of the document:this is the least
maintainable method, but can be used for testing.
Positioning of elements
What we have presented so far (the box model and some properties to
work with) allows you to modify the size or space between elements,
but they still always remain in place depending on their position in the
HTML source code. The position of the rendered elements can be
changed by using the position property.
position: static this is default, no need to write it if you just want
the elements to stay where they are;
position: relative sets the element to a position that is relative to its
current position (you can specify the new position by using the top, left,
right and bottom properties after declaring the position as relative)
ex. div {position: relative; top:50px; left:145px; } sets the all div
elements 50 pixels below and 145 pixels to the left from where they
previously were.
You can also specify negative values, this is the equivalent of setting
the opposite property as the absolute value of the specified amount
of pixels.
Positioning of elements,
cont.
Positioning of elements,
cont.
In the previous example, the div d2 is absolutely
positioned, and since the containing element, d1 is not
positioned, then the only available element is the html tag,
so the starting coordinates for d2 will be that of the html
element (left: 0 ,top: 0), and the final position of d2 will be
(top: 50, left: 50).
What happens, though, if we set d1 as position: relative?
position: fixed a subset of position: absolute, the
element will stay in place, regardless of scroll. This is only
useful if we need an element to be seen regardless of where
the respondent is on the page (ex. technically, we could set
the headers of a grid with 200 rows as fixed and these
headers will always be seen near the top of the grid).
Horizontal centering
Ex.
<style>
div#d1 {
background-color: lightgrey;
width: 350px;
min-height:30px;
border: 25px solid navy;
}
#d2{margin: 0 auto; width:60%;}
</style></head>
<body>
<div id = 'd1'><div id = 'd2'>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.<div/></div>
Floats
<div class="center">
<div id = 'p1'>SOME TEXT TO THE
<div id = 'p2'>SOME TEXT TO THE
</div>
<p id = 'p3'><b>Some text outside
<style>
.center {
margin: 0 auto;
width: 80%;
background-color: #b0e0e6;
border:1px solid black;
}
#p1{width:100px; border:1px solid
#p2{width:100px; border:1px solid
</style>
LEFT!</div>
RIGHT!</div>
the divs.</p>
black;}
black;}
Floats, cont.
We will float the first child div to the left and the second to the right.
<style>
div#p1 {float: left;}
div#p2 {float: right;}
</style>
You can see that the parent div has lost its floated children, and so it has shrunk to the height
of 1 pixel, since that was the only dimension property it had. However, this isnt the only
problem : the text that was previously below the divs is now visually on the same level as them.
We can fix these problems easily:
solve the parent problem by floating the parent element as well. However, this will mess up the
horizontal centering of the parent element.
Applying overflow:auto on the container also does the job, but this would be using auto to solve
a problem it isnt suited for.
Preferred way: solve the outside text problem by applying the clear property on the external p
with a value of both, or add an empty div in the container and apply clear:both on it this will
tell the browser that this element should not have any floated elements to its left or right. We
will also hide the parent div, since we wouldnt need it in this case.
<style>p {clear:both;}</style>
(if, for example, we had only one div floated to the left, it would have been enough to say clear:
left).
Specificity
So far we have seen ways of making element behave in a certain way, based on some
rules targeting them specifically. But what happens when we have 2 conflicting rules
targeting the same element(s)? What happens, for example, if were targeting the same
element, once by id and the second time by its name together with the class name?
Which rule is applied in the end?
This is actually decided by a number associated to each selector of a rule (and the
mechanism which allows this is called specificity). Each unique part of a selector has a
fixed number corresponding to it, and those numbers are added together in the end in
order to compute the specificity.
Id 100
Class 10
Attribute selector, pseudo-class selector, pseudo-element selector - 10
Element name 1
Using commas to separate elements does not raise specificity of rule (credits to Cosmin
Gheorghiu and Laura Avram )
In addition to these, any rule declared in an inline style (that is, inside the element tag),
has 1000 added to its specificity, in order to beat other rules. However, the general rule
is that inline styles should be avoided, as they are not at all maintainable.
Ex.
Specificity, cont.
Sometimes, in order to overwrite some of the values of some general
properties of an element, it helps to append !important to the property:
value pair (ex. .grid_scale_ng { border-left:1px solid red !important; }).
This will try to apply this property-value pair and ignore the other rules,
but the use of !important is generally frowned upon and seen only as a
last resort.
You can use web inspectors (like Firebug or Chrome WI) in order to see
the CSS rules and easily remove them/modify them.
So, considering the above, if a rule declared in a style just will not
apply
despite all your efforts, its possible that:
its specificity is less than that of a previously declared rule, so adding an
id (ex, the id of the page you are on) will help beat that rule
a rule that was declared in an inline style conflicts with yours, so you
need to raise it by 1000 (declare it as an inline style in some element)
and probably use an extra id in the selector.
https://specificity.keegan.st / - to calculate specificity
Table styling
You can collapse all the borders in a table into a single one by
using the border-collapse property. This will make all the table
cells have only one border width between them, as opposed
to the normal two (the right border of the left cell and the left
border of the right cell). The possible values for bordercollapse are collapse and separate.
Table elements also have the border-spacing property, which
lets you specify a distance between borders. This will work
only on the collapsed borders model (see below).
Ex. create a distance of 20 pixels between each row of a confirmit grid:
fieldset .confirmit-grid {
border-collapse: separate;
border-spacing: 0px 20px;
}
You can make containers behave like table elements if you structure them just like
a table by setting their display property to that part of the table that it should stand
for (so far we have used display: inline and block only)
Ex. A block of divs that will be displayed like a table.
<div style="display: table;">
<div style="display: table-row;">
<div style="display: table-cell;">td11</div>
<div style="display: table-cell;">td12</div>
<div style="display: table-cell;">td13</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell;">td21</div>
<div style="display: table-cell;">td22</div>
<div style="display: table-cell;">td23</div>
</div>
</div>
The vertical-align property is used for table cells and inline elements, but it
behaves differently depending on which type of element it is used. The most used
values are: top, middle and bottom.
Vertical-align in table cells or block elements specifies the vertical alignment of
the content inside the cells.
Vertical-align for inline elements is applied to the elements themselves