英文:
How can I horizontally align the text in separate block elements?
问题
我意识到,当我居中文本时,它的包围元素会尽量变宽,以便能够以正确的换行和对齐在真正的中心显示文本。
但是,如果我想将副标题对齐到居中文本的左侧,该怎么办?
在上面的图像中,我使用了“test”元素的边距来模拟所期望的结果,但是否有一种动态对齐的方法?
我的代码:
* {
outline: 1px solid red;
}
.contain {
margin: 0 auto;
max-width: 350px;
}
.wrap {
margin: 0 auto;
padding: 80px 0;
}
.content {
background: #ff000030;
font-size: 32px;
position: relative;
text-align: center;
width: fit-content;
}
.sub {
/* margin-left:30px; */
position: absolute;
top: 0;
transform: translateY(-100%);
}
<div class="contain">
<div class="wrap">
<div class="content">
Lorem ipsum dolor sit amet.
<div class="sub">test</div>
</div>
</div>
</div>
如果可能的话,我想避免使用JavaScript,并只找到一个纯CSS的解决方案。
英文:
I realize that when I center text, its enveloping element becomes as wide as possible to be able to display the text in the true center with correct linebreaks and alignment.
But, what if I want to align a subtitle on the left hand side of centered text?
In the above image I simulated the desired outcome with a margin on the 'test' element, but is there a way to dynamically align this?
My code:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
* {
outline: 1px solid red;
}
.contain {
margin: 0 auto;
max-width: 350px;
}
.wrap {
margin: 0 auto;
padding: 80px 0;
}
.content {
background: #ff000030;
font-size: 32px;
position: relative;
text-align: center;
width: fit-content;
}
.sub {
/* margin-left:30px; */
position: absolute;
top: 0;
transform: translateY(-100%);
}
<!-- language: lang-html -->
<div class="contain">
<div class="wrap">
<div class="content">
Lorem ipsum dolor sit amet.
<div class="sub">test</div>
</div>
</div>
</div>
<!-- end snippet -->
If at all possible, I'd like to avoid using javascript for this, and just find a pure css solution.
答案1
得分: 2
我只能得到你想要的效果,前提是“test”文本位于居中文本之前,并使用inline-block
显示;你可以测试改变居中文本以改变位置:
<div class="contain">
<div class="wrap">
<div class="content">
<div class="sub">test</div>
Lorem ipsum dolor sitametameta amet.
</div>
</div>
</div>
英文:
I could get the effect you want only if the "test" text is before the centered one, and uses an inline-block
display; you can test varying the centered text to change the position:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
* {
outline: 1px solid red;
}
.contain {
margin: 0 auto;
max-width: 350px;
}
.wrap {
margin: 0 auto;
padding: 80px 0;
}
.content {
background: #ff000030;
font-size: 32px;
position: relative;
text-align: center;
width: fit-content;
}
.sub {
position: absolute;
top: 0;
transform: translateY(-100%);
display: inline-block;
}
<!-- language: lang-html -->
<div class="contain">
<div class="wrap">
<div class="content">
<div class="sub">test</div>
Lorem ipsum dolor sitametameta amet.
</div>
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
以下是翻译好的部分:
未来使用锚定定位的解决方案:https://drafts.csswg.org/css-anchor-position-1/#anchoring
尚未准备好投入生产,但可以在启用实验性标志的Chrome上进行测试。
* {
outline: 1px solid red;
}
.contain {
margin: 0 auto;
max-width: 350px;
}
.wrap {
margin: 0 auto;
padding: 80px 0;
}
.content {
background: #ff000030;
font-size: 32px;
text-align: center;
width: fit-content;
}
/* 我们使用before伪元素(内联元素)作为锚点 */
.content::before {
content: "";
anchor-name: --align;
}
.sub {
position: absolute;
bottom: anchor(--align 0%);
left: anchor(--align 0%);
}
<div class="contain">
<div class="wrap">
<div class="content">
Lorem ipsum dolor sit amet.
<div class="sub">test</div>
</div>
</div>
</div>
英文:
A solution from the future using the Anchor Positioning: https://drafts.csswg.org/css-anchor-position-1/#anchoring
Not ready for production but can be tested on Chrome with experimental flag enabled.
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
* {
outline: 1px solid red;
}
.contain {
margin: 0 auto;
max-width: 350px;
}
.wrap {
margin: 0 auto;
padding: 80px 0;
}
.content {
background: #ff000030;
font-size: 32px;
text-align: center;
width: fit-content;
}
/* we use the before pseudo element (an inline element) as the anchor */
.content::before {
content: "";
anchor-name: --align;
}
.sub {
position: absolute;
bottom: anchor(--align 0%);
left: anchor(--align 0%);
}
<!-- language: lang-html -->
<div class="contain">
<div class="wrap">
<div class="content">
Lorem ipsum dolor sit amet.
<div class="sub">test</div>
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论