What is the best way to prevent an image element from automatically occupying the entire width of the screen?

I've been struggling with what seems like a simple issue... I have an image with a certain width and assigned a class. However, no matter what I try, the class always takes up 100% of the parent div's width.

HTML:

<div class="parent">
  <%= image_tag("picture.png"), class:"image" %>
</div>

CSS with SASS:

.parent {
  width: 100%;
  position: relative;
  text-align: center;
  .image {
    position: absolute;
    height: 80%;
    max-height: 1000px;
  }
}

After trying various solutions found online including setting .image to width: auto, display:inline, and display:inline-block, I'm still unable to get .image to adjust to the actual width of the image inside it. How can I make .image only as wide as the contained image?

Answer №1

<div class="container">
    <img src="path" class="picture" width="20%" />
</div>

Answer №2

After a bit of trial and error, I discovered that the class needed to be added directly to the image tag instead of the erb string:

<div class="container">
  <%= image_tag ("image.jpg", class:"img") %>
</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

Can an SVG be referenced from another location within the HTML document?

I am dealing with extensive dynamic lists that are associated with a large number of individual (generated) SVGs, each representing stylized acronyms. I prefer not to store the generated SVGs in separate files as it would result in numerous requests. Desp ...

Make a diagonal border using CSS styling

Is it possible to create a slanted border like the one shown in this image (red line) using CSS/CSS3 or JavaScript? <div id="DIV_1"> <img src="/uploads/2016/01/logo.jpg" width="250px" id="IMG_2" alt='' /> <nav id="NAV_3"& ...

Retrieve the HTML content starting from the nth tag and beyond

I am currently working on a table... <table class="col-xs-12"> <thead class="testhead col-xs-12"> <tr class="col-xs-12" id="row1" style="color: white;"> <th>0</th> ...

PHP working in certain directories, but not all

I currently have a few basic html websites that feature a contact form which successfully sends emails using PHP. Recently, I decided to create another website utilizing the same contact form PHP script. However, upon submitting the form on this new websit ...

Automatic Update Division in Jade Template (Node.js/Express)

In my development project using Node.js, Express, Jade, and Redis, I have successfully created an app that pulls telnet stream data from reversebeacon.net. This data is then cross-referenced with Redis databases containing amateur radio club member informa ...

"Customize the dimensions and scroll behavior of a division

I've been struggling to make a single-page site full-width, and I can't seem to get it right. My goal is to create sections that are: Full width Background image full width The height of the div should be at least the same as the image, adjust ...

I'm curious why I can only see the HTML code and not the three.js code as well

I attempted to run a sample three.js game today, but only the HTML code appeared. I've also installed three.js with npm and tried running it with the VSC Live Server, but it's not working. You can find the source code here. What should be my nex ...

CasperJs was unable to locate the CSS selector after transitioning from jade to pug, causing the test to fail

After transitioning my templates from jade to pug, all of my CasperJS tests started failing. The majority of the errors are related to the css selectors that cannot be found. Below is an example of the issue: CasperJS test: casper.thenOpen("http://local ...

"What is the best way to display an image from a table and then enlarge it to full size when a button is clicked, all

1. Understanding the Concept In my page, I have a table listing various images with unique names that are successfully displayed in the table. My goal is to display the selected image in full screen within a popup window when a button is clicked. 2. Que ...

Struggling with creating a CSS navigation bar, having issues with hover functionality

I have a question about the navigation menu I created using CSS. I have included a sub-menu that appears when hovering over the main menu item, along with a triangle created with CSS placed underneath the menu. You can view the sample code here: http://js ...

Creating custom CSS data-tooltip for image maps without the use of jquery

I stumbled upon the Pure CSS Tooltips (data-tooltip) feature showcased at http://www.frequency-decoder.com/demo/css-tooltips/, and I am eager to implement it on a specific area of an image map in a rectangular shape. However, despite finding a JavaScript c ...

Google Maps introduces a new feature that creates a blur effect on

Currently, I am utilizing the sample code provided below to superimpose an element on a Google map. <!DOCTYPE HTML> <html> <head> <title>Google Map Test</title> <style> html, body { margin: 0; ...

Challenges with Enhancing IE8 Performance (Floating content within divs, Styling gradients within headers...)

After running a test on my website using IE8, it appears that several elements are not displaying correctly. You can view the site on a test server: [REDACTED]. The layout looks fine in modern browsers, but when viewed in IE8 (or IE10 with Compatibility Mo ...

Iterating through JSON data and dynamically updating div contents based on the results

A basic HTML page structure is provided below: <li class="slide"> <h1>Hello</h1> <p>This is a test.</p> </li> <li class="slide"> <h1>Hello Again</h1> <p>This is ...

arranging select options in alphabetical order

One of the questions posed to me in an interview was about sorting a select option. The options were listed as follows: <select> <option>aaa</option> <option>ccc</option> <option>ddd</option> <option>bbb< ...

How does Jquery effectively implement filtering with multiple conditions?

Is there a way to make multiple filtering conditions work simultaneously? I am currently able to filter based on individual conditions, but when I input multiple conditions, it only filters based on the last condition specified. <script src="http ...

Issues with CodeIgniter Form Validation Implementation

Having trouble with form validation in CodeIgniter, not sure what's causing the issue! Below is my controller class: class registerController extends MY_Controller { // --- Methods ----------------- function __construct() { par ...

Achieving the perfect alignment for expandable divs next to a consistently centered element using CSS

On my page, I want to ensure that the logo is always perfectly centered both horizontally and vertically when the page loads without any interactions. To achieve this, I used simple margin properties: margin: auto; position: absolute; top: 0; bottom: 0; ...

What is the best way to extract data from two dropdown menus and send it to separate URLs?

I need assistance with extracting the selected year value from the first dropdown. I would like to append this value to the URL of the header page and also to the options in the second dropdown. This way, when I select a PHP page from the second dropdown ...

Having trouble getting SPRITE SVG or SIMPLE SVG to show up when using xlinkHref in a React JSX file

I'm struggling to get the SVG logo to display. I created the SVG using icomoon. I'm trying to use xlinkHref="path/to/my/svg" in my jsx code, but it just won't work. Can anyone point out what I might be missing? Here's the desired effec ...