简体   繁体   English

无法将DIV放置在图像幻灯片的内部

[英]Can't get DIV to center inside of image slideshow

First time here asking a question, been using this for a long time. 在这里第一次问一个问题,已经使用了很长时间了。 I'm relatively new to coding and I'm doing a project for school. 我是编码的新手,我正在为学校做一个项目。

I have an image slideshow on my website, based on the one created by W3schools and inside that slideshow I have created a caption box with a semi-transparent box background. 我在网站上有一个图片幻灯片,基于W3schools创建的幻灯片,在该幻灯片中,我创建了一个具有半透明框背景的标题框。 For some reason I cannot seem to get that caption box centered horizontally. 由于某种原因,我似乎无法使字幕框水平居中。 The code for my slideshow currently looks like this: 我的幻灯片的代码当前如下所示:

<!--- Image slideshow --->
<div class="slideshow-container">
<a href="#slideshow" id="slideshow">
    <div class="text">
        <div class="mySlides fade">
            <div class="numbertext">1 / 5</div>
            <img src="images/city2.jpg" style="width:100%">
            <div class="text2">Caption 1</div>
        </div>
        <div class="mySlides fade">
            <div class="numbertext">2 / 5</div>
            <img src="images/elevator3.jpg" style="width:100%">
            <div class="text2">Caption 2</div>
        </div>
        <div class="mySlides fade">
            <div class="numbertext">3 / 5</div>
            <img src="images/nanotech2.jpg" style="width:100%">
            <div class="text2">Caption 3</div>
        </div>
        <div class="mySlides fade">
            <div class="numbertext">4 / 5</div>
            <img src="images/hyperloop3.jpg" style="width:100%">
            <div class="text2">Caption 4</div>
        </div>
        <div class="mySlides fade">
            <div class="numbertext">5 / 5</div>
            <img src="images/vfarming2.jpg" style="width:100%">
            <div class="text2">Caption 5</div>
        </div>

        <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
        <a class="next" onclick="plusSlides(1)">&#10095;</a>
    </div>
</a>
</div>

This CCS and rest of the website can be seen at the link below. 该CCS和网站的其余部分可以在下面的链接中找到。 I've replaced the images with placeholders of the same size. 我已经用相同大小的占位符替换了图像。 http://codepen.io/Ethan_Matlack/pen/MyNdWG http://codepen.io/Ethan_Matlack/pen/MyNdWG

Based on other post I've seen on stack overflow I've tried assigning the parent element (text) a 根据我在堆栈溢出中看到的其他帖子,我尝试为父元素(文本)分配一个

 display: inline-block   

attribute and the child element (text2) a 属性和子元素(text2)

 text-align: center

attribute but this didn't work. 属性,但这无效。 For whatever reason no matter what I do it won't center. 无论出于何种原因,无论我做什么,都不会居中。 Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

Here is all of the relevant CSS code: 这是所有相关的CSS代码:

 /* ----------------------------------------------------------------- */ /* Slideshow CSS3 */ * {box-sizing:border-box} body {font-family: Verdana,sans-serif;margin:0} .mySlides { display:none; width: 100%; max-width: 1300px; height: 600px; margin: 0 auto; } .w3-left, .w3-right, .w3-badge { cursor:pointer } .w3-badge { height:13px; width:13px; padding:0 } /* Slideshow container */ .slideshow-container { max-width: 1300px; max-height: 600px; position: relative; margin: auto; } .slideshow-container2 { max-width: 650px; height: 300px; position: relative; margin: 0 auto; } /* Next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 0; top: 50%; width: auto; padding: 16px; margin-top: -22px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0,0,0,0.8); } /* Caption text */ .text { display: inline-block; } .text2 { text-align: center; border-radius: 3px; background: rgba(0,0,0,0.8); color: #f2f2f2; font-size: 15px; padding: 8px 12px; position: absolute; bottom: 16px; width: 60%; } /* Number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } /* Fading animation */ .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from {opacity: .4} to {opacity: 1} } @keyframes fade { from {opacity: .4} to {opacity: 1} } /* On smaller screens, decrease text size */ @media only screen and (max-width: 300px) { .slprev, .slnext,.text {font-size: 11px} } /* ----------------------------------------------------------------- */ 

EDIT: I've opted to put only the CSS into the snippet as this is relevant to the questions and will help anyone else easily find an answer. 编辑:我选择仅将CSS放入代码段中,因为这与问题相关,并且将帮助其他任何人轻松找到答案。 In order for the snippet to be functional I would need almost all the rest of my code which isn't relevant to the question. 为了使代码片段正常运行,我几乎需要与问题无关的其余所有代码。 The full code can be seen in use at the codepen link. 完整的代码可以在codepen链接上看到。

You can add left: 0 and right: 0 to your .text2 class and then use margin: 0 auto; 您可以在.text2类中添加left: 0right: 0 ,然后使用margin: 0 auto; to center it on the middle of its container. 将其居中放置在容器中间。

Updated codepen . 更新了codepen

All you need to do is make the following modifications to your text2 class: 您需要做的就是对text2类进行以下修改:

Add

margin: 0 auto;

and change 并改变

position: relative;

There are several ways of doing this. 有几种方法可以做到这一点。 The one I'd use is the following. 我要使用的是以下内容。 Just add this to your css: 只需将其添加到您的CSS:

.mySlides {
    position: relative;
}
.text2 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%);
    /* removed bottom: 16px; */
}

This will always center matter the height or width of your .text2 element 这将始终使您的.text2元素的高度或宽度.text2

http://codepen.io/mattmuirhead/pen/wGVbrr http://codepen.io/mattmuirhead/pen/wGVbrr

edit: This is to center both horizontally and vertically 编辑:这是水平和垂直居中

Add the following to your text2 class's CSS: 将以下内容添加到text2类的CSS中:

left: 50%;
margin-left: -30%;

The class is already absolutely positioned. 该类已经完全定位。 So the left: 50% attribute moves the left side of the element to the center. 因此left: 50%属性将元素的左侧移动到中心。 Then the margin-left: -30% moves it back to the left by half of its width (the element's width is 60% according to the CSS you provided). 然后margin-left: -30%将其margin-left: -30%移动一半宽度(根据您提供的CSS,元素的宽度为60% )。

Actually, you're doing it the wrong way around. 实际上,您做错了方法。 The parent gets the text-align property, and the child the display. 父级获得text-align属性,子级获得显示。

so: 所以:

.text {
  text-align:center;
}
.text2 {
  display:inline-block; 
  position:relative;
}

you can't do this with an absolute positioned element. 您不能使用绝对定位的元素来执行此操作。 If it HAS to be position:absolute; 如果必须定位:绝对; you could also use the transform property as follows: 您还可以按如下方式使用transform属性:

.text2 {
  position:absolute;
  left:50%
  -webkit-transform:translateX(-50%);
  transform:translateX(-50%);
}

Why does that work? 为什么行得通? Because Translate moves the element based on it's OWN width, whereas position:absolute; 因为Translate根据元素的OWN宽度移动元素,所以position:absolute; left:50%; 左:50%; moves the element based on it's PARENT width. 根据元素的父元素宽度移动元素。

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

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