简体   繁体   English

HTML:如何在同一行上正确格式化4个元素?

[英]HTML: How to correctly format 4 elements on the same line?

I have a page which should display 4 elements at a time: Name1: value Name2: value 我有一个页面,一次应显示4个元素:Name1:value Name2:value

I have used CSS to accomplish this and it works pretty well but if value is extremely long, it should continue on the next line like so: 我已经使用CSS来完成这个并且它工作得很好但是如果值非常长,它应该继续下一行,如下所示:

Name1:  sdlhjbdsl      Name2: value
        aldlhsdfidshf
        sifhflkgh
Name3:  hhlsdifsdlfh   Name4: sdhsdlkfbsdljkbs
                              aiuhdwdifnb
                              s;kkvnsdfk;vn

but it actually happens like this: 但它实际上是这样的:

Name1:  sdlhjbdsl      
        aldlhsdfidshf
        sifhflkgh
                       Name2: sdhsdlkfbsdljkbs
                              aiuhdwdifnb
                              s;kkvnsdfk;vn

How can I best accomplish the first example? 我怎样才能最好地完成第一个例子? This is my CSS thus far: 到目前为止这是我的CSS:

#printable_block1
{ 

    font-family: "Arial Unicode MS","Arial","Sans-Serif";
            font-size: 12px;
    display:inline;
            position:absolute;
    margin-left: 12px; 
    max-width:23%; 
}
#printable_block1_value 
{
    font-family: "Arial Unicode MS","Arial","Sans-Serif";
            font-size: 12px;
    display:block;
            position:relative;
    margin-left: 25%; 
    max-width:30%; 
}
#printable_block2 
{
    font-family: "Arial Unicode MS","Arial","Sans-Serif";
            font-size: 12px;
    display:inline;
            position:absolute;
    margin-left: 50%; 
    max-width:24%;
}
#printable_block2_value 
{ 
    font-family: "Arial Unicode MS","Arial","Sans-Serif";
            font-size: 12px;
    display:block;
            position:relative;
    margin-left: 75%; 
    max-width:25%;
}

Then this is my HTML: 那么这是我的HTML:

<div id="block_container_3_blocks"> 
    <div id="printable_block1">
        <span>
           Name1:
       </span></div>
            <div id="printable_block1_value">
       <span>
           value1
       </span></div>
    <div id="printable_block2">
        <span>Name2:</span></div>
    <div id="printable_block2_value">
        <span>
           value2
        </span></div>   
</div>

Your method is very "haxy", but you can add top:0 to re-align your blocks. 你的方法非常“haxy”,但你可以添加top:0来重新对齐你的块。

Instead of all this, consider just using: 而不是所有这些,考虑只使用:

#block_container_3_blocks {font-size:0}
#block_container_3_blocks>div {
    display:inline-block;
    vertical-align:top;
    width:25%; /* adjust to allow different numbers of blocks */
    font-size:12pt; /* whatever your default is */
}

The "font-size:0" part is to make sure any spaces between your elements don't mess up the layout. “font-size:0”部分是为了确保元素之间的任何空格都不会弄乱布局。

You can use another way like 你可以用另一种方式

 .mainBlocks { clear: both; } .divClass{ width:25%; float:left; font-family: "Arial Unicode MS","Arial","Sans-Serif"; font-size: 12px; } 
 <div class="mainBlocks"> <div class="divClass"> <span> Name1: </span> </div> <div class="divClass"> <span> value1 </span> </div> <div class="divClass"> <span>Name2:</span> </div> <div class="divClass"> <span> value2 </span> </div> </div> <div class="mainBlocks"> <div class="divClass"> <span> Name1: </span> </div> <div class="divClass"> <span> value1 </span> </div> <div class="divClass"> <span>Name2:</span> </div> <div class="divClass"> <span> value2 </span> </div> </div> 

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

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