Include a border around the list items within two separate columns

I am trying to create 2 columns using li and want to add borders to each of them. However, I'm facing an issue where there are double borders for 2 li. This is the code snippet I have tried:

.ul1 {
    column-count: 2;
    column-gap: 0px;
}

.li1 {
    border: 1px solid blue;
}
    <ul class="ul1">
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
    </ul>

After trying out the above code, I found a solution using the child selector:

View Demo

HTML File

    <ul class="ul1">
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
      <li class="li1">Test</li>
    </ul>

CSS File

.ul1 {
  column-count: 2;
  column-gap: 0px;
}

.li1 {
  border-top: 1px solid blue;
  border-left: 1px solid blue;
  border-right: 1px solid blue;
  margin-right: -1px;
  &:nth-child(5) {
    border-bottom: 1px solid blue;
  }
    &:nth-child(10) {
    border-bottom: 1px solid blue;
  }
}

However, I'm unsure if this approach is considered good practice or if there could be a better way to achieve the desired outcome. Any guidance and suggestions on how to improve this would be highly appreciated.

Answer №1

If you have a list that needs fixing, you can experiment with using the :nth-child(-n+5) selector to target the first five children in your list and manipulate their border properties.

Take a look at the snippet below for reference.

.ul1 {
  column-count: 2;
  column-gap: 0px;
}

.li1 {
  border: 1px solid blue;
  border-bottom: 0px;
}

.li1:nth-child(-n+5) {
  border-right: 0px;
}

.ul1 li:last-child,
.ul1 li:nth-child(5) {
  border-bottom: 1px solid blue;
}
<ul class="ul1">
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
  <li class="li1">Test</li>
</ul>

Answer №2

Take a look at the following CSS code snippet:

.ul1 {
  column-count: 2;
  column-gap: 0px;
}

.li1 {
  border-top: 1px solid blue;
  border-left: 1px solid blue;
  border-right: 1px solid blue;
  margin-right: -1px;
 list-style: none;
}

.ul1 li:last-child, .ul1 li:nth-child(5) {
 border-bottom: 1px solid blue; 
margin-right: -1px;
}

Answer №3

After considering your query, I have devised a method to add a border list to the <li> elements in 2 columns as specified below:

<html>
<head>
<style>
ul{
  width:760px;
  margin-bottom:20px;
  overflow:hidden;
  border-top:1px solid #ccc;
}
li{
  line-height:1.5em;
  border-bottom:1px solid #ccc;
  float:left;
  display:inline;
}
#double li  { width:50%;} <span class="code-comment">/* 2 col */</span>
#triple li  { width:33.333%; } <span class="code-comment">/* 3 col */</span>
#quad li    { width:25%; } <span class="code-comment">/* 4 col */</span>
#six li     { width:16.666%; } <span class="code-comment">/* 6 col */</span>
</style>
<title>demo</title>
</head>
<body>
<ul id="double"> <span class="code-comment"><!-- Adjust ID as needed --></span>
  <li>CSS</li>
  <li>XHTML</li>
  <li>Semantics</li>
  <li>Accessibility</li>
  <li>Usability</li>
  <li>Web Standards</li>
  <li>PHP</li>
  <li>Typography</li>
  <li>Grids</li>
  <li>CSS3</li>
  <li>HTML5</li>
  <li>UI</li>
</ul>
</body>
</html>

The visual representation of the above code can be viewed in the following image:

Click here for Output Image

I trust this coding solution will meet your needs. Thank you.

Answer №4

Kindly review the information below

.ul1 {
  column-count: 2;
  column-gap: 0px;
    margin:0;
     border-right: 1px solid blue;
     border-bottom: 1px solid blue;
     padding:0;
         
}

.li1 {

  border-left: 1px solid blue;
  border-top: 1px solid blue;
  
}
<ul class="ul1">
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
  <li class="li1">Example</li>
</ul>

For instance, this is a functional example

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

How do I customize the appearance of an Element-UI data table in Vue?

I'm struggling to customize the color of an Element UI datatable in my project. I've tried several approaches, but so far, nothing has worked. Here is the code I am currently using: <template> <el-table :data="tableData.filter ...

Control Over Fluid Grid Layouts

Apologies for reaching out, as I usually try to figure things out on my own. However, after spending hours reading through countless pages with no success, I am at my wit's end. I'm struggling to create a 4-column 'home' landing page f ...

Resize Pictures in Carousel for Smartphones

Currently, I am in the process of optimizing my website for mobile devices. The NivoSlider, which is used throughout the site, does not seem to scale well on smaller screens. Despite trying various methods in the mobile section of my stylesheet such as " ...

Create a fully responsive introduction page with three columns using Bootstrap, ensuring that the height of the

Currently, I am in the process of designing a website for a restaurant, hotel, and vineyard. My goal is to create three separate websites that are linked through one introductory page. The concept for the intro page involves three columns displayed side b ...

What is the best way to create an interactive menu icon that includes dropdown options using MUI menu features?

i am looking to create a component similar to this https://i.sstatic.net/fwIB0.png currently, I am utilizing a material UI pop menu in my project below is the JSX code snippet <div className="navMenu" style={{ position: "relative" ...

Ways to access dropdown menu without causing header to move using jQuery

Greetings everyone, I am currently working on a dropdown language selection feature for my website. The issue I am facing is that when I click on the language dropdown in the header, it causes the height of the header to shift. $('.language- ...

Switching icon images using JQuery based on state changes

I'm attempting to switch the background image of my webpage's "body" element when the user toggles between playing and pausing music icons. This is what the CSS for the "body" element looks like: body { background: url('https://s3-us-wes ...

Steps for selecting a specific checkbox

I need to interact with the "Select page" checkbox located in the drop-down menu labeled as "All". Here is the HTML code: <ul class="list-unstyled"> <li> <input id="selectAllTop" name="selectAllCheckBox" attr-sel="All" type="ch ...

The functionality of HTML labels may be limited when used in conjunction with AJAX

I am using Ajax to load an HTML page and encountering a problem with the label not working properly when associated with a checkbox. Here is the data: <input type="checkbox" name="dis_net" id="dis_net" value="1" /> <label for="dis_net">Test< ...

The Input element's onChange event is not functioning as expected

I am experiencing some issues with my code. I am trying to dynamically change the background color based on user input, but I am struggling to make it work. It seems like a simple task, so I must be missing something. Below is my HTML markup and jQuery: ...

Tips on completing landing page animations and displaying the main page of a website by utilizing React JavaScript

https://i.stack.imgur.com/M4VgY.pngIs there a way to stop the landing page animation after 2-3 seconds and show the main page instead? Can I achieve this by using setTimeOut function in JavaScript? [ export default Loader; ...

Integrating Twitter bootstrap into Django version 1.5

I have been attempting to incorporate Twitter bootstrap into my django application. Within settings.py, I have set up the following: STATIC_URL = '/static/' # Additional locations of static files STATICFILES_DIRS = ( # Add paths for static ...

How can I ensure a header is displayed on each page by utilizing CSS or JavaScript/jQuery?

On a lengthy page with approximately 15 pages of text, I would like to insert a header at the beginning of each page when the document is printed by the user. Can this functionality be achieved using CSS or JavaScript/jQuery? ...

Loading elements of a webpage in a consecutive order

I am currently working on implementing a content slider within my Django application. The specific slider that I am using can be found at this link. One challenge that I am facing is the need to load close to one hundred 'contentDiv' pages, and I ...

Converting a data type into a CSS format

Currently, I am in the process of creating a table and would like to implement a 4-color scheme randomly selected from a pool of 10 schemes. Below is my CSS/PHP code: <?php header('Content-type: text/css'); // 0 grey $color00 = '#95 ...

Adapt the stylesheet for mobile devices using CSS

I am struggling with updating file paths in CSS depending on whether the end user is accessing my site from a PC or mobile device. Below is my CSS code, where I attempted to redirect users if they are on a handheld device: <link rel="stylesheet" type=" ...

Encountered a 404 error while handling the Express 4 module, indicating that the 'html' module could not be

I need to update my express app to handle 404 (and 500) errors by displaying HTML instead of plain text. I have been able to show text to the client for 404 errors, but now I want to show HTML instead. My 404.html file is located in the /app directory. Cu ...

Positioning of bottom navigation bars

Is there a way to ensure that the bottom navigation bar remains fixed at the bottom of the screen, regardless of the device's screen size? position: absolute; bottom: 0; width: 100%; While this solution works, it has been observed that when th ...

Choosing only those elements that are not children of parents with a specific class by utilizing the `.not()` method

I am attempting to target all elements having the class .select that are nested somewhere within the DOM tree. The only condition is that these elements should not have any ancestors with the class .forbidden. This means it will not detect any elements ...

Enhancing elements with fade-in effects upon hovering

Is there a way to incorporate a subtle fade in/fade out effect when hovering over items on this webpage: http://jsfiddle.net/7vKFN/ I'm curious about the best approach to achieve this using jQuery. var $container = $("#color-container"), ...