【问题标题】:Permanent 2x3 CSS image gallery永久 2x3 CSS 图片库
【发布时间】:2012-09-10 18:47:30
【问题描述】:

我有非常基本的图片库,如何将其固定为 3 列 x 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代码

#画廊{ } div.img { 边距:2px; 高度:自动; 宽度:自动; 浮动:对; } div.img img { 宽度:200px; /* img 的大小是多少*/ 高度:200px; 显示:内联; 左边距:30px; 右边距:30px; 边距底部:20px; 边框:1px 实心#ffffff; } /* div.img a:hover img { 边框:1px 实心#0000ff; }*/ div.desc { 文本对齐:居中; 字体粗细:正常; 宽度:120px; 边距:30px; } .descHead { 右边距:30px; 边距顶部:20px; 底部边距:15px; 字体系列:Tahoma; 字体大小:24px; 颜色:#323232; } .desc { margin-right: 30px !important; margin-bottom: 40px !important; 字体系列:Tahoma; 字体大小:14px; 颜色:#323232; }

【问题讨论】:

  • 您的图片是否保证尺寸相同还是有所不同?

标签: html css


【解决方案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 显示属性将起作用。

还应该指出节/标题/段落标签可能比无意义的div 标签更合适。或者,figure/figcaption 集合也可能是合适的:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 1970-01-01
        相关资源
        最近更新 更多