div.container{
border:1px solid red;
position:relative;
}
span.parent{
display:inline-block;
width:30px;
height:30px;
border:1px solid gray;
border-radius:50%;
text-align: center;
position:absolute;
right:20px;
top:10px;
}
span.child{
background:green;
width:80%;
height:80%;
display:inline-block;
border-radius:50%;
left:10%;
top:10%;
position:relative;
}
<div class="container">
<p>some info goes here</p>
<span class="parent"><span class="child"></span></span>
</div>
I am trying to create a filled circle with border, but getting not the fill properly centered. how to fix this?
Another way to do this is by using Flexbox.
If you add the following to your parent:
display: flex;
align-items: center;
justify-content: center;
And remove the relative positioning from the child element, the child element will be centered inside the parent.
div.container{
border:1px solid red;
position:relative;
}
span.parent{
display: flex;
align-items: center;
justify-content: center;
width:30px;
height:30px;
border:1px solid gray;
border-radius:50%;
text-align: center;
position:absolute;
right:20px;
top:10px;
}
span.child{
background:green;
width:80%;
height:80%;
display:inline-block;
border-radius:50%;
}
<div class="container">
<p>some info goes here</p>
<span class="parent"><span class="child"></span></span>
</div>
Edit: If you encounter a problem with flexbox circles being squashed on smaller resolutions, try using min-height and min-width, and using margin: auto for centering instead of display: flex.
Give left:0; to span.child class.
div.container{
border:1px solid red;
position:relative;
}
span.parent{
display:inline-block;
width:30px;
height:30px;
border:1px solid gray;
border-radius:50%;
text-align: center;
position:absolute;
right:20px;
top:10px;
}
span.child{
background:green;
width:80%;
height:80%;
display:inline-block;
border-radius:50%;
left:0;
top:10%;
position:relative;
}
<div class="container">
<p>some info goes here</p>
<span class="parent"><span class="child"></span></span>
</div>
Just Change the position to position:absolute in place of position:relative for child span span.child.
div.container{
border:1px solid red;
position:relative;
}
span.parent{
display:inline-block;
width:30px;
height:30px;
border:1px solid gray;
border-radius:50%;
text-align: center;
position:absolute;
right:20px;
top:10px;
}
span.child{
background:green;
width:80%;
height:80%;
display:inline-block;
border-radius:50%;
left:10%;
top:10%;
align:center;
position:absolute;
}
<div class="container">
<p>some info goes here</p>
<span class="parent"><span class="child"></span></span>
</div>
You can use position: absolute in child instead to center it
span.child{
background:green;
width:80%;
height:80%;
display:inline-block;
border-radius:50%;
position:absolute;
left:50%;
top:50%;
transform: translate(-50%, -50%);
}
I also changed the value of left and top, and added transform:translate() to make sure it is always centered regardless of browser size
div.container {
border: 1px solid red;
position: relative;
}
span.parent {
display: inline-block;
width: 30px;
height: 30px;
border: 1px solid gray;
border-radius: 50%;
text-align: center;
position: absolute;
right: 20px;
top: 10px;
}
span.child {
background: green;
width: 80%;
height: 80%;
display: inline-block;
border-radius: 50%;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="container">
<p>some info goes here</p>
<span class="parent"><span class="child"></span></span>
</div>
Just Changed the position:relative to position:absolute of span.child
div.container{
border:1px solid red;
position:relative;
}
span.parent{
display:inline-block;
width:30px;
height:30px;
border:1px solid gray;
border-radius:50%;
text-align: center;
position:absolute;
right:20px;
top:10px;
}
span.child{
background:green;
width:80%;
height:80%;
display:inline-block;
border-radius:50%;
left:10%;
top:10%;
position:absolute;
}
<div class="container">
<p>some info goes here</p>
<span class="parent"><span class="child"></span></span>
</div>
Related
I have several divs, each 2px wide and they dont look the same (depending on their position). Although they are placed on full pixel, their width looks different. This is the problem in all browsers. Is this something that can be fixed?
.a{
position:relative;
width:300px;
height:10px;
background:#ccc;
}
.b1{
position:absolute;
left:50px;
width:2px;
height:100%;
background:blue;
}
.b2{
position:absolute;
left:100px;
width:2px;
height:100%;
background:blue;
}
.b3{
position:absolute;
left:150px;
width:2px;
height:100%;
background:blue;
}
<div class="a">
<div class="b1"></div>
<div class="b2"></div>
<div class="b3"></div>
</div>
If you mean that the first "bar" is 50px from the start and then the distance between the bars decreases by a little, it is because you do not include the width of the bar in the offset of the 2nd and 3rd "bar":
.a{
position:relative;
width:300px;
height:10px;
background:#ccc;
}
.b1{
position:absolute;
left:50px;
width:2px;
height:100%;
background:blue;
}
.b2{
position:absolute;
left:102px;
width:2px;
height:100%;
background:blue;
}
.b3{
position:absolute;
left:154px;
width:2px;
height:100%;
background:blue;
}
[class^="b"]:after {
content: '50px';
width: 50px; height: 2px;
position: absolute; bottom: -2px; left: -50px;
text-align: center;
font-size: 11px; font-family: sans-serif;
background: lightcoral;
}
<div class="a">
<div class="b1"></div>
<div class="b2"></div>
<div class="b3"></div>
</div>
*{
padding:0;
margin:0;
text-decoration:none;
}
#head{
width:400px;
height:40px;
border:1px solid red;
margin:0 auto;
line-height:40px;
font-size:20px;
text-align:center;
}
#main{
width:400px;
height:400px;
border:1px solid green;
margin:0 auto;
text-align:center;
vertical-align:middle;
}
<div id="head">set in middle and center</div>
<div id="main">
<img src="https://i.stack.imgur.com/9rdPN.png" alt="">
</div>
Both div head and div main was set as center by margin:0 auto,you can see.
I want to set the image in div main as middle vertically.
Just add display:table-cell in div main.
*{
padding:0;
margin:0;
text-decoration:none;
}
#head{
width:400px;
height:40px;
border:1px solid red;
margin:0 auto;
line-height:40px;
font-size:20px;
text-align:center;
}
#main{
width:400px;
height:400px;
border:1px solid green;
margin:0 auto;
text-align:center;
vertical-align:middle;
display:table-cell;
}
<div id="head">set in middle and center</div>
<div id="main">
<img src="https://i.stack.imgur.com/9rdPN.png" alt="">
</div>
Now the image was set as middle vertically,however div main was not setted as margin:0 auto,why display:table-cell make margin:0 auto take no effect?
You can use position: absolute; on the image with transform: translate(-50%, -50%); to center it
check this:
*{
padding:0;
margin:0;
text-decoration:none;
}
#head{
width:400px;
height:40px;
border:1px solid red;
margin:0 auto;
line-height:40px;
font-size:20px;
text-align:center;
}
#main{
position: relative;
width:400px;
height:400px;
border:1px solid green;
margin:0 auto;
text-align:center;
vertical-align:middle;
}
#main img {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div id="head">set in middle and center</div>
<div id="main">
<img src="https://i.stack.imgur.com/9rdPN.png" alt="">
</div>
Let's say I have
<div class="fixed">
<div class="abs"></div>
</div>
and css:
.fixed{
position: fixed;
top:100px;
width:100%;
height:300px;
border:1px solid turquoise;
overflow:scroll;
}
.abs{
position: absolute;
width:50px;
height:50px;
top:-50px;
left:0;
border:1px solid orange;
}
Right now .abs and .fixed has the same parent and I use javascript to position it above the fixed element, but I wonder if there is an other way.
So is it possible somehow to make .abs visible? Fiddle
I did not really get the idea why you have to do that.
BTW. You should have a fixed container. HTML:
<div class="container">
<div class="fixed"></div>
<div class="abs"></div>
</div>
CSS:
.fixed{
position: absolute;
width:100%;
height:300px;
border:1px solid turquoise;
overflow:scroll;
}
.abs{
position: absolute;
width:50px;
height:50px;
top:-30px;
left:0;
border:1px solid orange;
}
.container {
position: fixed;
width: 300px;
top: 40px; left: 40px;
}
https://jsfiddle.net/fujmw79t/3/
Simply remove the overflow:scroll; on .fixed:
.fixed{
position: fixed;
top:100px;
width:100%;
height:300px;
border:1px solid turquoise;
}
.abs{
position: absolute;
width:50px;
height:50px;
top:-53px;
left:-1px;
border:1px solid orange;
}
<div class="fixed">
<div class="abs"></div>
</div>
Hello I'm trying to align the "container2" div to bottom of "cointainer" but I'm having troubles and I dont know where, any help?
HTML
<div id="container">
<div id="container2">
<p>Text</p>
</div>
</div>
CSS
#container{
/*Colors*/
background-color:rgb(129, 159, 255);
/*Size Box*/
width:400px;
height:200px;
margin:0 auto;
overflow:auto; }
#container2{
/*Colors*/
background-color:black;
color:white;
/*Size Box*/
width:50%;
height:50%;
padding:20px;
margin:0 auto;
overflow:auto; }
http://jsfiddle.net/G4GT4/1/
With the current structure you would have to position the child with position:absolute.
JSfiddle Demo
CSS
#container{
/*Colors*/
background-color:rgb(129, 159, 255);
/*Size Box*/
width:400px;
height:200px;
margin:0 auto;
overflow:auto;
position: relative;
}
#container2{
/*Colors*/
background-color:black;
color:white;
/*Size Box*/
width:50%;
height:50%;
padding:20px;
margin:0 auto;
overflow:auto;
position: absolute;
bottom:0;
left:50%;
margin-left: -25%;
}
Add
#container { position: relative; }
#container2 { position: absolute; bottom: 0; }
Or simply use tables
Demo
#container {
background-color:rgb(129, 159, 255);
display: table-cell;
width:400px;
height:200px;
margin:0 auto;
overflow:auto;
text-align: center;
vertical-align: bottom;
}
#container2 {
background-color:black;
color:white;
/*Size Box*/
width:50%;
height:50%;
padding:20px;
margin:0 auto;
overflow:auto;
display: inline-block;
}
You have to set margin-top (you know both heights) or using positioning, I´ve chosen second variant.
#container {postition: relative}
#container2 {position: absolute; bottom: 0; left: 25%;}
http://jsfiddle.net/G4GT4/2/
You can set position relative to container2
Working fiddle here
#container2{
/*Colors*/
position: relative;
top: 15%;
background-color:black;
color:white;
/*Size Box*/
width:50%;
height:50%;
padding:20px;
margin:0 auto;
overflow:auto;
}
If you don't want to use position absolute; you can do this:
fiddle
css
#container{
text-align:center;
}
#container:before {
content:"";
height:100%;
display:inline-block;
vertical-align:bottom;
}
#container2{
display:inline-block;
vertical-align:bottom;
}
I am trying to place my logo above a menubar i created in css, but what ever i try the logo always goes below the menu bar, i am trying to achieve a manubar the width of the page with a logo in the centre of the menubar.
My CSS code is ;
#bar {
margin-top:50px;
width: 1920px center;
height: 30px;
background: #2E2E2E;
border: 3px groove #FFD700;
}
#logo {
background-image:url(../img/LOGO1.png);
background-size:150px;
width:150px;
height:150px;
margin:0 auto;
}
and html is;
</head>
<body>
<div id="bar">
</div>
<div id="logo">
</div>
</body>
</html>
Thankyou for any help
Z:index is your friend
http://jsfiddle.net/BrvL2/1/
#logo {
position:absolute;
background-image:url(http://placehold.it/150x150/f16b20/ffffff);
background-size:150px;
width:150px;
height:150px;
margin:0 auto;
z-index:999;
top:0px;
margin: 0 auto;
left:0px;
right:0px;
}
#bar {
margin-top:50px;
width: 1920px;
height: 30px;
background-color: #2E2E2E;
border: 3px groove #FFD700;
text-align: center;
}
#logo {
position:relative;
background-image:url(http://placehold.it/150x150/f16b20/ffffff);
background-size:150px;
width:150px;
height:150px;
margin:0 auto;
z-index:999;
top:0px;
}
<div id="bar">
<div id="logo">
</div>
</div>
Hope this works for you.
Here's the CSS:
#wrap {
position:relative;
width:100%;
}
#bar {
position:relative;
margin-top:50px;
width: 100%;
height: 30px;
background-color: #2E2E2E;
border: 3px groove #FFD700;
}
#logo {
position:absolute;
background-image:url(http://placehold.it/150x150/f16b20/ffffff);
background-size:150px;
width:150px;
height:150px;
left:50%;
margin-left:-75px;
margin-top:-50px;
z-index:999;
top:0px;
}
I put in a wrap, so the #logo would have a parent container to reference when positioning. This will float the logo in the middle of the menu bar.
Here's the fiddle: http://jsfiddle.net/BrvL2/3/
alter the 'left:' attribute until it is centered
#logo {
position:absolute;
top:0;
left:45%;
right:0;
z-index:1000;
background-image:url(../img/LOGO1.png);
background-size:150px;
width:150px;
height:150px;
}