仅限 CSS Carousel:居中和固定大小的问题

问题描述 投票:0回答:2

我有一个仅使用 CSS 创建的轮播,但我发现了一些困难。所有图像的宽高比均为 4:3,并且可能具有 640x480 像素的最大(也就是说,它们可以小于此值),因此:

  • 我无法将下一个和上一个箭头居中;
  • 箭头不能“浮动”在屏幕上(如果下一个图像是 比前一个小,箭头不应改变 位置);
  • 不知道如何根据轮播图设置轮播的大小 当前使用的最大图像。

代码:

html {
  font-size: 10px;
}
body {
  padding: 2rem;

  background: #F8F8F8;

  font-size: 16px;
}

.wrap {
  max-width: 70rem;
  margin: auto;
  padding: 2rem;

  background: #FFF;
}
.carousel {
  position: relative;

  display: -webkit-box;
  display: flex;

  max-width: 64rem;
  min-height: 40rem;
  margin: auto;

  background: red;

  -webkit-box-pack: center;
  justify-content: center;
}
.group {
  display: -webkit-box;
  display: flex;

  background: blue;

  -webkit-box-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  align-items: center;
}
.group input {
  position: absolute;
  top: -100%;
  left: -100%;

  display: none;
}
.group input ~ .content {
  /* tentar fixar tamanho aqui */
  display: none;

  -webkit-box-ordinal-group: 3;
  order: 2;
}
div.group input:checked ~ div.content {
  display: block;
}
div.group input:checked ~ label.previous,
div.group input:checked ~ label.next {
  display: block;
}
div.group label {
  top: 50%;

  display: none;

  width: 50px;
  height: 50px;

  border: solid 1px black;

  background-color: #69C;

  transform: translateY(-50%);
}
img {
  width: 100%;
  height: 100%;
}
label {
  margin: 125px 0 0 0;

  font-size: 4em;
}
label.previous {
  padding: 0 0 30px 5px;

  -webkit-box-ordinal-group: 2;
  order: 1;
}
label.next {
  padding: 0 5px 25px 0;

  text-align: right;

  -webkit-box-ordinal-group: 4;
  order: 3;
}
<!DOCTYPE html>
<html>

<head>
    <title>Teste 3</title>
    <link rel="stylesheet" type="text/css" href="teste3.css">
    <meta charset="utf-8">
</head>

<body>
    <div class="wrap">
        <div class="carousel">
            <div class="group">
                <input type="radio" name="test" id="0" value="0">
                <label for="4" class="previous">&lt;</label>
                <label for="1" class="next">&gt;</label>
                <div class="content">
                    <img src="https://www.oneperiodic.com/products/photobatch/tutorials/img/scale_original.png" width="200" height="286">
                </div>
            </div>
            <div class="group">
                <input type="radio" name="test" id="1" value="1">
                <label for="0" class="previous">&lt;</label>
                <label for="2" class="next">&gt;</label>
                <div class="content">
                    <img src="https://www.ausmedia.com.au/Mmedia/4TO3.gif" width="200" height="139">
                </div>
            </div>
            <div class="group">
                <input type="radio" name="test" id="2" value="2">
                <label for="1" class="previous">&lt;</label>
                <label for="3" class="next">&gt;</label>
                <div class="content">
                    <img src="http://www.cegepsherbrooke.qc.ca/~cpi/images/photo_4-3.jpg" width="140" height="200">
                </div>
            </div>
            <div class="group">
                <input type="radio" name="test" id="3" value="3" checked="">
                <label for="2" class="previous">&lt;</label>
                <label for="4" class="next">&gt;</label>
                <div class="content">
                    <img src="http://www.projectorcentral.com/images/articles/4-3%20Bristlecone%20Pines.jpg" width="200" height="287">
                </div>
            </div>
            <div class="group">
                <input type="radio" name="test" id="4" value="4">
                <label for="3" class="previous">&lt;</label>
                <label for="0" class="next">&gt;</label>
                <div class="content">
                    <img src="http://www.wallpaperawesome.com/wallpapers-awesome/wallpapers-for-monitor-4-3-almost-square-awesome/wallpaper-for-monitor-4-3-almost-square-awesome-951.jpg" width="96" height="139">
                </div>
            </div>
        </div>
    </div>
</body>

</html>

任何人都可以帮我找到解决方案或至少在测量中找到平衡吗?

html css carousel
2个回答
0
投票

据我了解,

您还没有添加

position: absolute
到标签。 将顶部更改为 18% 以将其置于中心。 上一页
left:0;
接下来是
right:0;

这将根据轮播设置您的标签,因为它具有相对位置。

html{
  font-size: 10px;
}
body {
  padding: 2rem;
  background: #F8F8F8;
  font-size: 16px;
}
.wrap {
  max-width: 70rem;
  margin: auto;
  padding: 2rem;
  background: #FFF;
}
.carousel {
  position: relative;
  display: -webkit-box;
  display: flex;
  max-width: 64rem;
  min-height: 40rem;
  margin: auto;
  background: red;
  -webkit-box-pack: center;
  justify-content: center;
}
.group {
  display: -webkit-box;
  display: flex;
  background: blue;
  -webkit-box-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  align-items: center;
}
.group input {
  position: absolute;
  top: -100%;
  left: -100%;
  display: none;
}
.group input ~ .content {
  /* tentar fixar tamanho aqui */
  display: none;
  -webkit-box-ordinal-group: 3;
  order: 2;
}
div.group input:checked ~ div.content {
  display: block;
}
div.group input:checked ~ label.previous,
div.group input:checked ~ label.next {
  display: block;
}
div.group label {
  position:absolute;
  top: 18%;
  display: none;
  width: 50px;
  height: 50px;
  border: solid 1px black;
  background-color: #69C;
  transform: translateY(-50%);
}
img {
  width: 100%;
  height: 100%;
}
label {
  margin: 125px 0 0 0;
  font-size: 4em;
}
label.previous {
  padding: 0 0 30px 5px;
  -webkit-box-ordinal-group: 2;
  order: 1;
  left:0;
}
label.next {
  padding: 0 5px 25px 0;
  text-align: right;
  -webkit-box-ordinal-group: 4;
  order: 3;
  right:0;
}
<!DOCTYPE html>
<html>

<head>
  <title>Teste 3</title>
  <link rel="stylesheet" type="text/css" href="teste3.css">
  <meta charset="utf-8">
</head>

<body>
  <div class="wrap">
    <div class="carousel">
      <div class="group">
        <input type="radio" name="test" id="0" value="0">
        <label for="4" class="previous">&lt;</label>
        <label for="1" class="next">&gt;</label>
        <div class="content">
          <img src="https://www.oneperiodic.com/products/photobatch/tutorials/img/scale_original.png" width="200" height="286">
        </div>
      </div>
      <div class="group">
        <input type="radio" name="test" id="1" value="1">
        <label for="0" class="previous">&lt;</label>
        <label for="2" class="next">&gt;</label>
        <div class="content">
          <img src="https://www.ausmedia.com.au/Mmedia/4TO3.gif" width="200" height="139">
        </div>
      </div>
      <div class="group">
        <input type="radio" name="test" id="2" value="2">
        <label for="1" class="previous">&lt;</label>
        <label for="3" class="next">&gt;</label>
        <div class="content">
          <img src="http://www.cegepsherbrooke.qc.ca/~cpi/images/photo_4-3.jpg" width="140" height="200">
        </div>
      </div>
      <div class="group">
        <input type="radio" name="test" id="3" value="3" checked="">
        <label for="2" class="previous">&lt;</label>
        <label for="4" class="next">&gt;</label>
        <div class="content">
          <img src="http://www.projectorcentral.com/images/articles/4-3%20Bristlecone%20Pines.jpg" width="200" height="287">
        </div>
      </div>
      <div class="group">
        <input type="radio" name="test" id="4" value="4">
        <label for="3" class="previous">&lt;</label>
        <label for="0" class="next">&gt;</label>
        <div class="content">
          <img src="http://www.wallpaperawesome.com/wallpapers-awesome/wallpapers-for-monitor-4-3-almost-square-awesome/wallpaper-for-monitor-4-3-almost-square-awesome-951.jpg" width="96" height="139">
        </div>
      </div>
    </div>
  </div>
</body>

</html>


0
投票

您的代码 Bianca 有几个问题。首先,当我们编写 HTML 时,最好在实际的

width
中省略
height
html
属性。原因是浏览器会认为您的
html
中的任何内容比外部
css
文件更重要。当
inline-styling
直接写入
inline-styles
时,我们称之为
CSS
HTML
。 (这里对
CSS
的层次结构有一个很好的解释:内联
© www.soinside.com 2019 - 2024. All rights reserved.