Increase the size of a centered image within a table

Lately, I've felt like my mind is starting to unravel.

Here's the issue at hand: I've been attempting to resize an image within a table cell so that it spans the full width of the cell. Strangely enough, this seems to be harder than anticipated. Despite my efforts and exhaustive searches online, I haven't found a solution that actually works. I know the theory behind it should, but in practice, not so much.

Let me show you the code:

function scrollElmVert(el,num) { // Use a negative number to scroll up
  var re=/html$/i;
  while(!re.test(el.tagName) && (1 > el.scrollTop)) el=el.parentNode;
  if(0 < el.scrollTop) el.scrollTop += num;
}

var acc = document.getElementsByClassName("accordion");
var i;
var open = null;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    if (open == this) {
      open.classList.toggle("active");
      open = null;
    } else {
      if (open != null) {
        open.classList.toggle("active");
      }
      this.classList.toggle("active");
      open = this;
      // Scroll to clicked element
      open.scrollIntoView();
      scrollElmVert(open,-68);
    }
  });
}
.accordion {
  background-color: #fff;
  color: #444;
  cursor: pointer;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
  margin: -5px;
}

/* Code commented out for troubleshooting */
.bg {
 width: 100%;
}

.active,
.accordion:hover {
  background-color: #fff;
}

.panel {
  padding: 0 0px;
  display: none;
  width: 100%;
  background-color: white;
  overflow: hidden;
}

.accordion.active+div {
  display: block
}

.column {
    float: left;
    width: 50%;
    padding: 10px;
}

/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}
  
  

<button class="accordion">
    
<div class="cat_text">
Cat 2 Placeholder
</div>
       
  </button>
<div class="panel">
  
<div class="row">
  <div class="column" style="background-color:#aaa;text-align:center;position:relative;">
    <img src="http://via.placeholder.com/165x555" style ="width:100%">
    <p>Some text</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>
<div class="row">
  <div class="column" style="background-color:#aaa;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>
  
</div>


<button class="accordion">
    
<div class="cat_text">
Cat 3 Placeholder
</div>
       
  </button>
<div class="panel">
  <p>Cat 3 Content</p>
</div>

<button class="accordion">
    
<div class="cat_text">
Cat 4 Placeholder
</div>
       
  </button>
<div class="panel">
  <p>Cat 4 Content</p>
</div>

<button class="accordion">

<div class="cat_text">
Cat 5 Placeholder
</div>

</button>
<div class="panel">
  <p>Content 5</p>
</div>

Answer №1

Applying position:relative to the parent div and setting width:100% on the image, as exemplified in the code snippet below.

With position:relative, child elements are positioned relative to this element's location. This ensures that the width:100% of the child elements corresponds to the width of the element with position:relative.

.column {
    float: left;
    width: 50%;
    padding: 10px;
}

/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}
<div class="row">
  <div class="column" style="background-color:#aaa;text-align:center; position:relative;">
    <div class="img_enlarge_mobile"><img src="http://via.placeholder.com/165x550" style = 'width:100%'></div>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>
<div class="row">
  <div class="column" style="background-color:#aaa;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>

Answer №2

To make your image responsive, simply include img#largeImage {width: 100%;} in your CSS file.

.column {
    float: left;
    width: 50%;
    padding: 10px;
}

/* Make sure to clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}
img#largeImage {
  width: 100%;
}
<div class="row">
  <div class="column" style="background-color:#aaa;text-align:center;">
    <div class="img_enlarge_mobile"><img id="largeImage" src="http://via.placeholder.com/165x550"></div>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>
<div class="row">
  <div class="column" style="background-color:#aaa;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>

Answer №3

I was puzzled by the difficulty of this situation.

Here is the solution:

To address the challenge, simply include width:100% in your img tag.

Alternatively,

You can insert the following CSS code into your stylesheet:

.img_enlarge_mobile img{
    width:100%;
}

After updating OP's question:

I have shared a JSFiddle link.

.column {
    float: left;
    width: 50%;
    padding: 10px;
}

/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}
.img_enlarge_mobile img{
    width:100%;
}
<div class="row">
  <div class="column" style="background-color:#aaa;text-align:center;">
    <div class="img_enlarge_mobile"><img src="http://via.placeholder.com/165x550"></div>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
</div>
<div class="row">
  <div class="column" style="background-color:#aaa;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  <div class="column" style="background-color:#bbb;">
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </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

Change the controller's classes and update the view in Angular

In my angular application, I have a popup window containing several tabs. These tabs are styled like Bootstrap and need to be switched using Angular. The code for the tabs is as follows (simplified): <div class="settingsPopupWindow"> <div> ...

Retrieving JSON data with Node.js

Receiving a JSON response from TMDB: { "id": 283350, "results": [ { "iso_3166_1": "BR", "release_dates": [ { "certification": "12", "iso_639_1": "pt", "note": "Streaming", "release_date": ...

the value of properrty becomes undefined upon loading

In my code, there exists an abstract class named DynamicGridClass, containing an optional property called showGlobalActions?: boolean?. This class serves as the blueprint for another component called MatDynamicGridComponent, which is a child component. Ins ...

What is the best way to achieve full width for text on large screens in Bootstrap 3 while eliminating right margins?

I've been attempting to create a full-width text display on large screens using Bootstrap, but despite utilizing container-fluid and trying solutions from StackOverflow that should enable full width, there still remains whitespace at the right side of ...

Setting a const value (true or false) based on the size of the window - a step-by-step guide

While studying, I encountered a challenge: In the code below, I need to dynamically set useState(false) based on the screen size. If the screen is larger than 947px, for instance, it should automatically be changed to true. The issue arises because sett ...

The debate between classes and data attributes in terms of auto field initialization

A Brief Background In the realm of javascript/jQuery, I've crafted a method that traverses through various fields and configures them based on their type - be it dropdowns, autocomplete, or text fields... The motivation behind this is my personalize ...

Synchronize data bidirectionally between parent and child components in Vue 2

This special wrapper I created for the Vue-multiselect package acts as a child component within this scenario. <template> <div> <multiselect v-model="items" :options="filteredList" ...

Enhancing MEAN Stack Application by Updating Table Post Database Collection Modification

In my efforts to ensure that my table data remains synchronized with the database information, I have encountered an issue. Whenever Data Changes: $scope.changeStatus = function($event,label,status){ var target = $event.currentTarget; target ...

Ways to use jQuery to disable row form elements in the second and third columns

I need a way to deactivate form elements in the second and third columns, starting from the second row until the nth row using a jQuery selector. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> ...

The element.find() function is experiencing issues when utilizing a templateUrl within a directive

My aim is to apply focus on an input field using a custom directive within a form. Initially, everything was functioning correctly when utilizing the template property in the directive. However, upon transferring the template into a separate HTML file usin ...

Is it possible to generate a PNG blob using binary data stored in a typed array?

I have a piece of binary data that is formatted as a PNG image. I am looking to convert it into a blob, generate a URL for the blob, and then showcase it as an image in various places where an image URL can be used, such as CSS. My initial approach invol ...

White border appears when hovering over MUI TextField

I've been troubleshooting this issue for what seems like an eternity. I've combed through Chrome's inspect tool, searching for any hover styles on the fieldset element, but to no avail. Here's my dilemma... I simply want a basic outline ...

Here is a unique rewrite of the text: "When a user clicks the 'no' button on the Bootstrap modal box, the

Is there a way to display the checkbox value retrieved from the database? I want to open a bootstrap modal box when the checkbox is clicked. If the user clicks on the "Yes" button, the checkbox value should be saved in the database. However, if the user cl ...

calling object functions using additional parentheses

After reviewing the passport.js documentation, I came across this piece of code: app.get('/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err); } if (!u ...

Ways to change the default blue dropdown color in the Chrome browser

When you click on the drop-down menu, a blue color appears when hovered over. I want it to be red instead, but for some reason, it's not working in Chrome browser. Here is the CSS class that I have used: option:checked { box-shadow: 0 0 10px 100p ...

Unraveling the onsubmit FormObject in an HTML form within the Google Sheets Sidebar: Tips and tricks

In the sidebar, I am setting up a form with 27 inputs to create new sheets based on various criteria. The form includes a text entry field and multiple groups of radio buttons and checkboxes. However, I am currently facing an issue when trying to create a ...

Use JavaScript to identify and color the intersecting area of two triangles that overlap on a webpage

I created two triangular divs using HTML and I am utilizing jQuery UI to enable them to be draggable. Now, my goal is to overlap these two triangles and change the color of the overlapping area to a different shade. Here is an example: https://i.sstatic. ...

Uncovering the secrets of extracting form parameters with jquery.ajax in asp.net

In search of a method to extract form parameters in C# similar to Java's request.getParameter("blah"). I am currently serializing my form with jQuery and sending it to a web method, but I need assistance in extracting the data. Using ajax. $("#btn" ...

My pen doesn't accurately display the ID

CHALLENGE var shoppingCenters = [{ id: 0, name: 'Leclerc', location: 'Paris,France', address:'Boulevard Rahal El Meskini Casablanca Maroc', ] }, { /*Malls B*/ id: 1, name: 'Carefour', location ...

Using PHP, JavaScript, and Bootstrap to display success or error messages for form fields through AJAX

I am working on a form where users input an 'authorisation code' that is then checked against a database using AJAX and PHP. Currently, the form displays a tick if the code is valid and a cross if it is incorrect. I would like to utilize Bootstra ...