Advertisement
decembre

CSS - CENTER

May 8th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1.  
  2. ============================
  3. ======== CSS - CENTER v.1 - POSTé
  4. ============================
  5.  
  6. =========================================
  7. =========== CENTER
  8. ==http://www.noobcube.com/tutorials/html-css/horizontal-and-vertical-centering-using-css-a-beginners-guide-/
  9.  
  10. ============================
  11. ===========CENTER === Vertical text centering
  12. ==http://stackoverflow.com/questions/2439177/use-margin-auto-and-center-to-center-float-left-div
  13. ==http://stackoverflow.com/questions/6823979/css-vertical-text-centering
  14. ============================
  15. ===========CENTER - HORIZONTALLY Centering ABSOLUTE Positioned Elements :
  16. ELEMENT WIDTH + ABSOLUTE >> LEFT (50% of element width in %) and NEGATIVE MARGIN-LEFT (1/2 ELEMENT WIDTH in PX)
  17. == http://www.noobcube.com/tutorials/html-css/horizontal-and-vertical-centering-using-css-a-beginners-guide-/
  18. Centering an element that has position: absolute declared is quite simple if there's a width declared.
  19. Let's create a simple div with a fixed width, absolute positioning, and some basic styles for appearance.
  20. HTML
  21. <div>I'm a horizontally centered div that's absolutely positioned in the browser.</div>
  22. CSS :
  23. div{
  24. width: 600px;
  25. position: absolute;
  26. left: 50%;
  27. margin-left: -300px;
  28. background: #CCC;
  29. }
  30.  
  31. What we've done is given our div element a width of 600px and a position: absolute declaration.
  32. We gave it a background color just to show the 600px element.
  33. What's important here is the left and margin-left values:
  34. - We've set our left value to 50% which moved our element over 50% from the left.
  35. - We then take half of the element size and put a negative margin-left value equal to that.
  36. In our case, half of 600px is 300px.
  37.  
  38. ============================
  39. ===========CENTER - HORIZONTALLY Centering Block-Level Elements with an UNKNOWN Width :
  40. DISPLAY:TABLE >> RIGHT / LEFT MARGIN = AUTO
  41. == http://www.noobcube.com/tutorials/html-css/horizontal-and-vertical-centering-using-css-a-beginners-guide-/
  42. We declare our display property value as table on our p elements,
  43. they will be treated as a block-level table element.
  44. This means two things:
  45. - TABLE element will only take up as much width as it needs. So there's no need to declare a width for it.
  46. It's still treated as a block-level element, therefore it will still render the margin property.
  47. p {
  48. background: #ccc;
  49. padding: 5px;
  50. border: 1px solid #000;
  51. display: table;
  52. margin: 10px auto;
  53. }
  54. Added:
  55. - DISPLAY:TABLE declaration to get our p elements to render as table elements.
  56. - 10px bottom and top margin,
  57. - RIGHT / LEFT MARGIN = AUTO
  58.  
  59.  
  60. ============================
  61. ===========CENTER - VERTICAL ALIGN == table / table-cell
  62. == http://www.webtoolkit.info/css-vertical-align.html
  63. DEMO: CSS vertical align
  64. .v-outer {
  65. display: table;
  66. #position: relative;
  67. overflow: hidden;
  68. height: 100px;
  69. background: black;
  70. color: white;
  71. width: 100%;
  72. }
  73. .v-middle {
  74. display: table-cell;
  75. #position: absolute;
  76. #top: 50%;
  77. vertical-align: middle;
  78. }
  79. .v-inner {
  80. position: relative;
  81. #top: -50%;
  82. }
  83. htlm :
  84. <div class="border v-outer">
  85. <div class="v-middle">
  86. <div class="v-inner">
  87.  
  88. ============================
  89. ===========CENTER - VERTICAL Centering Text with Line-Height : (line-height text = height containing block)
  90. ==http://www.instantshift.com/2010/03/15/47-css-tips-tricks-to-take-your-site-to-the-next-level/
  91. Vertically center text within a containing block who’s height is fixed,
  92. simply set the line-height of the text to be the same as the height of the containing block.
  93. The HTML:
  94.  
  95. <div id="container">some text here</div>
  96. The CSS:
  97. div#container {height: 35px; line-height: 35px}
  98.  
  99. ============================
  100. ===========CENTER - VERTICAL Block Level Elements
  101. ==http://www.instantshift.com/2010/03/15/47-css-tips-tricks-to-take-your-site-to-the-next-level/
  102. Positioning can be used to vertically center block level elements.
  103. The HTML:
  104. <div id="content">your content here</div>
  105. The CSS:
  106. div#content {position: absolute; top: 50%; height: 500px; margin-top: -250px}
  107. The top left corner of the div will be positioned 50% from the top.
  108. Since we want the center of the div to be positioned 50% from the top
  109. you need to set a negative top margin equal to half the height of the div.
  110.  
  111. This same trick works for horizontal centering as well.
  112. Change top to left and margin-top to margin-left and set the negative margin to be half of the width of the element.
  113.  
  114. div#content {position: absolute; top: 50%; left:50%; width:800px; height: 500px; margin-left: -400px; margin-top: -250px}
  115. The above CSS will center the element both vertically and horizontally
  116.  
  117.  
  118. ============================
  119. ===========CENTER - VERTICAL With CSS : Using a floater and clearing the content
  120. == http://blog.themeforest.net/tutorials/vertical-centering-with-css/
  121. Because the content will clear:both;,
  122. you can also put other elements above it, and when the windows collapses,
  123. the centered content will not cover them up. See the demo.
  124. <div id="top">
  125. <h1>Title</h1>
  126. </div>
  127. <div id="floater"></div>
  128. <div id="content"> Content Here </div>
  129. #floater {float:left; height:50%; margin-bottom:-120px;}
  130. #top {float:right; width:100%; text-align:center;}</strong>
  131. #content {clear:both; height:240px; position:relative;}
  132.  
  133.  
  134. ============================
  135. ===========CENTER - WITH Fixed-Sized Element (parent container : relative property for this to work.)
  136. Center a fixed-width/fixed-height div at the center of its container.
  137. This could be adapted to centering text, images, etc. within their containers.
  138. Essentially, we do a bit of arithmetic to get the fixed-sized element centered using absolute positioning and margins.
  139. Note that the parent container must have a position: relative property for this to work.
  140.  
  141. div {
  142. position: absolute;
  143. top: 50%;
  144. left: 50%;
  145. width: 400px;
  146. height: 300px;
  147. margin-top: -150px; /* 1/2 of your element height*/
  148. margin-left: -200px; /* 1/2 of your element width */
  149. }
  150.  
  151. ============================
  152. ===========CENTER - Vertically Align Text : line-height to the same height as the containing div
  153. == http://sixrevisions.com/css/10-random-css-tricks-you-might-want-to-know-about/
  154. We can use a variety of methods for horizontally aligning text :
  155. text-align: center or margin: 0 auto) but it’s slightly trickier to vertically align text.
  156. However, for single-line text, we can use the line-height property.
  157. By setting the line-height property of the text element to the same height of its container, it will become vertically centered.
  158. Here is a p element:
  159. horizontally-centered inside a 100x100px div using text-align: center:
  160.  
  161. ==Vertically Align Text :
  162. As you can see, text-align doesn’t center it vertically.
  163. To fix that, we can set line-height to the same height as the containing div (100px).
  164.  
  165. div { width:100px; height:100px; }
  166. div p { line-height:100px; }
  167.  
  168. Note that this assumes the p element has no margin or padding.
  169. If you have margin or padding at the top or bottom of the p element,
  170. you need to compensate for them accordingly or just simply set padding and margin to 0 to make life easier.
  171.  
  172. Also, this trick becomes troublesome when there is more than one line of text (i.e. when the text wraps)
  173. because there will be a space between the text lines that is equivalent to the line-height value.
  174.  
  175. ============================
  176. ===========CENTER - Vertically Align Text : table-cell and vertical-align:middle
  177. We can make text center of the box by using this method display:table-cell and vertical-align:middle
  178.  
  179. .class {
  180. width:100px;
  181. height:100px;
  182. background-color:#999999;
  183. color:#000000;
  184. border:1px solid red;
  185. text-align:center; /*to make text horizontally center*/
  186. display:table-cell; /*this is the trick*/
  187. vertical-align:middle; /*to make text vertically center*/
  188. }
  189.  
  190. ============================
  191. ===========CENTER - WITH Vertical-align: (Tableau + inline-block)
  192. Appliquée à un élément en display: inline-block
  193. (normalement Réservée aux éléments de contenu ou aux cellules de tableaux)
  194. va aligner cet élément au sein de son parent, comme s’il s’agissait d’un texte,
  195. via un large assortiment de possibilités :
  196.  
  197. ===Valeurs possible pour la propriété vertical-align :
  198.  
  199. - baseline
  200. Aligne la base de l’élément avec celle de son parent (valeur par défaut).
  201. - bottom
  202. Aligne le bas de l’élément avec le bas du parent.
  203. - text-bottom
  204. Aligne le bas de l’élément avec le bas de la police de l’élément parent.
  205. - top
  206. Aligne le haut de l’élément avec le haut du parent.
  207. - text-top
  208. Aligne le haut de l’élément avec le haut de la police de l’élément parent.
  209. - sub
  210. Aligne la base de l’élément avec la ligne de base indice de son parent,
  211. tel l’élément HTML <sub>.
  212. - super
  213. Aligne la base de l’élément avec la ligne de base exposant de son parent,
  214. tel l’élément HTML <sup>.
  215. - middle
  216. Aligne le milieu de l’élément avec le milieu de son parent.
  217. - <longueur>
  218. Aligne la base de l’élément à la longueur donnée au-dessus de la ligne de base de son parent.
  219. - <pourcentage>
  220. Idem à la valeur <longueur>, où le pourcentage est
  221. calculé par rapport à la propriété line-height.
  222.  
  223. ========================================
  224. ======== FLOAT CENTER : Faking ‘float: center’ with Pseudo Elements ( :before )
  225. ==http://css-tricks.com/float-center/
  226. This is a little bit like float, as the text wraps around the image,
  227. but the text wraps around both directions, so it would be a bit like float: center; or float: both; which don't exist.
  228. Using floated pseudo element placeholders.
  229. We'll put one in each column of text, one floated left, one floated right.
  230. The pseudo element should be the height of the image, and half of the width
  231. (or so... remember you'll want some padding and there is the gutter to factor in).
  232.  
  233. #l:before,
  234. #r:before {
  235. content: "";
  236. width: 125px;
  237. height: 250px;
  238. }
  239. #l:before {
  240. float: right;
  241. }
  242. #r:before {
  243. float: left;
  244. }
  245.  
  246. Now there is a hole in the text ready to place our image there.
  247. We could do that by absolute positioning it there, as in the demo.
  248. Or you could just leave it in an element above, centered,
  249. and use negative top margin on the columns to pull the text up around.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement