Design a dynamic rectangular division using CSS and Bootstrap 4 that adapts to different screen sizes

I am encountering difficulties attempting to replicate a full-width rectangle div on a website, particularly with issues related to responsiveness and mobility.

Here is an example of the design I am trying to recreate.

And here's what I've accomplished so far: example here.

The primary issue I'm facing is that the text does not fit inside the designated boxes as intended, especially when viewed on mobile devices.

Below is a snippet of my current HTML and CSS:

HTML

<div class="row remove-container">
  <div class="speech-bubble1 test-text">
    <div class="row">
      <h3 class="strong-text felinetitle">ADD</h3>
    </div>
    <div class="row">
      <ul id="feline-ul">
        <li>
          <h6>Heartworm Test</h6>
        </li>
        <li>
          <h6>$25</h6>
        </li>
      </ul>
    </div>
  </div>

  <div class="speech-bubble speech-bubble-top">
    <div class="container" style="padding-top: 35px; padding-bottom: 35px;">
      <h3 class="strong-text felinetitle2">
          A LA CARTE </h3>
      <div class="row">
        <div class="col-lg-9">
        </div>
        <div class="col-lg-4">
          <div class="row">
            <div class="col-sm-6">
              DHPP or DHLPP
            </div>
           <div class="col-sm-6">
              Canine Flu
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS

.speech-bubble {
  position: relative;
  background-color: #045faf;
  width: 100%;
  height: 150px;
  color: white;
  text-align: center;
  font-family: sans-serif;
}

.speech-bubble1 {
  position: absolute;
  background-color: #045faf;
  width: 32%;
  height: 150px;
  left: 0;
  color: white;
  text-align: center;
  z-index: 1;
  font-family: sans-serif;
}

.remove-container {
  margin: 0 !important;
  padding: 0 !important;
}

.serviceprice {
  display: flex;
  justify-content: center;
  padding-top: 50px;
}

#feline-ul{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}

#feline-ul li{
float: left;
margin-left: 70px;
}

.test-text{
webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-shadow: 18px 0px 35px -23px rgba(0,0,0,0.52);
box-shadow: 18px 0px 35px -23px rgba(0,0,0,0.52);
}

The problem arises in the second box where my texts do not align properly within a square/container, instead occupying full width.

Answer №1

Just recreated the structure to demonstrate a minimal example.

In summary, your bootstrap code is not properly nested.

What have I changed? I enclosed the row within a container. A container wraps all rows within it. There are two types of containers: .container and .container-fluid, with the latter being for full-width layouts. Below the container is a row, which is always divided into 12 parts in bootstrap. You can specify the number of columns for each display width. Refer to the width breakpoints in the bootstrap grid documentation. It is crucial to avoid nesting a .row within another .row. A row should always be a parent of a column. If you resize the example, you will see that the two columns (.col-md-8 and .col-md-4) adjust to two 12-width columns on smaller devices.

If you have key-value data pairs, using tables instead of lists is recommended. This is more semantically appropriate. Lists are typically used for enumerations like shopping lists, whereas tables are better suited for object-oriented formatting (starting from one attribute), such as a dish with a price.

*{
  color: white;
}

footer{
  background-color: #045faf;
  padding: 20px;
}
#footer--left, #footer--right{
  padding: 0 20px;
}

#footer--left{
  -webkit-box-shadow: 18px 0px 35px -23px rgba(0,0,0,0.52);
    box-shadow: 18px 0px 35px -23px rgba(0,0,0,0.52);
}

table{
  width: 100%;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<footer>
  <div class="container">
    <div class="row">
      <div id="footer--left" class="col-md-4">
        <h3>Some Head Text</h3>
          <table>
            <tr>
              <td>Value</td>
              <td>25$</td>
            </tr>
          </table>
      </div>
      <div id="footer--right" class="col-md-8">
        <h3>Some Head Text</h3>
        <table>
          <tr>
            <td>Value</td>
            <td>25$</td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</footer>

Answer №2

It seems that the code example provided is quite complex. Here's an alternate approach I would suggest:

.blocks {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.blocks .block {
  border: 1px solid #000;
  height: 150px;
}
.blocks .block h3 {
  text-align: center;
  padding: 0;
  margin: 0;
}
.blocks .block ul {
  list-style-type: none;
  padding-left: 0;
}
.blocks .block ul li {
  padding: 0 0 0 15px;
  margin: 0;
}
.blocks .block ul li:nth-child(1n) {
  display: inline;
}
.blocks .block:first-child {
  width: 33%;
}
.blocks .block:last-child {
  width: 66%;
}
<div class="blocks">
  <div class="block">
    <h3>Add</h3>
    <ul>
      <li>Service 1</li>
      <li>5$</li>
      <br>
      <li>Service 2</li>
      <li>10$</li>
      <br>
      <li>Service 3</li>
      <li>15$</li>
    </ul>
  </div>
  <div class="block">
    <h3>Add</h3>
    <ul>
      <li>Service 1</li>
      <li>5$</li>
      <br>
      <li>Service 2</li>
      <li>10$</li>
      <br>
      <li>Service 3</li>
      <li>15$</li>
    </ul>
  </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

Guide on incorporating HTML data into an existing webpage

Requesting assistance in creating an HTML page that features a dynamic drop-down menu. The drop-down should trigger a list to appear upon selection, which can be sourced from either a text file or plain HTML document. The envisioned functionality involves ...

Show all entries belonging to a specific ID on individual rows for each ID using PHP

I have a table generated from inner joins that displays different articles associated with outfit IDs. id_outfit | name | article ---------------------------------------- 56 | one | shoe.png 56 | one | dress.png 56 | one ...

Ways to perform a CSS redirection

How can you create a webpage where only the CSS can be altered to show a completely different page to the user? ...

A guide on incorporating jQuery alert messages into Angular 2

Whenever I submit a form by clicking on the "send message" button, I want to display an Alert message using jQuery. However, currently, I have to double click for the alert message to appear. How can I make it so that the alert message is shown with just o ...

shard: the dropdown menu can be interacted with by clicking, but the options

Having trouble selecting an item from a dropdown menu on a modal using splinter. Despite the dropdown being visible and clickable, the selection is failing unexpectedly. (Pdb) dropdown = next(i for i in my_browser.find_by_xpath('//select[@name="exist ...

Optimal method for storing large arrays of numbers in localStorage using JavaScript

*"Efficient" in this context refers to smaller size (to reduce IO waiting time) and fast retrieval/deserialization times. Storing times are less important in this scenario. I need to store multiple arrays of integers in the browser's localStorage, ea ...

Designing a search form to browse through the database by entering a specific title

I am currently working on designing a form to perform database searches for specific titles. However, I have encountered an issue where nothing happens when I try to search, possibly due to a problem with connecting to the database. The link to the appli ...

Vertical Alignment Challenge in Bootstrap Container

I'm struggling with formatting my simple website. I have a navbar, footer, and a centered container where I want to display text and buttons. However, I can't seem to get the elements to appear on separate lines - I want the text on one line and ...

What steps are necessary to activate javascript in HTML for WebView?

I recently discovered that when my HTML/JavaScript site is visited via an Android webview, JavaScript is disabled by default. This causes a pricing list on my page to not display properly because it requires a JavaScript class to be added for it to open. I ...

Appending a row to a table will not trigger events in Select2

Despite several attempts, I can't seem to get the select2:select event working on dynamically added rows in a table using select2. The event only works on the original row. Unfortunately, I don't have any additional details about this issue. COD ...

A error was encountered stating that the formValidation function is not defined when the HTML form element is submitted

Having trouble calling a function and receiving an error message. How can I resolve this issue? The error message reads: index.html?email=&gebruikersnaam=&wachtwoord=&submit=Submit:1 Uncaught ReferenceError: formValidation is not defined at HT ...

Show table information from backend operations

In my program, I have a function that generates an HTML table based on data retrieved from a SQL database. getWhileLoopData(){ string htmlStr = ""; SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionS ...

What is the best way to right-align a label in a vertical MUI Slider in ReactJS?

I am currently working with ReactJS/TS and MUI (Material-UI) to develop a vertical slider that requires the label to be positioned on the right side. By default, MUI places the label on the left for vertical sliders. I have reviewed the API documentation e ...

Make sure all Bootstrap elements have square edges

Can someone help me with using bootstrap3 to achieve square corners for all buttons, menus, and navs? I have tried setting the edges to be square using this code snippet, but my specific requirement is to only have square corners for the buttons, menus, a ...

Ways to customize hover color of Bootstrap 5 Dropdown menu

I'm having trouble modifying the hover color for a dropdown menu using Bootstrap 5. I'm unsure of the correct method to achieve this. The desired outcome is as follows: However, at the moment it appears like this: Apologies for the oversight, h ...

Transitioning the height of a webpage element from a fixed value to fit the content dynamically

I'm trying to create a div that smoothly transitions from a set height to a height based on its text content. This likely involves considering the window width, as narrower windows cause text wrapping and increase the required height. #object { w ...

Keeping the Bootstrap popover anchored to the content

I have a functional bootstrap popover that includes a time attribute. However, I am looking to enhance its functionality so that it remains open when the mouse is on the content and closes only when the mouse leaves the content. Here is the relevant code ...

After clicking, I would like the text from the list item to have a style of "background-color: aqua" when displayed in the textarea

Is there a way to apply a highlight with a style of "background-color: aqua" in the textarea only to the word selected from the list above after clicking on it? See below for my HTML: <head> <script src="https://ajax.googleapis.com/ajax/libs/ ...

Adding HTML elements dynamically using jQuery: A how-to guide

My objective is to start with one element upon loading the page, and this element should have an ID of "something_O". When the user clicks on an add link, a new identical HTML element should be added underneath the existing element. The new element should ...

Error alert: Blinking display, do not dismiss

I am trying to make it so that when the "enviar" button is clicked, the "resultado" goes from being hidden ("display:none") to being visible ("display:block"). This is my html document: <script type="text/javascript"> function showResu ...