I would like to create a vertical line similar to the ones seen on Twitter

I'm currently working on a project to create a social media platform similar to Twitter. One thing I'm trying to accomplish is to draw a vertical line between two images, like the one in this tweet from PewDiePie, and have it centered between two separate <div> elements.

So far, I've attempted placing a

<div class="vertical-line"></div>
after the image <div>.

.vertical-line {
    border-width: 2px;
    border-top-left-radius: 2px;
    border-color: #1da1f2;
    border-style: solid;
}

After implementing this CSS, the result looked like this.

Answer №1

To easily generate a vertical line using only CSS, the method is to utilize the border-left property within an empty div with a defined height. As pointed out by tkore in the comments, calculating the height of the .line class using JS is necessary based on the gap between the images, as this value is not predetermined. However, the remaining code will function without any issues.

img {
    border-radius: 50%;
    margin: 5px;
}

.line {
    position: relative;
    bottom: 2px;
    left: 38px;
    border-left: 6px solid red;
    height: 85px;
}
<img src="https://pbs.twimg.com/profile_images/1113436678050316289/t-Agpngx_bigger.jpg">
<div class="line"></div>
<img src="https://pbs.twimg.com/profile_images/1113436678050316289/t-Agpngx_bigger.jpg">

Another option is to utilize pseudoelement selectors for creating a vertical line. However, without having access to your specific code, providing a precise answer using this technique would be challenging.

Answer №2

My coding skills are put to the test with each basic listing that I write. The content dictates the layout, as the left bar expands with more content. Give this code a try, it should provide some assistance. Thank you.

ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  display: flex;
  height: auto;
  overflow: hidden;
}

li:last-child .line {
  display: none;
}

.avatarWrap {
  display: flex;
  flex-direction: column;
  width: 28px;
}

.avatarWrap .circle {
  background-color: #ccc;
  border: 3px solid #fff;
  border-radius: 25px;
  display: block;
  flex-shrink: 0;
  position: relative;
  height: 22px;
  width: 22px;
}

.avatarWrap .line {
  background-color: #ccc;
  border-radius: 3px;
  content: '';
  display: block;
  flex: 1;
  height: 100%;  
  margin-left: 11px;
  width: 5px;
}

.content {
  font-size: 12px;
  margin: 8px 0 0 20px;
}
<ul>
  <li>
    <div class="avatarWrap">
      <span class="circle"></span>
      <span class="line"></span>
    </div>
    <div class="content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    </div>
  </li>
  <li>
    <div class="avatarWrap">
      <span class="circle"></span>
      <span class="line"></span>
    </div>
    <div class="content">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
  </li>
  <li>
    <div class="avatarWrap">
      <span class="circle"></span>
      <span class="line"></span>
    </div>
    <div class="content">
      There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form,
    </div>
  </li>
  <li>
    <div class="avatarWrap">
      <span class="circle"></span>
      <span class="line"></span>
    </div>
    <div class="content">
      There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form,
    </div>
  </li>
</ul>

Answer №3

A clever technique to achieve the desired effect is by using a negative margin along with positive padding. By utilizing this method, you can effectively push the top element down to the child element, allowing the line to be drawn and then repositioned using padding. This eliminates the need for javascript or any additional complexities.

.tweet {
  height: 100px;
  background-color: yellow;
  width: 40px
}
.avatar {
  width: 40px;
}
.vertical-line {
  margin-bottom: -60px;
  padding-bottom: 60px;
  background: linear-gradient(#ff0000, #ff0000) no-repeat center/2px 100%;
}
<div class="tweet " style="background-color: pink">

  <div class="avatar vertical-line">
    <img src="https://i.pravatar.cc/40">
  </div>

</div>

<div class="tweet" style="background-color=yellow">

  <div class="avatar">
    <img src="https://i.pravatar.cc/40">
  </div>

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for customizing a button's font with CSS

Update: I have noticed that the issue mentioned in this question is specific to OS X browsers. I am looking to modify the font style of my input buttons using CSS, similar to the following: input[type="button"] { font: italic bold 3em fantasy; } How ...

Customize JqGrid Edit/Delete button CSS style

Within my JQGrid, I have successfully implemented Edit and Delete buttons that redirect users to another page for editing or deleting specific records. However, I am facing a CSS issue with the styling of these buttons. How can I override the current style ...

What steps should be taken to prevent divs from overlapping a centrally positioned, unchanging div when the browser is resized

Looking to create a layout with two columns, featuring a fixed sidebar on the left and centering the main content area. The goal is for the centered content to remain in place when resized and not move further left than where it touches the nav bar (left: ...

Tips for resolving Layout Shift issues in responsive images utilizing the picture & source HTML tags

One issue I encountered with the <source> element is that even when specifying the width and height attributes for both the <source> and image fallback, there is still significant layout shift. This could be due to using images with varying dim ...

How to bring in Bootstrap 4 Sass mixins to the scss editor on codepen.io

Experimenting with Bootstrap4 on codepen.io, I decided to import the production bundle of bootstrap (bootstrap.bundle.min.js) from a CDN. But now I'm stuck trying to figure out how to import _breakpoints within my SCSS. I really need to use Bootstrap ...

A guide on triggering a new chart to appear beside the adjacent <div> when a bar is clicked in a highchart

I'm a beginner with Highcharts and I have a requirement for two charts (let's call them Chart A and Chart B). Creating one chart is straightforward. What I need is, upon clicking on a bar in Chart A, a new chart (Chart B) should open next to the ...

Button-controlled automated presentation

I have devised a method to create an automatic slideshow using JavaScript. However, I am looking to incorporate manual control buttons as well. After adding manual control code, the slideshow seems to be malfunctioning. The provided code below is used for ...

Setting objects in parallel in HTML involves using the display property along with the value

Creating two sign-up forms using HTML and CSS Bootstrap. Want them to be parallel, tried using the position tag but there is unwanted space created between the forms. View the output How can I prevent this circle of space from being generated? <main cl ...

Aligning the text in the middle of a button

Currently delving into Front-End development and in the process of coding my maiden site using bootstrap. Encountered a roadblock on something seemingly simple. How can I center text within a button? Here's the button, the default one utilizing an " ...

Could this be the most straightforward and versatile method for vertically centering text inside a block element?

Discovering something new in the world of coding is always exciting: HTML: <div>Vertically Centered Text<span></span></div> CSS: div { height: 100px; /* adjust as needed */ } div > span { display: inline-block; v ...

What methods can I use to identify the selector for this element?

After pinpointing the specific element in Google Chrome using inspect element, what kind of path or syntax should I use to apply a method to this element? The hierarchy for the element is outlined below: html body div#contentDiv div#searchFormDiv ...

Combining two images using HTML and CSS resulted in conflicts when attempting to overlay any additional elements

I have managed to overlay two images successfully, but I am experiencing conflicts when trying to do the same with other div images. Here is the code on jsfiddle: https://jsfiddle.net/cLew1t2v/ and here is the code snippet: <div> <div style ...

Is it possible to rotate the caret in the dropdown 180 degrees using only CSS?

Is there a way to achieve a 180° rotation of the caret in a dropdown menu upon clicking it, creating the illusion of an animation? I would prefer to accomplish this using CSS only, without the need for JavaScript, but I am open to any suggestions. .se ...

Design a styled rectangular tab using CSS

Does anyone know of any cool CSS techniques for achieving the tabbed rectangle appearance depicted in the image below? While it's clear that accomplishing this with just one div is not possible, is there a more efficient method than layering one div ...

Bootstrap 5: After tapping on the button, the hue was transformed

I've been using Bootstrap buttons and I decided to change the color of one. However, when I clicked the button, it still changed to blue even though I specified ".btn-primary:active". HTML: <button type="button" class="btn btn-prima ...

Exploring the functions of Safari input types: search, reset, and normalize

Is there a proper way to reset the appearance of a search input so that it resembles a text field in Safari? screenshot I'm experiencing an issue with Safari where there is unwanted padding on the left side of the search input that I can't seem ...

Tips on ensuring that PRE elements expand to fit column size without growing when additional data is added

I am facing a design challenge where I have a main card (1) that contains two sub-cards (2 and 3) using Bootstrap columns. Sub-card number 2 adjusts its height dynamically based on the screen size, while sub-card number 3 consists of a code snippet element ...

PHP function that verifies the post category and dynamically modifies a specific CSS element associated with it (without using WordPress)

Looking for a PHP5 function to determine a post's category and change a part of its style on the homepage accordingly. Any suggestions on where to start? (Not using WordPress) Thanks. ...

How can I synchronize two webkit-animations at the same speed but with one covering a greater distance?

[Update: The solution involved creating two containers, with the second animation container configured to have left: 100%.] I designed a simple animation that moves a large gif image across the page horizontally, where the width of the gif is 1536px. Sin ...

Inject some vibrancy by incorporating color in the space between the navbar and banner on the

Hello! I am relatively new to front-end development and I am currently working on converting a PSD design to code for a project. I am struggling a bit to understand certain concepts but I am eager to learn and improve. I am using Bootstrap 4 for this proje ...