CSS: I can't set width to “auto” always appear 100%

后端 未结 7 867
太阳男子
太阳男子 2021-01-28 16:56

for some reason

the width of the div is 100% and if i set it to auto, nothing changes. tried display: block; and still nothing.

what I have in

7条回答
  •  伪装坚强ぢ
    2021-01-28 17:16

    Setting width:auto; is close to the same as setting width:100%; (the only real difference is that they handle margin and padding differently).

    Also, div objects are by default block elements, so setting display:block; won't change their behavior.


    You said you want the div to take up the width of the words. To do that you can either set display:table-cell (which is not very IE friendly) or you can float the div and it will snap to fit the contents inside.

    .box { float:left; }
    

    Make sure to properly clear your float after the div to avoid breaking the layout of contents below it.

    .clear { clear:both; height:0px; } 
    
    

提交回复
热议问题