I am struggling to understand how CSS lays out elements on a page. This is a sketch of the layout I am trying to achieve:
And inside canvas-column-1
I want to add a canvas element i.e it will be a child of canvas-column-1
. However when I do this I find that this moves the position of canvas-column-2
which is sibling of canvas-column-1
so I anticipated it would not be affected by any child elements of its siblings:
CSS:
:root {
--header-height: 100px;
--footer-height: 20px;
--header-background: cornflowerblue;
--column-1-width: 200px;
}
.header {
height: var(--header-height);
min-height: var(--header-height);
max-height: var(--header-height);
background-color: var(--header-background);
}
.main-content {
min-height: calc(100vh - var(--header-height) - var(--footer-height));
max-height: calc(100vh - var(--header-height) - var(--footer-height));
min-width: 100vw;
max-width: 100vh;
width: 100vw;
position: absolute;
top: var(--header-height);
left: 0;
right: 0;
}
.footer {
min-height: var(--footer-height);
max-height: var(--footer-height);
background-color: var(--header-background);
position: absolute;
top: calc(100vh - var(--footer-height));
left: 0;
right: 0;
}
.column-1 {
min-width: var(--column-1-width);
max-width: var(--column-1-width);
width: var(--column-1-width);
min-height: calc(100vh - var(--header-height) - var(--footer-height));
max-height: calc(100vh - var(--header-height) - var(--footer-height));
background-color: indianred;
/* display: none; hidden and does NOT take up space. */
/* -OR USE- */
/* visibility: hidden; Hidden but still takes up space. */
display: inline-block;
position: absolute;
top: 0;
left: 0;
}
.column-2 {
min-width: calc(100vw - var(--column-1-width));
max-width: calc(100vw - var(--column-1-width));
width: calc(100vw - var(--column-1-width));
min-height: calc(100vh - var(--header-height) - var(--footer-height));
max-height: calc(100vh - var(--header-height) - var(--footer-height));
background-color: blueviolet;
display: inline-block;
position: absolute;
top: 0;
left: var(--column-1-width);
}
.canvas-column-1 {
display: inline-block;
min-width: 50%;
max-width: 50%;
width: 50%;
min-height: calc(100vh - var(--header-height) - var(--footer-height));
max-height: calc(100vh - var(--header-height) - var(--footer-height));
height: calc(100vh - var(--header-height) - var(--footer-height));
background-color: antiquewhite;
}
.canvas-column-2 {
display: inline-block;
min-width: 50%;
max-width: 50%;
width: 50%;
min-height: calc(100vh - var(--header-height) - var(--footer-height));
max-height: calc(100vh - var(--header-height) - var(--footer-height));
height: calc(100vh - var(--header-height) - var(--footer-height));
background-color: darkgray;
}
.left-canvas {
width: 200px;
height: 200px;
border: 4px dashed darkblue;
}
HTML
<div>
<div class="header" >This is the header</div>
<div class="main-content">
<div class="column-1"></div>
<div class="column-2">
<div class="canvas-column-1">Canvas Column 1
<canvas #leftCanvas class="left-canvas" height="200px" width="200px"></canvas>
</div>
<div class="canvas-column-2">Canvas Column 2</div>
</div>
</div>
<div class="footer" ></div>
</div>
I have used a lot of absolute
positioning in mainly because I found that when it came to absolute positioning of canvas-column-1
and canvas-column-2
they would be relative to they also needed a non-static parent element otherwise they would not be positioned or sized correctly. Please note this example is for my understanding of how CSS positions and lays out elements.
This is a jsfiddle of the example but that renders something completely different again