使文本适应div

6

我一直在重新制作我父亲的网站,该网站是在九十年代创建的(呃),但是我一直无法让文本适应div并水平对齐。我需要让文本相邻以便它们适合div中。以下是页面的代码:jsfiddle

示例HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<body>
    <div> 
        <img id="header" src="http://www.salesprofessionalsinc.com/images/new%20logo.jpg">
    </div>
    <div id="links">
        <div class="home">
            <a href="index.html"><span></span>Home</a>
        </div>
        <div class="home">
            <a href="insidestaff.html"><span></span>Inside Staff</a>
        </div>
        <div class="home">
            <a href="mission.html"><span></span>Our Mission</a> 
        </div>
    </div>

样例 CSS

div img#header{
    width: 50%;
    height: 15%;
    margin-left: 125px;
    margin-right: auto;
}center input#search{
    width: 300px;
    height: 45px;
    border: solid 1px black;
    margin-top: 55px;
    font-size: 25px;
}center button#searchbutt{
    border: solid 1px black;
    width: 65px;
    height: 30px;
}#searchbutt:hover{
    color: #FFF;
    background-color: #000;
}#searchbutt{
    background-color: #FFF;
    color: #000;
}#links{
    height: 40px;
    width: 100%;
    text-align: center;
    line-height: 40px;
}.home{
    width: 80px;
    text-align: center;
    height: 40px;
    background-color: white;
    color: black;
    padding-left: auto;
    padding-right: auto;
    display: inline-block;
    border: solid 1px black;
    vertical-align: middle;
}.DL{
    width: 95;
    text-decoration: none;
    text-align: center;
    height: 40px;
    background-color: white;
    color: black;
    padding-left: auto;
    padding-right: auto;
    display: inline-block;
    border: solid 1px black;
}.home a{
    text-decoration: none;
    color: gray;
}.DL a{
    text-decoration: none;
    color: gray;
}div center a#DLbutt{
    border: solid 1px black;
    width: 100px;
    height: 50px;
    background-color: black;
}div center a#DLbutt{
    text-decoration: none;
    color: gray;
}#download{
    padding-top: 30px;
}html{
     background-image: url("watermark.gif");
}.home a span{
     position:absolute; 
     width:100%;
     height:100%;
     top:0;
     left: 0;
     display: inline-block;
     vertical-align: middle;
     line-height: normal;
}

我需要“我们的使命部分”在同一个 div 中。

为什么你要设置 .home a span 规则,它位于整个页面的顶部? - j08691
此外,将行高设置为与您的 div 高度匹配仅适用于垂直居中单行文本。您可以通过将 .home div 的宽度从 80px 更改为 90px 来轻松解决这个问题。http://jsfiddle.net/j08691/h2hchg2q/6/ - j08691
你考虑过在home类中添加* word-wrap: break-word; *吗? - cubanGuy
1
我想让这个 span 只占用它所在的 div。 - user4920910
1
不要使用line-height:40px来控制填充,只需在<a>标签上使用padding: 20px即可。避免在定义ID #searchbutt时使用选择器button#searchbutt,因为它已经是唯一的,所以不需要添加DOM元素...这是不好的做法。 - MonteCristo
3个回答

0
现代Web组件解决方案:<fit-text> 使用<fit-text> Web组件就能实现这个功能,详情请见:https://fit-text.github.io

<script src="https://fit-text.github.io/element.js"></script>

<style>
  fit-text {
    width:25vw;
    border:1px solid blue;
  }
</style>

<fit-text>Hello Cool<br>Web Component<br>World!</fit-text>


0
你需要将 div.home 的显示设置为 display: table-cell,然后移除你设置的 line-height。这样应该会自动对齐文本。
div img#header {
    width: 50%;
    height: 15%;
    margin-left: 125px;
    margin-right: auto;
}
#links {
    height: 40px;
    margin-left: 125px;
    text-align: center;
    display: inline-block;
}
.home {
    width: 80px;
    text-align: center;
    height: 40px;
    background-color: white;
    color: black;
    padding-left: auto;
    padding-right: auto;
    display: table-cell;
    border: solid 1px black;
    vertical-align: middle;
}
.home a {
    text-decoration: none;
    color: gray;
}

http://jsfiddle.net/03syn1to/


-3

我相信这就是你所要求的。你想让“我们的使命”垂直地显示在按钮上。只需更新此代码即可。然后它应该可以正常工作。告诉我你的想法。

.home{
   width: 80px;
   text-align: center;
   height: relative;
   background-color: white;
   color: black;
   padding-left: auto;
   padding-right: auto;
   display: inline-block;
   border: solid 1px black;
   vertical-align: middle;
}

我改变了高度.. 高度: 相对;


2
"relative" 不是 "height" 的有效值。 - j08691

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接