简体   繁体   English

div定位:绝对,相对等

[英]div positioning: absolute, relative, etc

I have pure CSS image slider which I want to have positioned ( margin:auto ) with text underneath. 我有一个纯CSS图片滑杆,我想将其定位在( margin:auto )下方且文字下方。 Slider images are absolutely positioned as they are stacked. 滑块图像在堆叠时绝对定位。 I can't figure out how to position divs around it all. 我不知道如何将div放置在所有周围。 I have content and wrapper divs with relative position. 我有内容和包装div与相对位置。 Image size should be responsive (therefore max-width:100% ) but wrapper or content divs can be exact size. 图像大小应该是自适应的(因此max-width:100% ),但是包装器或内容div可以是准确的大小。 Or maybe they don't need to either? 也许他们也不需要?

This is what I am after: 这就是我所追求的: 所需的布局

And this is what I managed so far: www.jsfiddle.net/1qxxnxbf/1/ 这是我到目前为止所管理的: www.jsfiddle.net/1qxxnxbf/1/

If your image slider is a carousel, you can't make it responsive without js. 如果您的图片滑块是轮播,那么如果没有js,您就无法使其具有响应性。 If you give your slider a height in the css, you can adjust it in the js to make it responsive. 如果您将滑块的高度设为CSS,则可以在js中对其进行调整以使其具有响应性。

The only other thing you can do is maintain an aspect ratio. 您唯一可以做的就是保持宽高比。 So in your example you have 350x220 images. 因此,在您的示例中,您有350x220的图片。 so If you get your padding-bottom on your .slider class to 62.857% (roughly 220/350) you get a variable height based on the width. 因此,如果您将.slider类的padding-bottom设为62.857%(大约220/350),则您将获得基于宽度的可变高度。 If your width grows/shrinks, the height will grow/shrink as well. 如果宽度增加/缩小,则高度也将增加/缩小。

http://jsfiddle.net/1qxxnxbf/2/ http://jsfiddle.net/1qxxnxbf/2/

Edit: I just noticed that none of your code around the slider is responsive. 编辑:我只是注意到您的滑块周围的代码都没有响应。 Why are you trying to make the slider responsive? 为什么要尝试使滑块响应?

Checkout this design 签出此设计

https://jsfiddle.net/jalayoza/zvy87dcv/9/ https://jsfiddle.net/jalayoza/zvy87dcv/9/

HTML code HTML代码

<div class="content">content

  <div class="wrapper">wrapper

    <div class="slider">
      <img src="https://placeimg.com/350/220/any" class="slide" alt="slide1">
      <img src="https://placeimg.com/350/220/nature" class="slide" alt="slide2">
      <img src="https://placeimg.com/350/220/abstract" class="slide" alt="slide3">
    </div>

    <!-- text should go underneath the image -->
    <div class="text">
      <div class="text_left">
        left text
      </div>
      <div class="text_right">
        right text
      </div>
    </div>

  </div>

</div>

CSS code CSS代码

.content {
  width: 500px;
  background: #fff;
  margin: auto;
}

.wrapper {
  max-width: 400px;
  position: relative;
  background: purple;
  margin: auto;
  padding:10px;
}

.slider {
  margin: auto;
  left: 0;
  right: 0;
  max-width: 100%;
  position: relative;
  padding-bottom: 62.857%;
}

.slide {
  max-width: 400px;
  margin: auto;
  position: absolute;
  opacity: 0.5;
  width: 100%;
}

.text {
  max-width: 100%;
  position: absolute;
  background: transperant;
  opacity: 0.9;
  bottom:10px;
  width: 95%;
}

.text_left {
  max-width: 50%;
  background: #fff;
  float: left;
  text-align: left;
  padding:5px;
}

.text_right {
  max-width: 50%;
  background: #fff;
  float: right;
  text-align: right;
    padding:5px;
}

Hope you will like this design 希望你会喜欢这个设计

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

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