简体   繁体   English

我无法将div居中

[英]I can't center a div over the image

enter image description here i am trying to center the div over the image , i tried positioning the container relative and the elements to absolute but didn't work.The code below shows what i have done till now.The image is a full size background and the container should be centered over it. 在这里输入图像描述,我试图将div居中放置在图像上,我试图将容器的相对位置和元素定位为绝对,但没有用。下面的代码显示了我到目前为止所做的操作。图像是全尺寸背景并且容器应居中。

 .circle123{ position: relative; float: none; width: 600px; height: 200px; top: 0; margin-right:auto; margin-left:auto; text-align: center; border: 1px solid black; } #circle1{ position: absolute; display: inline-block; height: 150px; width: 150px; margin: 10px; padding: auto; border-radius: 50%; background-color: rgba(50,205,50, 0.75); } #circle2{ position: absolute; display: inline-block; height: 150px; width: 150px; margin: 10px; padding: auto; border-radius: 50%; background-color:rgba(135,206,235, 0.75); } #circle3{ position:absolute ; display: inline-block; height: 150px; width: 150px; margin: 10px; padding: auto; border-radius: 50%; background-color: rgba(220,20,60, 0.55); } .back-bar{ display: inline-block; width: 100%; height: 100%; background: url(image.jpg); background-repeat: no-repeat; background-attachment: fixed; background-size: cover; background-position: center center; } 
 <div class="back-bar"></div> <div class="circle123"> <div id="circle1"><h2>1<h2></div> <div id="circle2"><h2>2<h2></div> <div id="circle3"><h2>3<h2></div> </div> </div> 

First, there is problem in your html code, the last </div> close nothing. 首先,您的html代码中有问题,最后</div>关闭任何内容。 To center a container with position : absolute , the container who has the element should be on position : relative . 为了使容器的中心position : absolute ,具有元素的容器的position : relative应该为position : relative And after that you can center your element with position : absolute with : 然后,您可以将您的元素居中于position : absolute with:

`position : absolute;
top : 0;
bottom : 0;
left : 0;
right : 0;
margin: auto;`

Your HTML has issues, <h2> are not closed and you have an extra </div> at the bottom. 您的HTML存在问题, <h2>没有关闭,并且底部还有一个</div>

To center each circle in the container, add this to each circle: 要将每个圆在容器中居中,请将其添加到每个圆中:

  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;

Here is jsfiddle: https://jsfiddle.net/jLodgoc7/ 这是jsfiddle: https ://jsfiddle.net/jLodgoc7/

I can't understand what you want, but I believe you want to center content horizontally and vertically on a div that has an image as background. 我无法理解您想要的内容,但是我相信您希望将内容水平和垂直地放在以图片为背景的div上。 So, here you have. 所以,这里有。

 .image { width: 300px; height: 300px; position: relative; background: url('https://placehold.it/300x300/?text=This is your image'); } .image > .centered { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } 
 <div class="image"> <div class="centered">This is at center</div> </div> 

First step: remove 'Position' from css . 第一步:从css中删除“ Position”。

Second step: add float:right for cricle1 and added float:left to cricle3 . 第二步:为cricle1添加float:right,并向cricle3添加float:left。

finally add this style(div h { }) on css: 最后在CSS上添加以下样式(div h {}):

#circle1{
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color: rgba(50,205,50, 0.75);
    float: right;
}
#circle2{
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color:rgba(135,206,235, 0.75);
    float: none;
}
#circle3{
    display: inline-block;
    height: 150px;
    width: 150px;
    margin: 10px;
    padding: auto;
    border-radius: 50%;
    background-color: rgba(220,20,60, 0.55);
    float: left;
}

div h { 
        text-align: center;
        vertical-align: middle;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM