有没有办法要求浏览器在其父元素之后渲染一个HTML元素?

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

Is there a way to ask a browser to render an HTML element after its parent?

问题

有没有一种方法,仅使用CSS,来要求浏览器呈现这段代码:

<p>One <span class="foo">Two</span> Three</p>

就好像我写成了:

<p>One Three</p>
<span class="foo">Two</span>

我想这样做的原因是我希望在移动设备上动态重新排列某些span,在宽屏幕上在其父元素内部呈现。

英文:

Is there a way, using CSS only, to ask a browser to render this

<p>One <span class="foo">Two</span> Three</p>

as if I had written

<p>One Three</p>
<span class="foo">Two</span>

The reason I want to do this is that I want to dynamically reorder certain spans to render after their parent on mobile devices and inside their parent on wide screens.

答案1

得分: 2

一个非常不推荐的、非常巧妙但只适用于你特定情况的方法:

<!-- 开始代码片段:JavaScript 隐藏:否 控制台:是 Babel:否 -->

<!-- 语言:CSS -->

p {
  display: grid;
  grid-auto-flow: dense;
  justify-content: start;
  gap: 5px;
}

.foo {
  grid-row: 2;
  grid-column: span 2;
}

<!-- 语言:HTML -->

<p>One <span class="foo">Two</span> Three</p>

<!-- 结束代码片段 -->
英文:

A very hacky way to use with caution that works only for your specific case

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p {
  display: grid;
  grid-auto-flow: dense;
  justify-content: start;
  gap: 5px;
}

.foo {
  grid-row: 2;
  grid-column: span 2;
}

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

&lt;p&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three&lt;/p&gt;

<!-- end snippet -->

答案2

得分: 0

这有点脆弱,但可以修改为不那么脆弱,比如为p元素添加底部填充区域,以确保有空间放置这个span

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
p {position: relative;}
span {position: absolute; top: 100%; left: 0;}

<!-- language: lang-html -->
<p>One <span class="foo">Two</span> Three</p>

<!-- end snippet -->

如果您需要进一步的信息或翻译,请告诉我。

英文:

This is a tad brittle, but could be modified to be less so, like adding a bottom padding area for the p element to make sure there's space for the span:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p {position: relative;}
span {position: absolute; top: 100%; left: 0;}

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

&lt;p&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three&lt;/p&gt;

<!-- end snippet -->

答案3

得分: 0

grid和flex可以在这里帮助:
最短的CSS方法是使用grid + order,但它会将所有内容堆叠成3行

.g {
  display: grid;
}

.foo {
  order: 2;
}
<p class="g">One <span class="foo">Two</span> Three</p>

如果只有span需要包装在其所在的文本下方,flex也可以使用。

p.f {
  display: flex;
  flex-wrap: wrap;
}

p.f .foo {
  order: 2;
  flex-basis: 100%;
}
<p class="f">One <span class="foo">Two</span> Three</p>

flex和grid可以为您提供一些选项来整理和重新排序文本和单词之间的标签。以下是一些更多的测试示例供您参考:grid可以轻松将所有内容显示在单列中,每个单词将位于不同的行上。Flex可以为您提供更多选项,类似于列的网格,但也可以在一行上,您可以将标签发送到容器的前端或末端。这仅仅是视觉效果。

p {
  border: solid;
  margin: 1em;
}

p.if {
  display: inline-flex;
  flex-wrap: wrap;
}

p.f {
  display: flex;
  flex-wrap: wrap;
}

p.fc {
  display: flex;
  flex-direction: column;
}

p.g {
  display: grid;
}

p.ifc {
  display: inline-flex;
  flex-direction: column;
}

p.ig {
  display: inline-grid;
}

.f .foo,
.if .foo {
  min-width: 100%;
}

.foo {
  order: 2;
  border: solid;
}

.bar {
  order: -1;
}
<h3>grid blocks</h3>
<h4>flex</h4>
<p class="f">One <span class="foo">Two</span> Three <span class="bar">- </span></p>
<p class="fc">One <span class="foo">Two</span> Three <span class="bar">- </span></p>
<h4>grid</h4>
<p class="g">One <span class="foo">Two</span> Three <span class="bar">- </span></p>
<h3>inline grid blocks</h3>
<h4>inline-flex</h4>
<p class="if">One <span class="foo">Two</span> Three <span class="bar">- </span></p>
<p class="ifc">One <span class="foo">Two</span> Three <span class="bar">- </span></p>
<h4>inline-grid</h4>
<p class="ig">One <span class="foo">Two</span> Three <span class="bar">- </span></p>
英文:

grid and flex could help here:
the shortest CSS way would be grid +order but, it will pile everything into 3 lines

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.g {
  display: grid;
}

.foo {
  order: 2
}

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

&lt;p class=&quot;g&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three&lt;/p&gt;

<!-- end snippet -->
Flex can come along too if only the span has to wrap below the text it stands within.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p.f {
  display: flex;
  flex-wrap: wrap;
}

p.f .foo {
  order: 2;
  flex-basis: 100%;
}

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

&lt;p class=&quot;f&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three&lt;/p&gt;

<!-- end snippet -->

flex and grid can give you a few option to sort out and reorder the text and the tags in between the words.
here is a few more test example to play with :grid makes it easy to display everything in a single column, each words will be on a different row. Flex can give your more options, a column alike grid , but also on a line, where you can send the tags to the front or the end of the container . It is only visual.
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p {
  border: solid;
  margin: 1em;
}

p.if {
  display: inline-flex;
  flex-wrap: wrap;
}

p.f {
  display: flex;
  flex-wrap: wrap;
}

p.fc {
  display: flex;
  flex-direction: column;
}

p.g {
  display: grid
}

p.ifc {
  display: inline-flex;
  flex-direction: column;
}

p.ig {
  display: inline-grid
}

.f .foo,
.if .foo {
  min-width: 100%;
}

.foo {
  order: 2;
  border: solid;
}

.bar {
  order: -1
}

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

&lt;h3&gt;grid blocks&lt;/h3&gt;
&lt;h4&gt;flex&lt;/h4&gt;
&lt;p class=&quot;f&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three &lt;span class=&quot;bar&quot;&gt;- &lt;/span&gt;&lt;/p&gt;
&lt;p class=&quot;fc&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three &lt;span class=&quot;bar&quot;&gt;- &lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;grid&lt;/h4&gt;
&lt;p class=&quot;g&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three &lt;span class=&quot;bar&quot;&gt;- &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;inline grid blocks&lt;/h3&gt;
&lt;h4&gt;inline-flex&lt;/h4&gt;
&lt;p class=&quot;if&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three &lt;span class=&quot;bar&quot;&gt;- &lt;/span&gt;&lt;/p&gt;
&lt;p class=&quot;ifc&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three &lt;span class=&quot;bar&quot;&gt;- &lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;inline-grid&lt;/h4&gt;
&lt;p class=&quot;ig&quot;&gt;One &lt;span class=&quot;foo&quot;&gt;Two&lt;/span&gt; Three &lt;span class=&quot;bar&quot;&gt;- &lt;/span&gt;&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 07:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452896.html
匿名

发表评论

匿名网友

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

确定