简体   繁体   English

在HTML 5中垂直对齐div

[英]Aligning divs vertically in HTML 5

vertically I am trying to make a carousel by myself without using any other tools like Bootstrap. 垂直方向上,我试图自己制作轮播,而不使用Bootstrap等其他工具。

I came across the situation where my images are not aligned horizontally. 我遇到了图像未水平对齐的情况。 I tried using display: inline-block and float: left separately and desperately, but they didn't seem to be the case of mine. 我尝试使用display: inline-blockfloat: left拼命地分开float: left ,但我的情况似乎并非如此。 Also, using different techniques dealing with the overflow did not affect the divs either. 同样,使用不同的技术来处理溢出也不会影响div。

So here is my HTML structure: 所以这是我的HTML结构:

<div id="image-slider">
    <div id="images">
        <div class="item"><img src="images/image0.jpg"></div>
        <div class="item"><img src="images/image1.jpg"></div>
        <div class="item"><img src="images/image2.jpg"></div>
        <div class="item"><img src="images/image3.jpg"></div>
        <div class="item"><img src="images/image4.jpg"></div>
    </div>
    <ul id="indicator">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>
    <input type="button" class="switch" id="prev" value="&lt;">
    <input type="button" class="switch" id="next" value="&gt;">
</div>

And here comes the corresponding CSS code: 这是对应的CSS代码:

#image-slider, .item, .item img { /* Tried removing the ".item img" but the images were too big if so */
    width: 100%;
    height: 100%;
}

#image-slider {
    position: relative;
    overflow: hidden;
}

#indicator {
    position: absolute;
    bottom: 0;
}

#images, .item { position: relative; }
.item { display: inline-block; }

So how do I fix it? 那么我该如何解决呢? Oh, another question: how do I modify the #indicator so that the elements inside are clickable elements (without using tools)? 哦,另一个问题:如何修改#indicator,以使其中的元素是可单击的元素(不使用工具)?

Answer for first one 回答第一个

One thing can be the usage of display: inline-block and the other method of using float: left requires that other tag is having float: right so that they can float infront of each other. 一件事可能是display: inline-block的使用display: inline-block ,另一种使用float: left要求其他标记具有float: right以便它们可以彼此浮动。

And then just give them a width of, lets say 60% to the left div and 35% to the right (you can use 40% too). 然后给它们一个宽度,比如说左div的宽度为60% ,右边为35% (您也可以使用40%)。 This way, they won't mix-up with each other. 这样,他们就不会彼此混淆。

One issue in not-floating-infront-of-each-other can be that the width of the divs is more than the width allowed. 彼此之间不浮动的一个问题可能是div的宽度大于允许的宽度。 To prevent that, you can use max-width to ensure that they are having a max of this value and they donot exceed the value. 为防止这种情况,您可以使用max-width来确保它们具有此值的最大值,并且不超过该值。

Answer for second question 回答第二个问题

You can create a clickable element using jQuery as: 您可以使用jQuery创建一个clickable元素,如下所示:

$('selector').click(function () {
 /* trigger click with 
  * $(this).trigger('click') or do what ever you want..
  */
}

This way, you can make an element clickable, and to change the cursor use 这样,您可以使元素可单击,并更改光标的使用

selector {
  cursor: pointer; // to create a pointer for that..
}

And the feel would be such as a click was occured! 感觉好像发生了点击!

Here is a jsfiddle of your code, which can be very helpful to allow people to see the interaction and quickly diagnose an issue. 这是您的代码的jsfiddle ,这对允许人们查看交互并快速诊断问题可能非常有帮助。

The issue is basically twofold, a you need a float:left on the images. 问题基本上是双重的,您需要在图片上使用float:left And your bounding box needs to match the exact size of the images. 并且您的边界框需要匹配图像的确切大小。 In the example I used 200px due to no images. 在示例中,由于没有图像,我使用了200px

In html all elements are clickable, but you need jQuery (easily handles the events) to capture the click event and "do" something (callback function). 在html中,所有元素都是可单击的,但是您需要jQuery(轻松处理事件)来捕获click事件并“执行”操作(回调函数)。 You then just set the cursor to pointer so that the UI feels like a clickable item. 然后,只需将光标设置为指针,即可使UI 感觉像是可单击的项目。

A sample jQuery item would be: 一个示例jQuery项目将是:

$('.item img').on('click',function(){
       // Do something here.
       alert('img clicked');
});

Suggest using classes over ids, minor issue though. 建议在ID上使用类,尽管是次要问题。

And this is almost the same answer as above...just saw it. 这几乎和上面的答案一样……只是看到了。

not really sure what your asking for so i made everything horizontal.. hope this helps 不太确定您的要求是什么,所以我将所有功能设置为水平。

HTML: HTML:

<div id="image-slider">
  <div id="images">
    <div class="item"><span class="finger"><img src="images/image0.jpg"></span> <span class="finger"><img src="images/image0.jpg"></span><span class="finger"> <img src="images/image0.jpg"></span> <span class="finger"><img src="images/image0.jpg"></span><span class="finger"> <img src="images/image0.jpg"></span> <span class="finger"><img src="images/image0.jpg"></span><span class="finger"> <img src="images/image0.jpg"></span> </div>
  </div>
  <table>
    <tr>
      <td><ul id="indicator"></td>
      <td><li>1</li></td>
      <td><li>2</li></td>
      <td><li>3</li></td>
      <td><li>4</li></td>
      <td><li>5</li>
        </ul></td>
    </tr>
  </table>
  <input type="button" class="switch" id="prev" value="&lt;">
  <input type="button" class="switch" id="next" value="&gt;">
</div>

CSS: CSS:

.finger:hover{cursor:pointer;}

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

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