Difference between revisions of "User:RainSlide/CSS Boxes"

From PINE64
Jump to navigation Jump to search
(web dev tools)
 
(2 intermediate revisions by the same user not shown)
Line 16: Line 16:
and maybe you are maintaining a MediaWiki [[Template:Template|Template]],
and maybe you are maintaining a MediaWiki [[Template:Template|Template]],
with only inline styles.
with only inline styles.
'''You may want to view this page with Web developer tools opened.'''


<div style="position: fixed; top: 12.5vh; font-size: 25vh; line-height: 1; font-weight: bold; white-space: pre; color: lightblue; opacity: .1; user-select: none; pointer-events: none;">CSS<br>is<br>Awesome</div>
<div style="position: fixed; top: 12.5vh; font-size: 25vh; line-height: 1; font-weight: bold; white-space: pre; color: lightblue; opacity: .1; user-select: none; pointer-events: none;">CSS<br>is<br>Awesome</div>
Line 36: Line 38:


</div>
</div>
=== The border-collapse & padding problem ===
==== The border-collapse & padding problem ====


{| class="wikitable"
{| class="wikitable"
Line 55: Line 57:
|-
|-
| <div style="display: table; background-color: #FEE; border-collapse: separate;
| <div style="display: table; background-color: #FEE; border-collapse: separate;
margin: .5em 0; padding: .5em 1em; border: 1px solid;">The padding is back!</div>
margin: .5em 0; padding: .5em 1em; border: 1px solid;">The padding is back with <code>border-collapse: separate</code>!</div>
|}
|}


<div style="display: table; background-color: #FEE; border-collapse: separate;
<table style="border-collapse: collapse; margin: .5em 0; padding: .5em 1em; border: 1px solid; background-color: #EFE;">
margin: .5em 0; padding: .5em 1em; border: 1px solid;">
<tr>
<div style="display: table-cell; padding: .25em .5em; border: 1px solid;>But...</div>
<th colspan="2" style="padding: .25em .5em; border: 1px solid;">Here inside a <code>border-collapse: collapse</code> table...</th>
<div style="display: table-cell; padding: .25em .5em; border: 1px solid;>Maybe that's not what we want.</div>
</tr>
</div>
<tr>
<td style="padding: .25em .5em; border: 1px solid;">Every thing is fine.</td>
<td style="padding: .25em .5em; border: 1px solid;">Just fine</td>
</tr>
</table>
 
<table style="border-collapse: separate; margin: .5em 0; padding: .5em 1em; border: 1px solid; background-color: #FEE;">
<tr>
<th colspan="2" style="padding: .25em .5em; border: 1px solid;">But, here inside a <code>border-collapse: separate</code> table...</th>
</tr>
<tr>
<td style="padding: .25em .5em; border: 1px solid;">The separated border...</td>
<td style="padding: .25em .5em; border: 1px solid;">Maybe that's not what we want.</td>
</tr>
</table>


For those who are interested,
For those who are interested,

Latest revision as of 07:02, 21 January 2022

This page is mainly for showing why Template:Hint, Template:Note and Template:Warning uses display: table to archive compatibility for both adaptive width and floating sibling(s).

If a set of style is not compatible with adaptive width, it span to its full width even when it don't need to.

If a set of style is not compatible with floating sibling(s), the element may still work well with some short content in it; in another word, it may still looks fine when its content width + margin + padding + border is lower than the "remaining width" lefted by the floating sibling(s). So we have to use Lorem ipsum (as well as a floating box, for sure) to test it.

Ideally, you may just set the widths to fixed values, and pretend nothing happens and CSS is awesome. However, the content container of MediaWiki got adaptive width, and maybe you are maintaining a MediaWiki Template, with only inline styles.

You may want to view this page with Web developer tools opened.

CSS
is
Awesome

Table

display: table

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No problem for adaptive width.
No problem for floating siblings. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

The border-collapse & padding problem

This is a wikitable
No padding when it's in a wikitable.

This is beacuse .wikitable has border-collapse: collapse on it, and our box inherits it from .wikitable. border-collapse: collapse Disables padding on the table itself, allowing only padding on the table cells, to collapse the outer table borders.

To fix this padding problem in .wikitables, one must apply border-collapse: separate to the box. However, if one day, someone REALLY need to use the box as a CSS table, the border of its cell will be separated by default, which may affect the visual.

This is another wikitable
The padding is back with border-collapse: separate!
Here inside a border-collapse: collapse table...
Every thing is fine. Just fine
But, here inside a border-collapse: separate table...
The separated border... Maybe that's not what we want.

For those who are interested, to use the box or other elements with display: table as a CSS table and actually do something, or to work around with this padding problem with an additional div, see ungrid. But, don't use two elements when you can just use one element + one CSS rule to archive the exact same result.

Since I don't think the Hint, Note and Warning boxes should be put into a .wikitable, and it seems no one will use them as CSS tables any time sooner, I'm not favouring either of those two usage, and keep those templates minimal.

display: table-cell

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No problem for adaptive width.

Table cells are not blocky! Without this sentence, they are in the same row by now.

No problem for floating siblings. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Table cells have no margin.

display: table-caption

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No adaptive width.
It's hiding. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

There can only be one table caption per table, or, per anything.

display: table-row

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No problem for adaptive width?
No problem for floating siblings? Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Table rows have no margin, padding or border.

display: table-others

Other table display values works just like table rows, (table-row-group, table-header-group and table-footer-group), or won't even display its contents (table-column and table-column-group).

A new block formatting context

See guide Block formatting context on MDN.

float: left

Will everything go right with float set to left?

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No problem for adaptive width.
It falls off from its floating siblings. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

No, something falls down.

display: inline-block

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No problem for adaptive width.
But inline blocks are inline.
It also falls off from its floating siblings. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

display: flow-root

This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
This is a floating box.
No adaptive width.
No problem for floating siblings. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

And…

This is the end of part one. Part two will feature contain: content, width: fit-content, width: max-content, box-sizing: border-box; max-width: 100%, display: flex, display: grid, and more, if I ever got time to work on the part two.