CSS 如何在CSS中的DIV后删除换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17973207/
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 23:45:03  来源:igfitidea点击:

How to remove line break after DIV in CSS

css

提问by alias51

Is there a way to prevent a line break after a div with css, but to have one div min width and the other max width?

有没有办法防止在带有 css 的 div 之后换行,但是有一个 div 最小宽度和另一个最大宽度?

For example I have

例如我有

<div class="label">My Label:</div>
<div class="text">My text</div>

and want it to display like:

并希望它显示如下:

My Label: My text

Where My text floats right, and my label takes all the remaining width?

我的文本向右浮动,而我的标签占据所有剩余宽度?

I am using the following CSS, but the .text div keeps wrapping:

我正在使用以下 CSS,但 .text div 不断换行:

div
{
    display: inline-block;
}

.label {
    border:1px solid blue;
    width:100%;
}

.text {
    border:1px solid red;
float:right;
}

**UPDATE: JS FIDDLE *http://jsfiddle.net/jPrMG/

**更新:JS FIDDLE * http://jsfiddle.net/jPrMG/

Thanks for your help

谢谢你的帮助

采纳答案by Ilya Streltsyn

You can use CSS table model:

您可以使用 CSS 表模型:

.label {
    border:1px solid blue;
    display: table-cell;
    width: 100%;
}

.text {
    border:1px solid red;
    display: table-cell;
    white-space: nowrap;
}

Fiddle

小提琴

回答by Spudley

It's not a 'line break' that you're seeing; it's because <div>elements default to being a blockelement.

这不是您看到的“换行符”;这是因为<div>元素默认为block元素。

If you want to change the behaviour so that they appear on the same line, you can change the displayproperty in CSS, like so:

如果你想改变行为使它们出现在同一行,你可以改变displayCSS 中的属性,如下所示:

display:inline;

or

或者

display:inline-block;

If you still want to have widthor min-widthproperty (as stated in the question) then you would need the latter of those two, inline-block.

如果您仍然想要拥有widthmin-width财产(如问题中所述),那么您将需要这两者中的后者,inline-block.

Hope that helps.

希望有帮助。