简体   繁体   English

HTML水平扩展div以适合儿童

[英]HTML expand div horizontally to fit children

I'm creating a horizontally scrolling web site. 我正在创建一个水平滚动的网站。 There's a container div which I want to retain a fixed height but expand as required horizontally to fit the content inside it. 有一个容器div,我想保持固定的高度,但可以根据需要水平扩展以适合其中的内容。 At the moment the div only expands horizontally as far as the page width. 目前,div仅水平扩展到页面宽度。 There are actually 9 images to display but only the first 4 are shown. 实际上有9张图像要显示,但仅显示前4张。 See code and image below. 请参见下面的代码和图像。 How do I make the container div expand horizontally to show all images please? 如何使容器div水平展开以显示所有图像?

在此处输入图片说明

css: CSS:

body
{
    background-color:#dbdbdb; 
}

div.infinite-container
{
    background-color:#db0080; 
    height:180px;
}

img.infinite-item
{
width="320"; 
height="180";
margin-right:8px;
margin-bottom:8px;
display:inline-block;
}

.infinite-more-link 
{
visibility:hidden;
}

PHP: PHP:

<div class="infinite-container">');


if ($num_results > 0)
{
    $array = array();
    while ($row = mysql_fetch_assoc($result))
    {
        $array[] = $row;
    }

    for ($i = 0; $i < $numImagesPerPage; $i++)
    {   
        $filePath = "animations/".$array[$i]['animationid'].".gif";
        echo('<img class="infinite-item" src="'.$filePath.'"/>');
    }
}    

echo('</div>');

This screenshot is after the changes below suggested by Andrei. 此屏幕快照是在Andrei建议的以下更改之后进行的。 The pink area is the container div. 粉色区域是容器div。 The images appear to break out below it. 图像似乎在其下方破裂。

在此处输入图片说明

From the code you posted, doing something like this should work: 从您发布的代码中,执行以下操作应该可以:

body
{
    background-color:#dbdbdb;
    overflow:auto;
}
div.infinite-container
{
    background-color:#db0080; 
    height:180px;
    display:inline-block;
    white-space: nowrap;
}

img.infinite-item
{
    width: 320px; 
    height: 180px;
    margin-right:8px;
    margin-bottom:8px;
    display:inline-block;
}

jsFiddle example: jsFiddle示例:

http://jsfiddle.net/S6Abd/ http://jsfiddle.net/S6Abd/

What this does is: 这是什么:

  • set the display mode to inline-block on the container. 将显示模式设置为在容器上inline-block This way the container will be as large as needed to contain all items. 这样,容器将达到容纳所有项目所需的大小。
  • set overflow:auto on body to show scroll-bars. body上设置overflow:auto以显示滚动条。
  • correct the width and height of each item. 纠正每个项目的widthheight
  • add white-space: nowrap; 添加white-space: nowrap; to the container to force the items to stay on one line. 到容器上以迫使物品停留在一条线上。

Add this CSS style :) 添加此CSS样式:)

div.infinite-container
{
    width:2952px; /* (320 + 8) * 9 = 2952 */
}

But on the serious note - DIV shows (kind of) all your images, only images 5-9 are in next line and because container have fixed height, then they are hidden. 但要特别注意的是-DIV显示(全部)您的所有图像,只有5-9图像在下一行,并且由于容器的高度固定,因此它们被隐藏了。

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

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