简体   繁体   English

使用CSS浮点数排列div元素

[英]Arranging div elements using css floats

I have the following div elements that are generated randomly so I can't make changes to a single div element.Here is the example http://jsfiddle.net/y638o46h/2/ .But I need a particular change. 我有以下div元素是随机生成的,因此我无法对单个div元素进行更改。这里是示例http://jsfiddle.net/y638o46h/2/,但是我需要进行特殊更改。 I want the space occupied by the first and the fourth posts to be occupied by a single post.ie the first image needs to be twice the size of the rest.In total five images will be rendered then. 我希望第一条和第四条帖子占用的空间被一个帖子占据,即第一张图片需要是其余图片的两倍。然后总共要渲染五张图片。 I've tried doing it but can't figure out how. 我曾尝试这样做,但不知道如何做。

html HTML

<div class="relatedposts">
 <div class="relatedthumb">
     <img src="http://placekitten.com/336/200" >
        <h3 class="justin-cover">Lets make this work</h3>
 </div>
  <div class="relatedthumb">
     <img src="http://placekitten.com/336/200" >
         <h3 class="justin-cover">Lets make this work</h3>
 </div>
  <div class="relatedthumb">
     <img src="http://placekitten.com/336/200" >
         <h3 class="justin-cover">Lets make this work</h3>
 </div>
  <div class="relatedthumb">
     <img src="http://placekitten.com/336/200" >
         <h3 class="justin-cover">Lets make this work</h3>
 </div>
  <div class="relatedthumb">
     <img src="http://placekitten.com/336/200" >
         <h3 class="justin-cover">Lets make this work</h3>
 </div>
  <div class="relatedthumb">
     <img src="http://placekitten.com/336/200" >
        <h3 class="justin-cover">This one clearly has too many lines that do not fit</h3>
 </div>   

css CSS

*{
margin: 0;
padding: 0;
box-sizing: border-box;}



.relatedposts {
display:table; 
width:1024px;font-size: 0;
/* fix inline gap */
margin: 0 auto;}



.relatedthumb {
float: left;
margin-left:5px;
position: relative;
margin-bottom:10px;
}
.relatedthumb img {
text-align:center;
}
.justin-cover {
color: #fff;
font-size: 30px;
font-weight: 500;
/* height: 30%; */
width: 100%;
position: absolute;
bottom: 0;
left:0;
background: rgba(0,0,0,0.5);
padding: 10px;
transition: all 0.5s;
}

You can try something like this in CSS : 您可以在CSS中尝试类似的方法:

.relatedthumb:first-child img {
    height: 300px;   /* with your desire size */
    width:300px;     /* with your desire size */
}

Here, the :first-child will target the first element only. 在这里, :first-child将只针对第一个元素。

as i figure out you need those div to represent differently according to the number. 如我所知,您需要那些div根据数字来表示不同的内容。

an example : the first image red border and the fourth blue border 示例:第一个图像红色边框和第四个蓝色边框

you can use jquery to solve this 您可以使用jQuery来解决这个问题

you can set two class 你可以设置两个班级

class="relatedthumb special"

for the div you want to make it special 对于div,您要使其特别

$(".specail").css("border","red")

and if it not the solution you search please explain your problem little more 如果不是您要寻找的解决方案,请多解释一下您的问题

or you can do it with out a class : 或者你也可以不用上课:

var count = 0;

$( "#relatedthumb " ).each(function( i ) {
    if(count%4 == 0)
    {
       this.style.width= "400";
       this.style.height= "400";
     }
     count++;
  });

so first image in every iterate will be with size 400*400 因此,每个迭代中的第一张图片的尺寸为400 * 400

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

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