Showing posts with label CSS Programing. Show all posts
Showing posts with label CSS Programing. Show all posts
16/08/2016
CSS Horizontal Align
In CSS, we can use different properties (like margin, position or float) to align elements horizontally.
The will define the element as a center aligned:
Example
Using the margin Property for Center Aligning
By setting the margin-left and margin-right property to "auto" for a block element, specifies the available margin equally.The will define the element as a center aligned:
Example
#example-center
{
margin-left: auto;
margin-right: auto;
width: 50%;
background-color: #336699;
}
Using the position Property to Align Left and Right
Using the absolute positioning also, we can achieve the left or right alignment of HTML element:#example-right
{
position: absolute;
right: 0px;
width: 150px;
background-color: #336699;
}
Using the float Property to Align Left and Right
Another way to aligning elements is by using the float property:#example-right
{
float: right;
width: 150px;
background-color: #336699;
}
CSS Position Property
In CSS, the position property is used to set position for the HTML elements. Mainly the position property can have four possible values - static, relative, fixed or absolute.
Example
Below example will set a fixed element in the lower-right corner of the page:
The position:static property
By default, the HTML elements have the position value as static which can not be affected by the top, bottom, left, and right properties.Example
#example-static
{
position: static;
border: 1px solid black;
}
The position:relative property
Positioning any HTML element as relative, is positioned relative to its normal position. This can be done by setting the top, right, bottom, and left properties of a relatively-positioned element.#example-relative
{
position: relative;
left: 25px;
border: 1px solid black;
}
The position:fixed property
Positioning any HTML element as fixed, allows to position an HTML element to a particular spot on the page (relative to the viewport). It will fix the position of the element that always stays in the same place even if the page is scrolled.Below example will set a fixed element in the lower-right corner of the page:
#example-fixed
{
position: fixed;
bottom: 0;
right: 0;
width: 250px;
border: 1px solid black;
}
The position:absolute property
An element defined with the position: absolute property is positioned at the specified coordinates relative to the screen top-left corner.#example-absolute
{
position: absolute;
left: 250px;
top: 250px;
border: 1px solid black;
}
11/08/2016
CSS Display and Visibility
In CSS display property defines how an element is displayed (used for controlling layout).
The visibility property defines whether an element should be visible or hidden.
A block value specifies the full width available for an element and by default use a line break before and after it.
Some common HTML elements which have the display value as block are < h1 >, < p >, < li >, < div > etc.
An inline value specifies the necessary width of the element only, and never force for line breaks before and after the element.
Some common HTML elements which have the display value as inline are < span >, < a > etc.
we can change the display of any element by using the CSS display property. Below is the example:
using display:none property
This property hides an element and it will not take any space for the element on the webpage.
using visibility:hidden property
This property hides an element but still it will take the same space for the element as before on the webpage.
The visibility property defines whether an element should be visible or hidden.
CSS Display Property
In HTML each and every element has a default display value either block or inline.A block value specifies the full width available for an element and by default use a line break before and after it.
Some common HTML elements which have the display value as block are < h1 >, < p >, < li >, < div > etc.
An inline value specifies the necessary width of the element only, and never force for line breaks before and after the element.
Some common HTML elements which have the display value as inline are < span >, < a > etc.
we can change the display of any element by using the CSS display property. Below is the example:
h1
{
display: inline;
}
a
{
display: block;
}
Hiding any Element - using display:none or visibility:hidden
Any element can be hide by using the CSS display or visisbility property.using display:none property
This property hides an element and it will not take any space for the element on the webpage.
h1
{
display: none;
}
using visibility:hidden property
This property hides an element but still it will take the same space for the element as before on the webpage.
h1
{
visibility: hidden;
}
CSS Font
CSS Font property is used to control the look of texts by defining their font family, boldness, size and the style.
Note: Here we have specified two font families Times and Serif. This approach is helpfule when a browser does not support the first one then it tries the next font. Use quotation marks if the name of a font family is more than one word like "Times New Roman".
The posible value for this property can be:
Possible values of the font-size property can be:
Font Family
The font-family property is used to set the font family of a text.p
{
font-family: Times, serif;
}
Note: Here we have specified two font families Times and Serif. This approach is helpfule when a browser does not support the first one then it tries the next font. Use quotation marks if the name of a font family is more than one word like "Times New Roman".
Font Style
The font-style property is used to specify style (normal, italic or oblique) of the text.The posible value for this property can be:
- normal - Specify a normal text
- italic - Specify a text in italics
- oblique - Specify a text "leaning" (very similar to italic, but less supported)
h1
{
font-style: normal;
}
h2
{
font-style: italic;
}
p
{
font-style: oblique;
}
Font Size
The font-size property is used to sets the size of the text either in absolute (like in pixels, point etc) or relative (like in percentage, em etc).h1
{
font-size: 16px;
}
h2
{
font-size: 2em;
}
The em size unit is recommended by the W3C. 1em = 16px ( 16 px is the default size of text in browsers).Possible values of the font-size property can be:
| Value | Description |
| medium | Sets the font-size to a medium size. This is default |
| xx-small | Sets the font-size to an xx-small size |
| x-small | Sets the font-size to an extra small size |
| small | Sets the font-size to a small size |
| large | Sets the font-size to a large size |
| x-large | Sets the font-size to an extra large size |
| xx-large | Sets the font-size to an xx-large size |
| smaller | Sets the font-size to a smaller size than the parent element |
| larger | Sets the font-size to a larger size than the parent element |
| length | Sets the font-size to a fixed size in px, cm, etc. |
| % | Sets the font-size to a percent of the parent element's font size |
Font Weight
The font-weight property is used to sets the weight (how bold a font) of the text. Possible values for this property could be normal, bold, bolder, lighter, 100, 200, 300, 400, 500, 600, 700, 800, 900.h2
{
font-size: 2em;
font-weight:bold;
}
All CSS Font Properties
| Property | Description |
| font-family | Specifies the font family for text |
| font-size | Specifies the font size of text |
| font-style | Specifies the font style for text |
| font-variant | Specifies whether or not a text should be displayed in a small-caps font |
| font-weight | Specifies the weight of a font |
19/07/2016
CSS Text
In CSS it is possible to manipulate the text used in the webpage with the help of text properties.
Below are the various text properties which can be applied to any HTML element -Text Color
The color property is used to set the color of the text.h1
{
color: #336699;
}
To define the default color of the text we use the body selector:
body
{
color: blue;
}
Text Alignment
The text-align property is used to set the alignment of text horizontal.h1
{
text-align: left;
}
The posible values for this property are:
| Value | Description |
| left | Aligns the text to the left |
| right | Aligns the text to the right |
| center | Centers the text |
| justify | Stretches the lines so that each line has equal width (like in newspapers and magazines) |
| initial | Sets this property to its default value. |
| inherit | Inherits this property from its parent element. |
Text Decoration
The text-decoration property is used to set or remove decorations from text (like removing underlines from links etc).a
{
text-decoration: none;
}
The posible values for this property are:
| Value | Description |
| none | Defines a normal text. This is default |
| underline | Defines a line below the text |
| overline | Defines a line above the text |
| line-through | Defines a line through the text |
| initial | Sets this property to its default value. |
| inherit | Inherits this property from its parent element. |
Text Transformation
The text-transform property is used to turn the selectd text into uppercase, lowercase letters or capitalizing the first letter of each word.h1
{
text-transform: uppercase;
}
h3
{
text-transform: lowercase;
}
h3
{
text-transform: capitalize;
}
Text Indentation
To specify the indentation of the first line of a text, CSS has "text-indent" property.p
{
text-indent: 25px;
}
All Text Properties
Below are the all CSS Text properties:| Property | Description |
| color | Sets the color of text |
| direction | Specifies the text direction/writing direction |
| letter-spacing | Increases or decreases the space between characters in a text |
| line-height | Sets the line height |
| text-align | Specifies the horizontal alignment of text |
| text-decoration | Specifies the decoration added to text |
| text-indent | Specifies the indentation of the first line in a text-block |
| text-shadow | Specifies the shadow effect added to text |
| text-transform | Controls the capitalization of text |
| unicode-bidi | Used together with the direction property to set or return whether the text should be overridden to support multiple languages in the same document |
| vertical-align | Sets the vertical alignment of an element |
| white-space | Specifies how white-space inside an element is handled |
| word-spacing | Increases or decreases the space between words in a text |
CSS Background
In CSS, there are different background properties which can be used to define the background effects of any HTML element. The various background effects are -
The above example will change the background color of the h1 element with the "#336699" color.
In CSS the color code can be specified by the following ways:
The image will cover the entire element because the by default behaviour of the "background-image" property is repeated.
So it will repeats an image both horizontally and vertically. CSS "background-repeat" property allows to set the values like repeat, no-repeat, repeat horizontally only etc.
In the above example, the image will repeated horizontally only.
Other possible values of "background-repeat" property are given below
Background Color
To apply a color in the background of any HTML Element, In CSS there is property "background-color".h1
{
background-color: #336699;
}
The above example will change the background color of the h1 element with the "#336699" color.
In CSS the color code can be specified by the following ways:
- By HEX value - like "#336699"
- By RGB value - like "rgb( 255, 0, 0 )"
- By color name - like "blue"
Background Image
To apply an image in the background of any HTML Element, In CSS there is property "background-image".The image will cover the entire element because the by default behaviour of the "background-image" property is repeated.
body
{
background-image: url( "example.jpg" );
}
background-image property supports all the major types of image (like .jpg, .png, .gif, .bmp etc)Background Repeat
CSS "background-repeat" property oftenly used with the "background-image" property. The by default behaviour of the "background-image" property is repeated.So it will repeats an image both horizontally and vertically. CSS "background-repeat" property allows to set the values like repeat, no-repeat, repeat horizontally only etc.
body
{
background-image: url( "example.jpg" );
background-repeat: repeat-x;
}
In the above example, the image will repeated horizontally only.
Other possible values of "background-repeat" property are given below
| Value | Description |
| repeat | Specify the repetition both vertically and horizontally. |
| repeat-x | Specify the repetition only horizontally |
| repeat-y | Specify the repetition only vertically |
| no-repeat | The background-image will not be repeated |
| initial | Sets this property to its default value. |
| inherit | Inherits this property from its parent element. |
Background Attachment
The background-attachment property is used to specify if the background image is fixed or scroll with the rest of the page in browser window. Means that if any background image is set as fixed with the "background-attachment" property then the image will not move during scrolling in the browser.body
{
background: url( "example.jpg" );
background-repeat: no-repeat;
background-attachment: fixed;
}
The possible values of "background-attachment" property are given below:| Value | Description |
| scroll | The background scrolls along with the element. This is default |
| fixed | The background is fixed with regard to the viewport |
| local | The background scrolls along with the element's contents |
| initial | Sets this property to its default value. Read about initial |
| inherit | Inherits this property from its parent element. Read about inherit |
Background Position
The "background-position" property allows to set the position of the image or color used in the background of any HTML element.body
{
background-image: url( "example.jpg" );
background-repeat: no-repeat;
background-position: right top;
}
The possible values of "background-position" property are given below:| Value | Description |
| left top left center left bottom right top right center right bottom center top center center center bottom | If you only specify one keyword, the other value will be "center" |
| x% y% | The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. Default value is: 0% 0% |
| xpos ypos | The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions |
| initial | Sets this property to its default value. |
| inherit | Inherits this property from its parent element. |
Background - Shorthand property
To shorten the code while using the CSS background properties, it is also possible to specify all the background properties in one single property.body
{
background: #336699 url( "example.jpg" ) no-repeat right top;
}
CSS Measurement Units
CSS supports different measurement units such as absolute units ( like inches, points, pixels etc) and relative units (like percentages, em etc.). These values will required when a designer need to specify the measurements in the CSS document like " font-size:15px " etc
Below is the list of different measurement units used in CSS -
| Unit | Description | Example |
|---|---|---|
| % | Specifies measurement in percentage of another relative value. | p { line-height: 150%; } |
| cm | Specifies measurement in centimeters | p { margin-bottom: 5cm; } |
| em | Specifies relative measurement for the height of a font in em spaces. Because an em unit is equivalent to the size of a given font, if you assign a font to 10pt, each "em" unit would be 10pt; thus, 2em would be 20pt. | p { letter-spacing: 5em; } |
| ex | Specifies a measurement relative to a font's x-height (determined by the height of the font's lowercase letter x.) | p { line-height: 3ex; } |
| in | Specifies a measurement in inches. | p { word-spacing: .12in; } |
| mm | Specifies a measurement in millimeters. | p { word-spacing: 10mm; } |
| pc | Specifies a measurement in picas (1 pica = 12 points) | p { font-size: 10pc; } |
| pt | Specifies a measurement in points (1 point = 1/72nd of an inch.) | p { font-size: 18pt; } |
| px | Specifies a measurement in screen pixels. | p { font-size: 15px; } |
| vh | 1% of viewport height. | p { font-size: 3.0vh; }/td> |
| vw | 1% of viewport width | p { font-size: 5.9vw; } |
| vmin | 1vw or 1vh, whichever is smaller | p { font-size: 2vmin;} |
18/07/2016
CSS Include
There are mainly 3 ways to insert CSS in any webpage -
When the same style need to be applied to many pages of a website then we prefer to use the external style sheet. Using of External style sheet allows to change the look of an entire website by changing this one file only. The < link > element is used in the < head > section of HTML document to include an external style sheet (separate text file with .css extension).
The style attribute is used to define any inline styles of any HTML element. The style attribute can contain any CSS property but applicable to that element only.
< p style = "color:blue;" > My first paragraph.< /p >
To apply CSS mainly on a single or specific document having a unique style. The HTML element < style > is used in the < head > section of HTML document to define any internal style sheet.
In any HTML document multiple style sheet can be used. If some properties have already been defined for the same selector in the different style sheets, the values will be inherited from the more specific style sheet.
The following rule will define that what style will be used when there is more than one style specified for an HTML element -
External Style Sheet
When the same style need to be applied to many pages of a website then we prefer to use the external style sheet. Using of External style sheet allows to change the look of an entire website by changing this one file only. The < link > element is used in the < head > section of HTML document to include an external style sheet (separate text file with .css extension).
< head > < link rel=" stylesheet " type = "text/css" href = "example.css" > < /head >
Inline style
The style attribute is used to define any inline styles of any HTML element. The style attribute can contain any CSS property but applicable to that element only.
< p style = "color:blue;" > My first paragraph.< /p >
Internal style sheet
To apply CSS mainly on a single or specific document having a unique style. The HTML element < style > is used in the < head > section of HTML document to define any internal style sheet.
< head >
< style >
h1
{
color: blue;
text-align: left;
}
< /style >
< /head >
Multiple Style Sheets
In any HTML document multiple style sheet can be used. If some properties have already been defined for the same selector in the different style sheets, the values will be inherited from the more specific style sheet.
The following rule will define that what style will be used when there is more than one style specified for an HTML element -
- Browser default (Lowest Priority)
- External style sheet
- Internal style sheet (in the head section)
- Inline style (Highest Prioprity)
CSS Selectors
CSS selectors indicates the HTML element on which a style will be applied based on their id, class, type, attribute and more.
Element selector select elements based on the element name like < h1 >, < table > etc.
h1 { color: blue; }
The above statement will the change the color of all h1 elements color to blue in a webpage.
CSS allows to change the style using the id attribute of an HTML element.
To select an element with a specific id, there is a need to use hash ( # ) character before the id name.
The above example will change the style of only HTML element which has content-wrap as the id attribute in a webpage.
CSS allows to change the style using the specific class attribute of an HTML element.
To select an element with a specific class, there is a need to use period ( . ) character before the class name.
The above example will change the style of only HTML element which has content-wrap as the class attribute in a webpage.
It is also possible to specify the only specific HTML elements should be affected by a class. like below:
In CSS it is possible to apply a style to many selectors if required. To do so, just separate the selectors with a comma.
Instead of initializing the same style to different element as in above example, we can follow the below example -
Selector Types
- Element Selectors - are global in nature. They affect each and every element of that type.
- Class Selector - working with HTML class attributes. A class attribute name can be anything can be used as many elements as possible.
- Descendent Selectors - allows target an element based on where it's found. To do so, simply string the selectors together, separating them with whitespace. The parent selectors are added first, followed by each successive nested selector.
Element Selector
Element selector select elements based on the element name like < h1 >, < table > etc.
h1 { color: blue; }
The above statement will the change the color of all h1 elements color to blue in a webpage.
id Selector
CSS allows to change the style using the id attribute of an HTML element.
To select an element with a specific id, there is a need to use hash ( # ) character before the id name.
#content-wrap
{
text-align: left;
color: #333333;
}
The above example will change the style of only HTML element which has content-wrap as the id attribute in a webpage.
The class Selector
CSS allows to change the style using the specific class attribute of an HTML element.
To select an element with a specific class, there is a need to use period ( . ) character before the class name.
.left
{
text-align: left;
color: #333333;
}
The above example will change the style of only HTML element which has content-wrap as the class attribute in a webpage.
It is also possible to specify the only specific HTML elements should be affected by a class. like below:
h1.left
{
text-align: left;
color: blue;
}
Grouping Selectors
In CSS it is possible to apply a style to many selectors if required. To do so, just separate the selectors with a comma.
h1
{
text-align: left;
color: blue;
}
h2
{
text-align: left;
color: blue;
}
h3
{
text-align: left;
color: blue;
}
Instead of initializing the same style to different element as in above example, we can follow the below example -
h1, h2, h3
{
text-align: left;
color: blue;
}
Subscribe to:
Posts (Atom)


