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.

WidgetStylingUsingOnlyCSS.asciidoc 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. title: Widget Styling Using Only CSS
  3. order: 65
  4. layout: page
  5. ---
  6. [[widget-styling-using-only-css]]
  7. = Widget styling using only CSS
  8. The preferred way of styling your widget is to only use static CSS
  9. included in the theme's styles.css file. For information on how to
  10. create custom themes, please refer to
  11. https://vaadin.com/book/-/page/themes.creating.html. Your component can
  12. be styled using the CSS class names that are defined to the widget using
  13. `setStyleName`.
  14. Normal styling of components works in the same way as any styling using
  15. CSS, but there are some special features to pay attention to when Vaadin
  16. 7 is used.
  17. [[some-sizing-theory]]
  18. Some sizing theory
  19. ~~~~~~~~~~~~~~~~~~
  20. All Vaadin 7 components get the CSS class v-widget which sets the
  21. box-sizing to border-box. This causes borders and paddings to be
  22. considered when the browser calculates the component's size. This means
  23. that e.g. a component with padding: 5px and width: 100% inside a 200px
  24. wide slot will fill the slot without any overflow because the inner
  25. width of the component will be calculated to 190px by the browser.
  26. The Vaadin 7 server side API allows setting the size of the component in
  27. three different ways:
  28. * Undefined size, set e.g. using `setWidth(null)`, means that the
  29. component's size should have it's default size that might vary depending
  30. on its content.
  31. * Relative size, set e.g. using `setWidth("100%")` means that the
  32. component's size is determined by the size and settings of the
  33. component's parent.
  34. * Fixed size, set e.g. using `setWidth("250px")` or `setWidth("10em")` means
  35. that the component's size is fixed, so that the parent doesn't affect
  36. the size and neither does the component's content.
  37. The three different ways of setting the size means that a component
  38. should both support having its own natural size and filling the
  39. allocated space depending on how the size is set. This usually means
  40. that the main area of the component should adjust based on the settings.
  41. [[a-simple-sample]]
  42. A simple sample
  43. ~~~~~~~~~~~~~~~
  44. Consider e.g. a simple date picker component with a text field where a
  45. date can be entered and where the currently selected is displayed and a
  46. button that is used to open a calendar view where a date can be picked
  47. using the mouse.
  48. [source,java]
  49. ....
  50. public class MyPickerWidget extends ComplexPanel {
  51. public static final String CLASSNAME = "mypicker";
  52. private final TextBox textBox = new TextBox();
  53. private final PushButton button = new PushButton("...");
  54. public MyPickerWidget() {
  55. setElement(Document.get().createDivElement());
  56. setStylePrimaryName(CLASSNAME);
  57. textBox.setStylePrimaryName(CLASSNAME + "-field");
  58. button.setStylePrimaryName(CLASSNAME + "-button");
  59. add(textBox, getElement());
  60. add(button, getElement());
  61. button.addClickHandler(new ClickHandler() {
  62. public void onClick(ClickEvent event) {
  63. Window.alert("Calendar picker not yet supported!");
  64. }
  65. });
  66. }
  67. }
  68. ....
  69. We then add this basic styling to the theme CSS file
  70. [source,scss]
  71. ....
  72. .mypicker {
  73. white-space: nowrap;
  74. }
  75. .mypicker-button {
  76. display: inline-block;
  77. border: 1px solid black;
  78. padding: 3px;
  79. width: 15px;
  80. text-align: center;
  81. }
  82. ....
  83. `display: inline-block` makes the button continue on the same line as the
  84. text field, placing it to the right of the field. We also add
  85. `white-space: nowrap` to the main div element to ensure the button is not
  86. wrapped to the next row. Finally, there is some padding and a border to
  87. make the button look more like a real button.
  88. [[using-available-space]]
  89. Using available space
  90. ^^^^^^^^^^^^^^^^^^^^^
  91. This simple layout works well as long as the component's has it's
  92. default undefined width. Changing the width from the server does however
  93. not have any visible effect because only the size of the main div is
  94. changed. If the component is made smaller, the contents goes beyond the
  95. size of the element (this can be verified by adding `overflow: hidden;` to
  96. `.mypicker`) and if it gets larger the extra space is just left as empty
  97. space to the right of the button.
  98. The first step towards making the size adjust is to make the text field
  99. adjust to the main div element's width whenever the width is something
  100. else then than undefined. In these situations, Vaadin 7 adds a
  101. `v-has-width` class to the component's main element (`v-has-height` added
  102. when the height is not undefined).
  103. [source,scss]
  104. ....
  105. .mypicker.v-has-width > .mypicker-field {
  106. width: 100%;
  107. }
  108. ....
  109. With this additional CSS, the text field directly inside a picker that
  110. has a defined width gets full width.
  111. [[making-room-for-the-button-again]]
  112. Making room for the button again
  113. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  114. We're however not done yet. Setting the width of the text field to 100%
  115. makes it as wide as the main div is, which means that the button goes
  116. outside the main div. This can be verified using the DOM inspector in
  117. your browser or by setting `overflow: hidden` to the style for `mypicker`.
  118. To reserve space for the button, we can add some padding to the right
  119. edge of the main div element. This does however cause the padding space
  120. to remain unused if the component size is undefined. To compensate for
  121. this, negative margin can be added to the right edge of the button,
  122. effectively reducing its occupied size to 0px.
  123. Finally, we need to use `box-sizing: border-box` to make the field's
  124. borders and paddings be included in the 100% width.
  125. The full CSS for the sample component:
  126. [source,scss]
  127. ....
  128. .mypicker {
  129. white-space: nowrap;
  130. padding-right: 23px;
  131. }
  132. .mypicker-button {
  133. display: inline-block;
  134. border: 1px solid black;
  135. padding: 3px;
  136. width: 15px;
  137. text-align: center;
  138. margin-right: -23px;
  139. }
  140. .mypicker.v-has-width > .mypicker-field {
  141. width: 100%;
  142. }
  143. .mypicker-field {
  144. -moz-box-sizing: border-box;
  145. -webkit-boz-sizing: border-box;
  146. box-sizing: border-box;
  147. }
  148. ....