Challenge with resizing the element content appropriately

I'm having an issue with the text of the second h5 element not resizing properly and overflowing onto the background color. I tried wrapping the h5 in a div and applying CSS to the div, but it didn't solve the problem. Can you tell me what I might be overlooking?

Below is the relevant code snippet (I've removed other elements and CSS properties for simplicity):

.container {
    position: relative;
    display: inline-block;
    margin-top: 50px;
}
.card .front {
    color: white;
    text-align: center;
    font-weight: bold;
    position: absolute;
}
.card .front h5 {
    background-color: #5E92F2;
    padding: 10px 0 10px 0;
    margin-top: 98%;
    width: 198px;
    height: 44px;
}
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
</head>
<body>
<section class="container">
            <div class="card">
                <div class="front" style="background-image: url('image.jpg');">
                    <h5>Image 1</h5>
                </div>
                <div class="back">Abc</div>
            </div>
</section>

<section class="container">
            <div class="card">
                <div class="front" style="background-image: url('image.jpg');">
                    <h5>Image 1 With more text</h5>
                </div>
                <div class="back">Abc</div>
            </div>
</section>

Answer №1

Give this a shot:

p{
    font-size: 1em;
}

Answer №2

Since the h5 element has a fixed height, this behavior is to be expected.

To resolve this, update the .card .front h5 rule with min-height: 44px;.

.container {
    position: relative;
    display: inline-block;
    margin-top: 50px;
}
.card .front {
    color: white;
    text-align: center;
    font-weight: bold;
    position: absolute;
}
.card .front h5 {
    background-color: #5E92F2;
    padding: 10px 0 10px 0;
    margin-top: 98%;
    width: 198px;
    min-height: 44px;
}
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
</head>
<body>
<section class="container">
            <div class="card">
                <div class="front" style="background-image: url('image.jpg');">
                    <h5>Image 1</h5>
                </div>
                <div class="back">Abc</div>
            </div>
</section>

<section class="container">
            <div class="card">
                <div class="front" style="background-image: url('image.jpg');">
                    <h5>Image 1 With more text</h5>
                </div>
                <div class="back">Abc</div>
            </div>
</section>

Answer №3

When working with the h5 element, if you set the height: 44px;, the font size defaults to the standard h5 font size. To address this issue, consider adjusting the font size or increasing the height of the h5 element.

You can try implementing one of the following solutions:

.card .front h5{
 font-size:16px;
}

or

.card .front h5{
 height:64px;
}

Answer №4

The CSS rule defines the element's height as 44px and sets the font size to 1.25rem. To alter either or both properties, you could adjust the height to 'auto' while preserving the font size, or change the font size to 100% while maintaining the current height of the element. There are multiple approaches to accomplish this.

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

Is it possible to transfer a variable to a modal window by clicking on a link?

Here is a link with sample data value "data-id" that I want to pass: <a href="#edit_task" data-toggle="modal" data-id="WAH"><i class="fas fa-edit fa-lg fa-fw"></i></a> Javascript code to open the modal and assign the data value to ...

"Safari browser may hide placeholder text, making it invisible to viewers

When adding color to the placeholder, I utilize this CSS property: .pro_heading input::-webkit-input-placeholder { color: #777; } However, it appears that in Safari, the color is not visible. https://i.sstatic.net/hXmjX.jpg ...

step-by-step guide to disabling grayscale mode automatically

In recent times, a trend has emerged in Thailand where many websites are appearing in grayscale. This is achieved through the use of -webkit-filter: grayscale(100%), filter: grayscale(100%) and similar filters. Although it is possible to revert these webs ...

How to center text both horizontally and vertically in HTML

I am struggling with centering a background image along with a 1-line text vertically and horizontally inside a div. Although I have successfully centered the image, the text remains misaligned. I attempted using vertical-align: middle without success. Be ...

Implementing jQuery Datepicker with custom validation for start and end dates

Hey there! I could really use some help with a coding issue I'm facing. I've been able to successfully implement the jQuery Datepicker for start/end date validation when using only one Datepicker for both dates. However, I'm struggling to f ...

The fabulous four of web development: Ajax, PHP, SQL, as well

I have encountered a problem that has left me stumped and unable to find a solution. Although I don't usually ask questions here, any help would be greatly appreciated. Here is the PHP code that handles the Ajax call: <?php session_start(); ...

npm installation of bootstrap fails with error message "Module not found: 'jQuery'

Just starting out with node, npm, and express.js. I recently discovered that you can use Bootstrap by running npm install. So, I went ahead and installed Bootstrap with npm install bootstrap --save, and did the same for jQuery and required them in my app. ...

Ways to utilize a single HTML page for various URLs while changing one variable value based on the queried URL

My current HTML page structure looks like this: <body ng-controller="DashboardDisplay" onload="submit()"> <div class="container-fluid" > {{scope.arr}} </div> </body> <script> var myApp = angular.module(&apos ...

Steps for incorporating monaco-editor into a website without utilizing nodejs and electron

Currently, I am working on building a basic web editor and came across Monaco-editor. I noticed someone using it with Electron, but I am interested in integrating it into plain JavaScript just like on their webpage (link). However, I am struggling to fin ...

Implement a jQuery feature to gradually increase opacity as the user scrolls and the page loads

On a dynamically loaded page via pjax (except in IE), there are several links at the bottom. Whenever one of these hyperlinks is clicked, the page scrolls to the top while still loading. Although I am okay with this behavior, I'm curious if it' ...

Incorporate a single md-switch to control various other md-switches within an HTML/JS setup

I am looking to implement a feature that allows users to toggle entire group sections on and off. When a group section is toggled off, I want the individual sections within that group to also be disabled. Conversely, when a group section is toggled on, I w ...

Are there any quick tricks for condensing multiple hover CSS properties into a single line of code?

Imagine wanting to apply the hover CSS property to multiple classes, you would typically have to write it like this: a:hover, p:hover, div:hover{ // CSS properties } It can become repetitive having to add ":hover" to each class. Is there a way to stream ...

Manipulating SVG on a UIWebview dynamically

Seeking guidance on manipulating SVG files loaded into a UIWebview. Currently, I load an SVG file into an HTML file and then into a UIWebview. Presumably, Javascript is required for this, but unsure about the exact process. Ideally, I aim to dynamically ma ...

Adjusting the height of a div inside a Material-UI Grid using Styled Components

When the mouse hovers over the two cells highlighted in pink, they turn pink. Is there a way to make the entire grid fill with pink when hovered over, both width and height at 100%? Check out the sandbox here ...

Switch to display only when selected

When I click on the details link, it will show all information. However, what I actually want is for it to toggle only the specific detail that I clicked on. Check out this example fiddle Here is the JavaScript code: $('.listt ul').hide(); $( ...

5 Simple Steps for Adding a Value to a Popup Textbox Automatically

I want to send a value to another php page and then display the values. How can I achieve this? Here is the PHP code snippet: if(!empty($_POST["mytext"])) { for ($x=1; $x<=$a; $x++) { echo $txtLine[$x] = $_POST['mytext'.$x]; } } B ...

The AJAX data variable fails to send to PHP script although $_POST is defined

I am at my wit's end with this problem. I am attempting to send a variable to a PHP script using AJAX, and although I can confirm that $_POST is set, the variable remains undefined. Oddly enough, I have used almost the same code in other instances an ...

How can ReactJS conceal text when it wraps to the next line?

I am currently developing a web application that is compatible with both small and large screens. I am facing an issue where when the browser window is resized to be smaller, a title such as "Stackoverflow helps a lot" ends up breaking into the next line. ...

Do flexible layouts require more effort and time to create and upkeep compared to fixed width layouts?

Are flexible layouts more challenging to create and maintain than fixed width layouts, and do they take longer to design? Does a flexible layout only involve setting the width, height, and font size in em or %? What are the main advantages of using a fle ...

Maximizing the functionality of PHP switch to create clickable list items

I have successfully created a simple switch for my website, but I am encountering an issue with clickable list items. The concept is that when I click on any of the list items, the switch should display the appropriate greeting (whether it's Night or ...