英文:
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: '';
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: '';
border: dashed 1px;
}
<!-- language: lang-html -->
<div>TEXT</div>
<!-- end snippet -->
答案2
得分: -1
Sure, here are the translated parts:
- First way:
HTML
<div class="divstyle">
<span class="out">文本</span>
</div>
CSS
.divstyle {
position: relative;
background-color: 灰色;
}
.out {
position: relative;
top: -1em;
}
- Second way (after reading your first comment):
HTML
<div class="divstyle" content="文本">&nbsp;</div>
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 :
- First way :
HTML
<div class="divstyle">
<span class="out">TEXT</span>
</div>
CSS
.divstyle {
position: relative;
background-color: grey;
}
.out {
position: relative;
top: -1em;
}
2/ Second way (after reading your first comment) :
HTML
<div class="divstyle" content="TEXT">&nbsp;</div>
CSS
.divstyle {
background-color: grey;
}
.divstyle:before {
content: attr(content);
position: absolute;
top: -1em;
}
答案3
得分: -1
你可以使用另一种样式添加一个span:
<div class="divstyle">
<span class="textstyle">
文本
</span>
</div>
可能看起来像这样:
.textstyle {
位置: 绝对;
顶部: -20px;
字体大小: 24px;
}
这里需要注意的重要部分是设置了位置为绝对,所以可以相对于其第一个定位祖先元素定位。
英文:
You could add a span using another styling (here: textstyle)
<div class="divstyle">
<span class="textstyle">
TEXT
</span>
</div>
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.
<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%;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论