将分隔线添加到CSS列

发布于 2025-02-10 12:54:44 字数 432 浏览 1 评论 0原文

我正在尝试用2列与分隔线创建一行,如下所示:

“在此处输入图像说明”

有一个浅色分隔线,还有一个白色分隔线,应该根据这样的文本数量增加:

如何在CSS中实现这一目标,以便根据任一列中的文本量增加白线分隔线?

I am trying to create a row with 2 columns with a divider as shown below:

enter image description here

There is a light divider and also a white divider that should increase according to text amount like this:

enter image description here

How can I achieve this in CSS so that the white line divider increases based on the text amount in either column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2025-02-17 12:54:44

您可以为长效边框创建一个容器,并在文本元素上应用一个短裤

.flexbox {
  display: flex;
}

.text-container {
  padding: 1rem 0; /*Create a top-bottom padding for text container*/
  width: 50%;
}

.text-container span {
  padding: 0 1rem; /*Create a left-right padding for text*/
  display: block;
}

.text-container:first-of-type {
  border-right: 1px solid #ccc; /*Border on text container*/
}

.text-container:first-of-type span {
  border-right: 4px solid #ddd; /*Border on text*/
  margin-right: -2.5px;
}
<div class="flexbox">
  <div class="text-container">
    <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
  </div>
  <div class="text-container">
    <span>Lorem ipsum dolor sit amet</span>
  </div>
</div>

You can create a container for the long-stick border, and apply a short-stick border on text elements

.flexbox {
  display: flex;
}

.text-container {
  padding: 1rem 0; /*Create a top-bottom padding for text container*/
  width: 50%;
}

.text-container span {
  padding: 0 1rem; /*Create a left-right padding for text*/
  display: block;
}

.text-container:first-of-type {
  border-right: 1px solid #ccc; /*Border on text container*/
}

.text-container:first-of-type span {
  border-right: 4px solid #ddd; /*Border on text*/
  margin-right: -2.5px;
}
<div class="flexbox">
  <div class="text-container">
    <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
  </div>
  <div class="text-container">
    <span>Lorem ipsum dolor sit amet</span>
  </div>
</div>

只想待在家 2025-02-17 12:54:44
@import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');
body {
  background: #111;
  display: flex;
  height: 100vh;
  width: 100vw;
  padding: 0;
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  font-size: 1.2rem;
  align-items: center;
  justify-content: center;
}

.container {
  width: 700px;
  box-shadow: 0px 0px 10px #000;
  background: #222;
  color: rgb(140, 140, 140);
}

.text {
  margin: 0;
  padding: 40px;
  width: 50%;
  display: flex;
  align-items: center;
}

.textcontainer {
  display: flex;
  align-items: stretch;
  margin: 0;
  padding: 0;
}

.divider {
  display: inline-block;
  width: 1px;
  background: #fff;
  align-self: stretch;
  margin-top: 40px;
  margin-bottom: 40px;
  z-index: 1;
}

.text:nth-of-type(even) {
  border-left: 0.5px solid #444;
}
<div class="container">
  <div class="textcontainer">
    <p class="text">
      It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
    </p>
    <span class="divider"></span>
    <p class="text">
      Hello World. this is a test. And it looks awesome to design.
    </p>
  </div>
</div>

@import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');
body {
  background: #111;
  display: flex;
  height: 100vh;
  width: 100vw;
  padding: 0;
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  font-size: 1.2rem;
  align-items: center;
  justify-content: center;
}

.container {
  width: 700px;
  box-shadow: 0px 0px 10px #000;
  background: #222;
  color: rgb(140, 140, 140);
}

.text {
  margin: 0;
  padding: 40px;
  width: 50%;
  display: flex;
  align-items: center;
}

.textcontainer {
  display: flex;
  align-items: stretch;
  margin: 0;
  padding: 0;
}

.divider {
  display: inline-block;
  width: 1px;
  background: #fff;
  align-self: stretch;
  margin-top: 40px;
  margin-bottom: 40px;
  z-index: 1;
}

.text:nth-of-type(even) {
  border-left: 0.5px solid #444;
}
<div class="container">
  <div class="textcontainer">
    <p class="text">
      It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
    </p>
    <span class="divider"></span>
    <p class="text">
      Hello World. this is a test. And it looks awesome to design.
    </p>
  </div>
</div>

风铃鹿 2025-02-17 12:54:44

其他各自的答案不能解决列ab可以具有变量内容的问题。并明确将保证金应用于任何一列都不会解决问题白色分隔线,该划分应根据此类文本数量增加 白线分隔线的增加,根据任一列中的文本量增加/代码>。

解决方案

我想不出任何CSS解决方案,因为没有任何可用方法可以通过CSS获得元素的高度。必须使用一小部分JS解决问题。

// get paragraphs height
let heightColA = document.querySelector('.col-a p').offsetHeight;
let heightColB = document.querySelector('.col-b p').offsetHeight;
// applying border after checking if either Col A or Col B has more height
if (heightColA > heightColB) {
  document.querySelector('.col-a p').style = 'border-right: 5px solid #fff; margin-right: -5px;'
} else {
  document.querySelector('.col-b p').style = 'border-left: 5px solid #fff; margin-left: -5px;'
}
// console.log(heightColA, heightColB)
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  width: 100%;
  background-color: #000;
  height: 500px;
  color: #fff;
}

.container>div {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container .col-a {
  border-right: 5px solid #d00;
}

.container>div>p {
  padding: 2rem;
}
<div class="container">
  <div class="col-a">
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure, hic provident? Iste commodi neque corrupti debitis mollitia atque, eveniet iure!
    </p>
  </div>
  <div class="col-b">
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure, hic provident? Iste commodi neque corrupti debitis mollitia atque, eveniet iure! Lorem ipsum dolor sit amet consectetur, adipisicing elit. Minima ipsum dolorem, nisi voluptates, rerum
      ipsa labore eius aliquam quas magni amet. Accusantium architecto perspiciatis, minus non voluptatibus totam sequi. Placeat sint error doloremque eveniet quibusdam quo delectus, laboriosam alias autem voluptatum unde eum, officiis omnis vel molestias
      dolorem distinctio nulla! Voluptatem qui explicabo corrupti autem consequuntur cupiditate sequi quos reprehenderit velit, porro, assumenda, officiis deserunt! Corrupti qui perspiciatis nemo. Repellendus qui facilis similique earum molestias distinctio
      quae sed, laboriosam laudantium corporis, suscipit, dolor accusamus pariatur iste nam. Exercitationem ab vitae eveniet. Blanditiis, facilis quidem eos ad aut quibusdam laudantium a!
    </p>
  </div>
</div>

尝试增加任何列中的内容,白色分隔线将相应增加。

希望这回答问题。

Other respective answers do not address the problem that either column A or B can have variable content. And explicitly applying margin to any of the columns wouldn't solve the problem white divider that should increase according to text amount like this and white line divider increases based on the text amount in either column.

Solution

I couldn't think of any CSS solution as there is not any available way to get elements height through CSS. Have to use a tiny bit of JS to solve the problem.

// get paragraphs height
let heightColA = document.querySelector('.col-a p').offsetHeight;
let heightColB = document.querySelector('.col-b p').offsetHeight;
// applying border after checking if either Col A or Col B has more height
if (heightColA > heightColB) {
  document.querySelector('.col-a p').style = 'border-right: 5px solid #fff; margin-right: -5px;'
} else {
  document.querySelector('.col-b p').style = 'border-left: 5px solid #fff; margin-left: -5px;'
}
// console.log(heightColA, heightColB)
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  width: 100%;
  background-color: #000;
  height: 500px;
  color: #fff;
}

.container>div {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container .col-a {
  border-right: 5px solid #d00;
}

.container>div>p {
  padding: 2rem;
}
<div class="container">
  <div class="col-a">
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure, hic provident? Iste commodi neque corrupti debitis mollitia atque, eveniet iure!
    </p>
  </div>
  <div class="col-b">
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure, hic provident? Iste commodi neque corrupti debitis mollitia atque, eveniet iure! Lorem ipsum dolor sit amet consectetur, adipisicing elit. Minima ipsum dolorem, nisi voluptates, rerum
      ipsa labore eius aliquam quas magni amet. Accusantium architecto perspiciatis, minus non voluptatibus totam sequi. Placeat sint error doloremque eveniet quibusdam quo delectus, laboriosam alias autem voluptatum unde eum, officiis omnis vel molestias
      dolorem distinctio nulla! Voluptatem qui explicabo corrupti autem consequuntur cupiditate sequi quos reprehenderit velit, porro, assumenda, officiis deserunt! Corrupti qui perspiciatis nemo. Repellendus qui facilis similique earum molestias distinctio
      quae sed, laboriosam laudantium corporis, suscipit, dolor accusamus pariatur iste nam. Exercitationem ab vitae eveniet. Blanditiis, facilis quidem eos ad aut quibusdam laudantium a!
    </p>
  </div>
</div>

Try increasing content in any of the column, the white divider will increase accordingly.

Hope this answers the questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文