【问题标题】:How to get two lines of text fixed at positions?如何将两行文本固定在位置?
【发布时间】:2022-02-11 20:29:31
【问题描述】:

我怎样才能做到这一点

到这里

调整浏览器大小时。 供您参考,这些是带有和弦的歌词行。我想在歌词上方实现完全相同的和弦固定位置以使其响应。我正在使用 HTML、CSS。 任何人,请帮忙。

.lines {
    margin: 5px 0;
}

.lyrics,.notes {
    font-family: 'Open Sans', sans-serif;
    font-size: 16px;
    color: rgb(45, 45, 45);
    font-weight: 600;
}

.lines>span:first-child:after {
    content: "\A";
    white-space: pre-line;
}

.notes {
    color: rgb(0, 150, 0);
    background-color: rgb(241, 241, 241);
}
<div class="lines">
        <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="notes">G</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="notes">Em</span></span>
        <span class="lyrics">Well, I found a girl, beautiful and sweet</span>
        </div>

        <div class="lines">
        <span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="notes">C</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="notes">D</span></span>
        <span class="lyrics">Oh, I never knew you were the someone waiting for me</span>
        </div>

【问题讨论】:

  • 您需要使用表格元素。我以前在这里做过这种东西……
  • 请添加您迄今为止尝试过的内容(HTML 和 CSS)。
  • 是的,我已经添加了 HTML 和 CSS

标签: html css media-queries responsive


【解决方案1】:

这样……

(()=>  // IIFE code on load for lyrics presentation
  {
  const rgxCut   = /(?=\[)|(?<=\])/;

  document.querySelectorAll('div.lyrics p').forEach( pLine =>
    {
    let newP = pLine.textContent
    .replaceAll('] [',']&emsp;[')
    .split(rgxCut)
    .map( el =>
      {
      if (el[0]!=='[') return el
      let chord = el.substring(1,el.length-1)
      return `<span class="chord" data-chord="${chord}"></span>`
      })
    pLine.innerHTML = newP.join('')
    })
  }
)()
/* --- for testing --- */
body {
  font-family : Arial, Helvetica, sans-serif;
  font-size   : 20px;
  }
.reSizable {
  resize   : both;
  overflow : scroll;
  padding  : .5em;
  border   : 1px solid orange;
  }
/* ------------------- */

div.lyrics p  {
  line-height : 2.5em;
  }
div.lyrics p span[data-chord] {
  position : relative
  }
div.lyrics p span[data-chord]::after {
  position   : absolute;
  top         : -1.2em;
  left        : -.1em;
  line-height : 1.2em;
  padding     : 0 .2em;
  font-size   : .9em;
  color       : darkgreen;
  background  : lightgrey;  
  content     : attr(data-chord);
  }
<div class="reSizable">

    <div class="lyrics">
      <p>Well, I found a [G]girl, beauti[Em]full and sweet</p>
      <p>Oh, I never [C]knew you where the someone waiting for [D]me</p>
    </div>

</div>

【讨论】:

  • 嗯,这很好用,但是用这种方式写完整的歌曲会变得很困难。可以通过我附加到问题的HTML来实现吗?
  • @PRASHANTVERMA 好吧,我们不能说您是一个非常合作的对话者……。做出的改变是否适合您的殿下?
  • 哇,多么棒的解决方案!
  • @MisterJojo,你做得非常好。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
相关资源
最近更新 更多