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>
Related
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>
.top{
width:100%;
height:50vh;
background-color:red;
}
.bot{
width:100%;
height:50vh;
background-color:black;
}
.content{
position:relative;
width:50px;
height:50px;
background-color:white;
left: 0px;
bottom:0px;
}
<div class='top'>
<div class='content'>
</div>
</div>
<div class='bot'>
</div>
content is not laying on the inner bottom of the first div .top, why not position:relative bottom:0px works , while positioning on absolute it comes in bottom of the screen , so can I lay that div on the bottom of the first div .top using position absolute, content width height have to change though.
Add position: relative to .top and set position: absolute to .content
.top{
width:100%;
height:50vh;
background-color:red;
position:relative;
}
.bot{
width:100%;
height:50vh;
background-color:black;
}
.content{
position:absolute;
width:50px;
height:50px;
background-color:white;
left: 0px;
bottom:0px;
}
<div class='top'>
<div class='content'>
</div>
</div>
<div class='bot'>
</div>
bottom 0px not works with position relative , and if you want to do with postion relative i found a solution for you
.top{
width:100%;
height:50vh;
background-color:red;
}
.bot{
width:100%;
height:50vh;
background-color:black;
}
.content{
position:relative;
width:50px;
height:50px;
background-color:white;
left: 0px;
bottom: calc(-100% + 50px);
}
<div class='top'>
<div class='content'>
</div>
</div>
<div class='bot'>
</div>
This is what I've tried so far:
.content{
margin-bottom: 50px;
}
.bottom{
position:absolute;
height:50px;
line-height: 50px;
bottom:0;
width:80%;
background:green;
}
<div class="content">
......
</div>
<footer class="bottom">
<p>
this is always at bottom
</p>
</footer>
Here is a Fiddle is an example too.
Did someone know why this happen, and how I can solve it?
Thank you!
1.You have make small change in CSS.According to your demo link.
.parent {
height:1500px;
width:200px;
border:1px solid grey;
position: relative;
background-color: white;
}
.topDiv {
display:block;
background:silver;
width:100px;
height:100px;
position:absolute;
top:0;
left:0
}
.bottomDiv {
background:red;
width:100px;
height:100px;
position:absolute;
bottom:0;
}
body {
height:1500px;
background: linear-gradient(#111, #777);
}
You have to use position:fixed, to achieve this !!
CSS
<style type="text/css">
.content{
margin-bottom: 50px;
}
.bottom{
position:fixed;
height:50px;
line-height: 50px;
bottom:0;
width:80%;
background:green;
}
</style>
HTML
<div class="content">
......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br> ......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br> ......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br> ......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br> ......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br> ......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>......<br>
</div>
<footer class="bottom">
<p>
this is always at bottom
</p>
</footer>
I have one DIV positioned right.
.right {
width:25%;
height:100%;
background-color:#000;
position:fixed;
right:0px;
z-index:1;
And left
.left {
width:25%;
height:100%;
background-color:#000;
position:fixed;
left:0px;
z-index:1;
And I'm trying to put this circle
.circle {
height:100px;
width:100px;
border-radius:50px;
background-color:#F00;
position:fixed;
left:45%;
z-index:99;
in the middle
this is my HTML
<div class="left">
</div>
<div class="centerc">
<div class="circle">
</div>
<div class="right">
</div>
What am I doing wrong and how do I fix it?
Your code seems to be working. However, the circle is off-center.
I suggest that you define the circle's position as 50% of the container's width minus 50% of the circle's width:
.circle {
...
width:100px;
left:50%;
margin-left:-50px;
}
Also, since everything is position:fixed, I don't see the purpose of div.centerc. I removed it.
<div class="left"></div>
<div class="circle"></div>
<div class="right"></div>
Working example (jsFiddle)
Try to put this inside .circle styling:
left:50%;
margin-left:-50px;
left:50%; will put the left side of the .circle in the middle of the screen, then margin-left:-50px; will put the .circle 50px to the left (half of its width).
Also, it's a good idea to remove the non-closed .centerc div.
Demo
*{margin:0;}
body{
background:#fff;
}
.left{
position:fixed;
height:100%;
width:25%;
left:0;
background:#222;
}
.circle{
z-index:1;
position:fixed;
width:100px;
height:100px;
left:50%; /* Left side of the circle centered */
margin-left:-50px; /* A half of circle width to the left */
border-radius:50px;
background:#F33;
}
.right{
position:fixed;
height:100%;
width:25%;
right:0;
background:#222;
}
<div class="left"></div>
<div class="circle"></div>
<div class="right"></div>
If you are meaning to have the left and right divs aligned and the circle div floating over them in dead center here's a quick fiddle to set you in that direction.
http://jsfiddle.net/Hastig/zLsbE/
I added a container div wrapped around all three (left, right and circle) and set it to position: relative
I then set the circle div to position: absolute and played with it's left and top alignment to center it.
Note - It's not a responsive solution.
.container {
width: 500px;
height: 250px;
position: relative;
}
.left {
float: left;
width: 250px;
height: 250px;
background-color: #000;
}
.right {
float: right;
width: 250px;
height: 250px;
background-color: #555;
}
.circle {
height: 100px;
width: 100px;
border-radius: 50px;
background-color: #F00;
position: absolute;
top: 75px;
left: 200px;
}
<div class="container">
<div class="left"></div>
<div class="circle"></div>
<div class="right"></div>
</div>
http://jsfiddle.net/R8YRh/1/ demonstrates using:
.centerc {
text-align:center;
}
and the addition of display: inline-block; to .circle. This required the addition of top: 0; to .right.
.left {
width:25%;
height:100%;
background-color:#000;
position:fixed;
left:0;
z-index:1;
}
.right {
width:25%;
height:100%;
background-color:#000;
position:fixed;
right:0;
top: 0;
z-index:1;
}
.centerc {
text-align:center;
}
.circle {
display: inline-block;
height:100px;
width:100px;
border-radius:50px;
background-color:#F00;
z-index:99;
}
<div class="left"></div>
<div class="centerc">
<div class="circle"></div>
</div>
<div class="right"></div>
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;
}