【问题标题】:Creating a horizontal line between span and paragraph with flexbox使用 flexbox 在跨度和段落之间创建一条水平线
【发布时间】:2017-01-19 21:04:36
【问题描述】:

有没有办法让跨度和段落之间有一条水平线。该行应与段落具有相同的宽度。 我怎样才能完成这个? 我不能使用 div,因为那样它会被识别为弹性项目,并且会放在段落旁边。

如果您需要任何进一步的信息。请告诉我。

rentout.html.erb

<div class="vorteil_wrapper">
    <div class="rentout_content_left">
        <p class="rentout_paragraph">
            <span class="rentout_heading">Erstellung eines professionellen Inserats</span></br>
            Wir besuchen Ihre Unterkunft und erstellen professionelle Fotos und Videos,
            welche nach der Bearbeitung für Ihr Inserat auf unserer Internetseite 
            genutzt werden. Darüber hinaus verfassen wir eine hochwertige Beschreibung
            und Präsentation Ihrer Unterkunft, welche die Highlights sowie die
            Atmosphäre Ihrer Unterkunft in Vordergrund setzt.
        </p>
    </div>
    <div class="rentout_picture_right">
        <%= image_tag("inserat.png", alt: "inserat", :class => "inserat") %>
    </div>
</div>

<div class="vorteil_wrapper_reverse">
    <div class="rentout_content_right">
        <p class="rentout_paragraph">
            <span class="rentout_heading">Responsive Webdesign</span></br>
            Wussten Sie das 65% aller Internet Nutzer ein Handy zum Surfen benutzen!
            Ihr zukünftiges Inserat wird auf unserer Seite optimal auf allen Geräten
            angezeigt, um somit die Benutzerfreundlichkeit und die Umsatzrate zu
            steigern.
        </p>
    </div>
    <div class="rentout_picture_left">
        <%= image_tag("responsive_webdesign.png", alt: "responsive webdesign", :class => "responsive_webdesign") %>
    </div>
</div>

application.scss

.vorteil_wrapper {
  height: 200px;
  margin-bottom: 0px;
  display: flex;
  justify-content: space-around;
}

.vorteil_wrapper_reverse {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-around;
  flex-direction: row-reverse;
}

.rentout_content_left {
  height: 200px;
  width: 100%;
  padding-right: 30px;
  display: flex;
  align-items: center;
}

.rentout_content_right {
  height: 200px;
  width: 100%;
  padding-left: 30px;
  display: flex;
  align-items: center;
}

.rentout_heading {
  font: 28px Raleway-Medium, sans-serif;
  margin-bottom: 6px;
  display: inline-block;
  text-decoration: underline;
  text-decoration-color: red;
}

.rentout_paragraph {
  text-align: justify;
  font: 18px Raleway-Regular, sans-serif;
}

这里试图解决 position: absolute 的问题。问题是每个标题都以不同的高度开始,因为 div(rentout_content_left/rentout_content_right) 内的元素(段落/跨度)居中,并且每个段落和跨度中的字符数各不相同。

.rentout_wrapper {
  max-width: 1000px;
  width: 100%;
  margin: 0 auto;
}
.vorteil_wrapper {
  height: 200px;
  margin-bottom: 0px;
  display: flex;
  justify-content: space-around;
  position: relative;
}
.vorteil_wrapper_reverse {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-around;
  flex-direction: row-reverse;
}
.rentout_content_left {
  height: 200px;
  width: 100%;
  padding-right: 30px;
  display: flex;
  align-items: center;
}
.rentout_content_right {
  height: 200px;
  width: 100%;
  padding-left: 30px;
  display: flex;
  align-items: center;
}
.rentout_heading {
  font: 28px Raleway-Medium, sans-serif;
  margin-bottom: 6px;
  display: inline-block;
}
.rentout_heading::after {
  content: "";
  position: absolute;
  border-bottom: 1px solid red;
  left: 0;
  bottom: -5px;
  width: 100%
}
.rentout_paragraph {
  text-align: justify;
  font: 18px Raleway-Regular, sans-serif;
}
<div class="rentout_wrapper">
  <div class="vorteil_wrapper">
    <div class="rentout_content_left">
      <p class="rentout_paragraph">
        <span class="rentout_heading">Erstellung eines professionellen Inserats</span>
        </br>
        Wir besuchen Ihre Unterkunft und erstellen professionelle Fotos und Videos, welche nach der Bearbeitung für Ihr Inserat auf unserer Internetseite genutzt werden. Darüber hinaus verfassen wir eine hochwertige Beschreibung und Präsentation Ihrer Unterkunft,
        welche die Highlights sowie die Atmosphäre Ihrer Unterkunft in Vordergrund setzt.
      </p>
    </div>
    <div class="rentout_picture_right">
      <img src="//lorempixel.com/200/192">
    </div>
  </div>

  <div class="vorteil_wrapper_reverse">
    <div class="rentout_content_right">
      <p class="rentout_paragraph">
        <span class="rentout_heading">Responsive Webdesign</span>
        </br>
        Wussten Sie das 65% aller Internet Nutzer ein Handy zum Surfen benutzen! Ihr zukünftiges Inserat wird auf unserer Seite optimal auf allen Geräten angezeigt, um somit die Benutzerfreundlichkeit und die Umsatzrate zu steigern.
      </p>
    </div>
    <div class="rentout_picture_left">
      <img src="//lorempixel.com/200/192">
    </div>
  </div>
</div>

【问题讨论】:

  • 显然这应该是一个带有标题和段落的 div,而不是段落和跨度。您可能需要考虑重新格式化您的结构。

标签: html css flexbox


【解决方案1】:

您可以在span 中使用伪元素::after

注意&lt;/br&gt;无效,要么使用&lt;br&gt;,要么使用&lt;br/&gt;,要么使用&lt;br /&gt;

.vorteil_wrapper {
  height: 200px;
  margin-bottom: 0px;
  display: flex;
  justify-content: space-around;
}
.vorteil_wrapper_reverse {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-around;
  flex-direction: row-reverse;
}
.rentout_content_left {
  height: 200px;
  width: 100%;
  padding-right: 30px;
  display: flex;
  align-items: center;
}
.rentout_content_right {
  height: 200px;
  width: 100%;
  padding-left: 30px;
  display: flex;
  align-items: center;
}
.rentout_heading {
  font: 28px Raleway-Medium, sans-serif;
  margin-bottom: 6px;
  display: block;
  position: relative
}
.rentout_heading::after {
  content: "";
  position: absolute;
  border-bottom: 1px solid red;
  left: 0;
  bottom: -5px;
  width: 100%
}
.rentout_paragraph {
  text-align: justify;
  font: 18px Raleway-Regular, sans-serif;
}
<div class="vorteil_wrapper">
  <div class="rentout_content_left">
    <p class="rentout_paragraph">
      <span class="rentout_heading">Erstellung eines professionellen Inserats</span>
      <br />
      Wir besuchen Ihre Unterkunft und erstellen professionelle Fotos und Videos, welche nach der Bearbeitung für Ihr Inserat auf unserer Internetseite genutzt werden. Darüber hinaus verfassen wir eine hochwertige Beschreibung und Präsentation Ihrer Unterkunft,
      welche die Highlights sowie die Atmosphäre Ihrer Unterkunft in Vordergrund setzt.
    </p>
  </div>
  <div class="rentout_picture_right">
    <img src="//lorempixel.com/200/200">
  </div>
</div>

<div class="vorteil_wrapper_reverse">
  <div class="rentout_content_right">
    <p class="rentout_paragraph">
      <span class="rentout_heading">Responsive Webdesign</span>
      <br />
      Wussten Sie das 65% aller Internet Nutzer ein Handy zum Surfen benutzen! Ihr zukünftiges Inserat wird auf unserer Seite optimal auf allen Geräten angezeigt, um somit die Benutzerfreundlichkeit und die Umsatzrate zu steigern.
    </p>
  </div>
  <div class="rentout_picture_left">
   <img src="//lorempixel.com/200/200">
  </div>
</div>

【讨论】:

  • 实际上您对
    是正确的,但由于某种原因,它也可以与 以这种方式工作,正如您在图片中看到的那样;) 感谢您的尝试,但您的线路是不可见的建议。如果我必须使用 position: absolute 和 width: 100% 我还必须使包装器位置相对,即使这样它看起来也很乱......
  • 很乱??它在这里工作正常。哪里乱??显示你的 sn-p
  • 我在我的问题中添加了一个 sn-p ;)
  • 您在我的回答中是否至少仔细查看了.rentout_heading { font: 28px Raleway-Medium, sans-serif; margin-bottom: 6px; display: block; position: relative }?因为你没有更新。您需要添加position:relative 并将inline-block 更改为block
  • 天啊,对不起我的失明,这太尴尬了!谢谢分配!
【解决方案2】:

您可以在.rentout_heading 类上使用::after 尝试以下解决方案:

.vorteil_wrapper {
  height: 200px;
  margin-bottom: 0px;
  display: flex;
  justify-content: space-around;
}
.vorteil_wrapper_reverse {
  margin-bottom: 30px;
  display: flex;
  justify-content: space-around;
  flex-direction: row-reverse;
}
.rentout_content_left {
  height: 200px;
  width: 100%;
  padding-right: 30px;
  display: flex;
  align-items: center;
}
.rentout_content_right {
  height: 200px;
  width: 100%;
  padding-left: 30px;
  display: flex;
  align-items: center;
}
.rentout_heading {
  font: 28px Raleway-Medium, sans-serif;
  margin-bottom: 6px;
  display: inline-block;
  text-decoration-color: red;
  width:100%;
}
.rentout_heading::after {
  content:"";
  display:block;
  border:1px solid red;
}
.rentout_paragraph {
  text-align: justify;
  font: 18px Raleway-Regular, sans-serif;
}
<div class="vorteil_wrapper">
  <div class="rentout_content_left">
    <p class="rentout_paragraph">
      <span class="rentout_heading">Erstellung eines professionellen Inserats</span></br>
  Wir besuchen Ihre Unterkunft und erstellen professionelle Fotos und Videos,
  welche nach der Bearbeitung für Ihr Inserat auf unserer Internetseite 
  genutzt werden. Darüber hinaus verfassen wir eine hochwertige Beschreibung
  und Präsentation Ihrer Unterkunft, welche die Highlights sowie die
  Atmosphäre Ihrer Unterkunft in Vordergrund setzt.
  </p>
</div>
<div class="rentout_picture_right">
  <img src="http://placehold.it/200x200"/>
</div>
</div>

<div class="vorteil_wrapper_reverse">
  <div class="rentout_content_right">
    <p class="rentout_paragraph">
      <span class="rentout_heading">Responsive Webdesign</span></br>
  Wussten Sie das 65% aller Internet Nutzer ein Handy zum Surfen benutzen!
  Ihr zukünftiges Inserat wird auf unserer Seite optimal auf allen Geräten
  angezeigt, um somit die Benutzerfreundlichkeit und die Umsatzrate zu
  steigern.
  </p>
</div>
<div class="rentout_picture_left">
<img src="http://placehold.it/200x200"/>
</div>
</div>

【讨论】:

  • 这实际上只给了我标题下方的下划线。该行没有段落的宽度,但感谢您的尝试。 ;)
  • @trickydiddy - 该行具有段落的宽度。它认为您忘记在代码中将width:100% 设置为&lt;span&gt;?在此处查看 sn-p 并检查...
  • 是的,你是对的!谢谢两个答案都是正确的,但不幸的是我只能选择一个......:/
猜你喜欢
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多