javascript - 如何防止div文本溢出容器

标签 javascript html css

我有包含在 div 中的文本。我向其添加了 20px 的左侧填充,我认为这就是为什么文本从 div 右侧溢出的原因,但是有什么办法可以防止这种情况发生吗?这是代码。

.trainer-wrapper{
    position: relative;
    width: 100%;
    height: 700px;
    background-color: #F6F6F6;
}

.top-trainer{
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    height: 90px;
}

#bottom-trainer{
    position: relative;
    width: 100%;
    height: 100px;
    color: black;
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    overflow: hidden;
    padding-left: 20px;
    word-wrap: break-word;
}

#the-team{
    padding-left: 20px;
    padding-top: 20px;
    font-size: 42px;
    font-family: 'Roboto', sans-serif;
    font-weight: 500;
    color: black;
}

span{
    border-bottom: 1px solid black;
}
<html>
    <head>
        <link rel = "stylesheet" type = "text/css" href = "main.css">
         <link href="https://fonts.googleapis.com/css?family=Roboto:400,500" rel="stylesheet">
    </head>

<body>
    <div class = "trainer-wrapper">
        <div class = "top-trainer">
            <h2 id = "the-team"><span>The Team</span></h2>
        </div>

        <div id = "bottom-trainer">At Vizion Fitness we have assembled the greatest trainers in the country to help our customers achieve their dream body. Our trainers are all NSCA certified and have established themselves as being one of the best at what they do. Whether it be atheltes or commited individuals wanting to improve themselves, we have the team to help you achieve your goals. Each of our trainers specializes in a specific sport for athletes wanting to improve their skills however they are all trainers for any individual. Come check out the best team in DFW metroplex!</div>
    </div>

</body>
</html>

最佳答案

只需从#bottom-trainer中删除width: 100%:

.trainer-wrapper{
    position: relative;
    width: 100%;
    height: 700px;
    background-color: #F6F6F6;
}

.top-trainer{
    position: relative;
    top: 0;
    left: 0;
    width: 100%;
    height: 90px;
}

#bottom-trainer{
    position: relative;
    height: 100px;
    color: black;
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    overflow: hidden;
    padding-left: 20px;
    word-wrap: break-word;
}

#the-team{
    padding-left: 20px;
    padding-top: 20px;
    font-size: 42px;
    font-family: 'Roboto', sans-serif;
    font-weight: 500;
    color: black;
}

span{
    border-bottom: 1px solid black;
}
<html>
    <head>
        <link rel = "stylesheet" type = "text/css" href = "main.css">
         <link href="https://fonts.googleapis.com/css?family=Roboto:400,500" rel="stylesheet">
    </head>

<body>
    <div class = "trainer-wrapper">
        <div class = "top-trainer">
            <h2 id = "the-team"><span>The Team</span></h2>
        </div>

        <div id = "bottom-trainer">At Vizion Fitness we have assembled the greatest trainers in the country to help our customers achieve their dream body. Our trainers are all NSCA certified and have established themselves as being one of the best at what they do. Whether it be atheltes or commited individuals wanting to improve themselves, we have the team to help you achieve your goals. Each of our trainers specializes in a specific sport for athletes wanting to improve their skills however they are all trainers for any individual. Come check out the best team in DFW metroplex!</div>
    </div>

</body>
</html>

通过将该 div 的宽度设置为 100%,它实际上获得了其父级的宽度。问题是您的 padding-left 也为 20px,它会将该元素向右移动 20px。

关于javascript - 如何防止div文本溢出容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40952174/

相关文章:

html - 只需 1 格调整边距

javascript - 非构造函数中直接super调用是非法的,改用super ."constructor"()

html - 如何让我的应用程序在移动设备上填满整个高度?

javascript - 免费代码营 : TWITCH TV API: Trying to move everything from getJSONs to AJAX requests

javascript - Tablesorter 无法在 updateComplete 上应用搜索条件

html - 宽度为 100% 的 td 在 div 内,位置为绝对

javascript - 对 Bootstrap 下拉项选择执行操作

javascript - 在函数中对变量进行分组 - 第 2 部分

javascript - 操作系统和 javascript/HTML5 可以访问 localstorage 吗?

javascript - 文本框 css 悬停和按钮显示 asp.net c#