Arranging adjacent div blocks to maintain alignment in a stylish way

I am facing an issue with divs that are placed side by side inside a table.

These divs have been styled to appear as colored blocks.

Everything seems fine when the text within each div fits on two lines, however, if it overflows onto another line, the entire div shifts up one line. How can I prevent this from happening while maintaining the same width for the div and allowing the text to flow into the line below?

I have used line breaks in the text to ensure the gap remains when the text fits on one line, but I believe I will need to remove these eventually.

#main {
  margin-top:15vw;
  margin-left:5vw;
  margin-right:5vw;
  padding-right:3vw;
  padding-left:3vw;
  font-family:'Hind', sans-serif;
  color:black;
  text-align:center;
  position:relative;
  font-size:2vw;
  z-index: 2;
  background-color:white;
}

#green{
  background-color: #f0f0df;
  display: inline-block;
  margin:1vw;
  height: 8vw;
  width: 12vw;
  line-height: 3vw;
  border-radius: 0.5vw;
}
<div id=main>

<div id=green>Title 1<br><b>Answer 1</b></div><div id=green>Title 2 (long title)<br><b>Answer 2</b></div><div id=green>Title 3<br><b>Answer 3</b></div><p id="rep">Additional text goes here. This text needs to be here but is not important and so does not need a box of its own. It is styled with italics and smaller font, and will probably be ignored.</p>
  
</div>

Answer №1

Apply the style vertical-align: top; to element with id #green

#main {
  margin-top:15vw;
  margin-left:5vw;
  margin-right:5vw;
  padding-right:3vw;
  padding-left:3vw;
  font-family:'Hind', sans-serif;
  color:black;
  text-align:center;
  position:relative;
  font-size:2vw;
  z-index: 2;
  background-color:white;
}

#green{
  background-color: #f0f0df;
  display: inline-block;
  margin:1vw;
  height: 8vw;
  width: 12vw;
  line-height: 3vw;
  border-radius: 0.5vw;
  vertical-align: top;
}
<div id=main>

<div id=green>Title 1<br><b>Answer 1</b></div><div id=green>Title 2 (long title)<br><b>Answer 2</b></div><div id=green>Title 3<br><b>Answer 3</b></div><p id="rep">Additional text goes here. This text needs to be here but is not important and so does not need a box of its own. It is styled with italics and smaller font, and will probably be ignored.</p>
  
</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

Angular 5 and Bootstrap card with enhanced double-click functionality

I want to design a Bootstrap card that can trigger two of my custom methods. <div (click)="TEST1()" class="card" style="width: 18rem;"> <div class="card-body"> <h5 class="card-title">Card title</h5> <button (click)="(T ...

Looking to streamline your design? Find out how to translate multiple elements to the same position seamlessly

Greetings! This is my debut post on this platform. I am currently engrossed in a project that has presented me with a challenge, and I'm reaching out to seek your insights on a potential solution. Here's the scenario: I have a parent div containi ...

Implementing login functionality in an Angular application with the help of Kinvey.Social and utilizing the Promise API

I have been utilizing the Kinvey HTML5 library in an attempt to create a client-side application that integrates Google identities for Login. While the Oauth transaction appears to be functioning properly, I am encountering an issue with toggling the visib ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

The attempt to remove the ajax-loaded page while preserving the original div is proving to be

I have a function that is triggered when a link is clicked. This function uses ajax to load a new page over a div. <pre><code> function displayContent(id) { loadPage_signup = "register.php"; loadPage_info = "userinfo.php"; $.aj ...

What is the best way to create a navbar with a grid that spans the full height and includes two rows

My approach to structuring the layout using CSS grid is as follows: nav content nav footer This means that the 'nav' will occupy the entire height of the page with a specified width, and the remaining space will be divided between the 'cont ...

Develop a time-sensitive store system using HTML and JavaScript that toggles between open and closed status based on set

I am looking to develop a time-based Open/Closed store using HTML and JavaScript. The concept is that on Fridays, the element with id="friday" should be displayed, otherwise, show the element with id="week". Additionally, if the time i ...

Comprehending the Syntax of the ExtJS Framework

I have recently inherited a project utilizing Sencha's ExtJS framework without any handover or documentation. As I navigate through the code, I find myself trying to decipher certain syntax and exploring resources tailored for newcomers to this specif ...

Steps for generating an HTML button when an Angular function evaluates to true

I am working on a table in HTML that uses ng-repeat to display data. Here is the code snippet for this table: <tbody> <tr dir-paginate="x in serverData | filter:filterData | filter:filterData | itemsPerPage: itemPerPageValue | order ...

Adjust the header width and column alignment when freezing the header in a gridview interface

Looking to implement a gridview with a fixed header? I've tried various solutions including this link and this one. While I've managed to get it working, the main issue lies in the alignment of header width and column width. The scroll and freez ...

Tab Focus discrepancy observed in Mozilla browser

Tab Focus issue with elements on Mozilla Firefox: <div class="editor-field"> <div> <%: Html.TextBox(model => model.AddressLine1, new { maxLength = 30, style = "width:300px", tabinde ...

Remove identical options from the dropdown menu

After hard-coding and adding items to the dropdown list for team size, such as 1, 2, 3, I am encountering an issue when loading it for editing or updating. Duplicate values are appearing in the list: 1 1 2 3 4... How can I remove these duplicate value ...

What is the best way to target only the immediate children of a div when using tailwindcss?

Here is the HTML code snippet I am working with: <div class="section"> <div class="header">header</div> <div class="content"> <div>sub contents 1</div> <di ...

Checking for Internet Connectivity in Mobile HTML5

Is it possible to check for internet connectivity on a mobile device? Can websockets be utilized to ping a server and determine if the connection is available? I am feeling frustrated as I believed there was a ping function in websockets for client-side u ...

Tips for ensuring consistent dimensions on a bootstrap card

Currently, I am working on a project to gain a better understanding of API calls within Angular. While the functionality is running smoothly, my struggle lies in designing the HTML layout. To enhance the aesthetics, I have incorporated Bootstrap 5 into the ...

Initiating the ngOnInit lifecycle hook in child components within Angular

I am facing an issue with controlling the behavior of child components in my Angular application. The problem arises when switching between different labels, causing the ngOnInit lifecycle hook of the children components not to trigger. The main component ...

What is the process of retrieving user input from an HTML form and locating specific data within it?

Here is the structure of my form: <div id="employeeinfo" style="padding:40px" class="employee-body"> <form id="employeeform" title="" method="post"> <label class="title">First Name</label> < ...

Having trouble finding the element for an image of a body part using xpath

When using driver.findElement(By.xpath(".//*[@id='bodyPartContainer']/div[1]")).click(); to select a part of the human body image, I'm encountering the following exception in webdriver: org.openqa.selenium.NoSuchElementException: no such ...

Using jQuery to load form elements in web development

Can jQuery's .load() function load <form> elements? I noticed that it appears to be removing them. Here is an example of my code: $('<div></div>').load($link.attr('href') + ' #divDlgContent', function( ...

I am having trouble aligning the unordered list in the center

My struggle lies in centering the ul element which serves as a menu. Despite trying various CSS properties such as margin: 0 auto;, text-align: center;, and adjusting the margin value, I am unable to achieve the desired result. This is the CSS code I have ...