How can we make the border-bottom for all list items (connection) function effectively?

After creating an unsorted list with list items, here is how it appears:

<ul id="mylist">
 <li>
  <input type="checkbox" id="[[Array]]" />
  <label for="[[Array]]"><img src="images/[[ArrayImage]]"/></label>
  <div class="subtitle">[[Name]]</div>
 </li>
</ul>

Although the image and text are displayed as intended, I am facing an issue. When I try adding a border-bottom in CSS, only individual borders show up at each image. Is there a way to have a connected border, similar to what is shown in the picture below? Any assistance would be greatly appreciated. Thank you.

https://i.sstatic.net/b5U8e.png

Answer №1

I believe utilizing code snippets can enhance human understanding. I will modify some of your existing code and incorporate additional CSS styles:

#my-list {
  display: flex;
}

#my-list input {
  display: none;
}

#my-list input:checked ~ .subtitle {
  border-color: red;
}

#my-list input:checked ~ img {
  border-color: red;
}

#my-list li {
  width: 25%;
  list-style: none;
}

#my-list li:last-child {
  margin: 0;
}

#my-list li label {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#my-list li img {
  display: block;
  width: calc(100% - 20px);
  margin: 0 10px;
  border: 2px solid black;
}

#my-list li .subtitle {
  text-align: center;
  width: 100%;
  margin-top: 15px;
  border-bottom: 2px solid gray;
}
<ul id="my-list">
  <li>
    <label>
      <input type="checkbox">
      <img src="https://medicoads.ir/imgs/car1.jpg"/>
      <span class="subtitle">car-1</span>
    </label>
  </li>
  <li>
    <label>
      <input type="checkbox">
      <img src="https://medicoads.ir/imgs/car2.jpg"/>
      <span class="subtitle">car-2</span>
    </label>
  </li>
  <li>
    <label>
      <input type="checkbox">
      <img src="https://medicoads.ir/imgs/car3.jpg"/>
      <span class="subtitle">car-3</span>
    </label>
  </li>
  <li>
    <label>
      <input type="checkbox">
      <img src="https://medicoads.ir/imgs/car4.jpg"/>
      <span class="subtitle">car-4</span>
    </label>
  </li>
<ul>

Answer №2

A DIV element is set to display as a block by default, which means it will take up the full width of its parent element. Since the li element has no fixed width, the div will also take up the entire available width. By applying a border-bottom style to the div, it will span the full width and create a complete line.

There are two potential solutions to this issue, one of which is demonstrated in the code snippet below (you can click "Run Code Snippet" to see it in action).

<ul id="mylist">
  <li>
    <input type="checkbox" id="[[Array]]" />
    <label for="[[Array]]"><img src="images/[[ArrayImage]]"/></label><br>
    <span style="border-bottom: 2px solid grey;" class="subtitle">[[Name]]</span>
  </li>
</ul>

<ul id="mylist2">
  <li>
    <div>
      <input type="checkbox" id="[[Array]]" />
      <label for="[[Array]]"><img src="images/[[ArrayImage]]"/></label>
    </div>
    <span style="border-bottom: 2px solid grey;" class="subtitle">[[Name]]</span>
  </li>
</ul>

In the first solution, we convert the subtitle div into a span, which has inline-block display by default, and add an extra
tag to separate it visually.

In the second solution, we wrap everything except the subtitle in an extra div element to achieve the same outcome.

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

The beauty of Aurelia's scoped CSS

Embarking on my Aurelia journey, I find myself navigating through the vast world of web development with tools like nodejs, gulp, and more. The Aurelia CLI has made it convenient for me to set up a visually appealing Aurelia project in Visual Studio Code ...

Tips for adjusting the color of a <a> tag upon clicking, which remains unchanged until the URL is modified

My goal is to maintain the color of a link when it is clicked. Currently, hovering over the navbar link changes it to greyish, but I want it to remain a different color after clicking on it. I am implementing this using the react-router-dom library for the ...

HTML code for positioning an image after resizing

Within an HTML page, I have an image that has been resized to a width of 400px using CSS. .cropbox { width: 400px; height : auto; } Additionally, there is a JavaScript function associated with the image that allows users to select a point on the image. T ...

Causing a singular object to drift towards the right

Despite its simplicity, I have been struggling to position one of the items on the right side. Currently, I have multiple items and I am trying to get the 'Logout' button to stick to the right. body { margin: 0; background: #222; font-fa ...

How to perfectly center an image vertically within a div using CSS

Seeking assistance to properly align images of different sizes in the center of a container div. Check out the attached image for reference, showcasing the gray background and the desired alignment of images in the middle. https://i.sstatic.net/M16V1.png ...

Having Difficulty Applying a Background Color to a Class in Bulk

I am working on a unique HTML canvas for pixel art, which is created using a table made up of various divs all sharing the "pixel" class. I want to add a clear button that can reset the entire canvas, but changing the background color of each individual di ...

What is the best way to center align and add an icon to the header in Ionic?

How can I center align an "ion-ios-arrow-up" icon in the header of my Ionic app, similar to the concept shown below? This is my HTML template: <ion-view cache-view="false" view-title="Historical HPP"> <ion-nav-bar class="nav-title-slide-ios7 ...

Divide an HTML file into separate documents upon submitting a form

Is there a way to input HTML into a text area, then upon submission, have the system read the file, identify the class selector, and separate the HTML into multiple files saved in a directory? If you have any thoughts on how this could be accomplished, pl ...

Creating consistent row spacing between Bootstrap 5 rows that matches the spacing in Bootstrap 4

Is it possible to adjust the site.css file in order to achieve a similar row spacing effect as Bootstrap 5 when working with existing HTML code? ...

Navigation problems resolved: The fixed-top class causes issues with navbar-right and obscures the logo

I have implemented a bootstrap Nav on this page To achieve a sticky top navigation, I used the fixed-top class. However, this caused the navbar to align left instead of right as intended. It also ended up covering my logo, which was originally aligned to ...

Animating the Bookmark Star with CSS: A Step-by-Step Guide

I found this interesting piece of code: let animation = document.getElementById('fave'); animation.addEventListener('click', function() { $(animation).toggleClass('animate'); }); .fave { width: 70px; height: 50px; p ...

Is it possible to add some color to the first table in the div

Is it possible to change the background color of the first table within this Div tag that contains three tables? <div id="WebPartWPQ2" width="100%" HasPers="false" allowExport="false"> <table width="100%" border="0" cellSpacing="0" cellPadding= ...

What is the best way to position a background image so that it covers the entire screen but stops before reaching the bottom?

Having a bit of trouble with background image positioning here - it's simple enough to set a repeating background that starts from a specific point at the top. But what I'm trying to achieve is a black background that starts 100px from the top of ...

Guide to extracting JSON information in JavaScript that includes a URL as the key

I am having trouble parsing the json file from this URL. My goal is to retrieve the populationTotal, areaTotal, and populationDensity fields from the json data obtained through the URL mentioned above. Below is an excerpt of the json data retrieved from ...

Is it possible to incorporate a next.js image onto the background design?

I've encountered an issue while attempting to create a fixed background image with a parallax effect using Tailwind CSS and Next.js Image. If you need to see an example of this in action, check out this Template Monster theme. Here's the code s ...

Is using CSS as an HTML attribute considered poor practice?

I've been noticing an increasing trend of combining HTML, CSS, and JavaScript together in web development. However, I was taught to keep them separate with HTML linking to the sources/scripts. While using the style attribute in HTML is sometimes acce ...

Error: The term "Particles" has not been defined

I'm attempting to integrate code from a website into my project, but encountered an error when the particles failed to run after adding it. I downloaded and installed particle.js from "https://github.com/marcbruederlin/particles.js/issues" for this pu ...

I am attempting to showcase a retrieved value in HTML through an AJAX request, with the help of Flask

Hello everyone, I'm fairly new to web development and currently working on a flask application. My goal right now is to test an AJAX function to ensure it's functioning correctly. This function reads radio inputs from HTML, and my aim is to displ ...

Avoiding navigation bar overlap in CSS techniques

I have successfully implemented dropdown navigation with absolute positioning, but it is causing the left side to squash and the content above it to be hidden behind the navigation. Is there a way to prevent my navigation from overlapping other content wh ...

Methods to prevent images from spilling over into divs?

I am looking to implement a unique functionality that involves three images sliding out of the page to the right, fading in the process, and not overflowing. Simultaneously, three other images slide in to take their place. If you want to see the Framework ...