垂直移动容器外的文本

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

Move text outside of the container vertically

问题

我有DIV,带有一些CSS样式和DIV内的文本。如何将文本移出DIV并应用一些样式,可以使用CSS或JavaScript?

HTML

<div class="divstyle">TEXT</div>

CSS

.divstyle {
    width: 200px;
    height: 50px;
    background-color: grey;
}

我通过JavaScript创建了完整的DIV:

var inputfield = document.createElement("div");
inputfield.textContent = inputfieldData['TEXT'];
inputfield.classList.add("divstyle");
inputfieldsContainer.appendChild(inputfield);

在浏览器中呈现如下:

+-----------------+
| TEXT            |
+-----------------+

我希望是这样的:

TEXT
+-----------------+
|                 |
+-----------------+

我尝试过的:

.divstyle::before {
    top: -50px;
    left: -20px;
}

是否可以仅使用CSS在DIV内添加DIV?
也许解决方案是将文本包装在某个标签中并对其应用CSS,但如果可能的话,是否可以通过CSS来实现?

英文:

I have DIV with some CSS styling and TEXT inside DIV. How to move TEXT outside of DIV and apply some styling using either CSS or JavaScript?

HTML

<div class="divstyle">TEXT</div>

CSS

.divstyle {
    width: 200px;
    height: 50px;
    background-color: grey;
}

I create complete DIV over javascript:

var inputfield = document.createElement("div");
inputfield.textContent = inputfieldData['TEXT'];
inputfield.classList.add("divstyle");
inputfieldsContainer.appendChild(inputfield);

When it renders in browser it looks like this:

+-----------------+
| TEXT            |
+-----------------+

I wish to be like this:

TEXT
+-----------------+
|                 |
+-----------------+

What I tried:

.divstyle::before {
    top: -50px;
    left: -20px;
}

Is there a way to add DIV inside DIV using only CSS?
Maybe the solution could be to wrap the TEXT in some tag and apply CSS to it, but if it's possible to do it over CSS?

答案1

得分: 0

这是您要翻译的内容:

It is not entirely clear what you want, maybe something like this will suit you:

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

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

div {
  display: inline-grid;
  grid-auto-rows: 1fr;
}

div:after {
  content: &#39;&#39;;
  border: dashed 1px;
}

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

<div>TEXT</div>

<!-- end snippet -->
英文:

It is not entirely clear what you want, maybe something like this will suit you:

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

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

div {
  display: inline-grid;
  grid-auto-rows: 1fr;
}

div:after {
  content: &#39;&#39;;
  border: dashed 1px;
}

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

&lt;div&gt;TEXT&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

Sure, here are the translated parts:

  1. First way:

HTML

&lt;div class=&quot;divstyle&quot;&gt;
  &lt;span class=&quot;out&quot;&gt;文本&lt;/span&gt;
&lt;/div&gt;

CSS

.divstyle {
  position: relative;
  background-color: 灰色;
}
.out {
  position: relative;
  top: -1em;
}
  1. Second way (after reading your first comment):

HTML

&lt;div class=&quot;divstyle&quot; content=&quot;文本&quot;&gt;&amp;nbsp;&lt;/div&gt;

CSS

.divstyle {
  background-color: 灰色;
}
.divstyle:before {
  content: attr(content);
  position: absolute;
  top: -1em;
}
英文:

Although I don't understand why you want this, here is two ways to achieve it :

  1. First way :

HTML

&lt;div class=&quot;divstyle&quot;&gt;
  &lt;span class=&quot;out&quot;&gt;TEXT&lt;/span&gt;
&lt;/div&gt;

CSS

.divstyle {
  position: relative;
  background-color: grey;
}
.out {
  position: relative;
  top: -1em;
}

2/ Second way (after reading your first comment) :

HTML

&lt;div class=&quot;divstyle&quot; content=&quot;TEXT&quot;&gt;&amp;nbsp;&lt;/div&gt;

CSS

.divstyle {
  background-color: grey;
}
.divstyle:before {
  content: attr(content);
  position: absolute;
  top: -1em;
}

答案3

得分: -1

你可以使用另一种样式添加一个span

&lt;div class="divstyle"&gt;
    &lt;span class="textstyle"&gt;
        文本
    &lt;/span&gt;
&lt;/div&gt;

可能看起来像这样:

    .textstyle {
        位置: 绝对;
        顶部: -20px;
        字体大小: 24px;
    }

这里需要注意的重要部分是设置了位置为绝对,所以可以相对于其第一个定位祖先元素定位。

英文:

You could add a span using another styling (here: textstyle)

&lt;div class=&quot;divstyle&quot;&gt;
    &lt;span class=&quot;textstyle&quot;&gt;
        TEXT
    &lt;/span&gt;
&lt;/div&gt;

which could look like that

    .textstyle {
        position: absolute;
        top: -20px;
        font-size: 24px;
    }

The important part to notice here is that position is set to absolute, so you can position it relative to its first positioned ancestor element.

答案4

得分: -1

不要在 div 元素内设置文本,而是将伪元素的内容设置为 'TEXT' 并执行以下操作。

<div class="divstyle"></div>
.divstyle {
    width: 200px;
    height: 50px;
    background-color: grey;
    position: relative;
    margin-top: 50px;
}

.divstyle::before {
    content: 'TEXT';
    position: absolute;
    top: -50%;
}

链接:https://codepen.io/caglar62/pen/LYgaPPW

英文:

Instead of setting the text inside div, you can set the content of the pseudo-element as 'TEXT' and do the following.

&lt;div class=&quot;divstyle&quot;&gt;&lt;/div&gt;
.divstyle {
    width: 200px;
    height: 50px;
    background-color: grey;
    position: relative;
    margin-top: 50px;
}

.divstyle::before{
    content: &#39;TEXT&#39;;
    position: absolute;
    top: -50%;
}

https://codepen.io/caglar62/pen/LYgaPPW

huangapple
  • 本文由 发表于 2023年5月25日 01:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326048.html
匿名

发表评论

匿名网友

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

确定