What is the best way to align paragraphs vertically within a required square-shaped div using CSS (with images for reference)?

I am trying to style a table in a specific way, but I'm facing some challenges.

  1. First, I want to make the internal div inside the table square with a percentage height.
  2. Next, I need to have two paragraphs stacked on top of each other within the same div.
  3. Additionally, I want the font size inside these paragraphs to automatically adjust based on the content length.
  4. Lastly, I would like any long text to be ellipsized if it exceeds a certain length.

Everything is working as expected except for item #2. I've tried various solutions from different sources such as this link, this one, and this post. Despite my efforts, I can't seem to get the paragraphs to stack vertically.

In the provided screenshots:

https://i.sstatic.net/tAzdP.png https://i.sstatic.net/6Wvxe.png

Please excuse the rough image quality as I'm not skilled at using graphic design software. My goal is to align the title at the top of the box with 100% width, while the longer text should fill the remaining space below it also with 100% width.

body {
  margin: 0 auto;
  height: 100%;
  width: 100%;
  background-color: lightgray;
}

td {
  border: 1px solid black;
}

.card-stack {
  width: 8%;
  height: 20vh;
}

.card {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: auto;
  width: 70%;
  height: 0;
  padding-bottom: 70%;
  background-color: darkgray;
  border: 1px solid green;
}

.card-title {
  clear: both;
  width: 100%;
  height: 33%;
  float: left;
}

.card-title p {
  display: block;
  border: 1px solid red;
  font-size: 1vw;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-text p {
  border: 1px solid purple;
  font-size: 1vw;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
}

.card-text {
  clear: both;
  width: 100%;
  height: 67%;
  float: left;
}
<body>
  <table width="100%">
    <tr>
      <td id="opponent-deck" class="card-stack">
        <div class="card">
          <div class="card-title">
            <p>Title</p>
          </div>
          <div class="card-text">
            <p>This is long text</p>
          </div>
        </div>
      </td>
      <td id="opponent-hand" class="hand" "></td>
      <td id="opponent-disc " class="card-stack "></td>
    </table>
</body>

Answer №1

Apply the style flex-direction: column; to the card class.

body {
  margin: 0 auto;
  height: 100%;
  width: 100%;
  background-color: lightgray;
}

td {
  border: 1px solid black;
}

.card-stack {
  width: 8%;
  height: 20vh;
}

.card {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: auto;
  width: 70%;
  height: 0;
  padding-bottom: 70%;
  background-color: darkgray;
  border: 1px solid green;
  flex-direction: column; /* Added */
}

.card-title {
  /* clear: both; Removed */
  width: 100%;
  /* height: 33%; Removed */
  /* float: left; Removed */
}

.card-title p {
  display: block;
  border: 1px solid red;
  font-size: 1vw;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-text p {
  border: 1px solid purple;
  font-size: 1vw;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
}

.card-text {
  clear: both;
  width: 100%;
  height: 67%;
  float: left;
}
<body>
  <table width="100%">
    <tr>
      <td id="opponent-deck" class="card-stack">
        <div class="card">
          <div class="card-title">
            <p>Title</p>
          </div>
          <div class="card-text">
            <p>This is long text</p>
          </div>
        </div>
      </td>
      <td id="opponent-hand" class="hand" "></td>
      <td id="opponent-disc " class="card-stack "></td>
    </table>
</body>

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

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...

Utilizing a dropdown feature in Bootstrap for creating X columns of lists instead of a traditional single list

Check out my Fiddle. Beneath Dropdown1 lies a single-column list of actions. I'm looking to expand this list across multiple columns, like 5 or the width of the page. I'm hoping there's a Bootstrap CSS class I can use for this, but I migh ...

The issue with the `.load` function in jQuery not functioning properly

I'm currently tackling an issue with a project where I am encountering difficulties with the .load function not working in Google Chrome. Below is the JavaScript code: function link1() { $('#loadarea').html('loading.....' ...

Is there a way to show the input value in a different form while still retaining the original value?

When receiving a pagination number from user input, I store it in a variable like so: $html .= "<input type='submit' name='next' value='$next_page_number' />"; Now, I'm looking to replace the displayed text for th ...

Double-executing methods in a component

I have encountered an issue while trying to filter existing Worklog objects and summarize the time spent on each one in my PeriodViewTable component. The problem I am facing involves duplicate method calls. To address this, I attempted to reset the value ...

When using Selenium to locate a sub element within a previously obtained WebElement using xpath, it will always return the first match found on the entire

Currently, my project involves using selenium and python to automate website testing. I am facing an issue while trying to extract links to files on the website using the following approach: I first use divs = find_elements_by_css_selector("div.answer") to ...

Calculate the quantity of rows within a specific div container

Looking at the image provided, there are multiple divs inside a main div. I am trying to figure out how many rows the main div contains. For instance, in this particular scenario, there are 5 rows. It is important to mention that the inner div is floated ...

Change the input field to uppercase using JavaScript but do not use inline JavaScript

Hey there! I have a basic script set up (see below) with an input field allowing users to enter a query. This query is then sent to a socrata webservice to request specific data, which is displayed in an alert box. So far, everything works smoothly. var l ...

`Locating an image alongside text for seamless integration`

Take a moment to view the image displayed below Feature 1 and feature 4 are two distinct divs that I have styled with the Bootstrap class .col-md-4 Here is the corresponding HTML code <div class="col-md-4"> <div class="feature-group-1"> ...

Show buttons in varying styles aligned next to each other

Here is the code snippet I'm currently working with: <fieldset class=last> <button>Refresh</button> <button> Clear</button> </fieldset> <form method="POST" action="*******"> <button> Down ...

Ways to create a vertical bottom-up tree view using d3.js

I am trying to create a reverse tree view, starting from the bottom and moving up. What modifications do I need to make? Here is the link to my JS Fiddle: ...

``Is there a way to retrieve the file path from an input field without having to submit the form

Currently, I am looking for a way to allow the user to select a file and then store the path location in a JavaScript string. After validation, I plan to make an AJAX call to the server using PHP to upload the file without submitting the form directly. Thi ...

Creating a personalized .hasError condition in Angular 4

I am currently in the process of modifying an html form that previously had mandatory fields to now have optional fields. Previously, the validation for these fields used .hasError('required') which would disable the submit button by triggering a ...

Grid numbering with CSS in jQuery mobile design

Looking to create a numbered grid where the user inputs a number and the grid is generated. I am considering using CSS, HTML, or Jquery Mobile for an iPad project. I'm unsure of the best approach at the moment. I would like the grid and numbers to aut ...

Angular: How to restrict the compilation of rgba() to #rrggbbaa in your codebase

There are some browsers that do not support #rrggbbaa but do support rgba(). However, in my Angular project, background-color: rgba(58, 58, 58, 0.9) in common.css was compiled to background-color:#3a3a3ae6 in style.[hash].css. How can I prevent this co ...

Transforming jQuery into pure Javascript code

UPDATE SUCCESS! I've cracked the code. The solution was to include jQuery by adding it in Joomla through the template/index.php file with this line under "//Add Javascript Frameworks" JHtml::_('jquery.framework');. PROGRESS I'm seekin ...

Animate an image to the right when clicked, then return it to the left with a second click

Seeking help with animating a set of images to move individually 300px right on first click, and then 300px left when clicked again. I'm currently facing an issue where my code is not working. It could be due to A) syntax errors or B) the images not ...

Load HTML content without having to refresh the page

I'm struggling to decide whether I should have the html hidden before it is loaded or pulled from another source. My goal is to display a form on one page and dynamic content on other pages. The form data needs to be saved in mongo db, and when the p ...

Ways to Adjust Website Scaling for Varying Screen Sizes

Is there a way to adjust my website's scale based on the device width, such as changing it to 0.7 for certain devices? The only information I've found so far is using the <meta> tag: <meta name="viewport" content="width=device-width, i ...

Abundance of DOM elements - the difference between hidden and display none

When I have numerous DOM elements on my page and assign them all a display: none property, the browser continues to perform quickly (smooth scrolling and snappy page response). But if I hide the elements using visibility: hidden instead, the browser slows ...