I am trying to create a sort of circle and trying to embed a inner circle but the inner circle is not positioning as required in the image.
.outer-circle {
width: 200px;
height: 200px;
background-color: coral;
border-radius: 50% 50% 0 0;
transform: rotate(90deg);
}
.inner-circle {
width: 100px;
height: 100px;
margin-left: 10px;
margin-top: 10px;
background-color: blue;
border-radius: 50%;
}
<div class="outer-circle">
<div class="inner-circle"></div>
</div>
How can I position my inner circle as required without writing any Javascript.
Following works:
.outer-circle {
width: 200px;
height: 200px;
background-color: coral;
border-radius: 50% 50% 0 0;
transform: rotate(90deg);
padding-top: 15px;
}
.inner-circle {
width: 160px;
height: 160px;
margin-left: 20px;
margin-top: 10px;
background-color: blue;
border-radius: 50%;
}
<div class="outer-circle">
<div class="inner-circle"></div>
</div>
One of the solutions would be to use absolute positioning:
.outer-circle {
width: 200px;
height: 200px;
background-color: coral;
border-radius: 50% 50% 0 0;
transform: rotate(90deg);
position: relative;
}
.inner-circle {
width: 100px;
height: 100px;
background-color: blue;
border-radius: 50%;
position: absolute;
top: 50%;
right: 50%;
transform: translate(50%, -50%);
}
<div class="outer-circle">
<div class="inner-circle"></div>
</div>
.outer-circle {
width: 100px;
height: 100px;
background-color: coral;
border-radius: 50% 50% 0 0;
transform: rotate(90deg);
padding: 10px;
}
.inner-circle {
width: 100px;
height: 100px;
background-color: blue;
border-radius: 50%;
}
<div class="outer-circle">
<div class="inner-circle"></div>
</div>
I've removed the margin and added a padding to the .outer-circle. That's gotta be the easiest way for now. Basically you make height the same on both circles, adding the padding to the outer-circle creates the gap between the two, the bigger the larger it will get.
Additional info:
margin(https://developer.mozilla.org/en-US/docs/Web/CSS/margin)
position(https://developer.mozilla.org/en-US/docs/Web/CSS/position)
.outer-circle {
width: 200px;
height: 200px;
background-color: coral;
border-radius: 50% 50% 0 0;
transform: rotate(90deg);
}
.inner-circle {
position:absolute;
width: 100px;
height: 100px;
margin: 50px;
background-color: blue;
border-radius: 50%;
}
<div class="outer-circle">
<div class="inner-circle"></div>
</div>
Use transform:translate
.outer-circle {
width: 200px;
height: 200px;
background-color: coral;
border-radius: 50% 50% 0 0;
transform: rotate(90deg);
}
.inner-circle {
width: 100px;
height: 100px;
margin-left: 50px;
margin-top: 50px;
background-color: blue;
border-radius: 50%;
transform: translate(0px,50px);
}
<div class="outer-circle">
<div class="inner-circle"></div>
</div>
Related
I am learning React JS and also brushing up on some CSS styling. I have a small app that is basically a drum machine that I wanted to style to look as a launchpad using 3D transformations and CSS. The algorithm itself works, so I made the body of the launchpad, but I also wanted to make 3D buttons for it which would visually sink in when interacted with (either on click or key down). But I can't figure out how to make the buttons into cuboids that go on top of the main cuboid (the drumpad). They just remain flat in the top plane of the launchpad cuboid.
Codesandbox
Relevant JSX code:
The launchpad itself:
return (
<div className="drum-container">
<div className="drum-body">
<div id="drum-machine" className="side top">
<div className="drum-header">
<span>Beat Pro X</span>
<span>{clipName}</span>
</div>
<div id="display">
{keys.map((element, index) => {
return (
<Drumpad
key={element}
clip={clips[index]}
pressKey={element}
onClick={handleDrumpadClick}
onKeyDown={handleDrumpadClick}
/>
);
})}
</div>
</div>
<div className="side bottom"></div>
<div className="side left"></div>
<div className="side right"></div>
<div className="side front"></div>
<div className="side back"></div>
</div>
</div>
);
The Drumpad element:
return (
<div className="drumpad-button">
<div
ref={drumpadRef}
id={props.clip.name}
className="dpad-side dpad-top"
onClick={playAudio}
>
<span className="drum-pad-key">{props.pressKey}</span>
<audio ref={audioRef} className="clip" src={props.clip.source} />
</div>
<div className="dpad-side dpad-bottom"></div>
<div className="dpad-side dpad-left"></div>
<div className="dpad-side dpad-right"></div>
<div className="dpad-side dpad-front"></div>
<div className="dpad-side dpad-back"></div>
</div>
);
CSS:
* {
box-sizing: border-box;
}
body,
html {
margin: 0;
padding: 0;
background-color: darkgrey;
}
#root {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
.drum-container {
display: flex;
justify-content: center;
align-items: center;
border: 2px solid white;
border-radius: 4px;
height: 100%;
width: 100%;
perspective: 1500px;
perspective-origin: center;
}
.drum-body {
padding: 0;
margin: 0;
position: relative;
height: 700px;
width: 700px;
transform-style: preserve-3d;
transform: rotateX(-50deg);
}
.side {
border-radius: 15px;
position: absolute;
opacity: 0.9;
border: 2px solid white;
}
.front {
left: 100px;
top: 300px;
height: 100px;
width: 500px;
background-color: #d50000;
transform: translateZ(350px);
}
.back {
left: 100px;
top: 300px;
height: 100px;
width: 500px;
background-color: #aa00ff;
transform: translateZ(-350px);
}
.left {
top: 300px;
height: 100px;
width: 700px;
background-color: #304ffe;
transform: rotateY(90deg) translateZ(250px);
}
.right {
top: 300px;
height: 100px;
width: 700px;
background-color: #0091ea;
transform: rotateY(-90deg) translateZ(250px);
}
.top {
left: 100px;
height: 700px;
width: 500px;
background-color: #00bfa5;
transform: rotateX(90deg) translateZ(50px);
}
.bottom {
left: 100px;
height: 700px;
width: 500px;
background-color: #64dd17;
transform: rotateX(-90deg) translateZ(50px);
}
#display {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
row-gap: 20px;
column-gap: 20px;
padding: 0;
border: 0;
width: 80%;
margin: 200px auto 100px auto;
}
.drumpad-button {
border: 2px solid black;
position: relative;
padding: 0;
height: 120px;
width: 120px;
transform-style: preserve-3d;
transform: rotateX(30deg);
}
.dpad-side {
border-radius: 2px;
position: relative;
opacity: 0.9;
border: 2px solid white;
}
.dpad-front {
height: 20px;
width: 120px;
background-color: #d50000;
transform: translateZ(60px);
}
.dpad-back {
height: 20px;
width: 120px;
background-color: #aa00ff;
transform: translateZ(-60px);
}
.dpad-left {
height: 20px;
width: 120px;
background-color: #304ffe;
transform: rotateY(90deg) translateZ(60px);
}
.dpad-right {
height: 20px;
width: 120px;
background-color: #0091ea;
transform: rotateY(-90deg) translateZ(60px);
}
.dpad-top {
height: 120px;
width: 120px;
background-color: darkgreen;
transform: rotateX(90deg) translateZ(10px);
}
.dpad-bottom {
height: 120px;
width: 120px;
background-color: #64dd17;
transform: rotateX(-90deg) translateZ(10px);
}
Drumpads are in a grid, so I've tried setting the grid as the basis for the transform-style: preserve-3d, thinking that the grid elements as its children would then treat the top face of the launchpad as the base plane, but maybe there's something else I am doing wrong?
But from my understanding preserve-3d should be set on the element containing the elements that make up the figure in 3D space. When I change translateZ values I can see different planes popping up, but for some reason preserve-3d is ignored. Is it because of the grid? Or maybe specifics of the JSX nesting of imported components?
I'm working through the cssbattle.dev challenges to improve with CSS, and I'm stuck really close to the result on the 9 one. Can someone help me out?
The Target
My Code:
body {
background:#222730;
margin:0px;
width: 400px;
height: 300px;
}
.banner{
background: #4CAAB3;
width: 100%;
height: 150px;
margin-top: 75px;
}
.center-box{
position:relative;
top: -50px;
margin-left: 75px;
width: 150px;
height: 150px;
border: solid 50px #222730;
transform: rotate(45deg);
}
.dot{
position: absolute;
background: #393E46;
width: 50px;
height: 50px;
border-radius: 100%;
top: 125px;
left: 175px;
}
<body>
<div class="banner">
<div class="center-box"></div>
</div>
<div class="dot"></div>
</body>
try
.center-box{
position:relative;
top: -50px;
margin-left: 75px;
width: 150px;
height: 150px;
border: solid 50px #222730;
transform: rotate(45deg);
background: inherit ;
}
I am doing a this challenge on CSS Battle and get a very thin border around the rotated div object. Why is that? How can I get rid of it? When I submit it on the website it also scores only 98.somewhat %, so it's not just a rendering problem.
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<style>
body {
background: #222730;
margin: 0;
}
div {
position: absolute;
background: #4CAAB3;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#a {
width: 400px;
height: 150px;
}
#b {
z-index: 1;
border: solid 50px #222730;
width: 150px;
height: 150px;
transform: translate(-50%, -50%) rotate(45deg);
}
#c {
z-index: 2;
background: #393E46;
border-radius: 25px;
width: 50px;
height: 50px;
}
</style>
It's coming from the background property of #b (inherited from div).
Simply shift this property setting to be exclusive to #a:
body {
background: #222730;
margin: 0;
}
div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#a {
width: 400px;
height: 150px;
background: #4CAAB3;
}
#b {
z-index: 1;
border: solid 50px #222730;
width: 150px;
height: 150px;
transform: translate(-50%, -50%) rotate(45deg);
}
#c {
z-index: 2;
background: #393E46;
border-radius: 25px;
width: 50px;
height: 50px;
}
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
You can simplify your code like below:
html {
background:
linear-gradient(#4CAAB3 0 0)center/100% 150px repeat-x /* the bar below the rotate square */
#222730
}
body {
width: 150px;
height: 150px;
margin: 25px auto;
border: 50px solid #222730; /* the border */
background:
#4CAAB3 padding-box /* the main color */
radial-gradient(1px, #393E46 24px, #0000 25px); /* the circle */
transform: rotate(45deg);
}
Using CSS I'm trying to draw a black circle with a white circle centered within it. This is my HTML/CSS:
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
margin: 0 auto;
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
BUT as you can see (in Chrome and Firefox), the white circle is centered at the top of the white circle. I've tried various combinations of position:absolute and position:relative to no positive effect.
You can do with positions too, but easiest way is with flexbox:
#blackcircle {
background-color:black;
color:white;
width: 400px;
height: 400px;
border-radius:50%;
text-align:center;
margin: 0 auto;
display: flex;
align-items: center;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius:50%;
margin: 0 auto;
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
Since you know the sizes of the circles you can just position them with:
position:relative;
top: 155px;
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
margin: 0 auto;
position:relative;
top: 155px;
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
Here's another way using positioning and margins.
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
position:relative;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
position:absolute;
margin:auto;
top:0;
right:0;
bottom:0;
left:0;
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
Add position:relative; top:150px; to your whitecircle in css
Here is a working example, perfectly centered:
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
position:relative;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
margin: 0 auto;
position:absolute;
top:50%;
left:50%;
margin-top:-45px; /* half the height */
margin-left:-45px; /* half the width */
}
https://jsfiddle.net/zoxb3j3j/
Applying position:absolute to inner div and position:relative to the outer div.
HTML:
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
CSS:
#blackcircle {
background-color:black;
color:white;
width: 400px;
height: 400px;
border-radius:50%;
margin: 0 auto;
position: relative;
}
#whitecircle {
background-color: white;
color: black;
width: 100px;
height: 100px;
border-radius:50%;
top:150px;
left:150px;
position:absolute;
}
Fiddle.
Use "position:relative" for the black circle and "position:absolute" for the white circle.
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
position: relative;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
position: absolute;
left: 40%;
top: 40%;
}
This method of center element base by the position absolute and we set margin top is half of the height of the element and margin left will be half of the width .
replace the margin top , margin left with
transform: translate(-50%, -50%);
will make it dynamic thanks to #Magnus Buvarp
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
position: relative;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
margin-top: -40px;
margin-left: -40px;
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
You can use the absolute positioning on the white circle, plus a translation to make it fully centered depending on the size of the black circle. That way, you can freely change the size of the black circle.
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
position: relative;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
Hope this will help!
A quick solution would be to set position to relative, and set left and top to 50%, while setting the transform to translateX(-50%) translateY(-50%). Add prefixes to ensure wide compatibility.
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
position: relative;
top: 50%;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
While you know the width and height of #whitecircle then you can set it in absolute position, and relative position for it's parent. then give to #whitecircle left top 50% and subtract half of it's width height.
top: calc(50% - (90px / 2));
left: calc(50% - (90px / 2));
#blackcircle {
background-color: black;
color: white;
width: 400px;
height: 400px;
border-radius: 50%;
margin: 0 auto;
position: relative
}
#whitecircle {
background-color: white;
color: black;
width: 90px;
height: 90px;
border-radius: 50%;
margin: 0 auto;
position: absolute;
top:calc(50% - (90px / 2));
left:calc(50% - (90px / 2));
margin: 0 auto;
}
<div id="blackcircle">
<div id="whitecircle"></div>
</div>
You can create 3 divs with 2 css class for circle.
Here the css code for example:
.circlecenter {
margin:auto auto;
text-align:center;
}
.circle1 {
padding:10px;
border-radius:10000px;
width:200px;
height:200px;
background:#000;
}
.circle2 {
padding:10px;
border-radius:10000px;
width:50px;
height:50pxM;
background:#fff;
}
And then the div:
<div class="circlecenter"><div class="circle1"><div class="circle2"></div></div></div>
I am trying to rotate a div which is inside another div. whats wrong with my code.I come across with another method(:before child) but whats wrong with this methods? Thanks
body {
background: #ccc
}
.box {
width: 70%;
height: 200px;
background: #FFF;
margin: 40px auto;
}
.effect2 {
position: relative;
}
.box1 {
transform: rotate3d;
position: absolute;
width: 20%;
height: 20px;
background-color: aqua;
}
<div class="box effect2">
<div class="box1"></div>
</div>
body {
background: #ccc
}
.box {
width: 70%;
height: 200px;
background: #FFF;
margin: 40px auto;
}
.effect2 {
position: relative;
}
.box1{
transition: 1.5s;
position: absolute;
width: 20%;
height: 20px;
background-color: aqua;
}
.box1:hover{
transform: rotate3d(1,-1, 1,60deg);
}
<div class="box effect2">
<div class="box1"></div>
</div>
Give x,y or z to rotate and add the value
body {
background: #ccc
}
.box {
width: 70%;
height: 200px;
background: #FFF;
margin: 40px auto;
}
.effect2 {
position: relative;
}
.box1 {
transform: rotateZ(45deg);
position: absolute;
width: 20%;
height: 20px;
background-color: aqua;
}
<div class="box effect2">
<div class="box1"></div>
</div>
Here are some posible values
transform: rotate3d(1, 2.0, 3.0, 10deg)
transform: rotateX(10deg)
transform: rotateY(10deg)
transform: rotateZ(10deg)
SOURCE
rotate3d, where supported, needs parameters, example:
transform: rotate3d(1, 2.0, 3.0, 10deg)
body {
background: #ccc
}
.box {
width: 70%;
height: 200px;
background: #FFF;
margin: 40px auto;
}
.effect2 {
position: relative;
}
.box1 {
transform: rotate3d(1,2.0,3.0,90deg);
position: absolute;
width: 20%;
height: 20px;
background-color: aqua;
}
<div class="box effect2">
<div class="box1"></div>
</div>
You need to adapt to different browsers.
.class {
-webkit-transform:rotate(deg);
-moz-transform:rotate(deg);
-o-transform:rotate(deg);
transform:rotate(deg);
}