Make sure that your inline-block elements are aligned with both the left and right edges of their

Explaining what I'm attempting can be a bit tricky, but I'll do my best.

  • I have X number of categories that I need to organize.
  • Each category needs to be enclosed in a box with a grey border.
  • I want the category boxes to be displayed "inline-block" from left to right.
  • The number of boxes displayed across the page will vary based on the user's screen size.
  • The boxes on the left should align with the left side of the container, and those on the right should align with the right side.

Visual representation:

.container {
  width: 100%;
}

.category {
  width: 100px;
  display: inline-block;
  border: 1px solid grey;
}
<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
  <div class="category">Category 3</div>
  <div class="category">Category 4</div>
  <div class="category">Category 5</div>
  <div class="category">Category 6</div>
</div>

If I were to change:

.container {
  column-count:4; 
}

This gives me the desired effect, but the categories are not displayed in order across the screen. They show vertically instead of horizontally.

Answer №1

Flexbox comes to the rescue once again. In this particular scenario, the elements have fixed width and are evenly distributed with space between them:

.container {
  margin: 1em 0;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
}

.category {
  border: 1px solid gray;
  width: 100px;
}
<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
  <div class="category">Category 3</div>
  <div class="category">Category 4</div>
  <div class="category">Category 5</div>
  <div class="category">Category 6</div>
</div>

<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
  <div class="category">Category 3</div>
</div>

<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
</div>

In another example, the elements have fluid width and the space between them is fixed:

.container {
  margin: 1em 0;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.category {
  margin: 0 1em;
  border: 1px solid gray;
  width: calc(25% - 2em - 2px);
}
<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
  <div class="category">Category 3</div>
  <div class="category">Category 4</div>
  <div class="category">Category 5</div>
  <div class="category">Category 6</div>
</div>

<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
  <div class="category">Category 3</div>
</div>

<div class="container">
  <div class="category">Category 1</div>
  <div class="category">Category 2</div>
</div>

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

The issue with MUI TextField: the outline is overlapping the label

Recently, I encountered an issue with a MUI TextField in my project. In its default state, everything appeared fine: https://i.stack.imgur.com/WFzOr.png However, when I increased the font size as per the theme provided below, the label started to overlap ...

Error in Layout of Navigation Panel and Tabbed Pages

Working on a school project, I encountered a challenge. I found two useful solutions from the W3 website - a sticky navigation bar and the ability to include tabs on a single page for a cleaner presentation of information. However, when trying to implement ...

Using embedded js files in jQuery's .load() function does not function properly

In the index.html file, I have the code snippet below: <div id="page-insert"></div> function openPage(pageid) { $('#page-insert').load(pageid); } openPage('testpage.html'); Additionally, there are script files embedde ...

Utilize the input field value in an HTML dialog box to assign it to a variable in the code.gs file with the help

I'm trying to create a dialog box where the user can select a year, and then have the server process the selected value using the function doSomethingWithCompetitionYear(theYear). I've researched several discussions, but I'm having trouble ...

Tips for creating a captivating full-screen parallax section on your website

Is there a way to make the first section of a parallax site full screen? I've been scouring the internet for a solution to this problem, but I'm not having any luck. Almost every parallax site I come across has their first section full screen, a ...

The dimensions of the background for a span element

I have a span element with a background-color applied to it. Is there a way to adjust the size of this specific background? I attempted using background-size: auto 2px;, but it did not produce the desired effect. I am looking to make the background color ...

Tips on concealing and deactivating the fullscreen option in flowplayer

Could someone please assist me in resolving this issue? I've been attempting to hide the fullscreen button from the flowplayer, but so far, I haven't found a solution. Below is my JavaScript code: <script> $f("player", "flowplayer/flowpla ...

Steps to handle the change event of a p:inputText element

In my current setup, I am utilizing p:inputText and have the need to trigger a javascript function that will update the searchField in the backend bean. <p:inputText required="true" placeholder="#{cc.attrs.searchTip}" value="#{cc.attrs.queryProperty}" ...

Is it possible to reset a form without using JavaScript? (The input type=reset function is not functioning correctly

So, basically I need a quick way to clear all the fields in my form. I've experimented with a couple of options: <input type="reset" value="Clear all fields"> And <button type="reset">Clear all fields</button> Unfortunately, none ...

In the present technological landscape, is it still considered beneficial to place Javascript at the bottom of web pages?

As a beginner in web programming, I've recently delved into Javascript. A hot debate caught my attention - where should javascript be placed, at the top or at the bottom of a webpage? Supporters of placing it at the top argue that a slow loading time ...

Is it possible to incorporate two ng-repeat directives within a single td element in a table?

The results so far Expected outcome Please advise me on how to incorporate two ng-repeats within one td. When I use a span tag afterwards, the expected result is not achieved. I have used one ng-repeat in the td and the other in a span tag, which is why t ...

Ways to detect the use of vue.js on a webpage without relying on vue-devtools

One way to determine if the page is utilizing Angular or AngularJS is by inspecting window.ng and window.angular. Is there a similar method to identify whether Vue is being used on the page directly from the console, without relying on vue-devtools? ...

Ways to reach the standard look

Check out this follow-up to a previously posted issue. Click here to view it I am currently facing an issue with the code below, where I am unable to make the second button inherit the default border and padding styles. Any assistance would be greatly app ...

The concept of CSS inheritance within div elements

I've been researching extensively about the concept of CSS inheritance, but I have come across a puzzling question that remains unanswered. Consider the code snippet below: <!DOCTYPE HTML> <html> <head> <style type="text/css"> ...

Strategies for avoiding text wrapping in Bootstrap labels on Firefox

Within my panel and panel-body, there are multiple span elements that display dynamically. These spans behave perfectly in Safari and Chrome, wrapping to the next line when needed. However, in Firefox, they overflow strangely. Here are the comparisons: Ch ...

Most effective approach to managing state in a React UI Component

I recently delved into the world of React. I've been using it to build a design system library for my work, which consists of standalone UI components tailored for use in React applications. I've developed a specific approach to managing the sta ...

Start up JQuery User Interface

I've been having some trouble with Jquery UI and it's been quite frustrating. To try to understand better, I have created a simple test case which you can view here: http://jsfiddle.net/vmZap/ Essentially, I have included JQUERY, JQUERY-UI, and ...

My custom CSS being overridden by Bootstrap styles

I'm currently working with Twitter Bootstrap's CSS, but it seems to be overriding some of my custom classes. Initially, I placed it before my own CSS in the HTML header (I am using jade and stylus template engines): doctype html html head ...

Validation of default values in contact forms

Obtained a free contact form online and hoping it works flawlessly, but struggling with validation for fields like "Full Name" in JS and PHP. Here is the HTML code: <input type="text" name="contactname" id="contactname" class="required" role="inpu ...

What is the best way to deactivate a hyperlink of an <a> tag specifically for a particular child within an element?

Imagine you have a clickable container that leads to another page when clicked. However, there are elements within this container that you want to disable so that clicking on them does not activate the link. How can this be achieved? For instance, ...