Arrange li elements in a row with the ability to scroll horizontally

I'm attempting to create a paging effect using CSS but I've run into a bit of confusion. I want to arrange all the li elements next to each other within a specified parent width so that they scroll horizontally.


<ul class="flow" style="width:200px">  
    <li class="selected">1</li>
    <li><a href="#" title="Pagina 2">2</a></li>
    <li><a href="#" title="Pagina 3">3</a></li>
    
    <li><a href="#" title="Pagina 15">15</a></li>
</ul>

Check out my attempt on this Fiddle link. The issue I'm facing is that the li elements are breaking into new lines. How can I make them scroll horizontally in a single line?

Answer №1

white-space

nowrap - The nowrap value for white-space property collapses whitespace as for normal, but suppresses line breaks (text wrapping) within text.

* {
  margin: 0;
  padding: 0;
}
ul {
  list-style: none;
  white-space: nowrap;
  overflow: auto;
  /*width:300px;*/ /* <  uncomment to check with fix width*/
}
ul li {
  display: inline-block;
  border: 1px solid grey;
  padding: 15px;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li>10</li>
  <li>11</li>
  <li>12</li>
  <li>13</li>
  <li>14</li>
  <li>15</li>
  <li>16</li>
  <li>17</li>
  <li>18</li>
  <li>19</li>
  <li>20</li>
</ul>

Answer №2

Enclose your second list in a div element and apply the following styling to that div Check out this code snippet on http://jsfiddle.net/bn4f9j58/

   div{ 
        display: inline-block;
        width: 250px;
        overflow-y: hidden;
      }

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

Altering the appearance of a component that is currently selected and in use

Currently, I have incorporated a component with its selector within another component as shown below: <div class="col-xl-4" style="margin-bottom: 30px;"> <app-patient-info-accordion *ngIf="patient" [cardTitle]=&qu ...

Struggling to adjust the width of an <img> tag to fit the screen within a UITextView/HTML using NSAttributedString

When I try to load HTML with images into a UITextView, the images do not want to fit the width of the screen, they appear too large. Setting the width attribute for the IMG tag and using CSS does not seem to help. Here is the code snippet: var content = ...

How to properly format credit card input using JavaScript or jQuery

I need to implement a feature where users input their credit card information, and once they complete the form, I want to display the credit card number in this format: xxxxxxxxxxxx1111 or xxxx-xxxx-xxxx-1111, without altering the actual input value. Is ...

Conflict between multiple jQuery files

Currently, I am in the process of creating a test website for an upcoming project. However, I have encountered some issues with jQuery. This is a static HTML5 website that includes a social widget. The problem arises when I remove a particular jQuery lin ...

Strange glitch in the rendering of rectangles on HTML5 canvas?

While experimenting with pre-rendering sprites using HTML5 canvas, I encountered some strange behavior that appears to be a rendering bug. The issue can be seen in the following minimal example: var CT = document.getElementById("myCanvas").getContext("2 ...

Minimize white spaces when validating input fields in a form using Javascript

I'm currently facing a challenge with JavaScript, specifically regarding achieving five tasks without using jQuery. Despite trying various RegExp codes, none have successfully worked for me so far. Let's begin with the first task (number 1): El ...

Please select the links located beneath the see-through triangular or polygonal shapes

Review the code snippet provided at the following link: http://jsfiddle.net/q8Ycz <div style="background-color: red; width: 200px;" onclick="alert('behind')"> <div><a href="test">test test test test test test test test test ...

What is the best way to ensure that two divs in the same row float in opposite directions?

Check out this JSFiddle to see what I have done so far: JSFiddle Link html <div class="results"> <h2>Some data</h2> <ul style="margin:0;padding:0;"> <li class="resultsListItem" style="list-style-type: none;"> ...

What is the best storage location for div elements used in jQuery Mobile pop-up functionality?

I'm struggling with what seems like should be an easy task. Even after reading through the documentation and grasping the JQM pop-up concept, I'm still stuck. It involves a simple link with: href="popupBasic" data-rel="popup" and a div with: ...

Prevent accordion expansion triggered by the More button click

When the More button is clicked, it should NOT expand and collapse. https://i.sstatic.net/toV1b.gif If the accordion initial state is open, it must stay open even if button is clicked, if initial state is collapsed then after the more button the accordio ...

Tips for incorporating a personalized CSS stylesheet while utilizing Bootstrap

When trying to use my custom CSS alongside Bootstrap, I'm running into some issues. The !important tag doesn't seem to have any effect (I've also heard that using !important is not recommended). Additionally, using IDs instead of classes isn ...

What is the best way to adjust the size of the initial text field in a Bootstrap input group

I'm attempting to incorporate a country code and phone number using Bootstrap v5 input group. Unfortunately, the code I'm using doesn't seem to be functioning correctly. I would like the format to be adjusted by changing the width. Is that ...

How to customize a disabled paper-input element in Polymer 1.0?

Help with Polymer 1.0: I've encountered an issue where setting a paper-input element to 'disabled' results in very light gray text and underline, making it hard to read. I've been trying to use CSS to change the text color but haven&ap ...

Combining the jquery-UI slider functionality with the canvas rotate() method

Is there a way to rotate an image using html2canvas plugin and jQuery UI slider? I am new to programming and need some guidance on how to achieve this feature. Here is my current progress: http://jsfiddle.net/davadi/3d3wbpt7/3/ ` $("#slider").slider({ ...

Tips for creating a full screen div layout with fixed top and bottom navigation bars

Can someone help me achieve the following layout style? +-----------------------------------------------------------+ | Top Sticky Nav | +--------------------------------------------------------+--+ | ...

Tips for ensuring that images fully occupy my carousel slides

I am struggling to make my images fit properly within the slides. Here is the code snippet I have tried along with some screenshots: <div className="relative h-24 w-20 sm:h-44 sm:w-100 md:h-44 md:w-60 flex-shrink-0"> <Carousel showThu ...

When using Angular with mdbootstrap, the mdb-tabs directive will move to the end if the ngIf condition is true

Currently facing a challenge with a significant amount of code here. It is referenced as follows: "ng-uikit-pro-standard": "file:ng-uikit-pro-standard-8.3.0.tgz", I am attempting to display a tab between 1 and 3 if a certain condition ...

What steps are needed to enable the keyboard on a Otree application for mobile devices?

I previously utilized an Implicit Association Task (IAT) in an experiment conducted on computers. However, I now need to adapt this IAT for use on tablets or mobile devices. Here is how the IAT appears on a cellular device: https://i.stack.imgur.com/kNwo ...

The functionality of `onclick="location.href='link.html'" seems to be malfunctioning

Trying to find a way to include onclick="location.href='link.html'" in my code. I want to make it so that when a user clicks on an image, they are directed to a specific URL. I'm not exactly sure how to implement this in the code below. Her ...

The basic jQuery script seems to be malfunctioning

I am trying to attach an on click event to an li element using jQuery. I have written a simple jQuery code within the document ready function, but for some reason it is not functioning as expected. I have checked in both Chrome and Firefox, and there are n ...