Tips for designing a responsive element with a repeating border

Desired Design:

  • Create a dotted border on top of each li-element
  • Adjust the size of the dots and spacing between them using CSS or image/SVG manipulation
  • Make the width of the ul responsive for varying border widths
  • Ensure that dots are not partially displayed when resizing the viewport, only full circles should be visible

In Brief: I want to avoid this issue (visible in the last dot?) while resizing the window:

https://i.sstatic.net/Esozt.jpg

I have ruled out these methods for achieving this effect:

  • Using a tiled background image
  • Utilizing an enormous background graphic
  • Implementing border-image

Solution Attempt:

I discovered a solution, although it requires tedious work. It functions by utilizing numerous unnecessary span elements since the maximum element width is unknown.

The concept is simple: Dots that don't fit float into the hidden overflow.

Source

HTML

<ul>
  <li>
    <div>
      <span></span><span></span><span></span><span></span><span></span><span></span>
    </div>
    Item 1
  </li>
  <li>
    <div>
      <span></span><span></span><span></span><span></span><span></span><span></span>
    </div>
    Item 2
  </li>
</ul>

CSS

ul {
  margin: 0;
  padding: 0;
  list-style: none;
  line-height: 60px;
}

ul > li div {
  overflow: hidden;
  height: 2px;
}

ul > li div span {
  float: left;
  width: 2px;
  height: 2px;
  margin: 0 4px 0 0;
  background: grey;
}

JSFiddle

Test it here

Is there an elegant approach to address this, perhaps through inventive SVG or gradient techniques?

Answer №1

border-image is the ideal solution in my opinion. You have the flexibility to control the size and specify that the repeats are rounded to your desired width.

li {
  font-size: 40px;
}

.small {
  border: solid transparent;
  border-width: 15px 0 0;
  border-image:url("https://mdn.mozillademos.org/files/4127/border.png") 27 27 round;
}

.large {
  border: solid transparent;
  border-width: 30px 0 0;
  border-image:url("https://mdn.mozillademos.org/files/4127/border.png") 27 27 round;
}
<ul>
  <li class="small">First list item</li>
  <li class="large">Second list item</li>
</ul>

I opted for a diamond-shaped PNG from the MDN website, but you can easily create a PNG (or SVG) with your preferred dot shape.

Answer №2

Below is an illustration showcasing the usage of SVG:

function findMidpointDotLocation( position, dotWidth ) {
  return ( dotWidth / 2 ) + ( position ) * dotWidth * 2;
}
function getRightPosition( position, dotWidth ) {
  return findMidpointDotLocation( position, dotWidth ) + dotWidth/2;
}

function generateSVG( options ) {
  var width = options.width;
  var height = options.dotWidth;
  var svgContent = '<svg class="top-dot" width="' + width + 'px" height="' + height + 'px">';
  var left;
  var radius = options.dotWidth / 2;
  var i = 0;
  var right = getRightPosition( i, options.dotWidth );
  while( right < width ) {
    center = findMidpointDotLocation( i, options.dotWidth );
    svgContent += '<circle cx="' + center + '" cy="' + radius + '" r="' + radius + '" stroke-width="0" fill="' + options.color + '" />';
    i++;
    right = getRightPosition( i, options.dotWidth );
  }
  svgContent += '</svg>';
  return svgContent;
}

function displayDots() {
  var options = {
    width    : $('ul').first().innerWidth(),
    dotWidth : 2,
    color    : 'grey'
  };
  var svgCode = generateSVG( options );
  $( 'li svg.top-dot' ).remove()
  $( 'li' ).prepend( svgCode ); 
}

displayDots();
$( window ).resize(function() {
  displayDots();  
});
ul {
  margin: 0;
  padding: 0;
  list-style: none;
  line-height: 60px;
  width: 50%;
}

svg.top-dot {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>Item 1</
  <li>Item 2</li>
</ul>

Answer №3

Run a simple and direct test on various browsers and report back to me with the results. Make sure to test it on Firefox, Chrome, Opera, and IE 11. View it in full screen mode and resize the browser for accuracy.

The image tiles are rounded to fill the area and will be rescaled if needed to prevent tile division.

li {
  padding: 5px;
  border-style: dotted;
  border-color: rgba(213, 126, 0, 1);
  list-style: none;
  border-width: 15px;
  border-image: url("http://f.cl.ly/items/0V28170w0f0k3t3S2p0g/dots.svg") 33% round;
  border-bottom: 0;
  border-left: 0;
  border-right: 0;
}
<ul>
  <li>First Element</li>
  <li>Second Element</li>
  <li>Third Element</li>
</ul>

Answer №4

To improve the structure, I would suggest inserting a <hr/> tag above every <ul>.
Does this align with what you were looking for?

<div>
  <hr/>
  <ul> 
    <li>Item 1</li>
  </ul>
  <hr/>
  <ul> 
    <li>Item 2</li>
  </ul>
</div>



hr{
  width:100%;
  color:#333;
  border-top: dotted 3px;
}

ul > li div span {
  float: left;
  width: 2px;
  height: 2px;
  margin: 0 4px 0 0;
  background: grey;
}

Answer №5

li {
  font-size: 40px;
}

.small {
  border: solid transparent;
  border-width: 15px 0 0;
  border-image:url("https://i.sstatic.net/Esozt.jpg") 30 30 round;
}

.large {
  border: solid transparent;
  border-width: 30px 0 0;
  border-image:url("https://i.sstatic.net/Esozt.jpg") 30 30 round;
}
<ul>
  <li class="small">1st list item</li>
  <li class="large">2n list item</li>
  <li class="small">3rd list item</li>
</ul>

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

Completely triggering a forced refresh on a single page application, disregarding cache, service workers, and all other

After experimenting with service workers on my Vue-built website, I made a critical error that has left Safari on my iPhone displaying only a blank page. To troubleshoot the issue, I connected my phone to my Mac and utilized Safari's inspector tool t ...

Floating elements within scrolling loading

I've been trying to figure out how to add a loading scroll feature to my webpage, but I'm running into issues with the div not displaying correctly. Instead of loading underneath the scroll, the content pops up on the side as shown here when scro ...

Non-clickable image

My social media page icons are not hyperlinked, and I can't figure out why. I've surrounded them with the href attribute, but for some reason, it's not working. Can anyone provide assistance? Here is the HTML code: <!DOCTYPE html> ...

What could be causing the issue of my p tag not being centered within both of my content divs?

I'm struggling to center the paragraphs within the divs with classes .content1 and .content2. Can anyone help me figure out why? Check out the demo here Here is the code: .content1 p, .content2 p { text-align: center; background-color: rgba ...

Adjust Scale to Less than 1 in JVectorMap

I am in the process of developing a website that includes a full-size map using JVectorMap. The map currently occupies 100% of the width and height of the page, but I would like to add a border around it when fully zoomed out. However, when the map is zoom ...

What is the best strategy for managing pagination when dealing with a large number of pages?

Up until now, I have understood that pagination only links different pages together. This works fine when dealing with a limited number of pages or posts to display. However, what if I have 30 or more pages to paginate? Wouldn't it be impractical to c ...

How to link a sandbox file in HTML with an iPhone

Looking to create an HTML application specifically for the iPad, I am interested in developing a background process using Objective-C that will handle the downloading of images and data. The goal is to be able to access these resources from within an HTML ...

"Utilize jQuery to load a file when hovering over a specific

How can I dynamically load an external file using jQuery when a user hovers over a specific div? I attempted to load the file like a CSS file on hover but was unsuccessful. Is it possible to load a CSS file on hover as I've seen in some examples? $(d ...

How can I turn off the animation for a q-select (quasar select input)?

I'm just starting out with Quasar and I'm looking to keep the animation/class change of a q-select (Quasar input select) disabled. Essentially, I want the text to remain static like in this image: https://i.stack.imgur.com/d5O5s.png, instead of c ...

Creating a variety of themes with unique color palettes for Angular Material along with custom-designed components

One of the goals for my app is to have multiple themes, including Angular Material themes, with the ability to define custom colors for specific components and elements that are not part of Angular Material. It's important that when I change a theme, ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: https://i.sstatic.net/fZXMH.png I've attempted setting the z-in ...

How can I extract the page's output using JQuery?

Being a rookie in this area, I am eager to learn how to extract specific content from a page using AJAX in JQuery. Currently, I have been able to fetch the data of a page and display it as text: $.ajax({ type: "POST", url: "myfile.html", su ...

Eliminate the navigation bar background using MaterializeCSS

I want to create a unique extended navigation bar for my website that has the following structure: <nav class="nav-extended"> <div class="nav-wrapper"> <ul id="nav-mobile" class="right hide-on-med-and-down"> <li><a ...

The BBCode seems to be malfunctioning

I created a custom BBCode for my PHP website to display tabbed content on a page. After testing the BBCode on a separate webpage and confirming there are no errors, I am facing an issue where the tabbed content is not showing up on the actual posting page. ...

Having trouble changing the Font Awesome icon on click?

I'm struggling to figure out why I can't change a font awesome icon using a toggle. I've tried various solutions but nothing seems to be working. Any insights on what might be causing this issue? (HTML) <span class="toggle-icon" ...

Obtain the identifier of a div within nested HTML components by utilizing jQuery

How do I retrieve the id of the first div with the class: post, which is "367", using jquery in the given HTML code: <div id="own-posts"> <span class="title">Me</span> <div class="posts_container"> <div class="post"& ...

Strange Behavior of HTML5 Audio Elements

I'm currently in the process of designing a shop website with a nostalgic Windows98 theme. On the product's page, I want to include a preview of audio. Here is the CSS code that I have used for the HTML5 audio element: audio::-webkit-media-contr ...

Utilizing Sage 9 to Customize Woocommerce Templates

I am attempting to update Woocommerce template files using the latest Wordpress Sage. However, I am facing an issue where the new version of Sage with blade extension does not recognize the old Woocommerce template files. In the past, I would simply copy ...

Attempting to arrange six images in a row using dividers

Having trouble aligning six images (2 per row with text in between) to the center of your webpage? Don't worry, we've got you covered! Check out the screenshot attached for reference and let us know how we can assist. Click here for a snapshot of ...

Content is the main driver for CSS Flexbox dynamic aspect-ratio code

When creating a grid layout for a webpage, I opted to use Flexbox. My goal was to incorporate some automatic functionality so that additional grid boxes could be included without the need to manually add classes or styles in the HTML code. One particular f ...