Design a page consisting of three multi-colored columns. The left column is 100 pixels wide, the center and right columns occupy the remaining space to the edge of the page evenly. The height of all three 100% of the page. There should be no scrollbar and white stripes around the page.
<body> <div class="one"></div> <div class="two"></div> <div class="three"></div> </body>
html, body { width: 100%; height: 100%; margin: 0; display: flex; } .one { width: 100px; height: 100%; background: red; } .two { width: calc(50% - 50px); height: 100%; background: green; } .three { width: calc(50% - 50px); height: 100%; background: blue; }
body
styling. At this stage, students immersed in the study of the naming convention in BEM and the principles of DRY, DIE, KISS, SOLID, YAGNI .body
, since there was no single answer on the network from the first time. This solution came from personal practice: initially, in one of the projects, the following was asked: body { max-width: 1024px; }
.red { background: red; } .green { background: green; } .blue { background: blue; }
<table class="blocks"> <td class="blocks--block__fixed-width red"></td> <td class="blocks--block green"></td> <td class="blocks--block blue"></td> </table>
html, body { margin: 0; width: 100%; } .blocks { height: 100vh; width: 100%; border-collapse: collapse; } .blocks--block__fixed-width { width: 100px; } .blocks--block { width: calc(50% - 50px); }
<div class="blocks"> <div class="blocks--block__fixed-width red"></div> <div class="blocks--wrapper"> <div class="blocks--block green"></div> <div class="blocks--block blue"></div> </div> </div>
html, body { margin: 0; } .blocks, .blocks--wrapper { display: flex; height: 100vh; } .blocks--wrapper { width: calc(100% - 100px); } .blocks--block { width: 50%; } .blocks--block__fixed-width { width: 100px; }
:first-child
and :not(:first-child)
to select the first and all_crome_first elements, respectively, having class="block"
. Secondly, more features of the Flexbox Layout are used , in particular the flex
child properties, which combine the flex-grow, flex-shrink flex-basis
properties and thus determine the relative sizes of the elements. <div class="blocks"> <div class="block red"></div> <div class="block green"></div> <div class="block blue"></div> </div>
html, body { margin: 0; } .blocks { display: flex; height: 100vh; } .block:first-child { flex: 0 0 100px; } .block:not(:first-child) { flex: 1 1 50%; }
map
function to create them, then this method of styling will make it much easier. Consider this case using the following code: <div id="root"></div>
const colours = ['red','blue','green']; const blocks = colours.map(colour => <div className={"block " + colour}></div> ); ReactDOM.render( <div className="blocks">{blocks}</div>, document.getElementById('root') );
#root, html, body { margin: 0; } .blocks { display: flex; height: 100vh; width: 100%; } .block:first-child { flex: 0 0 100px; } .block:not(:first-child) { flex: 1 1 50%; }
<div class="container"></div>
html, body { margin: 0; } .container { width: 100%; height: 100vh; background: linear-gradient( to right, red 100px, green 100px, green calc(50% + 50px), blue calc(50% + 50px) ); }
background
property is used, which sets the gradient color change from left to right. For a clear border, each subsequent color begins with the stop point of the previous color. <div class="blocks"> <div class="red"></div> <div class="green"></div> <div class="blue"></div> </div>
html, body { margin: 0; } .blocks { display: grid; height: 100vh; grid-template-columns: 100px 1fr 1fr; }
display:table
property, which allows you to set table behavior for any element. <div class="blocks"> <div class="block fixed-width red"></div> <div class="block green"></div> <div class="block blue"></div> </div>
html, body { margin: 0; } .blocks { display: table; width: 100%; height: 100vh; table-layout: fixed; } .block { display: table-cell; width: 50%; } .block.fixed-width { width: 100px; }
display:block
proposed by nebsehemvi in the comment . Such a need may arise, for example, to ensure backward compatibility, since there are still widely used browsers that do not support Flexbox and Grid . <div> <div class="block fixed-width red"></div> <div class="block green"></div> <div class="block blue"></div> </div>
html, body { margin: 0; } .block { display: block; float: left; height: 100vh; width: calc((100% - 100px)/2); } .fixed-width { max-width: 100px; }
calc
function so that it can fly even on CCS2. <div class="blocks"> <div class="block fixed-width red"></div> <div class="block green"></div> <div class="block blue"></div> </div>
body, html { margin: 0; } .blocks { margin-left: 100px; } .block.fixed-width { width: 100px; margin-left: -100px; } .block { width: 50%; height: 100vh; float: left; }
Source: https://habr.com/ru/post/341262/
All Articles