简体   繁体   English

我怎样才能让我的 css 边框一直环绕我的 html 代码?

[英]How can I get my css border to wrap all the way around my html code?

I am having an issue with a border all the way around my HTML text using CSS.我在使用 CSS 的 HTML 文本周围一直存在边框问题。

How can I get the border to go around the text completely instead of going through the text?如何让边框完全绕过文本而不是穿过文本?

My HTML and CSS code is the following:我的 HTML 和 CSS 代码如下:

 .header2 { position: relative; float: left; height: 75px; width: 1250px; background-color:white; left: 50px; top: 45px; margin-top: none; word-spacing: 25px; font-size: 14; border: 1px solid black; }
 <div class = "header2"> <h1>News</h1> <h2> <a href="#" class="H"><span>Home</span></a> <a href="#" class="N"><span>Caronavirus</span></a> <a href="#" class="S"><span>Video</span></a> <a href="#" class="R"><span>World</span></a> <a href="#" class="W"><span>US Canada</span></a> <a href="#" class="T"><span>Uk</span></a> <a href="#" class="F"><span>Business</span></a> <a href="#" class="C"><span>Tech</span></a> <a href="#" class="M"><span>Science</span></a> <a href="#" class="M"><span>Stories</span></a> <a href="#" class="M"><span>More</span></a> </h2> </div>



The results are the border interrupting the text at the bottom, rather than wrapping around it.结果是边框在底部打断文本,而不是环绕它。

html css代码1]

You defined height as 75px , which simply isn't high enough, ie higher than the contents.您将height定义为75px ,这根本不够高,即高于内容。 Change that to auto to automatically adjust the height (and with it the border) to the contents:将其更改为auto以自动调整内容的高度(以及边框):

 .header2 { position: relative; float: left; height: auto; width: 1250px; background-color:white; left: 50px; top: 45px; margin-top: none; word-spacing: 25px; font-size: 14; border: 1px solid black; }
 <div class = "header2"> <h1>News</h1> <h2> <a href="#" class="H"><span>Home</span></a> <a href="#" class="N"><span>Caronavirus</span></a> <a href="#" class="S"><span>Video</span></a> <a href="#" class="R"><span>World</span></a> <a href="#" class="W"><span>US Canada</span></a> <a href="#" class="T"><span>Uk</span></a> <a href="#" class="F"><span>Business</span></a> <a href="#" class="C"><span>Tech</span></a> <a href="#" class="M"><span>Science</span></a> <a href="#" class="M"><span>Stories</span></a> <a href="#" class="M"><span>More</span></a> </h2> </div>

The problem is the height of your header div is an absolute value (px) and 75px is shorter than your links.问题是您的标题 div 的高度是一个绝对值 (px),而 75px 比您的链接短。

You can fix the border going through the links by increasing the height of the header class.您可以通过增加标题类的高度来修复通过链接的边框。

For example:例如:

.header2 {
  position: relative;
  float: left;
  height: 120px;
  width: 1250px;
  background-color: white;
  left: 50px;
  top: 45px;
  margin-top: none;
  word-spacing: 25px;
  font-size: 14;
  border: 1px solid black;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM