Creating a horizontal scrolling section in your CSS and HTML to avoid line breaks

I have noticed that this question, or similar ones, has been asked multiple times on various platforms. My query is regarding preventing line breaks in floated items within a scrolling section. Specifically, I have a div where I desire horizontal scrolling.

One common solution I came across involves adding margin or left values to each item individually. Here is an example demonstrating this fix: JSFIDDLE From this Post

This method works effectively when the container's width is known. However, my concern arises with using a variable width (auto) as the data will be dynamically retrieved from a database, leading to varying widths based on results.

Below is an example of what I am referring to:

CSS:

.items{
    width:100%;
    overflow-x:scroll;
    overflow-y:hidden;
    background:rgba(255,255,255,.8); 
    height:350px;
    position:relative;
}
.items .scroll{
    height:100%;
    padding:0px;
    width:auto;
    position:absolute;
    white-space:nowrap;
}
.items .item{
    height:100%;
    margin-left:25px;
    margin-right:25px;
    width:150px;
    float:left;
    position:relative;
    display:inline-block;
}
.items .item a{
    padding:5px;
    background:#0e76bc;
    color:#FFF;
    text-decoration:none;
}
.items .item .img:hover{
    box-shadow:0px 0px 3px rgba(0,0,0,0.5);
    -moz-box-shadow:0px 0px 3px rgba(0,0,0,0.5);
    -webkit-box-shadow:0px 0px 3px rgba(0,0,0,0.5);
}
.items .item .img{
    height:175px;
    width:100%;
    background-size:contain;
    background-position:center center;
    background-repeat:no-repeat;
}

HTML :

<div class="items">
     <div class="scroll">
         <div class="item">
             <p class="center"><div class="img" style="background-image:url(images/sample1.jpg);"></div></p>
             <p class="title">Product Name</p>
             <p class="left"><strong>Current Bid:</strong> &pound;10.00</p>
             <p class="right"><a href="#">View Item</a></p>
         </div>
     <!-- .item  repeats a few times after a DB query, hence exact width is unknown -->
    </div>
</div>

An alternative approach could involve utilizing an init() function in JavaScript, although I am inclined towards finding a CSS-based solution for this issue.

Answer №1

Give this a try in conjunction with your own code: DEMO

CSS:

.items .item{
    height:100%;
    margin-left:20px;
    margin-right:20px;
    width:120px;   
    position:relative;
    display:inline-block;
}

Answer №2

If you remove float:left; from .items .item, the desired functionality appears to be achieved:

        .items .item{
    height:100%;
    margin-left:25px;
    margin-right:25px;
    width:150px;
    position:relative;
    display:inline-block;
}

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

Disabling click events on a span tag in Angular 6: A step-by-step guide

Is there a way to disable the click event for deleting hours with the 'X' symbol based on a condition? Thank you in advance. <table navigatable class="<some_class>"> <tbody> <tr *ngFor="let item of ...

Implementing nested popup windows using Bootstrap

I am facing an issue with my two-page sign-in and sign-up setup in the header of my angular2 project. On the sign-up page, I have a link that should redirect to the sign-in page when clicked, but I am struggling to make it work. Can someone provide guidanc ...

Exploring the elimination of box shadow on elements that overlap

view image hereI'm experiencing an issue with my HTML code where a box shadow appears in the mobile view. Despite my efforts, I cannot seem to remove it. This problem only arises in the mobile view, leading me to believe that it may be related to how ...

Concerns with textbox placement while scrolling with an absolute position style

I have an ASP:textbox on a page with various controls and divs, where I am setting the style to position:absolute on the "onkeyup" event. The height of the textbox increases dynamically based on the characters entered, but the issue is that the textbox alw ...

Customizing tooltips in SharePoint 2013 for enhanced user experience

I am working with a list that consists of various fields. When hovering over a field, the label name and validated field name are currently displayed. I would like to show a new message explaining what should be filled in that particular field. Additiona ...

Storing data in localStorage and serializing it using JSON.stringify and

Currently, I am developing a project that enables users to share memories of places they have visited and also tracks the location and time when the memory was recorded. One hurdle I'm facing involves incorporating localStorage into the application. I ...

I'm struggling to make the jquery parentsUntil function work properly

Would appreciate some help with using the jquery parentsUntil method to hide a button until a radio box is selected. I've been struggling with this for a few days now and can't seem to figure out what I'm doing wrong. Any insights would be g ...

Tips on increasing the click zone size without disturbing surrounding elements

Is there a way to make an inline element more clickable without affecting other elements? Adding padding helps, but it can disrupt the layout. I tried using negative margin to counteract the padding, but it produced some unexpected results: span { p ...

The overflow property is not functioning as anticipated

I've browsed Google and this site, but I couldn't find a solution to my issue. On my webpage, I have a shoutbox and a teamspeak viewer with a specific height for the outer div. The scrollbars don't look good on our new design, so I attempt ...

Is there a way to separate a string similar to the way bbcode does?

$answer = "This is simply a placeholder text example. WOW! Check out this link [link=http://www.yahoo.com] yahoo yahoo[/link]"; Hey there, I am developing a discussion platform, and my goal is to allow users to use specific tags like [link=*link*]*text*[/ ...

Dynamic navigation menu with Bootstrap's responsive multi-level design

I recently developed a multi-level navigation menu using Bootstrap. Everything seems to be working smoothly when the screen size is 1200px or above. However, I encountered an issue with the second level dropdown menu not opening upon clicking on screens sm ...

Attempting to integrate a chatroom name entered by the user using a combination of Ajax and Python

Despite reading numerous textbooks on computer science, I have been struggling with sending ajax form data to Python and checking it in a dictionary before parsing it into a JSON object. The concept of dictionaries with key-value pairs is clear; however, a ...

Tips for avoiding variable overwriting while utilizing it in a for loop to color objects

I am looking to enhance the efficiency of my function in creating HTML objects using native JS script. The current function paints text, but I want it to optimize. When running the function, I expect different iterations of text to be written in HTML thro ...

Changing the Size of React Bootstrap Cards to Fit Your Grid Layout with Custom CSS

I am facing an issue with the varying sizes of react-bootstrap cards within a grid display. Check out the problem I am encountering: https://i.sstatic.net/szHjI.png I need the size of Over Under 0.5 card to be different from the one for Match Winner, as ...

Troubleshooting Problem in Angular 4: ngIf Error

In a component template, I have included both a search icon and a search bar. Below is the HTML snippet: <div class="fixed-action-btn horizontal click-to-toggle search" id="search"> <span class="menu-label"></span> <a class="btn-f ...

How can I use jQuery to either display or hide the "#" value in a URL?

I have a question that I need help with. Let's say I have the following links: <a href="#test1"> Test </a> <a href="#test2"> Test 2 </a> When I click on Test, the URL will change to something like siteurl/#test1. However, whe ...

When content editable is assigned a value, it skews the direction of the writing

I am currently working on an editable text component with the 'editable' attribute. However, I am facing an issue where the text is not aligning in the correct direction as I type - the first character always ends up at the end of the text. Expe ...

Retrieving the Top 10 Values from a JSON Data Set

Take a look at this snippet from my JSON data. [ {"Element":"Water","Value":20}, {"Element":"Fire","Value":30}, {"Element":"Earth","Value":40}, {"Element":"Air","Value":50}, {"Element":"Spirit","Value":60}, {"Element":"Sun","Value":100}, { ...

Guide to setting the ng-selected value within AngularJS

I am currently working on a project where I have a select element in my AngularJS application. I am populating the options using JSON values with ng-repeat, and I want to display the first value from the JSON as selected by default. I have tried two diffe ...

I'm having trouble getting Jquery's insertAfter() function to function properly

After creating a parent div and using jQuery's insertAfter() method to insert additional divs, an unexpected issue arises. The first record goes to the bottom, while subsequent records are inserted above it. Below is the function in question: functi ...