简体   繁体   English

使用JavaScript添加内容时,使HTML div扩展

[英]Make HTML div expand when content is added with Javascript

I know there are other questions that deal with this, but they aren't working for me. 我知道还有其他问题可以解决,但这些问题对我没有帮助。

I have a <div class="column"> and inside this div there is another div called <div class="truck"> . 我有一个<div class="column">和这里面div还有另一个div称为<div class="truck"> Now there is going to be a button that will add other "truck" columns to the column div. 现在将有一个按钮,该按钮会将其他“卡车”列添加到column div中。 I want the column div to expand when I add a new truck. 我要在添加新卡车时扩大div列。 As it stands, the extra truck is added, but to view the other truck, I have to scroll to the left. 就目前而言,添加了额外的卡车,但是要查看其他卡车,我必须向左滚动。 I want the column to expand with each new truck that is added. 我希望该列随着添加的每个新卡车而扩展。 The below code doesn't have the button that I was referring to, as I have Javascript adding the next truck to each column at the moment. 下面的代码没有我所指的按钮,因为我现在使用Javascript将下一个卡车添加到每列中。

Here is my HTML code: 这是我的HTML代码:

<div id="main">
    <div id="calendar">
        <div class="column" id="day1">
            <div class = "truck" id="day1_truck1">
                <p>This is truck one</p>
            </div>
        </div>

        <div class="column" id="day2">
            <div class = "truck" id="day2_truck1">
                <p>This is truck one</p>
            </div>
        </div>

        <div class="column" id="day3">
            <div class = "truck" id="day3_truck1">
                <p>This is truck one</p>
            </div>
        </div>

        <div class="column" id="day4">
            <div class = "truck" id="day4_truck1">
                <p>This is truck one</p>
            </div>
        </div>

        <div class="column" id="day5">
            <div class = "truck" id="day5_truck1">
                <p>This is truck one</p>
            </div>
        </div>

        <div class="column" id="day6">
            <div class = "truck" id="day6_truck1">
                <p>This is truck one</p>
            </div>
        </div>

        <div class="column" id="day7">
            <div class = "truck" id="day7_truck1">
                <p>This is truck one</p>
            </div>
        </div>
    </div>
</div>

Here is the CSS: 这是CSS:

#main
{
    border-style: solid;
    border-width: 1px;
    border-color: black; 
    width:97%;
    height:900px;
    margin:50px, auto, 10px, auto; 
    padding: 1%;
    overflow: auto; 
    white-space: nowrap;

}
#calendar
{

}
.column
{
    border-style: solid;
    border-width: 1px;
    border-color: black; 
    min-width:12%;
    max-width:100%;
    width:auto;
    height:800px;
    display: inline-block;
    margin-left:1%; 
    overflow: auto;

}
.header
{
    padding:0;
    margin:0;
    text-align: center; 
    font-style: bold; 
}

.truck
{
    width:80%;
    border-style: solid;
    border-width: 1px;
    border-color: black; 
    display:inline-block;
}

Here is what it looks like: 看起来像这样:

在此处输入图片说明

You can see that the extra truck div is cut off. 您会看到多余的卡车div被切断。 Here, you can scroll to see it, but I want both divs to be displayed. 在这里,您可以滚动查看它,但我希望同时显示两个div。

What am I doing wrong? 我究竟做错了什么? I have tried to set the overflow to hidden and other suggestions from these links: 我试图将这些链接的溢出设置为hidden和其他建议:

make div's height expand with its content 使div的高度随其内容扩展

Make non-wrapping div expand when content overflows the page 当内容溢出页面时,使非包装div扩展

I've also tried a couple other things from a couple other sites. 我还尝试了其他几个站点的其他一些事情。

Any help would be appreciated. 任何帮助,将不胜感激。 I know this can be done with Javascript, but I'm pretty sure that it can be done without it. 我知道可以使用Javascript完成此操作,但是我很确定没有它也可以完成。 If Javascript is my only option, that's fine, I just figured I'd try to avoid it as doing it in HTML/CSS would be simpler. 如果Java语言是我唯一的选择,那很好,我只是想避免使用它,因为在HTML / CSS中这样做会更简单。

So you are trying to make the new truck column go beside the other truck in the same column or underneath it? 因此,您是否要尝试使新的卡车立柱在同一列中或在其下方与其他卡车并排?

If you want the div to go beside ( DEMO ) the previous one change your .truck CSS code to this: 如果您希望div放在(DEMO)旁边,则前一个将.truck CSS代码更改为:

        .truck
    {
        width:auto;
        border-style: solid;
        border-width: 1px;
        border-color: black; 
        display:inline-block;
    }

To make it post underneath, change the .truck CSS to this: 要将其发布在下面,请将.truck CSS更改为此:

    .truck
    {
        width:auto;
        border-style: solid;
        border-width: 1px;
        border-color: black; 
        display:block;
    }

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

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