如何在一个div中垂直对齐元素?

我们可以使用以下任何一种方式轻松地在一个div中垂直对齐元素 −

  • position属性
  • 行高属性
  • 填充属性

让我们逐个看例子 –

使用position属性在div中垂直对齐元素

The position property is used in positioning an element. It can be used in conjunction with the top, right, bottom and left properties to position an element where you want it. Here are the possible values of the position property −

  • static − 元素框作为正常文档流的一部分进行布局,跟随前面的元素和后面的元素。

  • relative − The element’s box is laid out as a part of the normal flow, and then offset by some distance.

  • absolute − 元素的框相对于其包含块进行布局,并且完全从文档的正常流程中移除。

  • fixed − 元素框是绝对定位的,具有position: absolute的行为描述。主要区别在于固定定位元素的包含块始终是视口。

现在让我们看一个使用position属性在div中垂直对齐元素的示例−

Example

的中文翻译为:

示例

<!DOCTYPE html>
<html>
<head>
   <title>Align Elements</title>
   <style>
      #demo1 {
         position: relative;
      }
      #demo2 {
         position: absolute;
         top: 50%;
         height: 100px;
         margin-top: -50px;
      }
      #demo1 {
         background-color: yellow;
         border: 2px solid gray;
         height: 15em;
      }
      #demo2 {
         background-color: skyblue;
         border: 1px solid orange;
         width: 100%;
      }
   </style>
</head>
<body>
   <h1>Vertically Align Elements</h1>
   <p>Use the position property:</p>
   <div id="demo1">
      <div id="demo2">
         <p>This is a demo text</p>
         <p>This is another demo text</p>
      </div>
   </div>
</body>
</html>

使用line-height属性在div中垂直对齐元素

line-height属性修改了组成一行文本的内联框的高度。以下是line-height的可能值 –

  • normal − 指示浏览器将元素中的行高设置为“合理”的距离。

  • number − The actual height of lines in the element is this value multiplied by the font-size of the element.

  • length − 元素中行的高度是给定的值。

  • 百分比 − 元素中行的高度是根据元素的字体大小的百分比计算的。

让我们现在看一个使用line-height属性在div中垂直对齐元素的示例 –

Example

的中文翻译为:

示例

<!DOCTYPE html>
<html>
<head>
   <title>Align Elements</title>
   <style>
      p {
         margin: 0;
      }
      #demo {
         border: 3px solid yellow;
         background-color: orange;
         height: 100px;
         line-height: 100px;
      }
   </style>
</head>
<body>
   <h1>Vertically Aligned Element</h1>
   <p>Use the line-height property:</p>
   <div id="demo">
      <p>This is demo text</p>
   </div>
</body>
</html>

使用padding属性在div中垂直对齐元素

padding属性允许您指定元素的内容与其边框之间应该出现多少空间。此属性的值应为长度、百分比或单词inherit。如果值为inherit,则其填充将与其父元素相同。如果使用百分比,则百分比是相对于包含框的。

以下CSS属性可用于控制列表。您还可以使用以下属性为框的每个边设置不同的填充值 –

  • The padding-bottom specifies the bottom padding of an element.
  • The padding-top specifies the top padding of an element.
  • The padding-left specifies the left padding of an element.
  • The padding-right specifies the right padding of an element.
  • The padding serves as shorthand for the preceding properties.

Let us now see an example to Vertically align elements in a div using the padding property −

Example

的中文翻译为:

示例

<!DOCTYPE html>
<html>
<head>
   <title>Align Element</title>
   <style>
      .demo {
         padding: 60px 0;
         border: 2px solid #5c34eb;
         background-color: orange;
      }
   </style>
</head>
<body>
   <h1>Vertically Align Element</h1>
   <p>Use the padding property:</p>
   <div class="demo">
      <p>This is demo text.</p>
      <p>This is another text.</p>
   </div>
</body>
</html>

以上就是如何在一个div中垂直对齐元素?的详细内容,更多请关注双恒网络其它相关文章!

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

云资源网 » 如何在一个div中垂直对齐元素?

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
你们有qq群吗怎么加入?
当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群https://www.sudo1.com/page-qun.html。
  • 会员数(个)
  • 12310资源数(个)
  •        
  • 资源(G)
  •        
  • 今日下载
  • 1505稳定运行(天)

提供最优质的资源集合

立即查看 了解详情