将Div左对齐文本居中[关闭](Center a Div Left Justified Text [closed])

编程入门 行业动态 更新时间:2024-10-28 13:25:49
将Div左对齐文本居中[关闭](Center a Div Left Justified Text [closed])

我有一些标题文本,大字体,两到四行。 它左对齐,但我也希望它水平和垂直居中。 问题是它在div的末尾包裹并留下一些空白区域(设置为600px的宽度)。 所以我希望它水平居中,不是基于600px,而是基于最长线的宽度。 有没有办法让div宽度成为最长文本行的长度?

I have some header text that is large font, two to four lines. It is left justified, but I also want it centered horizontally and vertically. The problem is that it wraps and leaves some blank space at the end of the div (which is set at a width of 600px). So I want it to be centered horizontally, based not on 600px, but the width of the longest line. Is there a way to make the div width the length of the longest line of text?

最满意答案

我想这就是你想要的?

#container {
  background:gold;
  width:600px;
  font-size:2em;
  display:inline-block;
  }

#title {
  width:400px;
  margin-right:100px;
  margin-left:100px;
  margin-top:50px;
  margin-bottom:50px;
  font-weight:600;
  } 
  
<html>
<head></head>
<body>
  
<div id=container>
<p id=title>Here it comes a long sentence that takes about 2 lines.</p>
</div>
  
</body>
</html> 
  
 

I guess that's what you want?

#container {
  background:gold;
  width:600px;
  font-size:2em;
  display:inline-block;
  }

#title {
  width:400px;
  margin-right:100px;
  margin-left:100px;
  margin-top:50px;
  margin-bottom:50px;
  font-weight:600;
  } 
  
<html>
<head></head>
<body>
  
<div id=container>
<p id=title>Here it comes a long sentence that takes about 2 lines.</p>
</div>
  
</body>
</html> 
  
 

更多推荐

本文发布于:2023-07-08 00:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   Center   Div   Left   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!