【问题标题】:How to use a bottom-border for underneath a word, limited length and centred?如何在单词下方使用底部边框,长度有限且居中?
【发布时间】:2017-06-23 23:05:18
【问题描述】:

this页面的底部,我试图将版权文本下的灰线设置为30%,所以它比文本短。我的问题是,当设置为小于文本长度的 % 时,它会将文本放在 2 行上。我想要一行文字。

版权箱代码:

.copyright-container{
	box-sizing: border-box;
padding-right:20px;padding-left:20px;
background-color: black;
text-align: center;
position:relative;
bottom:0;
width:100%;
height:220px;
    color: white;
}

.vert-align{
position: relative;
top: 50%;
}
.headline {
    width: 30%; 
    line-height: 1.5em;
    border-bottom:2px solid #6d6666;
    margin:0px auto; 
    color:white;
}

其他代码:

<div class="copyright-container">
		<div class="vert-align">
			<p class="headline">Copyright &copy; 2017 RyanTeaches - All Rights Reserved.</p>
		</div>
	</div>

我是 html 和 css 新手

非常感谢, 瑞恩

【问题讨论】:

  • 仅使用border-bottom 规则是无法实现的。边框总是跨越它所附着的整个元素。您可以在 &lt;p class='headline'&gt; 中创建一个宽度为 30% 的新元素,将 height 设置为 2px 并将 background 设置为 gray

标签: javascript css html containers


【解决方案1】:

你可以使用伪元素:

.headline:before {
    content: "";
    position: absolute;
    left: 43%;
    bottom: 0;
    height: 1px;
    width: 15%;
    border-bottom: 1px solid #6d6666;
}

【讨论】:

    【解决方案2】:

    您将无法使用分配给文本的样式属性来执行此操作。但是,您可以如下图所示进行:

    .headline {
        line-height: 1.5em;
        margin: 0px auto;
        color: black;
        text-align: center;
    }
    
    .headline_under {
        border-bottom: 1px solid black;
        max-width: 120px;
        margin: 0 auto;
        margin-top: 10px;
    }
    <p class="headline">Copyright © 2017 RyanTeaches - All Rights Reserved.</p>
    <div class="headline_under"></div>

    【讨论】:

      【解决方案3】:

      .headline {
       
          line-height: 2.5em;
          margin:0px auto; 
      
      }
      
      .headline:before {
          content: "";
          position: absolute;
          left: 40%;
          bottom: 0;
          height: 1px;
          width: 20%;
          border-bottom: 2px solid #6d6666;
          color:white;
      }

      这个组合为我排序!谢谢你们!

      我想将它插入到每一页中,只使用一行代码。我不能使用php。我可以为此使用JS吗,如果可以,我将如何编写JS?

      干杯!

      【讨论】:

      • 没问题,很高兴为您提供帮助。如果您觉得我的回答解决了这个问题,请相应地标记!如果你想在每一页上插入它,你需要使用 PHP 之类的东西。
      猜你喜欢
      • 2021-05-14
      • 2012-12-25
      • 2021-12-24
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多