Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Css Positioning Elements


Published on

Published in: Technology, Education
  • pixel quality is so bad unable to understand.
    Are you sure you want to  Yes  No
    Your message goes here

Css Positioning Elements

  1. 1. Positioning Elements Prepared by: Sanjay Raval | http:// /
  2. 2. Box Model Recap Every element on the page is considered to be a rectangular box made up of the element’s content, padding, border, and margin <ul><li>Padding is applied around the content area. </li></ul><ul><li>If you add a background to an element, it will be applied to the area formed by the content and padding. </li></ul><ul><li>Border applies a line to the outside of the padded area . </li></ul><ul><li>Outside the border is a margin. </li></ul><ul><li>* Margins are transparent and cannot be seen. </li></ul><ul><li>* They are generally used to control the spacing between elements . </li></ul>
  3. 3. Box Model Recap In CSS, width and height refer to the width and height of the content area . Adding padding, borders, and margins will not affect the size of the content area but will increase the overall size of an element’s box. If you wanted a box 100-pixel wide with a 10-pixel margin and a 5-pixel padding on each sides, you would need to set the width of the content to be 70 pixels. #myBox { margin:10px; padding:5px; width:70px; }
  4. 4. Box Model Recap Padding, borders, and margins can be applied to all sides of an element or individual sides. Margins can also be given a negative value.
  5. 5. Block and inline elements HTML elements are either block or inline elements. A block-level box , such as a div, a paragraph, or a heading, begins rendering on a new line. An inline-level box , such as a <span> or an <em>, begins rendering wherever you place it within the document and does not force any line breaks.
  6. 6. Block and inline elements Using CSS property, display, you can make an inline element display like a block-level element or vice-versa. Example: Say you want tabs, which is a list (of links) and so should be marked up as a <ul> <ul id=&quot;maintabs&quot;> <li><a href=&quot;#&quot;>PersonalFinance</a></li> <li><a href=&quot;#&quot;>BillPay</a></li> <li><a href=&quot;#&quot;>FundsTransfer</a></li> <li><a href=&quot;#&quot;>FinancialCalendar</a></li> <li><a href=&quot;#&quot;>Customer Care</a></li> <li> </ul> Here's how it looks as a normal list
  7. 7. Block and inline elements #maintabs li { display:inline; list-style-type:none; float:left; background-color:#dce2c7; border:1px solid #c5d199; padding:5px 10px; margin-left:2px; } Here's how it looks with the CSS styles applied Here's the CSS code Using CSS property display:inline, you can display a block element <li> as an inline element and can show the list items <li> as tabs.
  8. 8. Floating A floated box can either be shifted to the left or the right until its outer edge touches the edge of its containing box, or another floated box. Because floated boxes aren’t in the normal flow of the document, block boxes in the regular flow of the document behave as if the floated box wasn’t there. As shown in below figure, when you float Box 1 to the right, it’s taken out of the flow of the document and moved to the right until its right edge touches the right edge of the containing block.
  9. 9. Floating If you float all three boxes to the left, Box 1 is shifted left until it touches its containing box, and the other two boxes are shifted left until they touch the preceding floated box. If the containing block is too narrow for all of the floated elements to fit horizontally, the remaining floats will drop down until there is sufficient space.
  10. 10. Floating If the floated elements have different heights, it is possible for floats to get “stuck” on other floats when they drop down.
  11. 11. Clearing the floats Lets say you have a grid of floated objects. Then let’s say you want to create a break in that grid in order to start a new row.
  12. 12. Clearing the floats There are times when you don’t want certain content to be next to floated elements. To accomplish this, you can use clear. The clear has four options: &quot;clear: left&quot;, &quot;clear: right&quot;, &quot;clear: both&quot; or &quot;clear: none&quot;. clear: left The element is moved below the bottom outer edge of any left-floating boxes that resulted from elements earlier in the source document.
  13. 13. Clearing the floats clear: right The element is moved below the bottom outer edge of any right-floating boxes that resulted from elements earlier in the source document. clear: both The element is moved below all floating boxes of earlier elements in the source document.
  14. 14. Line boxes and clearing Line boxes (text boxes) next to a floated box are shortened to make room for the floated box, and flow around the float. In fact, floats were created to allow text to flow around images
  15. 15. Line boxes and clearing To stop line boxes flowing around the outside of a floated box, you need to apply a clear to that box. The clear property can be left, right, both, or none, and indicates which side of the box should not be next to a floated box. To accomplish this, enough space is added above the cleared element’s top margin to push the element’s top border edge vertically down, past the float
  16. 16. The Position Property There are four values for the position property: static, absolute, fixed, and relative. Static is the default. Unless specified, all boxes start life being positioned in the normal flow (static position) let’s take a quick look at each using a simple example with four paragraphs.
  17. 17. The Position Property Static Positioning With the four paragraphs each displayed with the default position property value, static , they stack one above the other, as normal document flow dictates.
  18. 18. The Position Property Relative Positioning Let’s set the third paragraph to the relative position. You can then move this paragraph with respect to its default position using the properties top, left, bottom, and right. p#specialpara {position:relative; top:30px; left:20px;}
  19. 19. The Position Property Relative Positioning Now the top of your paragraph is moved down by 30 pixels and right by 20 pixels. Here although the element moves relative to its original position, nothing else changes.
  20. 20. The Position Property Absolute Positioning This type of positioning takes an element entirely out of the flow of the document. Let’s modify the code you used for relative positioning by changing relative to absolute. p#specialpara {position:absolute; top:30px; left:20px;}
  21. 21. The Position Property Absolute Positioning You can see that the space previously occupied by the element is gone. The absolutely-positioned element has become entirely independent of the surrounding elements in the markup, and it is now positioned with respect to the top-level element, body. Because the absolutely positioned element’s positioning context is body, the element moves when the page is scrolled to retain its relationship to the body element, which also moves when the page scrolls.
  22. 22. The Position Property Fixed Positioning Fixed positioning is similar to absolute positioning, except that the element’s positioning context is the viewport (the browser window or the screen of a handheld device, for example). So the element does not move when the page is scrolled. Note: However, position:fixed does not work in IE6, although it does work in IE 7.
  23. 23. The Position Property
  24. 24. The Position Property Positioning Context contextual positioning means that when you move an element using the properties top, left, right, or bottom, you are moving that element with respect to another element. That other element is known as its positioning context. As you saw in “Absolute Positioning” earlier, the positioning context of an absolutely positioned element is body—that is, unless you change it. body is the containing element of all other elements in your markup, but you can use any ancestor element as a positioning context of another element by changing the ancestor’s position value to relative.
  25. 25. The Position Property Positioning Context Let’s look at this markup <body> <div id=&quot;outer&quot;>The outer div <div id=&quot;inner&quot;>This is some text...</div> </div> </body> and this CSS div#outer_div {width:250px; margin:50px 40px; border-top:3px solid red;} div#inner_div {top:10px; left:20px; background:#AAA;}
  26. 26. The Position Property Positioning Context But why isn’t the inner div 10 pixels down from the top of the outer one and 20 pixels to the left, as specified? The answer is that the inner div has the default positioning of static. This means it is in the regular document flow, and because the outer div has no content, the inner div starts in the same place. Only when you set an element to one of the other three positioning options—relative, absolute, or fixed—do the top, left, right, and bottom properties actually do anything.
  27. 27. The Position Property Positioning Context Let’s see this in action by setting the inner div’s position property to absolute. div#outer_div {width:250px; margin:50px 40px; border-top:3px solid red;} div#inner_div {position:absolute; top:10px; left:20px; background:#AAA;} Because there is no other relatively positioned element for it to reference, it positions itself by default with respect to the body element.
  28. 28. The Position Property Positioning Context If you then set the position property of the outer div to relative, the positioning context of the absolutely positioned inner div is now the outer div If you now set the left and top position properties of the outer div to anything other than zero, the inner div would move to maintain its positioning relationship to the outer div—its positioning context. Get it?
  29. 29. Overflow Property There may be a case when an element's content might be larger than the amount of space allocated to it. CSS provides a property called overflow which tells the browser what to do if the box's contents is larger than the box itself . Example: Here's the HTML code <body> <div class=“myBox&quot;> This is some text... </div> This is some text... </body>
  30. 30. Overflow Property Here's the CSS code .myBox { float: left; width:150px; height:150px; background-color:#ccc; border: 1px solid #999; margin-right:5px; padding:5px; } Here's how it looks:
  31. 31. Overflow Property Overflow has 4 values namely visible, auto, hidden, and scroll .myBox { overflow:visible; } Overflow: visible Visible is the default value. No scrollbars will be added, and your content will just flow. Lets add some more text to the myBox div container and see how the text flows.
  32. 32. Overflow Property .myBox { overflow:auto; } Overflow: auto You should use this value when you want to let the browser decide what’s right. Scroll down? Scroll right? No scrolling at all? The browser makes this decision. This value usually is the best choice. Here's how it looks:
  33. 33. Overflow Property .myBox { overflow:hidden; } Overflow: hidden This value will not add any scrollbars or will not display more text then needed. When the content crosses the ‘height’ given to the container, it simply don’t display that content. Here's how it looks:
  34. 34. Overflow Property .myBox { overflow:scroll; } Overflow: scroll An overflow of scroll means that the browser should place scrollbars on the element whether or not the contents of the element have overflowed. Ensure there is always enough content in this box, because otherwise there are ugly scrollbars for nothing! Here's how it looks: Note: Theoretically, with the overflow property, the need for frames is greatly reduced. Unfortunately, some old browsers do not fully support the overflow property.