英文:
Is there a way to tweak this code so that it underlines multiple lines of text?
问题
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
#full-service-creative-studio::after {
content: "";
position: absolute;
left: 20px;
bottom: 10px;
width: 103%;
height: 28px;
background: #46989C;
z-index: -1;
}
<!-- language: lang-html -->
<h1 id="full-service-creative-studio">全方位设计工作室</h1>
<!-- end snippet -->
目前,它在单行文本上运行良好,但当我们缩小到移动视图时,它只会给文本的底部一行加下划线。有没有办法让每一行都有下划线?
英文:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
#full-service-creative-studio::after {
content: "";
position: absolute;
left: 20px;
bottom: 10px;
width: 103%;
height: 28px;
background: #46989C;
z-index: -1;
}
<!-- language: lang-html -->
<h1 id="full-service-creative-studio">Full Service Design Studio</h1>
<!-- end snippet -->
Currently, it works great for a single line but when we scale down to say mobile view it only underlines the bottom line of the text. Is there a way to make every line underlined?
答案1
得分: 1
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
#full-service-design-studio {
text-decoration: underline #46989C;
}
<!-- language: lang-html -->
<h1 id="full-service-design-studio">全方位设计工作室</h1>
<!-- end snippet -->
英文:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
#full-service-design-studio {
text-decoration: underline #46989C;
}
<!-- language: lang-html -->
<h1 id="full-service-design-studio">Full Service Design Studio</h1>
<!-- end snippet -->
答案2
得分: 0
我认为你要找的是:
#full-service-creative-studio{
text-decoration: underline #46989C;
}
伪选择器::after
会在h1
元素中包含的TEXT_NODE
下方放置一条线,而不是在h1
的ELEMENT_NODE
下方。
英文:
I think what you are looking for is:
#full-service-creative-studio{
text-decoration: underline #46989C;
}
The pseudo selector ::after
will put the line underneath the h1
ELEMENT_NODE
and not the TEXT_NODE
contained within the h1
element.
答案3
得分: 0
你需要的是一个 CSS 类。
.underline {
text-decoration: underline #46989C;
}
目前,你正在使用一个 id
选择器来应用 CSS 样式,这就是为什么它只应用于单行文本。使用 class
作为选择器,你可以将相同的样式应用于多个元素。
关键是,CSS 的 id
选择器用于查找一个元素,而 class
用于在多个 HTML 标签中应用共同的样式。
<h1 class="underline">Line 1</h1>
<h2 class="underline">Line 2</h2>
<h3 class="underline">Line 3</h3>
<p class="underline">Line 4</p>
希望这个 w3school 的链接能帮助你理解。
英文:
You need is a css class
.underline
{
text-decoration: underline #46989C;
}
currently, you are using an id
selector to apply the css that is why it is only applying to a single line, using a class
as a selector you can apply the same style to multiple elements.
Crux is that CSS id
selector is used to find only 1 element. while classes
are used to apply common styles across various HTML tags.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.underline {
text-decoration: underline #46989C;
}
<!-- language: lang-html -->
<h1 class="underline">Line 1</h1>
<h2 class="underline"> Line 2</h2>
<h3 class="underline">Line 3</h3>
<p class="underline"> Line 4</p>
<!-- end snippet -->
Hope this w3school link helps you understand
答案4
得分: 0
最困难的部分(其他答案都忽略了)是下划线元素上的left
偏移量,以及103%
的长度。
您可以使用数据属性、伪元素、text-decoration
和text-underline-offset
来处理这个问题。
这些方法并不完美,但可以接近您的要求,并且可能给您一些思路。
标题中提到了“调整代码”,因此我们在H1
标签中添加了一个具有相同标题内容的data-attribute
。然后,我们使用伪元素::after
来设置所有样式以将线条向左推。这种方法的问题是,在某些断点处,伪元素会在可见文本之前换行,从而创建一个悬挂的下划线。
除非整个下划线都能位于内容的左侧(参见第二个示例),否则我认为您无法完全实现这个效果,但这个方法可以接近:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
h1 {
position: relative;
line-height: 2.2;
font-family: sans-serif;
}
h1::after {
content: attr(data-content);
color: transparent;
z-index: -1;
position: absolute;
text-decoration: underline #46989C 28px;
text-underline-offset: 10px;
top: 0;
left: 0;
height: 100%;
font-family: inherit;
text-indent: 20px;
}
/* makes pseudo text a little smaller to try and prevent wrapping */
h1.small::after {
font-size: 99%;
}
<!-- language: lang-html -->
<h1 data-content="Full Service Design Studio Full Service Design Studio Full Service Design Studio ull Service Design Studio ">Full Service Design Studio Full Service Design Studio Full Service Design Studio Full Service Design Studio</h1>
<h1 data-content="Full Service Design Studio Full Service Design Studio Full Service Design Studio ull Service Design Studio " class="small">Full Service Design Studio Full Service Design Studio Full Service Design Studio Full Service Design Studio</h1>
<!-- end snippet -->
## Number 2 ##
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
h1 {
position: relative;
line-height: 2.2;
font-family: sans-serif;
}
h1::after {
content: attr(data-content);
color: transparent;
z-index: -1;
position: absolute;
text-decoration: underline #46989C 28px;
text-underline-offset: 10px;
top: 0;
left: 20px;
height: 100%;
font-family: inherit;
}
<!-- language: lang-html -->
<h1 data-content="Full Service Design Studio Full Service Design Studio Full Service Design Studio ull Service Design Studio">Full Service Design Studio Full Service Design Studio Full Service Design Studio Full Service Design Studio</h1>
<!-- end snippet -->
英文:
The hardest part (which every other answer ignores) is the left
offset you have on the underline element, as well as the 103%
length.
You can use a data-attribute, pseudo element, text-decoration
and text-underline-offset
to handle this.
These are not perfect by any means, but gets you kind of close - and may give you an idea.
The title says "tweak code", so this adds a data-attribute
to the H1
tag with the same title content. We then style the ::after
pseudo with everything to push the line left. The problem with this, is that at certain breakpoints the pseudo element will wrap before the visible text, creating a hanging underline.
I don't think you can get this right, unless the whole underline can be left of the content (see the second example) - which again, gets close:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
h1 {
position: relative;
line-height: 2.2;
font-family: sans-serif;
}
h1::after {
content: attr(data-content);
color: transparent;
z-index: -1;
position: absolute;
text-decoration: underline #46989C 28px;
text-underline-offset: 10px;
top: 0;
left: 0;
height: 100%;
font-family: inherit;
text-indent: 20px;
}
/* makes pseudo text a little smaller to try and prevent wrapping */
h1.small::after {
font-size: 99%;
}
<!-- language: lang-html -->
<h1 data-content="Full Service Design Studio Full Service Design Studio Full Service Design Studio ull Service Design Studio &nbsp;&nbsp;">Full Service Design Studio Full Service Design Studio Full Service Design Studio Full Service Design Studio</h1>
<h1 data-content="Full Service Design Studio Full Service Design Studio Full Service Design Studio ull Service Design Studio &nbsp;&nbsp;" class="small">Full Service Design Studio Full Service Design Studio Full Service Design Studio Full Service Design Studio</h1>
<!-- end snippet -->
Number 2
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
h1 {
position: relative;
line-height: 2.2;
font-family: sans-serif;
}
h1::after {
content: attr(data-content);
color: transparent;
z-index: -1;
position: absolute;
text-decoration: underline #46989C 28px;
text-underline-offset: 10px;
top: 0;
left: 20px;
height: 100%;
font-family: inherit;
}
<!-- language: lang-html -->
<h1 data-content="Full Service Design Studio Full Service Design Studio Full Service Design Studio ull Service Design Studio">Full Service Design Studio Full Service Design Studio Full Service Design Studio Full Service Design Studio</h1>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论