Tips for ensuring two blocks, each containing two pairs, remain next to each other within a div without being separated

As a newcomer to HTML/CSS and Liquid, I am exploring the creation and styling of custom blocks on a Product Page. My goal is to display these custom blocks in pairs within a single div.

The current code structure I have implemented functions well, with one exception - on mobile screens, the layout breaks and the 4th block moves onto a new line within the wrapper. I am seeking advice on how I can keep the pairs of two blocks together, so that when the layout shifts, a pair will move down to a new line as a unit instead of separating? Any assistance on resolving this issue would be highly appreciated. Thank you.

<div><p style="
  display: inline-block;
  background: #9b845a;
  padding: 6px 6px 6px 10px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 20px 0px 0px 20px;
  margin: 0px 0px 5px 0px;
  color: #ffffff;
  font-weight: bolder;"
<p>Condition
<p style="
  display: inline-block;
  background: #ffffff;
  padding: 6px 10px 6px 6px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 0px 20px 20px 0px;
  margin: 0px 0px 5px 0px;
  color: #232323;
  text-align: left;
  font-weight: bolder;"
<p>Condition Text pulled from custom Product field</p>
<p style="
  display: inline-block;
  background: #9b845a;
  padding: 6px 6px 6px 10px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 20px 0px 0px 20px;
  margin: 0px 0px 5px 10px;
  color: #ffffff;
  font-weight: bolder;"
<p>Size
<p style="
  display: inline-block;
  background: #ffffff;
  padding: 6px 10px 6px 6px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 0px 20px 20px 0px;
  margin: 0px 0px 5px 0px;
  color: #232323;
  text-align: left;
  font-weight: bolder;"
<p>Size Text pulled from custom Product field</p></div>

Issue: the 4th block is forced down to a new line on Mobile Screens. I'd like to force blocks 1 & 2, and 3 & 4, to stay together so the label and the text do not split?

Answer №1

You can enhance your layout using the power of CSS Flexbox in combination with the flex-wrap and align-content properties. By doing so, you can effectively group pairs of elements and dictate their behavior as the screen size varies.

<div style="display: flex; flex-wrap: wrap;">
  <div style="display: flex; flex-direction: row; align-content: flex-start;">
    <p style="
      display: inline-block;
      background: #9b845a;
      padding: 6px 6px 6px 10px;
      border-style: solid;
      border-width: 2px;
      border-color: #9b845a;
      border-radius: 20px 0px 0px 20px;
      margin: 0px 0px 5px 0px;
      color: #ffffff;
      font-weight: bolder;">Condition</p>
    <span style="
      display: inline-block;
      background: #ffffff;
      padding: 6px 10px 6px 6px;
      border-style: solid;
      border-width: 2px;
      border-color: #9b845a;
      border-radius: 0px 20px 20px 0px;
      margin: 0px 0px 5px -5px;
      color: #232323;
      text-align: left;
      font-weight: bolder;">Condition Text pulled from custom Product field</span>
  </div>
  <div style="display: flex; flex-direction: row; align-content: flex-start;">
    <p style="
      display: inline-block;
      background: #9b845a;
      padding: 6px 6px 6px 10px;
      border-style: solid;
      border-width: 2px;
      border-color: #9b845a;
      border-radius: 20px 0px 0px 20px;
      margin: 0px 0px 5px 10px;
      color: #ffffff;
      font-weight: bolder;">Size</p>
    <span style="
      display: inline-block;
      background: #ffffff;
      padding: 6px 10px 6px 6px;
      border-style: solid;
      border-width: 2px;
      border-color: #9b845a;
      border-radius: 0px 20px 20px 0px;
      margin: 0px 0px 5px -5px;
      color: #232323;
      text-align: left;
      font-weight: bolder;">Size Text pulled from custom Product field</span>
  </div>

Answer №2

To ensure the proper display of inline elements without whitespace interference, consider wrapping them in another inline-blocked tag and applying white-space:nowrap. I opted for using span tags due to the initial invalid markup. It's worth noting that removing whitespace between pairs of inline elements is crucial for maintaining a clean display.

For better organization and maintenance, it's advisable to extract the CSS styles from the HTML elements and utilize classes within a separate CSS stylesheet.

<div>
  <p style="display:inline-block; white-space: nowrap;"><span style="
  display: inline-block;
  background: #9b845a;
  padding: 6px 6px 6px 10px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 20px 0px 0px 20px;
  margin: 0;
  color: #ffffff;
  font-weight: bolder;">Condition</span><span style="
  display: inline-block;
  background: #ffffff;
  padding: 6px 10px 6px 6px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 0px 20px 20px 0px;
   margin:0;
  color: #232323;
  text-align: left;
  font-weight: bolder;">Condition Text pulled from custom Product field</span></p>
  <p style="display:inline-block; white-space: nowrap;"><span style="
  display: inline-block;
  background: #9b845a;
  padding: 6px 6px 6px 10px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 20px 0px 0px 20px;
  margin: 0px 0px 5px 10px;
  color: #ffffff;
  font-weight: bolder;">Size</span><span style="
  display: inline-block;
  background: #ffffff;
  padding: 6px 10px 6px 6px;
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 0px 20px 20px 0px;
  margin: 0px 0px 5px 0px;
  color: #232323;
  text-align: left;
  font-weight: bolder;">Size Text pulled from custom Product field</span></p>
</div>

Answer №3

To maintain a clean markup, it is recommended to create specific classes in CSS for styling purposes. This not only keeps the styles separate but also makes them reusable and easily portable.

For a consistent design, consider setting a uniform border and padding for all child elements within the badge. This approach allows for easy modification or disabling of specific border/padding properties when needed, enhancing the overall readability of the code.

.container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.badge {
  display: flex;
  font-weight: 900;
}

.badge div {
  border-style: solid;
  border-width: 2px;
  border-color: #9b845a;
  border-radius: 20px;
  padding: 6px;
}

.badge .badge-key {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  padding-left: 10px;
  background: #9b845a;
  color: #ffffff;
}

.badge .badge-value {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  padding-right: 10px;
  background: #ffffff;
  color: #232323;
}
<div class="container">
  <div class="badge">
    <div class="badge-key">Condition</div>
    <div class="badge-value">Condition Text pulled from custom Product field</div>
  </div>
  <div class="badge">
    <div class="badge-key">Size</div>
    <div class="badge-value">Size Text pulled from custom Product field</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

What is the best way to incorporate external libraries and plug ins such as owl.carousel.js into HTML and CSS?

I've been exploring the idea of adding a carousel to my website and stumbled upon owl.carousel.js during my research. The layout and features seem to align with what I'm looking for, which is great! As a newbie in this field, I could use some gu ...

Creating a dynamic layout using multiple columns in CSS with varying inputs

Can someone help me create two responsive columns with different inputs on the same row? The first column should display: The second column should show: How it currently appears: Here's how I want it to look: I believe there might be a mistake in ...

Organizing flex-items in a specific manner

My goal is to organize flex-items in a specific layout: To achieve this, I am referencing the following example: .Container { display: flex; overflow: hidden; height: 100vh; margin-top: -100px; padding-top: 100px; position: relative; widt ...

What is the most effective way to utilize CSS in order to contain a page filled with tall nested elements within the height of the viewport?

How can I restrict a page to the height of the viewport? I want to control the overflow-y on child elements, but it seems like I cannot limit the height--I can only set a max-height in pixels or a percentage. I believe I need to confine the height of certa ...

Having trouble with Bootstrap v4 dropdown menu functionality?

For some reason, I cannot get the dropdown menu to work in my Bootstrap v4 implementation. I have tried copying and pasting the code directly from the documentation, as well as testing out examples from other sources on separate pages with no luck. &l ...

Issues with DnD functionality in Chrome, images not visible during dragging operation

At first, I encountered an issue with event.dataTransfer.setDragImage not functioning properly in Chrome. This can be seen in the example below: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=html5-59.htm Upon further investigation, I noticed th ...

Form fields in Bootstrap 4 will consistently expand downward in the select dropdown menu

When using bootstrap 4, I want my field to always expand downwards and never upwards, even when half the screen is taken up by the form's select element. How can I achieve this effect? I have tried using data-dropup-auto="false" but it didn't wo ...

Is there a way to send multiple parameters in an array to a route?

I am working on deleting multiple rows from a MySQL database using checkboxes in my ejs view with node.js and React app. I have successfully deleted a single row, but I am struggling to figure out how to pass an array of parameters for deleting multiple ro ...

Error in browser caused by JQuery JavaScript, not Dreamweaver

I need help creating a webpage that can extract and display recent earthquake data based on user input location on Google Maps. I have been using Dreamweaver to test my code, and everything functions perfectly when I use the live webpage feature within th ...

What could be causing my screen reader to repeat lines?

I have this HTML structure: <button ng-disabled="vm.updating" ng-click="doSomething()" class="something" type="submit" aria-label="average score"> <span ng hide="hideConditional()" class="font-white">score</span> <span ng-show= ...

Utilizing a dropdown list in HTML to dynamically change images

On my HTML page, I have implemented a dropdown list box. My objective is to change an image and update a label based on the selection made from the dropdown list box. I already have an array called 'temp' which represents the number of items. The ...

Remove the column lines when hovering over a table to change the border color

I'm striving to update this table in a way that conceals the lines above the hovered point within that column. For instance, if the bottom middle box is hovered, three lines above it will be visible. My goal is to prevent those lines from being displa ...

Footer content is displayed at the center of the page before anchoring itself to

Looking for some assistance here. Every time I refresh the page or open it anew, the footer oddly pops up in the middle before swiftly moving back down to the bottom where it belongs. It's quite bothersome and despite my best efforts, I haven't b ...

Executing SQL Query on Button Click Event

I am working with PHP and trying to execute a SQL query when a button is pressed. However, the issue I'm facing is that nothing happens when I click the button! I checked the developer console but there doesn't seem to be any action detected when ...

Designing fixed sliding icons on the left side with CSS

I'm looking to replicate a sticky sliding icon set on both the left and right using CSS. While the right side icons with text are working perfectly, the ones on the left side are not functioning as expected. Here's the code snippet: .sticky-con ...

Challenges with Quirks Mode and Internet Explorer 8

Encountering a peculiar issue specific to IE8 and Windows Vista. Let's use some sample names for clarity. I have two pages, page1.html and page2.html. When directly typing their addresses into the browser, there are no quirks mode issues. However, w ...

When implementing flex-grow 1 on a flex box, the remaining space is not being filled as expected

I am struggling to make the green div fill the remaining space of a flex box container. My goal is to have the top row (blue) adjust its height based on content, and then have the row below (green) take up the rest of the space. However, it seems like the ...

Passing arguments inside the source attribute of an image or link tag in Node.js and

As a beginner in NodeJS, I am facing an issue with passing arguments inside links and image sources. In my template.html file, I have included various scripts and elements to create a search form. The issue arises when I try to incorporate values from the ...

options for adaptive web layout

My goal is to ensure my website is responsive by utilizing HTML5, CSS3, JavaScript, and jQuery. I am facing an issue with the responsiveness of my menu in the CSS code. PRÉSENTATION OFFRES PRODUITS RÉFÉRENCE CONTACT Menu nav { ...

An issue has been encountered within the node modules directory at the path node_modules/@angular/flex-layout/extended/typings

I encountered an error in my Angular 6.0.8 application while using Angular CLI and running from VSCode. ERROR in node_modules/@angular/flex-layout/extended/typings/style/style.d.ts(72,67): error TS1144: '{' or '; ' expected. no ...