css 我想把一个框中的文本固定在一列的右边,把图像固定在左边

xuo3flqw  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(141)
.timeline {
  position: relative;
  max-width: 800px;
  margin: 0 auto;
  padding: 50px 0;
}

.timeline:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  background-color: #ccc;
  transform: translateX(-50%);
  z-index: -1;
  /* Added z-index to move the line behind the year circles */
}

.timeline-item {
  position: relative;
  width: 100%;
  margin: 0 0 100px 0;
}

.timeline-year {
  position: absolute;
  top: -40px;
  /* Increased top position to make space for the year circle */
  left: 50%;
  width: 60px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  border-radius: 50%;
  background-color: #ccc;
  transform: translateX(-50%);
  color: #fff;
  z-index: 2;
  /* Added z-index to move the year circles in front of the line */
  justify-content: center;
}

.timeline-content {
  position: relative;
  padding-top: 80px;
  display: flex;
  flex-direction: row;
  align-items: center;
  background-color: #f2f2f2;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
  z-index: 2;
  /* Added z-index to move the content boxes in front of the line and year circles */
}

.timeline-content h3 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  text-align: center;
  margin: 0;
  padding: 10px;
}

.timeline-content p {
  margin: 0;
  padding: 10px;
  box-sizing: border-box;
}

.timeline-image {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 50px;
  align-self: flex-start;
}

.timeline-image img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.timeline-year::before {
  content: attr(data-year);
  font-size: 20px;
  font-weight: bold;
  color: #fff;
}

.timeline-year:hover::before {
  /* Added hover effect to year circles */
  content: "";
  width: 90px;
  height: 90px;
  line-height: 90px;
  font-size: 30px;
  border-radius: 50%;
  background-color: #000;
  opacity: 0.8;
}
<div class="timeline">
  <div class="timeline-item">
    <div class="timeline-year" data-year="2000"></div>
    <div class="timeline-content">
      <div class="timeline-image"><img src="https://via.placeholder.com/150"></div>
      <h3>title1</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </div>
  <div class="timeline-item">
    <div class="timeline-year" data-year="2001"></div>
    <div class="timeline-content">
      <div class="timeline-image"><img src="https://via.placeholder.com/150"></div>
      <h3>title2</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </div>
  <div class="timeline-item">
    <div class="timeline-year" data-year="2002"></div>
    <div class="timeline-content">
      <div class="timeline-image"><img src="https://via.placeholder.com/150"></div>
      <h3>title3</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </div>
</div>

但结果就像我发布的图片一样:

我希望我写的文本在图像的右侧对齐,一个在另一个下面,而不是像一行,而不是并排,我希望图像在框的左侧对齐,而不会被挤压,而不会改变其大小。但我做不到。你能帮我吗?

ekqde3dh

ekqde3dh1#

p元素并排的原因是因为它们是flex父元素的直接子元素。在本例中,timeline-content是flex父节点。
将文本 Package 在自己的div中是否可以让您更接近您想要的内容?

<div class="timeline-content">
  <div class="timeline-image"><img src="https://via.placeholder.com/150"></div>
  <h3>title1</h3>
  <div class="timeline-text">  
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>

</div>

这一更改使它们回到堆栈,因为p元素不再是flex父元素的直接子元素。相反,div Package 器是flex父级的直接子级。
编辑:根据您的评论,我已经添加了一个类的 Package 我添加,然后我们可以添加这些样式到您的CSS

.timeline-text{
  flex-basis:0;
  flex-grow:1;
}

flex-basis将防止 Package 器试图挤压图像,而flex-grow将允许文本 Package 器占用图像之外的所有可用空间。

相关问题