Overflow of Div Content

I need a way to showcase a collection of around 30 links in a horizontal layout without the links wrapping to a new line if they exceed the width of the parent container. A horizontal scroll should appear for users to navigate through the overflowing links. The issue lies in the fact that the parent div containing the links will also overflow if its contents surpass its own width. Below is the code snippet: Html

<div class="container">
    <div  class="pageContent">
        <br />
        <div class="links">
            <a href='#1'>link1 </a>
            <a href='#1'>link2 </a>
            ...
        </div>
        <br />
    </div>
    <div class="side">
        side here
    </div>
</div>

CSS:

.container {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
}
a {
    padding: 5px;
}
.pageContent {
    order: 2;
    flex-grow: 4;
    background: #ecf0f1;
}
.side {
    background: #3498db;
    flex-grow: 1;
    order: 1;
}
.links {
    white-space: nowrap;
    overflow-x: scroll;
    width: 100%;
}

See a live demo here

My main concern is: how can I adjust the width of the .links div to match that of its parent without causing an overflow?

If possible, I prefer a CSS-only solution.

I experimented with setting .links {width:500px} and it worked, but I'm looking for a more dynamic approach to accommodate various browser sizes.

Answer №1

Check out this solution to your problem

.container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  justify-content: space-around;
}
a {
  padding: 5px;
}
.pageContent {
  order: 2;
  flex-grow: 4;
  background: #ecf0f1;

}

.side {
  background: #3498db;
  flex-grow: 1;
  order: 1;

}
.links {
  white-space: nowrap;
  width: 100%;
  height: 40px;
  position: relative;
}
.links-scroll {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  overflow: auto;
}
<div class="container">
  <div class="pageContent">
    <br />
    <div class="links">
      <div class="links-scroll">
        <a href='#1'>link1</a>
        ... (repeat for additional links)
        <a href='#1'>link9</a>
      </div>
    </div>
    <br />
  </div>
  <div class="side">
    content for side
  </div>
</div>

Answer №2

Kindly eliminate the following CSS code block from the .links section

.links {
  /*overflow-x: scroll;
  white-space: nowrap;*/
  width: 100%;
}

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

Compatibility issues arise when trying to use jQuery Mobile in conjunction with jQuery UI

I'm currently developing an HTML5 application that needs to function seamlessly on desktops, tablets, and mobile devices. However, I've encountered a hurdle when it comes to implementing progress bars and dialog boxes. Initially, I was using jQue ...

Center column with header and footer flanking each side

Trying to replicate the layout of the Facebook Android app on my page. The app features a 3 column design, with the central column exclusively displaying the header (no footer included, although I require one for my version). This visual representation is ...

Using d3.js to dynamically change the color of svg elements based on their data values

I was searching for a way to dynamically color SVG rectangles based on values from a dataset. If I were to create rectangles for each data entry, how could I adjust the rectangle's color according to the data value? Here is what I currently have: // ...

What is the best way to include a parameter in the current URL in Django using Paginator?

After setting up a page to display database data, I added a paginator and a search bar to my website. When a user searches for something, the URL changes to include the search query like this: .../search/?q=xyz However, when paginating the search results ...

The content of btn-id element in Angular is showing as undefined

I have a JavaScript file located at sample/scripts/sample.js and there are 8 HTML files in the directory sample/src/templates/. My goal is to select a button on one of the HTML files. When I tried using angular.elemnt(btn-id).html(), I received an 'un ...

The issue persists with the JavaScript window.location script constantly refreshing

I am currently working on a small web application where I want to update 2 parameters in the URL using Javascript. However, every time I use "window.location.search = 'scene.html?name=' + person + '&scene=' + readCookie("scene");", ...

Develop a JavaScript function to declare variables

I am currently attempting to develop a small memory game where the time is multiplied by the number of moves made by the player. Upon completion of all pairs, a JavaScript function is executed: function finish() { stopCount(); var cnt1 = $("#cou ...

Cross-domain scripting with JavaScript

I currently manage 2 subdomains securityservices.example.com workstations.example.com All workstations are associated with the workstations.example.com One of the workstations, known as WS789012 on the domain, is fully qualified as: WS789012.workst ...

Is it possible to replicate an HTML table using JavaScript without including the headers?

Discover a JavaScript snippet in this stack overflow post that allows you to create a button for automatically selecting and copying a table to the clipboard. When pasting the data into an Excel template, my users prefer not to include the header informat ...

Styling components using classes in Material-UI

I recently started using material-ui and noticed that it applies inline styles to each component. After running a test with multiple instances of the same component, I realized that there was no CSS-based styling - only repeated inline styles were generate ...

Selecting parent class using JQuery

This is some HTML I have: <label class="rlabel">First Name</label> <input class="rinput" type="text" placeholder="Fisrt Name" required /> <label class="rlabel">Last Name</label> <input class="rinput" type="tex ...

Is it possible to include jQuery's selectors contain and closest within my CSS selector?

I created a script to hide specific table rows as follows: $('.ms-formtable nobr:contains("Question")').closest('tr').hide(); However, I'm unsure if there is a way to achieve the same result using only CSS. Can anyone provide adv ...

What is the best way to integrate PHP code within CSS?

I'm working on a script that requires me to add PHP code within a CSS stylesheet. However, when I attempt to do so, nothing seems to happen! Is it even possible to include PHP code in a CSS file? ...

A helpful tip on how to designate a specific color to the helperText in Material UI in order to emphasize

Is there a way to customize the color of helperText in material-UI for highlighting errors in a TextField? I've been struggling to change the color of helperText with no success. I attempted using MuiFormHelperText-root-406 to add CSS styles, but it ...

Guide on inserting a dropdown menu following a specific count of <li> elements using Javascript or Jquery

I am currently working on an HTML project <div class="ktmsg"> <ul> <li class="a1"> <a title="Link Tools" href="#"> … </a> </li> <li class="a2"> <a title="Link Tools" href="#"> ...

What is the best way to style a tiled design using CSS?

What is the best way to code the page without using javascript? Both of the HTML elements (div1, div2) need to have a fixed size. ...

Is there a way to make a single background image the frame for each individual post on a tumblr theme?

I've set my sights on a more challenging goal for my custom Tumblr themes - I want to have a background image for each post, similar to how other themes display posts within boxes. Is there a way to make these boxes into images? I've tried tinke ...

The `<Outlet/>` from react-router is being rendered in a location outside of its intended wrapper div

Utilizing MUI for crafting a straightforward dashboard featuring an <AppBar>, a side <Drawer>, my component appears as follows: <> <AppBar> // code omitted <AppBar/> <Drawer> // code omitted <Drawer/> / ...

Position the Bootstrap Modal at the start of the designated DIV

When using a Bootstrap Modal to display larger versions of thumbnails in a photo gallery, there can be some challenges. The default behavior of Bootstrap is to position the modal at the top of the viewport, which may work fine in most cases. However, if th ...

Uploading videos with Node.js

What is the best package for uploading videos in Node.js and saving them to a database? ...