Elements that are positioned in a single line in a text

I am experimenting with a layout where each item is displayed in a single line, with the width of the items determined by their content. The desired output looks like this:

Intended result: (This feature is only supported in modern browsers)

.item {
  padding: 10px;
  background-color: skyblue;
}
.wrap {
  width: max-content;  /* supported only in modern browsers */
  margin: 5px auto;
}
<div class="item wrap">hello</div><!-- NO SPACE --><div class="item wrap">hey</div>

I aim to achieve the same layout for IE9+ browsers without making changes to the HTML. Let's consider the parent element as the body tag.

If I apply white-space: pre-line to the parent element, it almost gives me the desired outcome:

Potential workaround attempted on my end:

.item {
  padding: 10px;
  background-color: skyblue;
}
.wrap {
  display: inline-block;
}
body {
  white-space: pre-line;
  text-align: center;
}
<div class="item wrap">hello</div>
<div class="item wrap">hey</div>

However, there is no space between the .item elements in the HTML code, so this approach doesn't fully work.

Unsuccessful workaround due to lack of space between HTML tags:

.item {
  padding: 10px;
  background-color: skyblue;
}
.wrap {
  display: inline-block;
}
body {
  white-space: pre-line;
  text-align: center;
}
<div class="item wrap">hello</div><!-- NO SPACE --><div class="item wrap">hey</div>

Answer №1

To make the layout work properly, simply include display: table; in the .wrap CSS style.

body {
  text-align: center;
}
.item {
  padding: 10px;
  background-color: skyblue;
}
.wrap {
  display: table;
  margin: 5px auto;
}
<div class="item wrap">hello</div><!-- NO SPACE --><div class="item wrap">hey</div>

Answer №2

UPDATE :

Here is a solution utilizing the display:table property.

Please inform me if this solves the issue ;)

.item {
  padding: 10px;
  background-color: skyblue;
    margin: 5px auto;
   display:table

}
<div class="item wrap">hello</div><!-- NO SPACE --><div class="item wrap">hey</div>

Answer №3

If you want to add a margin to the right of every item in a wrap but not the last one, you can use this code snippet as a reference.

.item {
  padding: 10px;
  background-color: skyblue;
}
.wrap {
  display: inline-block;
}
.wrap:not(:last-child){
  margin-right:10px;
}
body {
  white-space: pre-line;
  text-align: center;
}
<div class="item wrap">hello</div><!-- NO SPACE --><div class="item wrap">hey</div>

Answer №4

To ensure that the .item divs are displayed in the same line, make sure to set them as inline-blocks. This will also give you the flexibility to add padding and margins to them.

It is recommended to enclose your .items within a .wrap container for better organization.

.item {
  padding: 10px;
  background-color: skyblue;
  display: inline-block;
  margin: 5px;
}
.wrap {
  padding: 10px;
  text-align: center;
}
<div class="wrap">
    <div class="item">hello</div>
    <div class="item">hey</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 PDF file cannot be displayed due to the dynamic loading of the path and filename

Currently, I am working on an AngularJS and Java application. The following HTML code is utilized to open a PDF file in the browser. However, there seems to be an issue where the PDF does not open when dynamically passing the value for the data attribute. ...

Alignment problem detected in Firefox and Safari, but works perfectly in Chrome

Currently dealing with a CSS problem on my website that is only appearing in Safari and Firefox, while Chrome displays it correctly. The images are not positioning correctly and are stacking instead of being placed in each box as intended. I've expe ...

In my current project, I am developing a memory game using Ionic and Angular. The game involves displaying an ever-expanding list of numbers for the user to memorize, followed by the user typing

I am trying to make each number in an array appear and then disappear one by one. However, I have encountered an issue where only the last number shows up when there are multiple numbers in the array. I believe the problem lies within the for loop, but I h ...

Tips for adding a button to the SB Admin 2 Card header without altering its height

I am currently utilizing the SB Admin 2 template for my project and encountering difficulty when trying to add a button in the card header without altering the default height. The goal is to maintain a consistent look across both cards, as illustrated in t ...

React Intersection Observer not functioning properly

Hey there! I'm trying to create an animation where the title slides down and the left element slides to the right when scrolling, using the intersection observer. Everything seems to be fine in my code, but for some reason it's not working. Any t ...

Adjust the color of the glyphicon icon within a date and time picker dropdown component

I decided to implement the bootstrap datetimepicker using this gem and utilized the following HTML code: <div id="custom-dates" style=" clear:both;"> <div class="container"> <div class="row"> <div class='col-md-3 col-xs-3' ...

Clicking on a jQuery element will reveal a list of corresponding elements

I've retrieved a list of elements from the database and displayed them in a table with a button: <a href="#" class="hiden"></a> To show and hide advanced information contained within: <div class="object></div> Here is my jQ ...

Enhance your PrimeNG p-calendar by customizing the background-color of the dates

Hello, I am currently attempting to customize the appearance of the p-calendar, but I am struggling with changing the color of the displayed dates. Can anyone provide assistance? Thank you in advance. Below is my template: <div class="p-field p-co ...

Material UI Grid List rows are displaying with an unusually large gap between them, creating a

Currently, I am utilizing Material UI in combination with Reactjs. One particular issue that I am encountering involves the Grid List Component. In my attempt to establish a grid of 1000x1000px, I have specified the height and width within the custom gridL ...

React - Updating the document title upon user navigation away from the current page

I have encountered a challenge and I am seeking guidance on how to resolve it. Throughout our website, all pages have a default title based on our index.html file: <html lang="en"> <head> <title>My Page Title</title> ...

The HTML element in the side menu is not adjusting its size to fit the parent content

Update: What a day! Forgot to save my work... Here is the functioning example. Thank you for any assistance. I have set up a demo here of the issue I'm facing. I am utilizing this menu as the foundation for a page I am developing. The menu is built ...

Is there a way to pass the ng-repeat value or ID to my dynamically appended element? If so, how can I achieve

I am working on a project where I have a table with 5 data entries retrieved from a select query. My goal is to extract the ng-repeat value and ID, then transfer it to another element. As a beginner in Angularjs and HTML, I would greatly appreciate any ass ...

The Link Breaks the Overlay Hover Effect

Currently, the code functions as intended when you hover over or touch the thumbnail, an overlay will appear. The issue lies in the fact that to navigate to a specific URL, you have to click directly on the text. The overlay itself is not clickable and ca ...

Displaying the URL of a link on the screen using jQuery

I am looking for a solution to add a link address after the link itself in a table containing reference links for our staff. I have used CSS to achieve this in the past, but the address was not copyable by users. Instead, I am interested in using jQuery&ap ...

Display the code exactly as it appears

Important Note: I have to insert the following code three times in the HTML body of a static webpage. Instead of repeating it multiple times and cluttering the code, I prefer to have a single line (repeated three times) that calls the function to output th ...

Error: The OOP class value for translateX in the Web Animation API is returning as undefined

I'm currently working on a basic animation project using JavaScript. I have utilized the Animation class from the Web Animation API. My goal is to create multiple instances of this class in order to animate different elements with varying values and r ...

Xpath is effective in Chrome Dev Tools, but does not seem to be functioning properly in RS

I am currently engaged in a project that involves extracting an HTML table containing specific text ("Current Prison History:") from various URLs that vary based on individual ID. I have attempted to utilize CSS selectors for this task, but encountered a c ...

Styling the pseudo element ::part() on an ion-modal can be customized based on certain conditions

Looking for a solution regarding an ion-modal with specific CSS settings? I previously had the following CSS: ion-modal::part(content) { width: 300px; height: 480px; } Now, I need to adjust the height based on conditions: if A, the height should be lo ...

The full width of the div exceeds the parent's width, resulting in the box size being ineffective

I'm experiencing an issue where my cover is wider than my element and content. It seems to be covering the gunter, but I've tried using background-clip: content-box; and box-sizing: border-box;, which haven't resolved the problem. I also wan ...

Trouble with stroke width in Svg bezier path while using a mask

I'm having trouble hiding part of a path using a mask. When I apply the mask, the stroke width doesn't seem to work anymore. Any suggestions on why this is happening and how to fix it? svg { position: absolute; width: 100%; height: 100%; ...