gpt4 book ai didi

html - 如何将中心对齐到子 div 具有固定宽度的页面

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:48 25 4
gpt4 key购买 nike

我有如下 html...

<div>
<h1>centered text</h1>
<p>some text</p>
</div>

和CSS....

div{display: table; text-align: center; width: 100%;}
p{width: 100px;}

在上面的代码中,h1 居中而不是 p。我试过使用 margin-left: 50% to outer div 和 left: -50% to inner div 但显示额外的水平滚动条。所以,我想用另一种方法。

最佳答案

更新p元素的样式:

p{width: 100px; margin: 0 auto}

Demo here

解释:

h1 的文本居中,因为 h1 元素继承了 divtext-align 属性> 元素及其宽度等于 div 元素的宽度。

您的 p 元素也继承了中心文本对齐方式,但它只有 100px 长度并且具有默认位置属性,所以整个 p元素粘贴在父 div 的左侧。您必须设置 p 元素的左右边距,以使 整个 p 元素div 中居中>.

关于html - 如何将中心对齐到子 div 具有固定宽度的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com