Using CSS create circle within a circle - html

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>

Related

How to center and overlap DIVs

I have 2 DIVs, that I want to center and overlap. The smaller one is to lay on top of the bigger one.
It works great at full-screen, but if I decrease the browser size, the top/smaller one moves to the left.
<div style="position: relative; top: 160px; border: thin solid gray; border-radius: 10px; width: 300px; height: 64px; margin-left: auto; margin-right: auto; z-index: 1; background: url(...); background-repeat:no-repeat; background-position:top; background-color: #4b2f84"> </div>
<div style="position: absolute; top: 200px; left: 15%; width: 70%; background: white; border: thin solid gray; border-radius: 10px; height: 500px; padding: 50px 30px; margin: auto">something
</div>
I like to use left: 50%; combined with transform: translateX(-50%); when trying to center and overlap content.
The content is offset 50% to the left, and then -50% of its width to the left.. or (this.left == parent.x + parent.width* 0.5 - this.width*0.5)
#div1 {
position: relative;
top: 160px;
border: thin solid gray;
border-radius: 10px;
width: 300px;
height: 64px;
margin-left: auto;
margin-right: auto;
z-index: 1;
background: url(...);
background-repeat: no-repeat;
background-position: top;
background-color: #4b2f84
}
#div2 {
position: absolute;
top: 200px;
left: 50%;
transform: translateX(-50%);
width: 70%;
background: white;
border: thin solid gray;
border-radius: 10px;
height: 500px;
padding: 50px 30px;
margin: auto
}
<div id="div1"> </div>
<div id="div2">something</div>

How can I set the element's inner position fixed to middle of the screen?

So how can I set an element's inner position fixed to middle of screen? I was able to set the x-axis of the div by using text-align: center;.
So how could I center the y-axis just like I did the x-axis? And if that can't be done, what new method must I use?
Code:
.fixed{
text-align: center;
width: 100%;
height: 100%;
padding: 0px;
position: fixed;
background-color: #00FF00;
top: 0px;
overflow: hidden;
z-index: 99;
}
.popup{
display: inline-block;
width: 90%;
max-width: 900px;
min-height: 300px;
border: 2px solid rgba(72, 72, 72, 0.4);
margin-bottom: 50px;
margin-top: 20px;
}
<div class="fixed">
<div class="popup"></div>
</div>
I updated your fiddle. Just make your popup class position fix and make top,left,bottom,right = 0 then margin:auto. See this updated fiddle.
.popup{
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
}
https://jsfiddle.net/norxpmbr/
For .popup, try replacing this:
margin-bottom: 50px;
margin-top: 20px;
With this:
position: relative;
top: 50%;
transform: translateY(-50%);
Source: http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/
So this is what I did to your code- removed the margins for popup and centered using transform and position: relative:
.popup{
display: inline-block;
width: 90%;
max-width: 900px;
min-height: 300px;
border: 2px solid rgba(72,72,72,0.4);
margin : 0;
position: relative;
top: 50%;
transform: translate(0, -50%);
}
revised fiddle
Snippet below:
.fixed {
text-align: center;
width: 100%;
padding: 0;
position: fixed;
background-color: #00FF00;
height: 100%;
top: 0;
overflow: hidden;
z-index: 99;
}
.popup {
display: inline-block;
width: 90%;
max-width: 900px;
min-height: 300px;
border: 2px solid rgba(72, 72, 72, 0.4);
margin: 0;
position: relative;
top: 50%;
transform: translate(0, -50%);
}
<div class="fixed">
<div class="popup">
</div>
</div>

Overlay divs without absolute positioning and resize distortion

I want to create a line with tick marks and a ball (like a scale).
However the tutorials for this suggest using absolute positioning, or float. That works partially, but when I change the screen size, the divs shift out of place.
.line {
width: 100%;
min-height: 5px;
background-color: black;
padding: 20px;
margin-top: 20%;
}
.point {
-moz-border-radius: 50px/50px;
-webkit-border-radius: 50px 50px;
border-radius: 50px/50px;
border: solid 21px #f00;
width: 50px;
height: 50px;
background-color: red;
float: right;
overflow: visible;
position: relative;
z-index: 1000;
padding: 20px;
margin-top: -15%;
}
<div class="line"></div>
<div class="point"></div>
Wrap it in a div and do use absolute positioning for the inner divs, also, don't use margin-top percentages (https://jsfiddle.net/xv259d4p/1/):
.line {
width:100%;
min-height:5px;
background-color:black;
padding:20px;
margin-top:60px;
position: absolute;
}
.point {
-moz-border-radius: 50px/50px;
-webkit-border-radius: 50px 50px;
border-radius: 50px/50px;
border:solid 21px #f00;
width:50px;
height:50px;
background-color:red;
float: right;
overflow: visible;
position: absolute;
z-index: 1000;
padding:20px;
right: 0;
margin-top: 20px;
}
.outer {
display: block;
width: 100%;
top: 0px;
left: 0px;
position: relative;
}
<div class="outer">
<div class="line"> </div>
<div class="point"></div>
</div>

How to draw an inner div over an outer div using css?

I want to draw an inner div over an outer div for scrolling purposes. How can I change my CSS to fix this?
HTML code:
<div class="sliderPath">
<div class="slider"></div>
</div>
CSS code:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
}
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
}
https://jsfiddle.net/ImSonuGupta/0bx6uwyn/1/
Try setting top position to small value:
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
top: 3px; //added this
}
And also set position: relative on parent:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position: relative; //added this
}
updated jsfiddle
Simply make .sliderPath the base position for its childs, with position:relative, and make .slider top 100% off its parent height.
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position:relative;
}
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
top:100%
}
EDIT
if you need multiple slides, just add margin-bottom to .sliderPath equal to .slider height. So it would be:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position:relative;
margin-bottom:50px;
}

Element not coming to center. I am using absolute?

div #introbox is not centering. I have used container as relative and introbox as absolute. I have set top,bottom,left and right as 0. Still box is not centring. I want to centre the introbox in the intropic.
html,body{
padding: 0;
margin:0;
}
.container{
width: 960px;
margin:0 auto;
position: relative;
}
#header{
width: 100%;
height: 120px;
border-bottom: 1px solid black;
}
#nav{
height: 55px;
border-bottom: 4px solid lightblue ;
}
#intro-pic{
height: calc(100vh - 181px);
width: 100%;
background: url("img/introbg.jpg") center fixed;
}
#intro-box{
height: 55vh;
width: 800px;
background: rgba(0,0,0,0.74);
border-radius: 15px;
position: absolute;
top: 0px;
bottom: 0px;
right: 0px;
left:0px;
}
<div id="header">
<div class="container">
Header
</div>
</div>
<div id="nav">
<div class="container">
Nav
</div>
</div>
<div id="intro-pic">
<div class="container">
<div id="intro-box">
sdfdsfds
</div>
</div>
</div>
Using transform:translate will work for any size div.
html,
body {
padding: 0;
margin: 0;
height:100%;
}
.container {
width: 960px;
margin: 0 auto;
position: relative;
height:100vh;
}
#intro-box {
height: 55vh;
width: 800px;
background: rgba(0, 0, 0, 0.74);
border-radius: 15px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
/* vertical centering */
}
<div id="intro-pic">
<div class="container">
<div id="intro-box">
sdfdsfds
</div>
</div>
</div>
Find the below code.
Make left position 50% and give margin-left half of the wrapper width value.
#intro-box{
height: 55vh;
width: 800px;
background: rgba(0,0,0,0.74);
border-radius: 15px;
position: absolute;
top: 0px;
bottom: 0px;
left:50%;
margin-left: -400px; /* Half of the wrapper width */
}
Try below example if you are trying exact center (from top & left)
#intro-box{
height: 55vh;
width: 800px;
background: rgba(0,0,0,0.74);
border-radius: 15px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -400px; /* Half of the wrapper width */
margin-top: -27.5vh; /* Half of the wrapper height*/
}
JSFIDDLE DEMO
#intro-box {
height: 55vh;
width: 800px;
background: rgba(0,0,0,0.74);
border-radius: 15px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -400px;
margin-top: -27.5vh;
}
But again, .container should have height over or equal to #intro-box
There are many ways to center Elements:
using line-height:
you want to center text and you know the size of the box:
.box { background: red; height: 200px; }
.box span { display:block; text-align: center; line-height: 200px; }
<div class="box">
<span>Text</span>
</div>
using transform:
you want to center anything but dont know the size of your box:
.box, .box2 { background: red; height: 200px; }
.box span { top: 50%; text-align: center; position: relative; display: block; transform: translateY(-50%) }
.box2 span { top: 50%; left: 50%; position: relative; display: inline-block; transform: translate(-50%, -50%) }
<div class="box">
<span>Text</span>
</div>
OR WITHOUT TEXT-ALIGN:
<div class="box2">
<span>Text</span>
</div>
using absolute position:
you know the height of the element you want to center
.box, .box2 { background: red; height: 200px; position: relative; width: 100%; }
.box span { position: absolute; background: green; height: 50px; width: 50px; top: 50%; left: 50%; margin: -25px 0 0 -25px; }
<div class="box">
<span></span>
</div>
There are even more ways to manage this.