What is the best way to position a div with the 'inline-block' attribute inside another div element?

What is the best way to position the .square elements inside the #about_container?

The squares should be aligned 10px from the top of #about_container, regardless of whether there is text present.

#about_container {
  position: absolute;
  width: 100%;
  height: 200px;
  background-color: rgba(146, 106, 106, 1);
  margin-bottom: 50px;
  overflow-x: scroll;
  white-space: nowrap;
  overflow-y: scroll;
  padding-top: 10px;
  padding-bottom: 10px;
}

.square {
  display: inline-block;
  height: 180px;
  width: 180px;
  background-color: white;
  margin-left: 5px;
  margin-right: 5px;
  border-radius: 5px;
  font-size: 10px;
  text-align: center;
  word-wrap: break-word;
}
<div id="about_container">
  <div class="filler_space"></div>
  <div class="square">
    <p>Some random text here</p>
  </div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="filler_space"></div>
</div>

Answer №1

To align the content at the top of the box, include vertical-align: top in the .square class.

If you prefer the text to wrap when it reaches the end of the width, add white-space: normal to the .square class so it doesn't automatically inherit the white-space: nowrap property from its parent element.

#about_container {
  position: absolute;
  width: 100%;
  height: 200px;
  background-color: rgba(146, 106, 106, 1);
  margin-bottom: 50px;
  overflow-x: scroll;
  white-space: nowrap;
  overflow-y: scroll;
  padding-top: 10px;
  padding-bottom: 10px;
}

.square {
  display: inline-block;
  height: 180px;
  width: 180px;
  background-color: white;
  margin-left: 5px;
  margin-right: 5px;
  border-radius: 5px;
  font-size: 10px;
  text-align: center;
  word-wrap: break-word;
  vertical-align: top;
  white-space: normal;
}
<div id="about_container">
  <div class="filler_space"></div>
  <div class="square">
    <p>Meow mix delivers chicken and liver. Meow mix delivers chicken and liver.</p>
  </div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="filler_space"></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

JavaScript Navigation Bar Error: The value of 'undefined' is not recognized as an object

Having just started learning HTML, CSS, and JavaScript, I encountered an error message that reads: TypeError: 'undefined' is not an object. Despite my best efforts to troubleshoot the issue, I have been unable to resolve it. Is there anyone who c ...

Uncovering classes and selectors in CSS files that match an element with JavaScript

Trying to explain my idea with an example since it's a bit tricky. Imagine we have this HTML code: <ul> <li id="myli" class="myclass">Hello</li> </ul> along with the corresponding CSS: .myclass{ color:red; } li.m ...

Revamping the design of a menu bar in WordPress

I designed a navigation bar and now I'm looking to customize the CSS for it in WordPress. Specifically, I want to be able to reference the nav bars by name from PHP to CSS. It seems like there are just two dots representing variables. Here is the co ...

Dynamic Div that Continuously Adapts to Accommodate PHP/MySQL Data

I have developed a blog main page using PHP, incorporating a sidebar and main area for the posts. However, I am facing an issue where if I add more content to either the sidebar or the main content/posts area, the content overflows the main div without exp ...

The components are not anchored to the window

In the header, I require: The logo to be positioned on the left side of the banner (without space) Both the banner and logo to always be centered on the page regardless of screen size Four images that should always be at the top right corner of the windo ...

Tips for setting up personalized breakpoints for a Bootstrap navbar

I am currently facing an issue with my navbar on tablet view. Despite implementing custom breakpoints to collapse the navbar at 1050, the menu bar is appearing in two rows instead of one. I have tried using the code below but it doesn't seem to be wor ...

Issue with Jquery function not executing on second attempt

/** Creates a table of iTunes data in HTML format **/ var appendValues = function(songArray) { songArray.each(function() { var title = $(this).find("im\\:name").eq(0).text(); var songImage = $(this).find("im\\:image").eq(0).text(); var ...

Place one div element with a float property set to AUTO, followed by another div element that expands to

I am trying to place two divs inside another. The inner divs are floated, with one having a "width: auto;" and the other needing to fill the remaining space. I have searched for examples, but they all involve at least one fixed width element, which is not ...

When clicking on an image, it triggers a unique PHP query, however the results obtained are not accurate

Greetings, I have a search feature on my website. When a user inputs a keyword and clicks the search button, it directs them to jobsearch.php which then displays a list of relevant jobs from the MySQL database. The functionality is working perfectly. Howe ...

Tips for centering links in the navigation bar on Bootstrap 5 when viewing in a small size

I had previously inquired about customizing the Bootstrap 5 navbar in this post: Adjust navbar height to be smaller and I received some helpful answers. Thank you to everyone who responded. However, I now have a follow-up question: I am interested in ch ...

Having trouble getting a DIV to appear on top of another DIV containing an image

I need help with positioning an "InfoBox" above the main header image on my website. Despite trying z-indexes and adjusting position, I can't seem to get it right. If anyone has any suggestions, please share. Thank you! For reference, here is the HTM ...

How to center the navigation list vertically with double lines and maintain the href block?

Looking for a solution to align vertically in a basic navigation list bar, particularly when some items have two lines of text. I want the text to be vertically aligned to the middle, while still maintaining the ability to hover and click within the list ...

What is the best way to conceal a Material UI button with CSS?

For my TODO react app, I am using a Material UI button. My goal is to create a form with an input field and a submit button, but I want the submit button to be invisible so that users think the form submits when they press the "Return" key. import { Tex ...

Tooltips envelope around themselves

Several paragraphs contain tooltips, but some of them break onto a new line instead of staying within the text. <div class="row"> ... </div> Fiddle: https://jsfiddle.net/LhjmLw8k/29/ The issue is that certain words like "Trasimeno" and ...

Learn how to use onclick event in an anchor tag to send post values through Ajax!

Can I send post values via AJAX using an onclick event on an anchor tag? I've tried the following code but I'm not receiving any post values. How can I make it work? ............................................................................. ...

Tips for replacing default arrow icons with 'Previous' and 'Next' buttons in a Material-UI pagination element

I've been struggling to find a solution with my code provided below. Despite multiple attempts, the issue remains unresolved. import React from "react"; import { gridPageCountSelector, gridPageSelector, gridPageSizeSelector, useGridA ...

Combining CSS documents

In order to improve page load speed, I need to consolidate multiple CSS files into one large file. Will the styles function properly if I merge them all together, or are there potential issues when combining multiple CSS files? My software is built in Ja ...

Using Javascript to detect the existence of a class and execute a function or make changes

Hey there, all you amazing folks! I'm currently working on designing my game using Javascript. One of the elements is a box with the ID 'box1'. There's also a hidden button component. I've created a function to alter the class of ...

Erase embedded frame from a Google Sheets document

Is there a way for me to customize the frame and replace it with my own design? Click here to view the Google Sheet embedded frame ...

Utilizing CSS for customizing selectors in a Moodle form

In my Moodle 3.0 form (using formlib.php in Moodle), I have created a selector that I would like to customize with different colors. Here is an example of the selector code: $mform->addElement('header', 'ChartOptions', get_string(&a ...