让我们以这个为例:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <link type="text/css" rel="stylesheet" href="s.css">
    </head>

    <body>
        <div id="container">
            <p>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</p>
        </div>
    </body>
</html>

css:
* {
    margin:0px;
    padding:0px;
}

#container {
    margin:10px;
    width:400px;
    border: 2px solid red;
}

它显示如下:

我该怎么做才能将这种文本始终放在父 div 中?

最佳答案

您可以使用 word-wrap:break-word

#container {
    margin: 10px;
    width: 400px;
    border: 2px solid red;
    word-wrap: break-word;
}

jsFiddle example

或者,您也可以使用 overflow:hidden ,这将 overflow hidden 。这是假设您不希望文本换行。 jsFiddle example

最后,还有 overflow:scroll 可以让你滚动溢出。注意 - 无论文本长度如何,总会有一个滚动条。为避免这种情况,您还可以使用 overflow:auto 。我不知道你想要什么。 jsFiddle exampleoverflow:scroll

关于html - 如何将文本保留在父级内部?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19744299/

10-09 15:30
查看更多