`I don’t know how to position these elements. I’ve written the HTML, but I can’t position the elements in the CSS file
HTML:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Блочная верстка</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
div {
width: 500px;
height: 100px;
}
.orange {
background-color: orange;
}
.blue {
background-color: blue;
}
.pink {
background-color: lightpink;
}
.yellow {
background-color: yellow;
}
.green {
background-color: yellowgreen;
}
.purple {
background-color: purple;
}
.gray {
background-color: gray;
}
.red {
background-color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="orange"></div>
<div class="blue"></div>
<div class="pink"></div>
<div class="yellow"></div>
<div class="gray"></div>
<div class="purple"></div>
<div class="green"></div>
<div class="red"></div>
</div>
</body>
</html>
[this elements](https://i.stack.imgur.com/HpixF.png)
i'm tried this csss file:
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
height: 100%;
}
div {
position: absolute;
}
.purple {
background-color: purple;
width: 221px;
height: 715px;
top: 0;
left: 0;
}
.orange {
background-color: orange;
width: 700px;
height: 220px;
top: 0;
left: 221px;
}
.blue {
background-color: blue;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.pink {
background-color: lightpink;
width: 100px;
height: 100px;
bottom: 0;
left: 0;
}
.yellow {
background-color: yellow;
width: 100px;
height: 100px;
bottom: 0;
right: 0;
}
.gray {
background-color: gray;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.green {
background-color: yellowgreen;
width: 100px;
height: 100px;
top: 70%;
left: 70%;
}
.red {
background-color: red;
width: 100px;
height: 100px;
top: 30%;
left: 30%;
}
``
2
Answers
You can use display grid in css to position ur div correctly like in the example
HTML:
You can use Flexbox and Grid layout interchangeably to achive this.
Just make sure you use
flex: 1
appropriately. Also, if something isdisplay: flex
, all its parents and ancestors need to bedisplay: flex
or it won’t work.