2017-06-04 66 views
1

我试图调整我的<fieldset>元素的大小,但会自动缩小其内容。然后,我在我的CSS文件中添加了display:inline-block(正如许多类似主题的答案中所建议的那样)。但是,这完全没有帮助,一切都保持不变。我该怎么办?我想要一个小巧友好的解决方案,因为我不擅长CSS,因为您可能已经注意到了。干杯!防止字段集调整内容的大小

fieldset { 
 
    display: inline-block; 
 
    border: 1px solid blue; 
 
    background-color: white; 
 
    width: 40%; 
 
} 
 

 
legend { 
 
    display: inline-block; 
 
    padding: 4px 8px; 
 
    border: 1px solid blue; 
 
    font-size: 15px; 
 
    color: white; 
 
    background-color: blue; 
 
    text-align: left; 
 
} 
 

 
label { 
 
    width: 100px; 
 
    margin-right: 30px; 
 
    display: inline-block; 
 
    text-align: left; 
 
} 
 

 
input[type=text] { 
 
    width: 25%; 
 
    padding: 10px 20px; 
 
    margin: 0px 0; 
 
    display: inline-block; 
 
    border: 1px solid #ccc; 
 
    border-radius: 4px; 
 
    box-sizing: border-box; 
 
}
<fieldset> 
 
    <legend>XXX</legend> 
 
    <label for="username">Username:</label> 
 
    <input type="text" name="username"> 
 
</fieldset>

回答

1

如果您使用内容的宽度百分比,它会根据容器(字段集)的宽度将发生变化。

只需删除百分比值。

fieldset { 
 
    display: inline-block; 
 
    border: 1px solid blue; 
 
    background-color: white; 
 

 
} 
 

 
legend { 
 
    display: inline-block; 
 
    padding: 4px 8px; 
 
    border: 1px solid blue; 
 
    font-size: 15px; 
 
    color: white; 
 
    background-color: blue; 
 
    text-align: left; 
 
} 
 

 
label { 
 
    width: 100px; 
 
    display: inline-block; 
 
    text-align: left; 
 
} 
 

 
input[type=text] { 
 

 
    padding: 10px 20px; 
 
    margin: 0px 0; 
 
    display: inline-block; 
 
    border: 1px solid #ccc; 
 
    border-radius: 4px; 
 
    box-sizing: border-box; 
 
}
<fieldset> 
 
    <legend>XXX</legend> 
 
    <label for="username">Username:</label> 
 
    <input type="text" name="username"> 
 
</fieldset>