永久2x3 CSS图库

时间:2012-09-18 13:34:23

标签: html css html5 css3

我有非常基本的图库,如何将其固定为3列2行

这是HTML代码

<div id="gallery">
        <div class="img">
            <a target="_blank" href="klematis_big.htm"> <img src="../zeela/img/image_1.png" alt="Klematis"/> </a>
            <div class="descHead">
                כותרת
            </div>
            <div class="desc">
                Add a description of the image here
            </div>
        </div>
        <div class="img">
            <a target="_blank" href="klematis2_big.htm"> <img src="../zeela/img/image_1.png" alt="Klematis" /> </a>
            <div class="descHead">
                כותרת
            </div>
            <div class="desc">
                Add a description of the image here
            </div>
        </div>
        <div class="img">
            <a target="_blank" href="klematis3_big.htm"> <img src="../zeela/img/image_1.png" alt="Klematis" /> </a>
            <div class="descHead">
                כותרת
            </div>
            <div class="desc">
                Add a description of the image here
            </div>
        </div>
        <div class="img">
            <a target="_blank" href="klematis4_big.htm"> <img src="../zeela/img/image_1.png" alt="Klematis" /> </a>
            <div class="descHead">
                כותרת
            </div>

            <div class="desc">
                Add a description of the image here
            </div>

        </div>
        <div class="img">
            <a target="_blank" href="klematis4_big.htm"> <img src="../zeela/img/image_1.png" alt="Klematis"  /> </a>
            <div class="descHead">
                כותרת
            </div>
            <div class="desc">
                Add a description of the image here
            </div>

        </div>
        <div class="img">
            <a target="_blank" href="klematis4_big.htm"> <img src="../zeela/img/image_1.png" alt="Klematis" /> </a>
            <div class="descHead">
                כותרת
            </div>
            <div class="desc">
                Add a description of the image here
            </div>

        </div>

    </div>

这是CSS代码

#gallery{

}
div.img {
    margin: 2px;
    height: auto;
    width: auto;
    float: right;
}
div.img img {
    width: 200px; /* what is the img size*/
    height: 200px;
    display: inline;
    margin-left: 30px;
    margin-right: 30px;
    margin-bottom: 20px;
    border: 1px solid #ffffff;
}

/*
 div.img a:hover img {
 border: 1px solid #0000ff;
 }*/
div.desc {
    text-align: center;
    font-weight: normal;
    width: 120px;
    margin: 30px;
}
.descHead {
    margin-right: 30px;
    margin-top: 20px;
    margin-bottom: 15px;
    font-family: Tahoma;
    font-size: 24px;
    color: #323232;
}

.desc {
    margin-right: 30px !important;
    margin-bottom: 40px !important;
    font-family: Tahoma;
    font-size: 14px;
    color: #323232;
}

3 个答案:

答案 0 :(得分:1)

如果您无法更改HTML,则可以执行此操作:

.gallery {
    width: /* ((width of .image including padding/margin) * 3) */
    overflow: hidden;
    margin: /* top: 0, right: 0, bottom: -(size of .image's bottom margin), left: -(size of .image's left margin) */
    /*
    yes, that's right, the left/bottom margins are negative, but it's purely presentational
    for left floated .image, replace right with left
    */
}

否则,使用CSS显示属性将起作用。

还应该指出section / heading / paragraph标签可能比无意义的div标签更合适。或者,图/ figcaption集合也可能是合适的:

http://html5doctor.com/the-figure-figcaption-elements/

答案 1 :(得分:0)

我会考虑使用CSS显示:table:http://www.w3schools.com/cssref/pr_class_display.asp

答案 2 :(得分:0)

CSS:网格

我更喜欢使用 grid 来创建可靠的代码。

试试这个

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 0px 0px;
  grid-template-areas:
    "img1_1 img1_2 img1_3"
    "img2_1 img2_2 img2_3";
}

.img{
  border: 1px solid #000;
  text-align: center;
}

.img1_1 { grid-area: img1_1; }

.img1_2 { grid-area: img1_2; }

.img1_3 { grid-area: img1_3; }

.img2_1 { grid-area: img2_1; }

.img2_2 { grid-area: img2_2; }

.img2_3 { grid-area: img2_3; }
<div class="grid-container">
  <div class="img img1_1"> img1_1</div>
  <div class="img img1_2"> img1_2</div>
  <div class="img img1_3"> img1_3</div>
  <div class="img img2_1"> img2_1</div>
  <div class="img img2_2"> img2_2</div>
  <div class="img img2_3"> img2_3</div>
</div>