Positioning A Circle In The Middle Of Two Divs - html

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>

Related

make an element centered to an image on resizing using css

I have to make a div follow an image and sit on its center vertically and horizontally when responsive. I simply have no idea or don't think whether it is possible only by css. Any help is appreciated
.imageWrapper {
height:200px;
width:200px;
position:relative;
margin:50px auto 0px auto;
}
.imageWrapper > div:first-child {
position:absolute;
z-index:1;
top:0px;
left:0px;
right:0px;
bottom:0px;
overflow:hidden;
}
.imageWrapper > div:first-child img{
height:200px;
width:100%;
object-fit:cover;
position:relative
}
.imageWrapper > div:last-child {
position:relative;
z-index:2;
text-align:center;
line-height:200px;
height:200px;
width:100%;
}
<div class="imageWrapper">
<div><img src="https://upload.wikimedia.org/wikipedia/commons/e/ee/Billede_084.jpg"></div>
<div><p>bla bla</p></div>
</div>
make a wrapping div, make the image absolute as a background and place the text in front of the image.
Well you can make good use of an old trick to center element using position property.
as usual an example is better than an explanation.
.html
<div class="parent">
<div class="child"></div>
</div>
.css
.parent {
position: relative;
width: 100%;
height: 200px;
background-color: lightblue;
}
.parent .child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100px;
height: 100px;
background-color: grey;
}

How to set position in css

.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>

Two DIV's with auto-width [duplicate]

This question already has answers here:
CSS side by side div's auto equal widths
(5 answers)
Closed 8 years ago.
In my layout in the main part of site I need two flexible columns. The height is known and always the same. But the width should be auto-increases with the width of the browser.
I need this because in div#1 should be different content (float right I supposed) and background than in div#2 (float left I supposed). Whole layout is increasing their width with browser (width 100%).
It would be easy to make if the background of div 1 and 2 is the same (wrapper + background set on parent) but in this example backgrounds are different. I do not know how to auto-increase the width of these two divs.
adjust the width parameter of div#1 and div#2
div #header {
display: block;
position: absolute;
top: 0px;
height: 100px;
}
div #div1 {
display: block;
position: absolute;
top: 100px;
width: 50%;
left: 0px;
}
div #div2 {
display: block;
position: absolute;
top: 100px;
width: 50%;
right: 0px;
}
this is what you want:
<div class="container">
<div class="header">
This is header
</div>
</div>
<div class="container">
<div class="div1">
This is left div
</div>
<div class="div2">
This is right div
</div>
</div>
<div class="container">
<div class="footer">
This is footer
</div>
</div>
.container{
max-width:960px;
padding:0 15px;
height:auto;
position:relative;
clear:both;
}
.header{
position:relative;
height:100px;
background-color:yellow;
width:100%;
display:block;
}
.div1{
position:relative;
height:400px;
background-color:pink;
width:50%;
float:left;
}
.div2{
position:relative;
height:400px;
background-color:green;
width:50%;
float:left;
}
.footer{
position:relative;
height:100px;
background-color:cyan;
width:100%;
display:block;
}
I've created a jsfiddle demo
What about a good, simple, semantic layout? The below uses positioning to maintain a fixed footer, here is an example without
Demo Fiddle
HTML
<header></header>
<section></section>
<section></section>
<footer></footer>
CSS
html, body {
height:100%;
width:100%;
margin:0;
}
header, footer, section {
position:absolute;
width:100%;
}
header, footer {
background:green;
height:50px;
}
footer {
bottom:0;
}
section {
top:50px;
bottom:50px;
width:50%;
background:yellow;
overflow-x:auto;
}
section:last-of-type {
background:blue;
left:50%;
}
Try this
#container{
width:100%;
clear:both;
}
#header{
width:100%;
}
#div1{
height:400px;
width:50%;
float:left;
}
#div2{
height:400px;
width:50%;
float:right;
}
#footer{
width:100%;
}

How do you position or float one div next to an absolute div

I want a div to appear side-by-side with a google map. However, the google map HAS to be absolute positioned in order for it to take a percentage height.
<style>
#text{
height:40%;
width:40%;
float:left;
display:inline;
margin:10px;
background-color:white;
}
#map{
height:100%;
width:40%;
float:left;
position:absolute;
}
</style>
<div id="map1"> </div>
<div id="infoBox">text goes here </div>
While you are at it just go ahead with:
position: absolute;
left: 40%;
on the text element. You'll maintain your margin-left of 10px this way.
#infoBox{
height:40%;
width:40%;
margin:10px;
background-color:white;
position: absolute;
left: 40%;
}
#map{
height:100%;
width:40%;
float:left;
position: absolute;
}
http://jsfiddle.net/22Dhy/
#map {
left: 40%; /* width of #infoBox */
margin-left:10px; /* greater than or equal to left margin of #infoBox */
}

Display 3 horizontal divs centre page

I have this HTML:
<div id="cont">
<div class="chatarea">
<div class="row">
<div class="message">
<div class="nick">
<p>Some Nick</p>
</div>
<p>Some Message</p>
<div class="timestamp"><p>Some Timestamp</p></div>
</div>
</div>
</div>
</div>
and this CSS:
#cont {
width:100%;
text-align:center;
}
.chatarea
{
display: table;
height : 100%;
padding-top:50px;
margin:0px;
width:80%;
}
.nick
{
width: 400px;
border-right-style: solid;
text-align: center;
height:100%; position:absolute; top:0; left:0;
}
.timestamp
{
width: 400px;
border-left-style: solid;
position:absolute; top:0; right:0; height:100%;
}
.message
{
border-style: solid;
padding:0 50px 0 140px;
overflow:hidden;
position:relative;
}
im trying to display 3 divs (left and right smaller than the centre one) in the centre of the page. 80% of the browser width.
i have made a fiddle here: http://jsfiddle.net/zQ9pu/
im having a bit of trouble with it - what would be the best way to do this?
Just add
.chatarea
{
display: table;
height : 100%;
padding-top:50px;
margin:0px auto;
width:80%;
}
It works fine !! here is ur new fiddle http://jsfiddle.net/zQ9pu/2/
Contain those divs in a parent block level element which has a specified width and then apply CSS margin-left: auto; margin-right: auto; on it.