gpt4 book ai didi

html - 将文本正确放置在居中图像下方

转载 作者:行者123 更新时间:2023-11-28 07:23:06 25 4
gpt4 key购买 nike

我使用以下代码将图像居中并设置文本格式。

IMG.center {position:fixed; top:40%; left:50%; transform:translate(-50%,-50%)}

h1 span {display: block;}

h1 span.major {font-family:Avenir Lt Std; font-size:30px; font-weight:bold; border:0px solid black; padding:0px; text-align: center;}

h1 span.minor {font-family:Avenir Lt Std; font-size:20px; border:0px solid black; padding:0px; text-align: center;}

并按原样显示元素。但是,文本一直 float 到页面顶部。

<body>

<IMG class="center" src="picture.png" alt="icon" style="width:596px;height:166px;">

<h1>
<span class="major">some text</span>
<br>
<span class="minor">some text</span>
</h1>
</body>

为什么文字会浮到顶部?以及如何将文本移动到图像下方?关键是首先将图像居中并打印相对于图像的文本(即下方)。

IMG.center {position:fixed; top:40%; left:50%; transform:translate(-50%,-50%)}

h1 span {display: block;}

h1 span.major {font-family:Avenir Lt Std; font-size:30px; font-weight:bold; border:0px solid black; padding:0px; text-align: center;}

h1 span.minor {font-family:Avenir Lt Std; font-size:20px; border:0px solid black; padding:0px; text-align: center;}
<IMG class="center" src="picture.png" alt="icon" style="width:596px;height:166px;" /> 

<h1>
<span class="major">some text</span>
<br>
<span class="minor">some text</span>
</h1>

最佳答案

position:fixed 使图像相对于浏览器窗口而不是页面上的其他元素,并将其从页面的“流”中移除。从 h1 的 Angular 来看,img 被单独定位/在流程之外,而 h1 被放在页面的顶部。

您可以在其他元素上设置 position:fixed 并对齐它,但这里有一个您可能更喜欢的更简单的方法(在 上设置左右 margin >显示: block ):

HTML:

<div class="center-text">
<img class="center" src="https://jsfiddle.net/img/logo.png" alt="logo" />
<p>Caption</p>
</div>

CSS:

.center-text {
text-align:center;
}
img.center {
display:block;
margin:auto;
background-color:blue
}

https://jsfiddle.net/mw6djz8n/

(注意:如果您想使用上边距或下边距,您可能需要将 margin:auto 换成 margin-left:auto;margin-right:auto)

关于 CSS position 的更多信息:

w3schools , learnlayout , MDN , Codecademy Lesson

关于html - 将文本正确放置在居中图像下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31982319/

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