2016-11-30 51 views
0

寬度我有內容的可變長度內容股利,水平。 我想這取決於它的內容長度喜歡這個內容的div來自動調整大小寬度。例如,我希望能夠刪除任意的「20%」值,並相應地設置div大小。目前從寬度中移除20%的值會迫使其重新調整到其容器的100%。我錯過了什麼CSS?HTML/CSS - 自動尺寸的內容在一個包裝

Fiddle

.wrapper { 
 
    border: 2px solid green; 
 
} 
 
.content { 
 
    margin: 0 auto; 
 
    text-align: center; 
 
    border: 2px solid orange; 
 
    width: 20%; 
 
}
<div class="wrapper"> 
 
    <div class="content"> 
 
    <span>One</span> 
 
    <span>Two</span> 
 
    <span>Three</span> 
 
    <span>Four</span> 
 
    </div> 
 
</div>

回答

1

text-align:center在包裝並設置內容div的顯示內聯或內聯塊:

.wrapper { 
 
    border: 2px solid green; 
 
    text-align:center; 
 
} 
 
.content { 
 
    margin: 0 auto; 
 
    text-align: center; 
 
    border: 2px solid orange; 
 
    display: inline; 
 
}
<div class="wrapper"> 
 
    <div class="content"> 
 
    <span>One</span> 
 
    <span>Two</span> 
 
    <span>Three</span> 
 
    <span>Four</span> 
 
    </div> 
 
</div>

+1

正是我一直在尋找,謝謝 – noclist

+0

請記住調整較小的邊界就會崩潰 – Syden

+0

Downvoter保健佳品什麼時候? – j08691

1

如果你想流體伸縮動作我建議Flexbox的:

.wrapper { 
 
    border: 2px solid green; 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 
.content { 
 
    margin: 0 auto; 
 
    text-align: center; 
 
    border: 2px solid orange; 
 
    /*width: 20%;*/ 
 
}
<div class="wrapper"> 
 
    <div class="content"> 
 
    <span>One</span> 
 
    <span>Two</span> 
 
    <span>Three</span> 
 
    <span>Four</span> 
 
    </div> 
 
</div>

在線:

enter image description here

Flexbox的:

enter image description here