简体   繁体   English

使用CSS在图库中定位图像

[英]Image positioning in gallery with css

I'm new to css. 我是CSS的新手。 I have created a gallery with 6 images. 我创建了一个包含6张图像的画廊。 I have chose float: left. 我选择了float:左。 My issue is, the pictures are all on same line as each other. 我的问题是,图片彼此都在同一行上。 I want to have 3 on top and 3 below. 我想在上面放3个,在下面放3个。 How do I make this happen? 我如何做到这一点? Thanks 谢谢

您可以将图像包围在<div></div>标记内并在其上设置width属性或max-width,另一个选择是可以在每个图像后使用<br />跳到下一行。

There are many ways to do this so it depends on what fits your needs. 有很多方法可以做到这一点,所以这取决于您的需求。

You can add a <br> tag right after the 3rd image to display a new line. 您可以在第3张图片后添加<br>标记以显示新行。 (or wrap the new line in a paragraph <p><!-- code --></p> ). (或将新行包装在<p><!-- code --></p>段落中)。

Another way is wrapping your content in a block element. 另一种方法是将内容包装在block元素中。 For example, a DIV . 例如, DIV

Being that you're new to CSS I suggest starting off with good coding practices. 由于您是CSS的新手,因此建议您从良好的编码实践入手。

My snippet below is for RWD . 我的以下代码段适用于RWD

What I did is I wrapped my images in a div and used percents for the image size. 我所做的是将图像包裹在div中,并使用百分比表示图像大小。 By default images are displayed inline this will not go in a new line. 默认情况下,图像以内方式显示,因此不会在新行中显示。 The only time this happens is when they can't fill the space in the container. 发生这种情况的唯一时间是他们无法填充容器中的空间。 (if you want to have it float left without going on a new line; you would use white-space : nowrap . (如果您想让它向左浮动而不用换行,则可以使用white-space : nowrap

You may also want to take a look at CodePen . 您可能还想看看CodePen It's a community where they post live snippets of code that you can interact with (not to mention learn from). 在这个社区中,他们发布了您可以与之交互的实时代码片段(更不用说学习了)。 Plus Codepen supports lots of Preprocessors . 另外, Codepen还支持许多预处理器

Hope this helps to get started. 希望这有助于上手。

 .images { text-align: center; } .images img { width: 30%; } 
 <div class="images"> <img src="http://wallpapershdnow.com/download.php?f=animals/bears/baby-bears/baby-bear-wallpaper-4.jpg"> <img src="http://i.imgur.com/3eouyHS.jpg"> <img src="http://cdn4.teen.com/wp-content/uploads/2012/12/baby-polar-bears-main1.jpg"> <img src="http://cdn1.theodysseyonline.com/files/2015/11/14/6358312506953272101532239493_l-Baby-bear.jpg"> <img src="http://i.imgur.com/wYFHJNI.jpg"> <img src="http://image.mlive.com/home/mlive-media/width960/img/grandrapidspress/photo/2015/03/20/-6328b3da95a15820.JPG"> </div> 

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

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