The alternative text for the oversized image exceeds the boundaries

After setting the width and height for an image, along with testing overflow:hidden, I've encountered an issue where the alt text for a broken image overflows the bounds of the image and the image size is lost. How can I contain the alt text within the image box?

img{
width:100px;
height:100px;
border:1px solid #ff0000;
overflow:hidden;
}
<img src="http://www.example.com/broken.jpg" alt="This is a long text which overflows bounds of image">

Answer №1

When the image is styled with properties like float, flex, block, inline-block or table, it causes the alt text to wrap.

img{
width:100px;
height:100px;
border:1px solid #ff0000;
overflow:hidden;
float:left
}
<img src="http://www.example.com/broken.jpg" alt="This is a long text which overflows bounds of image">

img {
  width: 100px;
  height: 100px;
  border: 1px solid #ff0000;
  overflow: hidden;
  display: block;
}
<img src="http://www.example.com/broken.jpg" alt="This is a long text which overflows bounds of image">

img {
  width: 100px;
  height: 100px;
  border: 1px solid #ff0000;
  overflow: hidden;
  display: table;
}
<img src="http://www.example.com/broken.jpg" alt="This is a long text which overflows bounds of image">

Answer №2

To prevent overflow, wrap the image tag with a div and set alt text appropriately.

div {
  width:100px;
  height:100px;
}

img{
  border:1px solid #ff0000;
  overflow:hidden;
}
<div> 
  <img src="http://www.example.com/broken.jpg" alt="This is a long text which overflows bounds of image">
</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

Is it feasible to customize the icon for the expand/collapse button in the header of jqGrid?

To begin, let me provide some context. In my view, I have 2 or more grids (jqgrid) and I aim to insert an "X" into the expand/collapse button. After several attempts, I successfully added the "X" icon to all grids using this code: .ui-icon-circle-trian ...

Matching Regex Patterns for Parents and Children

Regex has always been a struggle for me, so I could really use some guidance. I'm attempting to create opening and closing 'tags' with the same regex permitted inside these tags. For example, a tag might look like: <: tagname("[captur ...

Is it possible for me to set my HTML body class as ".body" in CSS?

CSS .body { background: linear-gradient(-45deg, rgb(255, 0, 0), rgba(216, 29, 29, 0.856), #fdfdfdd7, #234cd5, #ffffff); background-size: 400% 400%; background-repeat:no-repeat; animation: gradient 35s ease infinite; height: 100vh; } HT ...

Is it possible to validate link clicks and integrate PHP within JavaScript functions?

Before anyone mentions security concerns, please refrain. Imagine I have an index.php. This page contains several links to 'home.php', each with different data stored in the href attributes. For example: Link 1: 'home.php?data=1&count ...

Error with HTML form validation

After clicking the "Send Request" button, the query string ?req_flag=0 is not appearing in the URL. What could be causing this issue? I want my URL to look like this: localhost/flavourr/send_req.php?req_flag=0&f_e_mail_add=value <pre> <form ...

Using Vue to display a Div inside a TD element of a table does not result in a reactive behavior

I am encountering an issue with a table where the last column contains a div with three options (View, Edit, and Delete). This submenu is initially hidden, but when clicking on the options button in the last column of the table, the array used to control i ...

Why isn't my Bootstrap navbar expanding properly?

I am facing an issue with my toggle menu where it is not dropping down as a solid black block but instead in the center transparent. How can I fix this to make it drop down as a solid block and be positioned to the left? I have a bootstrap navbar that I mo ...

Developing components through JavaScript

I am attempting to use JavaScript to create an element in my HTML using createElement, but there seems to be an issue in my code. HTML <button class="test_button">I am a test</button> <div class="test"></div> ...

Retrieving text that has been HTML-escaped from a textarea using jQuery

Within my HTML file, I have a <textarea id="mytextarea"></textarea>. Suppose a user inputs the following text: <hello>, world! How can I retrieve res = "&lt;hello&gt;, world!"; based on the user's input? The current code s ...

Attempting to create a div element that automatically adjusts itself horizontally in the center through the utilization of margins

I searched through examples on stack overflow that had similar problems to mine, but unfortunately, I couldn't find a solution. I'm not sure what I'm doing wrong, but the div is not aligning horizontally center as expected. It seems to work ...

Adaptive Design - Content Fragmentation

I am currently working on a webpage layout that has the following HTML structure: <div class="row"> <div class="col3">test 1</div> <div class="col3">test 2</div> <div class="col3">test 3</div> </div ...

Is there a way to display images in Vuetify similar to the linked image below?

I am looking to measure my v-img like the one shown in the image below. https://i.stack.imgur.com/edMKr.jpg Does anyone know how I can achieve something similar to the image shown using vuetify's v-img. <div class="row"> <v ...

Stylish Sudoku Grid Design with CSS-Grid Borders

I have designed a sudoku puzzle and I am seeking assistance with CSS to style it. My goal for styling using CSS is illustrated here... https://i.stack.imgur.com/nrA47.png What I currently have appears like this... https://i.stack.imgur.com/zpNp6.png T ...

Retrieving Basic HTML Source Code in UITextView Without CSS Styling Using Objective-C

I am looking to make changes to HTML text within a UITextView. The original text was created using an HTML editor in the web version of the app and needs to be edited with a default font on iOS. Here is the simple HTML code for the text that needs editing ...

New ways to style Links in NextJs 13

I am relatively new to Next.js and I am attempting to create a menu with a hover effect using the following code: import Link from 'next/link'; const MenuItem = ({ href, label }) => ( <Link href={href} className="menu-item"> ...

Issues with CSS compatibility have been identified on Internet Explorer version 7

Currently, I am in the process of working on a website. You can access it through this link. The good news is that the site functions well on FireFox, Chrome, and IE 8. However, the bad news is that it does not display properly on IE 7. There are alignme ...

"Troubleshooting: Why isn't my jQuery AJAX POST request successfully sending data to

Here is the jQuery code snippet that I am currently working with: $("#dropbin").droppable( { accept: '#dragme', hoverClass: "drag-enter", drop: function(event) { var noteid = "<?=isset($_POST['noteid']) ? ...

What is the best way to create a scrollable horizontal element using bootstrap?

Here is the specific issue I'm facing: https://i.sstatic.net/oi94N.png The problem is that the icons are wrapping onto 2 lines, which is not the desired outcome. This is what I currently have: <li class="list-group-item participants-grouping-li" ...

Unable to apply 100% height to flex child element

When it comes to creating a layout for my website, I have a simple idea in mind. I want the body content to take up at least 100% of the height of the page. This setup works perfectly on desktop screens, but when I switch to a mobile view (which changes th ...

What is the best way to integrate a ttf custom font into my Rails application?

Despite following the instructions in this thread, I am unable to achieve the same results. main.css h1 { font-family: 'Shadows Into Light', serif; src: url('/assets/fonts/shadows.ttf') format("Shadows Into Light"); font-size: 4 ...