Displaying images within a div container in a visually appealing manner across various screen sizes

As a CSS newbie, I am trying to create boxes with headers that have a little gap before the heart image, no matter the box size. Here is what I have attempted so far:

https://i.sstatic.net/AQfib.png

This is my current HTML code:

<div class="block">
  <p><em>04-05</em></p>
  <img class="block_pic" src="..."/>
</div>

Here's the CSS I've been working on:

.block {
  border: solid red;
  width:250px;
  height:150px;
 }

p{
 border-bottom:solid black 2px;
 width:83%;
 margin:5px;
}

.block_pic {
  width:30px;
  position:absolute;
  top: 5.5%;
  right:86.5%;
}

You can view my progress and approach on CodePen. While my method involved trial and error with adjusting positioning numbers, I am curious if there is a smarter way to position the heart image without this guesswork. Any tips for creating responsive boxes of varying sizes would be greatly appreciated!

Answer №1

To achieve the desired layout, place the image before the p element and apply float: right to it. Utilize calc to set the width of the p tags as demonstrated below:

Check out this example on CodePen

.block {
  border: solid red;
  width:250px;
  height:150px;
 }

p{
 border-bottom:solid black 2px;
 width: calc(100% - 45px);
 margin:5px;
}

.block_pic {
  width:30px;
  float: right;
  margin: 5px;
}
<div class="block">
  <img class="block_pic" src="..."/>
  <p><em>04-05</em></p>
</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

Having trouble with the Bootstrap accordion collapse not expanding as expected using CSS

I've been trying to make these collapsibles work, but I'm having no luck. I'm not sure what I'm doing wrong here. I thought it could be my PHP code, but everything seems fine when loading text from the database. Any assistance would be ...

Tips for avoiding duplicate Id tags while using Ajax in MVC

Hello everyone, I am curious to learn about the precautions and techniques you employ when working with Ajax or MVC functions such as @Html.Action in order to effectively manage and prevent duplicate Ids for Html tags. In addition, how does the page handl ...

Steps to deactivate the select element in backbone.js

Can someone help me with disabling the select option in my MVC template code using backbone.js? I tried using readonly="readonly" and disable="disable", but it sends null as value and doesn't update my address. <div class="login-register" data-val ...

The scroll function remains unchanged when using overflow-y scroll and border radius

I need help with styling a div that will contain a large amount of text on my webpage. I want to use the overflow-y property for scroll, but the issue arises when trying to style the scroll bar with a radius. Check out this jsfiddle link for reference: ht ...

Why isn't my API's JSON Response showing up in PHP?

Having trouble displaying the JSON response from an API call in PHP. Despite confirming that the server, IP settings, and API status are all fine on the provider's end, I am unable to identify the problem. This is how I'm handling the PHP part: ...

Adjusting the background hue of a bootstrap panel

I'm attempting to utilize JavaScript to change the background color of a panel in order to create a "selected" effect. Upon clicking, I retrieve the div by its ID and modify its background color. Although it initially works as intended, the backgrou ...

Steps to incorporate the latest Material Design Bottom App Bar into your project

In our company, we are currently utilizing Vue and SCSS for constructing our latest Progressive Web Application. Depending on specific conditions relating to the user's profile, we need to switch out the left drawer with a Bottom App Bar. Although we ...

Is there a way in Ruby to extract the inner HTML of the <title> tag by loading the HTML source code of a webpage into a string and then parsing it?

Currently, I am trying to extract the inner HTML of the <body> tag from an HTML source string using Ruby. Does anyone know how this can be achieved? Despite my attempts to find a solution, I have been unsuccessful so far. Any guidance would be grea ...

Fade out at varying speeds

As I delve into CSS3 animations, I have been experimenting with some example code. Here is the link to the code: http://jsfiddle.net/chricholson/z7Bg6/67/ I am curious about how to adjust the duration of animations when un-hovering. Currently, it seems th ...

Tips on effectively integrating HTML codes within PHP

I have a new idea I'd like to experiment with involving the incorporation of HTML into PHP. My code draft would look something like this: <?php foreach($element as $data) { <div class="view"> echo $data; </div> } ?> Thi ...

Looking to automatically scroll to the bottom by clicking on text using javascript/jquery?

I am currently working on a website project where I have a specific requirement. I need the webpage to scroll towards the bottom when a particular text is clicked, using JavaScript/jQuery. <p class="ml-2 mb-2 d-flex view_profile_options"><a hre ...

Ways to update images without having to reload the page following an ajax request

My current script uploads images to the server and updates the image source in HTML upon completion. However, I am facing an issue with my PHP code renaming the files to predefined names (ext-pix0.png, ext-pix1.png, etc.) during upload. This leads to the p ...

Make sure to load the gif image before any other images are loaded from the css

Hey there, I'm attempting to display a GIF image before the actual image loads. I found a great example to accomplish this: Example: Java Script function loadimage(imgsrc, change){ var loadimage = new Image(); loadimage.onload = changesrc(imgsrc, c ...

Saving the user's data in the database for future reference

I’m new to PHP and I'm experimenting with saving user input into a mysql database. Despite following an online tutorial on how to accomplish this, each time I try to input the user's details, the website informs me of a failure. The only potent ...

Is it permissible to employ CSS pseudo elements for enclosing specific text within brackets?

Is it possible to utilize CSS pseudo elements like ::before and ::after in order to enclose certain text within brackets? For example: Text can be transformed into: (Text) ...

Prevent user input in HTML

Currently, I am working on creating the 8 puzzle box game using JavaScript and jQuery Mobile. The boxes have been set up with <input readonly></input> tags and placed within a 9x9 table. However, an issue arises when attempting to move a box ...

Personalized element IDs and unique CSS styles

After editing the code snippet below... <div id="rt-utility"><div class="rt-block fp-roksprocket-grids-utility title5 jmoddiv"> I applied the changes in my custom.css file like this: #rt-utility .rt-block {CODE HERE} However, when attemptin ...

Troubleshooting error: Uncaught ReferenceError - Unable to display SVG elements using D3 and d3.slider due to undefined svg

Hey there, I'm currently exploring a D3 Example and trying to create my own slider to display data from a JSON file. I've included everything in a GitHub repository to share the working files without taking up too much space. Here's what I h ...

The HTML textarea is not updating properly when using jQuery's keypress event

I am facing an issue with my forms on a webpage, each containing an html textarea: <textarea name="Comment" class="inputTextArea">Hello World!</textarea> There is a javascript event handler that automatically submits the fo ...

Using window.location.href to Submit an Array in ASP.NET MVC

What is the correct approach for sending data to a Controller in order to Post data without using a form? Currently, I am only needing to send an array of parameters. The line of code below is what I am currently using, however, I am aware that it is ty ...