Create rounded corners and different shapes using CSS3

How to make rounded corners and other shapes like circle, heart, triangle etc using CSS3
If you are designing/developing a website, then basic elements will be HTML and CSS for sure apart from other biggie like jQuery, PHP/Perl blah blah. And page load into the browser also matters which is being determined by the size of images being used, js files, css files etc.

I still remember those days when I used to put images for every small shape like circle, rectangle, chat bubble, quotes, rounded corners etc. But using CSS3 you can not only save disk space but web space too!
There are few attributes in CSS through which you can do amazing stuffs. One of them is “border-radius.  This property allows us to add rounded corners to elements.

The border-radius property is a shorthand property for setting the four border-*-radius properties which mean you can specifically use top-left, top-right, bottom-right, bottom-left instead of that *

Its usual syntax is:
[css]
border-radius: 1-4 length|% / 1-4 length|%;     /* Here 1-4 means four corners 😀 */
[/css]
Either we can use length that we define using em or px or we can use percentage. How we can achieve various shape using this property. Let’s see rounded corner stuff first.
CSS Code:
[css]
<style type="text/css">
  #round-corner{
  padding:10px 40px;
  background-color:#FF0000;
  width:300px;
  border-radius:25px; 
}
</style>
 [/css]
You could write border-raidus in em or percentage too even.
Something like:
 [css]
border-radius: 2em 1em 4em / 0.5em 3em;
[/css]
which is equivalent to:
[css]
border-top-left-radius: 2em 0.5em;
border-top-right-radius: 1em 3em;
border-bottom-right-radius: 4em 0.5em;
border-bottom-left-radius: 1em 3em;
 [/css]
 
Then write below code inside body tag:
 [css]
<div id="round-corner">Isn't it amazing to use border-radius property instead of rounded images?</div>
 [/css]

How the browsers interpret which em for whom if we missed any of those four corners?
 
Answer is simple, follow this rule:
The four values for each radii are given in the order top-left, top-right, bottom-right, bottom-left. If bottom-left is omitted it is the same as top-right. If bottom-right is omitted it is the same as top-left. If top-right is omitted it is the same as top-left.

 
How about having circle or oval shape using border-radius property?
You just need to include height, width in proper ratio and then  border-radius would do the rest. What I meant is :
[css]
<style type="text/css">
#circle {
                height: 70px;
                width: 70px;
                border-radius: 35px;  /* or 50% */
                background-color: #FF0000;
}
 
#oval {
                width: 200px;
                height: 100px;
                background: #FF0000;
                border-radius: 100px / 50px;   /* or 50% */
}
</style>
 [/css]

Please check this link for fully working code and resulted shapes from these code snippets.
 
One task for you people

Can you make these shapes using border-radius property?

 Once you will be familiar with this I would write a post on :before and :after pseudo CSS properties. Then we will be able to make heart shape or infinity shape or even yin-yang shape (something like Symantec logo 😀 )

 
Note: It may not work in some older version of browsers specially IE! So, Test these codes in chrome or Firefox or safari
 

Minimal thing that one should know about CSS 3 -Part I

CSS Logo-aliencodersMinimal Introduction About CSS 😉 CSS means Cascaded Style Sheet which is a style sheet language used to describe the layout (look and feel) of a document in Markup Language. CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts.

This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share common formatting, and reduce complexity and repetition in the structural content (such as by allowing for tableless web design).

CSS can also allow the same markup page to be presented in different styles for different rendering methods, such as on-screen, in print, by voice (when read out by a speech-based browser or screen reader) and on Braille-based, tactile devices. While the author of a document typically links that document to a CSS style sheet, readers can use a different style sheet, perhaps one on their own computer, to override the one the author has specified.

CSS specifies a priority scheme to determine which style rules apply if more than one rule matches against a particular element. In this so-called cascade, priorities or weights are calculated and assigned to rules, so that the results are predictable. The CSS specifications are maintained by the World Wide Web Consortium (W3C).

Internet media type (MIME type) text/css is registered for use with CSS by RFC 2318 (March 1998). Its has gone through different version till date. Its lates version is CSS 3 which  has been under development since December 15, 2005. The minimal topic that one should know to use CSS 3 are written in structured way here and from the next post, we will try to explain all topics one by one.

1. External, Internal, Inline

2. Syntax h1{color:#FFFFFF;} or selector {property:value}

3. ID vs Class

4. Styling

  • Text styling

i. Color ii. Letter-spacing iii. Word-spacing iv. Text-align (left, center, justify, right) v. Text-indent vi. Text-transform (uppercase, lowercase, capitalize) vii. Text-decoration (none, overline, line-through, underline, blink) viii. Line-height

  • Font

i. Font-family ii. Font-style (normal, italic, oblique) iii. Font-size iv. Font-weight v. Font-variant (normal, small-caps)

  • List

i. List-style 1. List-style-image 2. List-style-type (circle, square, alpha, roman, latin, decimal-leading-zero) 3. List-style-position (inside, outside)

  • Table
  • Links

i. a:link ii. a:visited iii. a:hover iv. a:active

  • Background (color, image, position, attachment (scroll, fixed),repeat)

i. Background-color:#colorcode ii. Background-image: url(‘path of image’) iii. Background-position:left top/left center/left bottom/x pos y pos iv. Background-attachment:scroll/fixed/inherit v. Background-repeat: repeat/repeat-x/repeat-y/none 5. Box Properties

  • Model
  • Border
  • Margin
  • Padding
  • Outline

6. Dimension

  • Height
  • Width
  • Max-height
  • Min-width

7. Visibility and display

  • Visibility:hidden collapse(useful for table. Works in IE if !doctype is defined)
  • Display: block or inline or none

8. Positioning

  • Fixed (fixed position is positioned relative to the browser window)
  • Relative (relative positioned element is positioned relative to its normal position.)
  • Absolute (An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>)
  • Static (default)
  • Z-index

9. Float and clear

  • Float (left, right, none, inherit)
  • Clear (left, right, both, none, inherit)

10. Pseudo class

  • :focus (ex: input:focus { color:green;})
  • :first-child (ex: p:first-child{color:blue;})

11. Pseudo elements

  • :after
  • :before
  • :first-letter
  • :first-line

12. Opactiy (css3)

  • Opacity: 0-1 (FF)
  • filter:alpha(opacity=x). (for IE x=0-100)

Follow this link for CSS examples

CSS

  1. External, Internal, Inline
  2. Syntax h1{color:#FFFFFF;} or selector {property:value}
  3. ID vs Class
  4. Styling

    • Text styling

i. Color

ii. Letter-spacing

iii. Word-spacing

iv. Text-align (left, center, justify, right)

v. Text-indent

vi. Text-transform (uppercase, lowercase, capitalize)

vii. Text-decoration (none, overline, line-through, underline, blink)

viii. Line-height

    • Font

i. Font-family

ii. Font-style (normal, italic, oblique)

iii. Font-size

iv. Font-weight

v. Font-variant (normal, small-caps)

    • List

i. List-style

1. List-style-image

2. List-style-type (circle, square, alpha, roman, latin, decimal-leading-zero)

3. List-style-position (inside, outside)

    • Table
    • Links

i. a:link

ii. a:visited

iii. a:hover

iv. a:active

    • Background (color, image, position, attachment (scroll, fixed),repeat)

i. Background-color:#colorcode

ii. Background-image: url(‘path of image’)

iii. Background-position:left top/left center/left bottom/x pos y pos

iv. Background-attachment:scroll/fixed/inherit

v. Background-repeat: repeat/repeat-x/repeat-y/none

  1. Box Properties

    • Model
    • Border
    • Margin
    • Padding
    • Outline
  2. Dimension

    • Height
    • Width
    • Max-height
    • Min-width
  3. Visibility and display

    • Visibility:hidden collapse(useful for table. Works in IE if !doctype is defined)
    • Display: block or inline or none
  4. Positioning

    • Fixed (fixed position is positioned relative to the browser window)
    • Relative (relative positioned element is positioned relative to its normal position.)
    • Absolute (An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>)
    • Static (default)
    • Z-index
  5. Float and clear

    • Float (left, right, none, inherit)
    • Clear (left, right, both, none, inherit)
  6. Pseudo class

    • :focus (ex: input:focus { color:green;})
    • :first-child (ex: p:first-child{color:blue;})
  7. Pseudo elements

    • :after
    • :before
    • :first-letter
    • :first-line
  8. Opactiy (css3)

    • Opacity: 0-1 (FF)
    • filter:alpha(opacity=x). (for IE x=0-100)

Follow this link for CSS examples

 

ID vs Class in CSS

CSS allowClass vs ID in CSSs us to define our own custom selectors also, which is known as ID and class selectors. IDs and classes are applied to (X)HTML elements as simple attributes that provide much tighter control over our design. Most of the time, we get confused IDs with classes, either failing to utilize the real purpose of each or simply using one instead of the other.

Let’s treat ID as identification, which is unique to you an is not shared with anyone else. A class is different, in that there can be many people in a class, be it at school, in society, or wherever. This translates to CSS where an ID can only be used once per page, whereas classescan be used an unlimited number of times.

Another Example could be Bar Code (class) and Serial Number (ID).

There are two kinds of selectors mainly in CSS: One is base selector like h1,h2, body, table or any existing HTML tags.
Such as the following rule which sets all paragraphs to red (font-color): p {color: #F00;}

CSS allows us to define our own custom selectors also, which is known as ID and class selectors. IDs and classes are applied to (X)HTML elements as simple attributes that provide much tighter control over  our design. Most  of the  time, we get  confused IDs with classes, either failing to utilize the real purpose of each or simply using one instead of the other.

Let’s  treat ID as identification. Which s unique to you an is not shared with anyone else. A class is different, in that there can be many people in a class, be it at school, in society, or wherever. This translates to CSS where an ID can only be used once per page, whereas classescan be used an unlimited number of times. Another Example could be Bar Code (class) and Serial Number (ID).

IDs An ID can only be used once per page, and is a unique identifier to an element.
Typically, an ID is used for any unique page element such as the header, main navigation, footer, or other key part of the user interface. Applying an ID The most common way to apply an ID is to reference it in the (X)HTML using the id="name" attribute immediately after the opening tag within an element. In this case, our two IDs are named highlight and default, respectively, and are applied to two paragraphs:

<p id="pred">This paragraph has red text.</p>
<p id="default">This paragraph has dark gray text.</p>

The corresponding CSS uses the hash (#) character to denote the rule is a unique ID. The hash is combined with the ID name to start the rule, followed by the property declarations:

/* Define highlighted text */
#pred {color:#F00;}
/* Define default text */
#default {color:#333;}

Combining IDs with Selectors Existing or new IDs can be combined with selectors in the style sheet to add further control. In the following example, the base CSS defines all h2 headings as dark gray and 16 pixels in size: /* Basic heading style */ [code language="css"] h2 { color:#333; font-size:16px; } [/code] That is fine for most uses of <h2>, but let’s say the main <h2> on your page (the title of an article) needs to be emphasized with a different color. This calls for a new rule where the selector is defined in the form element#name:

/* Adjust the color of h2 when used as a title */
h2#title { color:#F00; }

Here the new rule will override the default <h2> color (color: #333;) with red (color: #F00;) whenever an <h2> is identified with in the (X)HTML. The new rule does not redefine font-size, so that will be carried over and unchanged. Simply add the unique identifier to the page:

<h2 id="title">Title Of My Article</h2>

Remember that title is a unique identifier, so it cannot be used again within that template. Any other instances of <h2> on the page will be rendered with the default styling.

When to Use an ID
Only one element on each page can be styled by each unique ID, and therefore IDs should be reserved for unique, single-use elements such as a header or sidebar, or the main navigation or page footer. This makes scanning your markup easier, as all ID attributes will denote unique content areas or special regions of the page, while also providing greater flexibility for more complex CSS application

When to Avoid an ID
IDs must be avoided when there is more than one requirement for the CSS rule. Do not use an ID for anything you are likely to multiply in the future, such as multiple images, link styles, or paragraphs where more than one will need to be styled a particular way.

Class
A class can be used an infinite number of times per page, making it a very flexible method of applying CSS. A class defines an element as belonging to a group, or as a reusable object or style. Classes solve problems in the short term, but can provide less flexibility for more complicated CSS designs.

Applying Classes The most common way to apply a class is to reference it in the (X)HTML using a class="name"attribute of an element. As with our ID example, the two classes are named highlight (for red text) and default (for dark gray text):

<p class="highlight">This paragraph has red text.</p>
<p class="default">This paragraph has dark gray text.</p>
<p class="default">This paragraph also has dark gray text.</p>

Note that as the identifiers are classes, they can be used more than once, hence in the example two paragraphs have been identified as default, so will be styled the same way. That would not be acceptable if using IDs. The corresponding CSS uses a full stop (.) character to denote the rule is a reusable class. The full stop is combined with the class name to start the rule, followed by the property declarations:

 /* Define highlight class */
.highlight {color:#F00;}
/* Define default class */
.default {color:#333;}

Combining IDs with Multiple Classes
Classes are especially useful when you wish to have control over a number of elements. Consider the following drinks list, the source code for which is available in the drinks.html file:

<ul id="drinks"> <li class="alcohol">Beer</li> <li class="alcohol">Spirits</li> <li class="mixer">Cola</li> <li class="mixer">Lemonade</li> <li class="hot">Tea</li> <li class="hot">Coffee</li> </ul>

Note first that the unordered list (<ul>) is given a unique ID. Thus, id="drinks" will not be used again on the page at any time, allowing that particular list to be styled uniquely. Note also that Beer and Spirits are within list elements defined with class="alcohol", Cola and Lemonade are within list elements defined with, and finally Tea and Coffee are defined in list elements with. This allows each drinks group to be treated individually. The CSS declares that the default text for that list will be red, so any list items without a class attribute will default to red text:

/* Drinks list styling */
ul#drinks {color:#F00;}

Next, the classes for each drink type are defined with unique shades of gray for font color:

/* Define alcohol color */
.alcohol {color:#333;}
/* Define mixer color */
.mixer {color:#999;}
/* Define hot drinks color */
.hot {color:#CCC;}

The result sees the list of items move through shades of gray (defined by the classes). Any further drinks added to the list can be assigned to a particular drinks group, such as <li>Wine</li>. Thus a logical color key is established using simple CSS classes.

Note: Before adding a class to an element, be sure that the element needs it. Too often web designers overuse classes when the (X)HTML is already providing more than enough hooks for the CSS. Make sure that the element cannot be targeted using a descendant selector or other method before opting for a class. This will help keep your code lean and make future redesigning much easier.

Overriding Base Styling with Classes Here is an example of base CSS rule that is being  used to turn all paragraphs red declaring all instances of paragraphs red and  add a class rule to the CSS that will bleach out any element it is identified with by turning text light gray:

/* Default styling for paragraphs */ p {color:#F00;font-size:12px;} /* Use this style to turn anything light gray */ .bleached {color:#CCC;}

All paragraphs will still be red by default, but this can still be overridden when necessary by identifying an element with the bleached class, as in this (X)HTML: <p>This paragraph has red text.</p> <p class="bleached">This paragraph has light gray text.</p> The second paragraph will now be light gray, as the color declaration in bleached overrides the red. Note that the paragraph is still rendered 12 pixels high, as bleached does not redefine font-size. Add a font-size declaration in bleached, and that value will override the original size for all paragraphs identified with.

Linking a Class Directly to an Element In this example, the CSS is constructed with the class attached directly to the element in the form element.classname, and like before, it is referenced using the class="classname" format within the (X)HTML.

/* Use this style to turn anything light gray */
.bleached {color:#CCC;}
/* Override the color of bleached when it identifies a paragraph */
p.bleached {color:#000;}

This method would be used when the standard declaration for the bleached class needs to be overruled. For example, any element given a class of bleached will remain light gray (color:#CCC;), but any instances of paragraph elements with a class of bleached will be rendered black (color: #000;). This method is useful when numerous instances of a class are littering your (X)HTML, and it would be too difficult to remove them all manually. Instead, simply target that class when it identifies the element you need to change using the form element.classname.

When to Use a Class
As described previously, classes are a very flexible method for applying your CSS rules, and can be used again and again within a page. Use classes to control elements that belong to a group, for temporary items, and also to enhance the behavior of an ID.

When Not to Use a Class
It is not recommended that classes be used for main structural elements within a page, such as headers and main navigation, although they will work. Doing so would decrease the flexibility of your design and make further customization difficult without overhaul or extra markup. Also, be sure a class is needed, and make sure the element cannot be targeted by defining a rule for the existing (X)HTML before getting class-happy. Remember that a class is used for exceptions to normal styling, and not to define the standard.

Elements can have BOTH
There is nothing stopping you from having both an ID and a Class on a single element. In fact, it is often a very good idea. Take for example the default markup for a WordPress comment list item: <li id="comment-27299" class="comment"> It has a class applied to it that you may want for styling all comments on the page, but it also has a unique ID value (dynamically generated by WordPress, nicely enough). This ID value is useful for direct linking. Now I can link directly do a particular comment on a particular page easily.

CSS doesn’t care
Regarding CSS, there is nothing you can do with an ID that you can’t do with a Class and vice- versa. I remember when I was first learning CSS and I was having a problem, sometimes I would try and troubleshoot by switching around these values. Nope. CSS doesn’t care.

Javascript cares
JavaScript people are already probably more in tune with the differences between classes and ID’s. JavaScript depends on there being only one page element with any particular, or else the commonly used getElementById function wouldn’t be dependable. For those familiar with jQuery, you know how easy it is to add and remove classes to page elements. It is a native and built in function of jQuery. Notice how no such function exists for ID’s. It is not the responsibility of JavaScript to manipulate these values, it would cause more problems than it would be worth.

If you don’t need them, don’t use them (try to avoid using if possible)