Html 如何为显示内联的 Div 元素设置固定宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8262459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:39:41  来源:igfitidea点击:

How to Set a Fixed Width for a Div Element with Display Inline?

asp.nethtmlcss

提问by The Light

I have the below div element:

我有以下 div 元素:

<div style="width: 40px; height: 40px; overflow: hidden; display: inline;">
                Hello my friend, How are you?</div>

The width is always ignored! how could I have it as 40px?

宽度总是被忽略!我怎么能把它设为 40px?

Thanks,

谢谢,

回答by Rob W

Use display:inline-block;. Size properties are ignored at most elements with display:inline.

使用display:inline-block;. 大多数带有display:inline.

<div style="width: 40px; height: 40px; overflow: hidden; display: inline-block;">
            Hello my friend, How are you?</div>