如何将文本在不同的块元素中水平对齐?

huangapple go评论68阅读模式
英文:

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 -->

&lt;div class=&quot;contain&quot;&gt;
	&lt;div class=&quot;wrap&quot;&gt;
		&lt;div class=&quot;content&quot;&gt;
			Lorem ipsum dolor sit amet.
			&lt;div class=&quot;sub&quot;&gt;test&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

<!-- 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 -->

&lt;div class=&quot;contain&quot;&gt;
    &lt;div class=&quot;wrap&quot;&gt;
        &lt;div class=&quot;content&quot;&gt;
            &lt;div class=&quot;sub&quot;&gt;test&lt;/div&gt;
            Lorem ipsum dolor sitametameta amet.  
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- 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: &quot;&quot;;
  anchor-name: --align;
}

.sub {
  position: absolute;
  bottom: anchor(--align 0%);
  left: anchor(--align 0%);
}

<!-- language: lang-html -->

&lt;div class=&quot;contain&quot;&gt;
	&lt;div class=&quot;wrap&quot;&gt;
		&lt;div class=&quot;content&quot;&gt;
			Lorem ipsum dolor sit amet.
			&lt;div class=&quot;sub&quot;&gt;test&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 04:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定