Circle appear gap at edge - html

I'm playing CSS battle and trying to achieve the result perfectly, but I don't know why there is some gap at the edge of the circle as you can see on the screenshot here: Screenshot
I know there is better solution like using gradient, but I'm trying to learn to solve the problem here and improve my understanding of CSS. Below is my code:
body {
background-color: #E3516E;
}
.Container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.Circle {
width: 199px;
height: 200px;
background-color: blue;
border-radius: 50%;
overflow: hidden;
position: relative;
}
.Square {
width: 99.5px;
height: 100px;
position: absolute;
}
.Green {
background-color: #51B5A9;
}
.Yellow {
background-color: #FADE8B;
right: 0;
}
.White {
background-color: #F7F3D7;
bottom: 0;
left: 0;
}
.Transparent {
background-color: #E3516E;
bottom: 0;
right: 0;
}
<div class="Container">
<div class="Circle">
<div class="Green Square"></div>
<div class="Yellow Square"></div>
<div class="White Square"></div>
<div class="Transparent Square"></div>
</div>
</div>

There is no gap at edges. Looks like Anti-Aliasing. I have added a border to it, so that you can see there's no gap.
<div class="Container">
<div class="Circle">
<div class="Green Square"></div>
<div class="Yellow Square"></div>
<div class="White Square"></div>
<div class="Transparent Square"></div>
</div>
</div>
<style>
body {
background-color: #E3516E;
}
.Container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.Circle {
width: 199px;
height: 200px;
border-radius: 50%;
overflow: hidden;
position: relative;
}
.Square {
width: 99.5px;
height: 100px;
position: absolute;
}
.Green {
background-color: #51B5A9;
}
.Yellow {
background-color: #FADE8B;
right: 0;
}
.White {
background-color: #F7F3D7;
bottom: 0;
left: 0;
}
.Transparent {
background-color: #E3516E;
border: 1px blue solid;
bottom: 0;
right: 0;
}
</style>

A conic gradient can do it:
.box {
width: 200px;
height: 200px;
border-radius: 50%;
background: conic-gradient(#FADE8B 25%, #0000 0 50%, #F7F3D7 0 75%, #51B5A9 0)
}
body {
background: #E3516E;
}
<div class="box"></div>

Related

How can I center this image inside of this div?

How can I center this image that I have in this div in a way that it won't move the 'line' div? I want the line to be touching the top of the square too.
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
Here is one way to prevent it from disrupting the flow layout of your container:
you can make the container a position of relative, and the image a position of absolute, positioned off the top and left by 50%, then transform it so that the center of the image is in the center position.
You could also just make the image a background-image of the div instead of using an image element, which may be easier to manipulate.
.black {
background: black;
}
.square {
position: relative;
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
</div>
I'm not sure I understand your exact desired end goal. But, if I understand correctly, you could create a flex parent to justify the image, and then position the line absolutely within that. See -
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
background-color: red;
position: absolute;
left: 0;
top: 0;
bottom: 0
}
<div class="square black">
<div class="line"></div>
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
</div>
You can just use these css for .square and .image
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
position: relative;
}
.image {
height: 60px;
width: 60px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You can easily center a image by using CSS position absolute. By making the position of square black class "absolute" and apply to properties "top: 45%;" and "left: 47%" . By applying this your problem will be definitely solve.
.black {
background: black;
}
.square {
display: flex;
align-item: center;
justify-content: center;
width: 200px;
height: 500px;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
</div>
</div>
.black {
background: black;
}
.square {
position: absolute;
top: 45%;
left: 47%;
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
position: absolute;
top:50%;
left:50%;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>

Vertical divs with divisions

I want to create a page on my site with X vertical divisions. These span from the top to the bottom of the page and take up, say 10vw.
This is fine, however what I am struggling with now is that INSIDE those vertical divs I want sections. Some of the vertical divs will have 1 section, some 2, and some three.
This is a fiddle of what I have so far
#topics_selection-container {
position: absolute;
top: 0;
left: 0;
right: 27px;
bottom: 20px;
overflow-x: auto;
overflow-y: none;
white-space: nowrap;
}
.topics_selection-level_container {
display: inline-block;
height: 99.5%;
width: 10vw;
margin: 0px -5px 0px 0px;
overflow: none;
}
.topics_selection-split_cell_1 {
background: green;
margin: 0px;
height: 100%;
}
.topics_selection-split_cell_2 {
background: gray;
margin: 0px;
height: 50%;
}
.topics_selection-split_cell_3 {
background: blue;
margin: 0px;
height: 50%;
}
.topics_selection-split_cell_4 {
background: magenta;
margin: 0px;
height: 33%;
}
.topics_selection-split_cell_5 {
background: orange;
margin: 0px;
height: 33%;
}
.topics_selection-split_cell_6 {
background: purple;
margin: 0px;
height: 33%;
}
.topics_selection-level_1 {
background: red;
}
.topics_selection-level_2 {
background: yellow;
}
<div id="topics_selection-container">
<div class="topics_selection-level_container topics_selection-level_1">
<div class="topics_selection-split_cell_1"></div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_2"></div>
<div class="topics_selection-split_cell_3"></div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_4"></div>
<div class="topics_selection-split_cell_5"></div>
<div class="topics_selection-split_cell_6"></div>
</div>
</div>
And as you SEE it works! Thats exactly what I want (except for the small space at the bottom of the three because of the 33%). However when I put content into those smaller divisions you get something different happening. The kind of wrap to the size of the text.
Can anybody suggest how to fix this? Positioning is CSS is not my forte!
Change overflow: none; into overflow: hidden; in .topics_selection-level_container. That will do the trick.
.topics_selection-level_container { display: inline-block; height: 99.5%; width: 10vw; margin: 0px -5px 0px 0px; overflow: hidden; }
https://jsfiddle.net/48tvezgv/4/
You could use flex for this then the level 2 divs can just grow to fit the column:
#topics_selection-container {
position: absolute;
top: 0;
left: 0;
right: 27px;
bottom: 20px;
overflow-x: auto;
overflow-y: none;
display: flex;
flex-direction: row; /* align level 1 children in columns */
flex-wrap: wrap;
}
.topics_selection-level_container {
height: 99.5%;
width: 10vw;
margin: 0px; /* not sure what your left margin was doing so removed it - you can add it back if you want */
overflow: none;
display: flex;
flex-direction: column; /* align level 2 children in rows within this column */
flex-wrap: nowrap;
}
.topics_selection-level_container > div {
flex:1; /* make level2 children grow to fill the column equally */
display:flex;
align-items: center; /* this is for vertical aligning */
justify-content: center; /* these 2 are for horizontal aligning */
text-align:center;
}
.topics_selection-split_cell_1 {
background: green;
}
.topics_selection-split_cell_2 {
background: gray;
}
.topics_selection-split_cell_3 {
background: blue;
}
.topics_selection-split_cell_4 {
background: magenta;
}
.topics_selection-split_cell_5 {
background: orange;
}
.topics_selection-split_cell_6 {
background: purple;
}
.topics_selection-level_1 {
background: red;
}
.topics_selection-level_2 {
background: yellow;
}
<div id="topics_selection-container">
<div class="topics_selection-level_container topics_selection-level_1">
<div class="topics_selection-split_cell_1">add</div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_2">stuff may be over multiple lines</div>
<div class="topics_selection-split_cell_3">stuff</div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_4">stuff that can wrap</div>
<div class="topics_selection-split_cell_5">this works</div>
<div class="topics_selection-split_cell_6">yeah!</div>
</div>
</div>
If you switch to floats, it'll work.
.topics_selection-level_container { float:left; height: 99.5%; width: 10vw; margin: 0; }
https://jsfiddle.net/48tvezgv/3/
#topics_selection-container { position: absolute; top: 0; left: 0; right: 27px; bottom: 20px; overflow-x: auto; overflow-y: hidden; }
.topics_selection-level_container { float:left; height: 99.5%; width: 10vw; margin: 0; }
.topics_selection-split_cell_1 { background: green; margin: 0px; height: 100%; }
.topics_selection-split_cell_2 { background: gray; margin: 0px; height: 50%; }
.topics_selection-split_cell_3 { background: blue; margin: 0px; height: 50%; }
.topics_selection-split_cell_4 { background: magenta; margin: 0px; height: calc(100% / 3); }
.topics_selection-split_cell_5 { background: orange; margin: 0px; height: calc(100% / 3); }
.topics_selection-split_cell_6 { background: purple; margin: 0px; height: calc(100% / 3); }
.topics_selection-level_1 { background: red; }
.topics_selection-level_2 { background: yellow; }
<div id="topics_selection-container">
<div class="topics_selection-level_container topics_selection-level_1">
<div class="topics_selection-split_cell_1">zxczxc</div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_2">asdas</div>
<div class="topics_selection-split_cell_3">qweqwe</div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_4">cvbcvb</div>
<div class="topics_selection-split_cell_5">urtyryr</div>
<div class="topics_selection-split_cell_6">hdhdfh</div>
</div>
</div>
Also "none" is not a valid value for overflow, I think you want to use hidden. And 33% is not precise enough, use calc (100% / 3)
Use height:33.333333% instead of 33% as 33*3=99...so your 1% is remaining...
You have applied display:inline-block to the outer containers which has by default vertical-align:baseline...
You have to change it to vertical-align:top
Updated Fiddle
Stack Snippet
#topics_selection-container {
position: absolute;
top: 0;
left: 0;
right: 27px;
bottom: 20px;
overflow-x: auto;
overflow-y: none;
white-space: nowrap;
}
.topics_selection-level_container {
display: inline-block;
height: 99.5%;
width: 10vw;
margin: 0px -5px 0px 0px;
overflow: none;
vertical-align: top;
}
.topics_selection-split_cell_1 {
background: green;
margin: 0px;
height: 100%;
}
.topics_selection-split_cell_2 {
background: gray;
margin: 0px;
height: 50%;
}
.topics_selection-split_cell_3 {
background: blue;
margin: 0px;
height: 50%;
}
.topics_selection-split_cell_4 {
background: magenta;
margin: 0px;
height: 33.333333%;
}
.topics_selection-split_cell_5 {
background: orange;
margin: 0px;
height: 33.333333%;
}
.topics_selection-split_cell_6 {
background: purple;
margin: 0px;
height: 33.333333%;
}
.topics_selection-level_1 {
background: red;
}
.topics_selection-level_2 {
background: yellow;
}
<div id="topics_selection-container">
<div class="topics_selection-level_container topics_selection-level_1">
<div class="topics_selection-split_cell_1"></div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_2"></div>
<div class="topics_selection-split_cell_3"></div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_4">ggg</div>
<div class="topics_selection-split_cell_5">ffff</div>
<div class="topics_selection-split_cell_6">dddd</div>
</div>
</div>
You just need to adjust the height a little you can give one 34% or give them all 33% with more decimal points.
EDIT: Added content to each inner div and align elements to top of parents.
#topics_selection-container { position: absolute; top: 0; left: 0; right: 27px; display:inline-block; bottom: 20px; overflow-x: auto; vertical-align:top; overflow-y: none; white-space: nowrap; }
.topics_selection-level_container { display: inline-block; height: 99.5%; width: 10vw; margin: 0px -5px 0px 0px; overflow: none; }
.topics_selection-split_cell_1 { background: green; margin: 0px; height: 100%; }
.topics_selection-split_cell_2 { background: gray; margin: 0px; height: 50%; }
.topics_selection-split_cell_3 { background: blue; margin: 0px; height: 50%; }
.topics_selection-split_cell_4 { background: magenta; margin: 0px; height: 34%; }
.topics_selection-split_cell_5 { background: orange; margin: 0px; height: 33%; }
.topics_selection-split_cell_6 { background: purple; margin: 0px; height: 33%; }
.topics_selection-level_1 { background: red; vertical-align:top; }
.topics_selection-level_2 { background: yellow; vertical-align:top; }
<div id="topics_selection-container">
<div class="topics_selection-level_container topics_selection-level_1">
<div class="topics_selection-split_cell_1">asdf</div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_2">asdf</div>
<div class="topics_selection-split_cell_3">asdf</div>
</div>
<div class="topics_selection-level_container topics_selection-level_2">
<div class="topics_selection-split_cell_4">asdf</div>
<div class="topics_selection-split_cell_5">asdf</div>
<div class="topics_selection-split_cell_6">asdf</div>
</div>
</div>

Getting CSS :hover on overlapping siblings

This is easiest to understand when running the code below. I'm looking to trigger the hover state on both a column and the middle row when hovering over the red bar.
I'd like to keep the columns based on flex and have the bar absolutely positioned over them.
Is this possible?
EDIT:
I'd like just the column that the mouse is hovering over to turn blue. Sorry for the ambiguity. Snippet updated with desired result.
The columns are divided by a white line. Hover over a grey area to see the column highlighted.
Thanks.
.root {
width: 100px;
height: 100px;
background: grey;
position: relative;
display: flex;
}
.column {
display: flex;
flex: 1 1 auto;
border-right: 1px solid white;
}
.column:hover {
background: blue;
}
.bar {
position: absolute;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
background: red;
}
.bar:hover {
background: green;
}
.green {
background: green;
}
.blue {
background: blue;
}
Hover over the middle of the square. I want the middle column to turn blue and the bar to turn green.
Right now, only the bar turns green.
<div class='root'>
<div class='column'>
</div>
<div class='column'>
</div>
<div class='column'>
</div>
<div class='bar'>
</div>
</div>
Desired result:
<div class='root'>
<div class='column'>
</div>
<div class='column blue'>
</div>
<div class='column'>
</div>
<div class='bar green'>
</div>
</div>
Final Edit:
I'm providing a fully fleshed out version of what my use case is. I don't think CSS will be able to solve this, but I've accepted an answer that works for my original question.
function enterColumn() {
document.getElementById('column-status').innerHTML = 'In column'
}
function leaveColumn() {
document.getElementById('column-status').innerHTML = 'Out of column'
}
function enterBar() {
document.getElementById('bar-status').innerHTML = 'In bar'
}
function leaveBar() {
document.getElementById('bar-status').innerHTML = 'Out of bar'
}
.root {
width: 100px;
height: 100px;
background: grey;
position: relative;
display: flex;
}
.column {
display: flex;
flex: 1 1 auto;
border-right: 1px solid white;
}
.column:hover {
background: blue;
}
.bar-container {
position: absolute;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
}
.bar {
position: absolute;
top: 0;
bottom: 0;
background: red;
}
.bar:hover {
background: green;
}
.green {
background: green;
}
.blue {
background: blue;
}
Hovering over a column or bar should be independent. Right now you can never have the 'In column' and 'In bar' status at the same time :(
<br />
It should scale to support any number of columns and any number of bars (where bars can be absolutely positioned anywhere along the x-axis)
<br />
Javascript events should be called on mouse events for both columns and bars.
<div class='root'>
<div class='column' onmouseenter='enterColumn();' onmouseleave='leaveColumn()'>
</div>
<div class='column' onmouseenter='enterColumn();' onmouseleave='leaveColumn()'>
</div>
<div class='column' onmouseenter='enterColumn();' onmouseleave='leaveColumn()'>
</div>
<div class='bar-container'>
<div class='bar' style='left: 5px; right: 40px' onmouseenter='enterBar();' onmouseleave='leaveBar()'>
</div>
<div class='bar' style='left: 65px; right: 5px' onmouseenter='enterBar();' onmouseleave='leaveBar()'>
</div>
</div>
</div>
<div id='column-status'>
Out of column
</div>
<div id='bar-status'>
Out of bar
</div>
There you go, after 2 hours of trial and error I finally came up with this little hack.
.root {
width: 100px;
height: 100px;
background: grey;
position: relative;
display: flex;
}
.column {
display: flex;
flex: 1 1 auto;
border-right: solid #fff 1px;
}
.column:hover {
background: blue;
}
.column .toggle{
margin-top:33px;
height: 33px;
width: 100%;
}
.column .toggle:before{
content: "";
position: absolute;
width: 34px;
height: 33px;
}
.column .toggle:hover:after{
content: "";
position: absolute;
z-index: 10;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
background: green;
pointer-events:none;
}
.bar {
position: absolute;
z-index: 1;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
background: red;
pointer-events:none;
}
<div class='root'>
<div class='column'><div class='toggle'></div></div>
<div class='column'><div class='toggle'></div></div>
<div class='column'><div class='toggle'></div></div>
<div class='bar'></div>
</div>
Now if you need to bind some javascript events to the .bar element, attach them to .toggle instead.
If rearrangement of divs is allowed, you can position the .bar just before the middle .column and use adjacent sibling selector.
.bar:hover + .column {
background: blue;
}
.root {
width: 100px;
height: 100px;
background: grey;
position: relative;
display: flex;
}
.column {
display: flex;
flex: 1 1 auto;
border-right: 1px solid white;
}
.column:hover {
background: blue;
}
.bar {
position: absolute;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
background: red;
}
.bar:hover {
background: green;
}
.bar:hover + .column {
background: blue;
}
.green {
background: green;
}
.blue {
background: blue;
}
<div class='root'>
<div class='column'>
</div>
<div class='bar'>
</div>
<div class='column'>
</div>
<div class='column'>
</div>
</div>
If I understand what you mean, you mean that if you hover over any element the .column should turn blue and .bar should turn green. If that's the case then actually its pretty simple. Just place your hover event on .root element instead like so:
.root {
width: 100px;
height: 100px;
background: grey;
position: relative;
display: flex;
}
.column {
display: flex;
flex: 1 1 auto;
border-right: 1px solid white;
}
.bar {
position: absolute;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
background: red;
}
.root:hover .bar {
background: green;
}
.root:hover .column {
background: blue;
}
<div class='root'>
<div class='column'>
</div>
<div class='column'>
</div>
<div class='column'>
</div>
<div class='bar'>
</div>
</div>
If that' not the case and you want the color of the .column to change when you hover over the .bar then check out the snippet below. Note that I've a changed the HTML markup a bit. Since .bar has position: absolute so it won't affect at all where you place it inside the .root element.
.root {
width: 100px;
height: 100px;
background: grey;
position: relative;
display: flex;
}
.column {
display: flex;
flex: 1 1 auto;
border-right: 1px solid white;
}
.bar {
position: absolute;
left: 0;
right: 0;
top: 33px;
bottom: 33px;
background: red;
}
.bar:hover {
background: green;
}
.bar:hover ~ .column {
background: blue;
}
<div class='root'>
<div class='bar'>
</div>
<div class='column'>
</div>
<div class='column'>
</div>
<div class='column'>
</div>
</div>
Let me know if that helps you :-)

Add rounded borders to selected corners of an element

How could I go about constructing something like this with pure CSS?
This is how far I've gotten so far: Fiddle
I'm struggling with how to get that rounded corner there, even if I continue to add additional spans.
CODE:
body {
background: #000;
}
.container {
position: relative;
width: 300px;
height: 150px;
margin: 10% auto;
}
.top-right {
position: absolute;
top: -10px;
right: 0;
width: 50px;
height: 1px;
background: white;
border-radius: 5px;
}
.box {
width: 100%;
height: 100%;
background: red;
border-radius: 15px;
display: flex;
align-items: center;
justify-content: center;
}
h3 {
color: white;
}
<div class="container">
<span class="top-right"></span>
<div class="box">
<h3>Content</h3>
</div>
</div>
you can achieve that by using pseudo elements ::before/::after in .box using the properties border and border-radius
body {
background: #000;
}
.container {
width: 300px;
height: 150px;
margin: 3% auto 0 /* changed for demo */
}
h3 {
color: white;
}
.box {
width: 100%;
height: 100%;
background: red;
border-radius: 15px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.box::before,
.box::after {
content: "";
position: absolute;
border: solid white;
width: 50px;
height: 50px;
}
.box::before {
top: -15px;
left: -15px;
border-radius: 15px 0; /* top-left */
border-width: 5px 0 0 5px;
}
.box::after {
bottom: -15px;
right: -15px;
border-radius: 0 0 15px; /* bottom-right */
border-width: 0 5px 5px 0;
}
<div class="container">
<div class="box">
<h3>Content</h3>
</div>
</div>
Using pseudo-elements would be the ideal solution.
This answer is just an alternative. Although not semantically elegant, it's crudely effective.
Create a container with four divs.
The first div will be the white border.
The last div will be your red box.
The two divs in the middle will be used to conceal areas of the white border.
The HTML is quite simple:
<div class="container">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4">
<h3>Content</h3>
</div>
</div>
With absolute positioning, .box2 (green) and .box3 (blue) can be moved to cover the border.
The order of the boxes in the source doesn't really matter. But with the HTML above there is no need for the z-index property.
Now, the only thing left is to change the background color of boxes 2 and 3 to black.
Full code:
body {
margin: 0;
height: 100vh;
background-color: black;
display: flex;
}
.container {
position: relative;
width: 300px;
height: 150px;
margin: auto;
}
.box {
position: absolute;
width: 300px;
height: 150px;
border-radius: 15px;
}
.box1 {
border: 5px solid white;
width: 320px;
height: 170px;
top: -14px;
left: -15px;
}
.box2 {
background-color: black;
top: -30px;
left: 30px;
}
.box3 {
background-color: black;
top: 30px;
left: -30px;
}
.box4 {
background-color: red;
border-radius: 15px;
display: flex;
align-items: center;
justify-content: center;
}
<div class="container">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4">
<h3>Content</h3>
</div>
</div>

Positioning the content of a div on another div

I don't understand why the float: right doesn't work on the other box.
Anyone who can help me about this?
This is my code:
.main-box {
height: 400px;
width: 400px;
position: relative;
background: black;
}
.right-box {
float: right;
width: 100px;
height: 100px;
background: red;
}
.left-box {
width: 100px;
height: 100px;
background: red;
}
.bottom-boxes {
position: absolute;
bottom: 0;
}
<div class="main-box">
<div class="top-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
<div class="bottom-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
</div>
This is the resulting image of my code:
This is the resulting image I want to achieve:
Because of position: absolute on bottom-boxes so you need to add width: 100%
.main-box {
height: 400px;
width: 400px;
position: relative;
background: black;
}
.right-box {
float: right;
width: 100px;
height: 100px;
background: red;
}
.left-box {
width: 100px;
height: 100px;
background: red;
}
.bottom-boxes {
position: absolute;
bottom: 0;
width: 100%;
}
<div class="main-box">
<div class="top-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
<div class="bottom-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
</div>
But here is better solution using flexbox
.main-box {
height: 200px;
width: 200px;
display: flex;
flex-direction: column;
justify-content: space-between;
background: black;
}
.row {
display: flex;
justify-content: space-between;
}
.box {
width: 50px;
height: 50px;
background: red;
}
<div class="main-box">
<div class="row">
<div class="box"></div>
<div class="box"></div>
</div>
<div class="row">
<div class="box"></div>
<div class="box"></div>
</div>
</div>
Here's a working fiddle
When you put absolute position on a container, you have to specify also top, right and left property with bottom property to set a width and a height of it.
.bottom-boxes{
position:absolute;
bottom: 0;
left: 0;
right: 0;
}
In this case, left: 0; and right: 0; are equivalent to width: 100%; and top: 0 and bottom: 0; are equivalent to height: 100%;
When you don't specify a value, by default it's "auto;"
float won't work on an absolutely positioned element - you need to give top or bottom and right or left parameters to it (the default setting is top: 0; and left: 0;, i.e. the upper left corner of the parent element).