Adjust the size of horizontal <li> elements dynamically to keep them in a single row

I've been trying to set up a 'gallery' section on one of my pages, with three elements featured. However, I'm running into an issue where resizing the window or viewing on a smaller screen could cause overlaps or force a second row with one of the elements. Is there any solution for this problem? I've attempted using viewport width (setting the li element width to 33vw), but it doesn't seem to be effective. Ideally, I'd like to maintain the resizing property based on 'vw'.

Below is a link to a JSfiddle demonstrating the problem. When the element width is adjusted to 33vw, it spills over to a second row. To illustrate what I mean by having a horizontal row with evenly spaced li elements, I kept it at 30vw. Additionally, is there a way to shrink these elements further, say to 25vw, and align them centrally with equal spacing between them?

https://jsfiddle.net/1ofc4275/3/

HTML:

<div id="gallery">
  <ul>
    <li> Thing1 </li>
    <li> Thing2 </li>
    <li> Thing3 </li>
  </ul>
</div>

CSS:

#gallery ul {
  margin: 0;
  padding: 0;
  width: 100%;
}

#gallery ul li {
  text-align: center;
  border-right: 2px solid black;
  list-style: none;
  display: inline-block;
  width: 30vw;
}

Thank you in advance for your assistance!

Answer №1

There are multiple factors at play here.

Firstly, the use of vw units to align the grid is causing issues. These units are based on the viewport size, not the container size. Additionally, the body element's margin is limiting the available space for laying out the list items. Using percentages instead of viewport widths can help solve this issue.

Secondly, the HTML code has line breaks between the <li> elements, which adds extra white space and affects the width of the elements. Two ways to address this are setting the font size to 0 for the <ul> and then resetting it for the child <li>, or removing the white space between the LI elements entirely.

To prevent box model spacing conflicts caused by adding borders to the LIs, setting the box-sizing CSS property is necessary. By default, the browser uses content-box, but changing it to border-box can resolve these issues.

A solution with CSS code examples is provided:

If you prefer the items to be centered and evenly spaced while being smaller, adjusting the text-align property for the parent <ul> and resetting it for the <li> elements can achieve the desired layout.

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

I have implemented the Angular "Date" filter, but I'm unsure about the meaning of this numerical value

The Angular tutorial showcases the date filter with the following example: {{ 1304375948024 | date }} --> May 2, 2011` How would you notate the expression 1304375948024? ...

Preview of Hoverbox Caption

I'm working on a website using the Hoverbox image gallery (http://host.sonspring.com/hoverbox/) and I'm trying to include captions for each image preview that appears when hovering over the enlarged image. However, I am facing difficulty as I hav ...

Creating a delay with jQuery before hiding elements

A script on my website displays a dropdown menu when the element with ID #icon-hamburger is clicked. The menu has a nice fading animation when it appears and disappears: // nav-mobile $('#icon-hamburger, .icon-close').click(function() { if ($ ...

What is the correct method for adjusting the height properties of an element?

I'm trying to adjust the height of my div using the code below var heightMainDiv = window.innerHeight - 50; var myDiv = $("#main_content")[0]; myDiv.clientHeight = heightMainDiv; myDiv.scrollHeight = heightMainDiv; However, the values of clientHeigh ...

Launch current screen inside a jquery dialog box

When attempting to open an existing aspx page within a jQuery dialog, I noticed that the CSS of the parent screen is being overridden by the dialog box. Is there a way to prevent this without making changes to the existing screen? If there are any altern ...

Typescript error: The property 'set' is not found on type '{}'

Below is the code snippet from my store.tsx file: let store = {}; const globalStore = {}; globalStore.set = (key: string, value: string) => { store = { ...store, [key]: value }; } globalStore.get = (key) => { return store[key]; } export d ...

What causes the issue of JavaScript code not loading when injected into a Bootstrap 4 Modal?

I've created a JavaScript function that triggers the opening of a Bootstrap Modal Dialog: function displayModal(message, headerMessage, sticky, noFooter){ var modal = $('<div />', { class: 'modal fade hide ...

ways to display divs horizontally

Hello, I am trying to display these columns horizontally, but they are currently showing up vertically. How can I change this? Here is the blade file code snippet: <div class="col-lg-3 mb-4"> <!-- Input & Button Groups --> ...

Disabling padding on TwitterTweetEmbed component in React Twitter Embed

Having an issue with Material UI and React Twitter Embed, unable to remove top and bottom margins. Here's the code snippet causing concern: const styles = theme => ({ listItem: { padding: '0px', }, tweetSize: { margin: 0, ...

Stop the React timer count for a moment

I am currently attempting to implement a pause feature in a countdown timer using React. Here is the code snippet: export default function Home() { const timerRef = useRef(180) const [minute, setMinute] = useState(3) const [second, setSecond] = useS ...

Is there a way to merge the .load function and the document.referrer function together?

Is there a way to load a specific div from the previous page that a user originated from using a form? $(document).ready(function() { $("#div_to_be_populated").load('url #div'); }); How can I utilize the document.referrer function in the ur ...

Unspecified information with jQuery

Here is the JavaScript code I am working with: function RHP_AJAX(a, b, c) { jQuery.ajax({ type : 'POST', url : custom.ajax_url, data : { 'action': a , id: b}, success : function(data){ c; ret ...

Ways to transmit information to the frontend JavaScript of one server from a different server

In my express js app, I have set up two routes as follows: router.get('/route', function (req, res) { res.redirect('/newRoute'); }); router.get('/newRoute', function (req, res) { var data = someCalculation(); }); I a ...

The NVD3 tooltip is being obscured by other divs

Attempting to make the NVD3 tooltip appear above all other divs has presented a challenge. With three charts lined up horizontally and tooltips that exceed the boundaries of their divs, adjusting the z-index creates a dilemma. Regardless of which side&apos ...

The download button consistently targets and downloads the initial SVG in my collection. How can I configure it to target each individual SVG for download?

I'm currently working on a QR code app using React. The goal is for users to submit a form with a value, which will then generate an SVG QR code that can be downloaded. However, I'm running into an issue where the same SVG file is being downloade ...

What could be the reason for having two HTML nodes within a one-node HTML document?

My goal is to use DOMdocument to retrieve a list of HTML nodes in order to create a visually appealing CSS3 tree visualization on a web page. However, I've encountered an issue with the code snippet below: // Compressed, testing HTML $text = '&l ...

Deciphering the LocalDate and nested object array in an AJAX response

Seeking assistance, looking for solutions to two problems. Firstly, how can I display LocalDate in an ajax response? And secondly, how do I iterate over a list of Custom objects received in the ajax response? I am passing a List of Custom Objects and Loca ...

Mistakes in my async/await workflow: How am I incorrectly loading and injecting this external script?

Encountering a simple problem: some calls to refresh() cause window.grecaptcha to become undefined. It doesn't happen all the time, probably due to network delays. Debugging this issue is proving to be tricky, especially since I'm still new to th ...

Tips for avoiding the automatic transition to the next slide in SwiperJS

How can I prevent the next button click in swiper based on my custom logic? I am using the swiperjs library within a Vue project and I need to stop users from swiping or clicking the next button to move to the next slide depending on certain conditions de ...

An issue with three.js involving sprites, the material of sprites, and detecting mouse clicks

Hello there, I appreciate you taking the time to read this. I've been diving into various tutorials on how to click objects using the mouse in three.js. However, none of them mentioned whether it's possible with sprites. To try and work around th ...