You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

layout-csslayout.asciidoc 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ---
  2. title: CssLayout
  3. order: 12
  4. layout: page
  5. ---
  6. [[layout.csslayout]]
  7. = CssLayout
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/layout/css-layout"]
  11. endif::web[]
  12. [classname]#CssLayout# allows strong control over styling of the components
  13. contained inside the layout. The components are contained in a simple DOM
  14. structure consisting of [literal]#++<div>++# elements. By default, the contained
  15. components are laid out horizontally and wrap naturally when they reach the
  16. width of the layout, but you can control this and most other behaviour with CSS.
  17. You can also inject custom CSS for each contained component. As
  18. [classname]#CssLayout# has a very simple DOM structure and no dynamic rendering
  19. logic, relying purely on the built-in rendering logic of the browsers, it is the
  20. fastest of the layout components.
  21. The basic use of [classname]#CssLayout# is just like with any other layout
  22. component:
  23. [source, java]
  24. ----
  25. CssLayout layout = new CssLayout();
  26. // Component with a layout-managed caption and icon
  27. TextField tf = new TextField("A TextField");
  28. tf.setIcon(new ThemeResource("icons/user.png"));
  29. layout.addComponent(tf);
  30. // Labels are 100% wide by default so must unset width
  31. Label label = new Label("A Label");
  32. label.setWidth(Sizeable.SIZE_UNDEFINED, 0);
  33. layout.addComponent(label);
  34. layout.addComponent(new Button("A Button"));
  35. ----
  36. The result is shown in <<figure.layout.csslayout.basic>>. Notice that the
  37. default spacing and alignment of the layout is quite crude and CSS styling is
  38. nearly always needed.
  39. [[figure.layout.csslayout.basic]]
  40. .Basic Use of [classname]#CssLayout#
  41. image::img/csslayout-basic.png[width=60%, scaledwidth=100%]
  42. The [literal]#++display++# attribute of [classname]#CssLayout# is
  43. [literal]#++inline-block++# by default, so the components are laid out
  44. horizontally following another. [classname]#CssLayout# has 100% width by
  45. default. If the components reach the width of the layout, they are wrapped to
  46. the next "line" just as text would be. If you add a component with 100% width,
  47. it will take an entire line by wrapping before and after the component.
  48. [[layout.csslayout.injection]]
  49. == CSS Injection
  50. Overriding the [methodname]#getCss()# method allows injecting custom CSS for
  51. each component. The CSS returned by the method is inserted in the
  52. [parameter]#style# attribute of the [literal]#++<div>++# element of the
  53. component, so it will override any style definitions made in CSS files.
  54. [source, java]
  55. ----
  56. CssLayout layout = new CssLayout() {
  57. @Override
  58. protected String getCss(Component c) {
  59. if (c instanceof Label) {
  60. // Color the boxes with random colors
  61. int rgb = (int) (Math.random()*(1<<24));
  62. return "background: #" + Integer.toHexString(rgb);
  63. }
  64. return null;
  65. }
  66. };
  67. layout.setWidth("400px"); // Causes to wrap the contents
  68. // Add boxes of various sizes
  69. for (int i=0; i<40; i++) {
  70. Label box = new Label("&nbsp;", ContentMode.HTML);
  71. box.addStyleName("flowbox");
  72. box.setWidth((float) Math.random()*50,
  73. Sizeable.UNITS_PIXELS);
  74. box.setHeight((float) Math.random()*50,
  75. Sizeable.UNITS_PIXELS);
  76. layout.addComponent(box);
  77. }
  78. ----
  79. The style name added to the components allows making common styling in a CSS
  80. file:
  81. [source, css]
  82. ----
  83. .v-label-flowbox {
  84. border: thin black solid;
  85. }
  86. ----
  87. <<figure.layout.csslayout.getcss>> shows the rendered result.
  88. [[figure.layout.csslayout.getcss]]
  89. .Use of [methodname]#getCss()# and line wrap
  90. image::img/csslayout-getcss.png[width=60%, scaledwidth=100%]
  91. [[layout.csslayout.compatibility]]
  92. == Browser Compatibility
  93. The strength of the [classname]#CssLayout# is also its weakness. Much of the
  94. logic behind the other layout components is there to give nice default behavior
  95. and to handle the differences in different browsers. With [classname]#CssLayout#,
  96. you may need to make use of the browser-specific style classes in the root
  97. element of the application to write browser specific CSS rules.
  98. // TODO: ... described in <<advanced.browserinfo>>
  99. Some features in the other layouts are not even solvable in pure CSS, at least
  100. in all browsers.
  101. [[layout.csslayout.css]]
  102. == Styling with CSS
  103. [source, css]
  104. ----
  105. .v-csslayout {}
  106. .v-csslayout-margin {}
  107. .v-csslayout-container {}
  108. ----
  109. The [classname]#CssLayout# component has [literal]#++v-csslayout++# root style.
  110. The margin element with [literal]#++v-csslayout-margin++# style is always
  111. enabled. The components are contained in an element with
  112. [literal]#++v-csslayout-container++# style.
  113. For example, we could style the basic [classname]#CssLayout# example shown
  114. earlier as follows:
  115. [source, css]
  116. ----
  117. /* Have the caption right of the text box, bottom-aligned */
  118. .csslayoutexample .mylayout .v-csslayout-container {
  119. direction: rtl;
  120. line-height: 24px;
  121. vertical-align: bottom;
  122. }
  123. /* Have some space before and after the caption */
  124. .csslayoutexample .mylayout .v-csslayout-container .v-caption {
  125. padding-left: 3px;
  126. padding-right: 10px;
  127. }
  128. ----
  129. The example would now be rendered as shown in
  130. <<figure.layout.csslayout.styling>>.
  131. [[figure.layout.csslayout.styling]]
  132. .Styling [classname]#CssLayout#
  133. image::img/csslayout-styling.png[width=50%, scaledwidth=70%]
  134. Captions and icons that are managed by the layout are contained in an element
  135. with [literal]#++v-caption++# style. These caption elements are contained flat
  136. at the same level as the actual component elements. This may cause problems with
  137. wrapping in [literal]#++inline-block++# mode, as wrapping can occur between the
  138. caption and its corresponding component element just as well as between
  139. components. Such use case is therefore not feasible.