简体   繁体   English

如何在Bootstrap 4轮播中居中显示字幕?

[英]How to center caption in Bootstrap 4 carousel?

I essentially copied the captioned carousel example from the Bootstrap 4 documentation: https://v4-alpha.getbootstrap.com/components/carousel/ 我基本上从Bootstrap 4文档中复制了带字幕的轮播示例: https : //v4-alpha.getbootstrap.com/components/carousel/

However, I cannot seem to achieve a carousel that only has text, where the text is centered... 但是,我似乎无法实现仅包含文本的旋转木马,其中文本居中...

Here is my code: 这是我的代码:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="carousel" class="carousel slide bg-chrome" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carousel" data-slide-to="0" class="active"></li> </ol> <div class="carousel-inner" role="listbox"> <div data-slide-no="0" class="carousel-item item active"> <div class="carousel-caption d-none d-md-block"> <h1>My Carousel</h1> ...is not centered! </div> </div> </div> <a class="carousel-control-prev" href="#carousel" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous slide</span> </a> <a class="carousel-control-next" href="#carousel" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next slide</span> </a> </div> 

Aside from some minor styling, this is what the above code renders: 除了一些小的样式外,以上代码还提供了以下内容:

在此处输入图片说明

As you can see, the text is not centered! 如您所见,文本未居中! Per some other questions I found here on StackOverflow, I tried: 根据我在StackOverflow上发现的其他一些问题,我尝试了:

  1. Setting .carousel-caption to have margin: 0 auto .carousel-caption设置为具有margin: 0 auto
  2. Setting .carousel-caption to have position: relative .carousel-caption设置为具有position: relative
  3. Setting .carousel-caption to have text-align: center 设置.carousel-caption以使text-align: center
  4. Wrapping each .carousel-item in a .container and styling to achieve a centered text 将每个.carousel-item包装在.container并设置样式以使文本居中

None of the solutions have worked, so I ask you all; 没有一个解决方案可行,所以我问大家。 how can I achieve a horizontally centered carousel with only captions? 如何获得仅带字幕的水平居中轮播?

Edit: JSFiddle 编辑: JSFiddle

As of BS alpha 6, the .carousel-item is display:flex; 从BS alpha 6开始, .carousel-itemdisplay:flex; which limits some of the positioning you can do with the contents of carousel-item . 这限制了您对carousel-item的内容可以进行的某些定位。 This is a known issue: https://github.com/twbs/bootstrap/issues/21611 这是一个已知问题: https : //github.com/twbs/bootstrap/issues/21611

So, one option is to change the active .carousel-item is display:block; 因此,一种选择是更改活动的.carousel-itemdisplay:block; . Then you can simply use text-center to center the contents. 然后,您可以简单地使用text-center来使内容居中。

.carousel-item.active,
.carousel-item-next,
.carousel-item-prev{
    display:block;
}

<div class="carousel-inner" role="listbox">
    <div class="carousel-item active text-center p-4">
        <h1>My Carousel</h1> ...is centered!
    </div>
    <div class="carousel-item text-center p-4">
        <h1>2 Carousel</h1> ...is centered!
    </div>
    <div class="carousel-item text-center p-4">
        <h1>3 Carousel</h1> ...is centered!
    </div>
</div>

http://www.codeply.com/go/ja3h44A7bQ http://www.codeply.com/go/ja3h44A7bQ


Another option is to simply use the flexbox utils, and change the direction to flex-column . 另一种选择是仅使用flexbox utils,并将方向更改为flex-column Then align-items-center can be used... 然后可以使用align-items-center ...

      <div class="carousel-inner text-white" role="listbox">
            <div class="carousel-item active align-items-center flex-column p-4">
                <h1>My Carousel</h1> ...is centered!
            </div>
            <div class="carousel-item align-items-center flex-column p-4">
                <h1>2 Carousel</h1> ...is centered!
            </div>
            <div class="carousel-item align-items-center flex-column p-4">
                <h1>3 Carousel</h1> ...is centered!
            </div>
       </div>

http://www.codeply.com/go/9I7voUaDUi http://www.codeply.com/go/9I7voUaDUi

In both cases I've removed the carousel-caption . 在这两种情况下,我都删除了carousel-caption The carousel-caption should only be used if you want the text to overlay a slide image. 仅当您希望文本覆盖幻灯片图像时,才应使用carousel-caption There's no reason to use the 'carousel-caption' if there is no image, just put the content inside carousel-item . 如果没有图像,则没有理由使用“旋转木马字幕”,只需将内容放在carousel-item


Also see: Vertically center text in Bootstrap carousel 另请参阅: 在Bootstrap轮播中将文本垂直居中

It looks like it's a combination of the position:relative; 看起来是position:relative;的组合 you were adding on the .carousel-caption and the absence of a 'height' value for the .carousel-item 您正在添加.carousel-caption并且.carousel-item没有'height'值

Here is a working fiddle: https://jsfiddle.net/z0cxhgc1/1/ 这是一个有效的小提琴: https : //jsfiddle.net/z0cxhgc1/1/

Update: Bootstrap's default carousel automatically takes on the height of the image, and then uses 更新: Bootstrap的默认轮播会自动采用图片的高度,然后使用

position: absolute;
right: 15%;
bottom: 20px;
left: 15%;

to style the caption over the lower center of the image. 在图片的下部中央设置字幕样式。 without the image's natural height, everything else in the carousel is positioned absolutely, and since the carousel itself has a height of 0, it was showing up 20px above the bottom -- or 20px above and outside of the carousel. 如果没有图像的自然高度,则轮播中的所有其他位置都将被绝对定位,并且由于轮播本身的高度为0,因此其显示的高度比底部高20px,或者比轮播的上方和外侧高20px。

your position: relative; 您的position: relative; style got it showing again because putting it back in the document flow gave the slide element a natural height again, but was conflicting with the other styles in terms of positioning. 样式再次显示,因为将其放回文档流中使幻灯片元素再次具有自然高度,但在定位方面与其他样式冲突。

Short answer: 简短答案:

Add some height to the carousel item so that it does not disappear (since you are not using an image): 为轮播项目增加一些高度,以使其不会消失(因为您没有使用图像):

.carousel-item{
    height:200px;
}

Remove: 去掉:

.carousel-caption {
    position: relative;
    text-align: center;
}

More detailed answer: 更详细的答案:

I believe that your answer can be found here . 我相信您的答案可以在这里找到。 Normally, your captions should be centred, but if not then try adding "text-center" to your "carousel-caption". 通常,标题应居中,但如果没有,请尝试在“轮播字幕”中添加“文本中心”。 That should fix it. 那应该解决它。

An example solution using Bootstrap 4 Alpha 6 showing: 使用Bootstrap 4 Alpha 6的示例解决方案显示:

  • the usage of d-* for hiding and showing text on different device screens and adjusting the display type ie d-md-block means set the display to block on medium devices d- *在不同设备屏幕上隐藏和显示文本以及调整显示类型的用法,即d-md-block表示将显示设置为在中等设备上阻止
  • the alignment of text 文字对齐

 .bg-chrome { background: blue; } .carousel-item{ height:200px; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> <div id="carouselIndicators" class="carousel slide bg-chrome" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#carouselIndicators" data-slide-to="0" class="active"></li> <li data-target="#carouselIndicators" data-slide-to="1"></li> <li data-target="#carouselIndicators" data-slide-to="2"></li> <li data-target="#carouselIndicators" data-slide-to="3"></li> </ol> <div class="carousel-inner" role="listbox"> <div class="carousel-item active"> <div class="carousel-caption d-block"> <h3>A carousel caption with text on all device screens and aligned to the center</h3> </div> </div> <div class="carousel-item"> <div class="carousel-caption d-block text-left"> <h3>A carousel caption with text aligned to the left</h3> </div> </div> <div class="carousel-item"> <div class="carousel-caption d-block text-right"> <h3>A carousel caption with text aligned to the right</h3> </div> </div> <div class="carousel-item"> <div class="carousel-caption d-none d-md-block"> <h3>A carousel caption with hidden text on small devices</h3> </div> </div> </div> <a class="carousel-control-prev" href="#carouselIndicators" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous slide</span> </a> <a class="carousel-control-next" href="#carouselIndicators" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next slide</span> </a> </div> 

I updated your fiddle. 我更新了你的小提琴。 Left and right 15% PLUS text centered let the heading confuse 左和右15%PLUS文本居中,使标题混淆

https://jsfiddle.net/jchp6bg9/10/ Added https://jsfiddle.net/jchp6bg9/10/添加

left:0%; 
right: 0%; 

to your css code 到你的CSS代码

For carousel with fixed height (eg: 400px) this works for me: 对于固定高度(例如:400px)的轮播,这对我有用:

.carousel-caption {
    position: relative;
    left: 0;
    padding-top: 150px; // Set relative to your carousel height
}
.carousel-caption{
    background-color: rgb(209, 209, 154);
    top: 30%;
    height: auto;
    max-height: 120px;
}

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

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