Position text above an image using CSS

.content {
  float: left;
  width: 33.33%;
  font-weight: bold;
  text-align: center;
  margin-top: 5px;
  margin-bottom: 2px;
  overflow-wrap: break-word;
}

.content_div {
  position: absolute;
  width: 100%;
  left: 0;
  top: 100%;
  font-size: 16px
}

.title {
  float: left;
  width: 100%;
  font-weight: bold;
  text-align: center;
  margin-top: 5px;
  margin-bottom: 2px;
  overflow-wrap: break-word;
}

.title_div {
  position: relative;
  width: 100%;
  left: 0;
  top: 0;
  font-size: 20px
}
<div style="position: relative; float:left;">
  <div class="title_div">
    <p class="title">801 BAOER BLACK ROLLER PEN</p>
  </div>
  <img src="http://52.66.90.164/Show_barcode">
  <div class="content_div">
    <p class="content">52305</p>
    <p class="content">Packet</p>
    <p class="content">MRP: 300</p>
  </div>
</div>

The layout needs to be modified so that the title and content width match the image they are displayed alongside. If the title is removed, the content width adjusts to the image size but not the title. How can I ensure that the title width matches the image size without changing the overall layout? Solutions involving adding an absolute position in the parent div have led to undesired layout changes. Any suggestions on how to resolve this issue?

Answer №1

To resolve this issue, one could consider including the CSS property width: 100%; in the image styling. However, it is worth noting that as VXp mentioned, using inline styles is generally not recommended.

Answer №2

To ensure proper sizing and layout, I have added a fixed width of 285px to the container div (.container). This separation of CSS from HTML allows for better control over styling elements. By setting the container's width equal to that of the image (285px), we avoid any issues with content overflowing and the container expanding uncontrollably.

.container {
  float: left;
  position: relative;
  width: 285px;
}

.product {
  position: relative;
  width: 100%;
  left: 0;
  top: 0;
  font-size: 20px
}

.product > p {
  float: left;
  width: 100%;
  font-weight: bold;
  text-align: center;
  margin-top: 5px;
  margin-bottom: 2px;
  overflow-wrap: break-word;
}

.below-barcode {
  position: absolute;
  width: 100%;
  left: 0;
  top: 100%;
  font-size: 16px
}

.below-barcode > p {
  float: left;
  width: 33.33%;
  font-weight: bold;
  text-align: center;
  margin-top: 5px; margin-bottom: 2px;
  overflow-wrap: break-word;
}
<div class="container">
   <div class="product">
      <p>801 BAOER BLACK ROLLER PEN</p>
   </div>
   <img src="http://52.66.90.164/Show_barcode"> 
   <div style="position:absolute; width:100%; left:0; top:100%;font-size:16px">
      <p style="float:left;width: 33.33%;font-weight:bold;text-align:center;margin-top:5px;margin-bottom:2px;overflow-wrap: break-word;">52305</p>
      <p style="float:left;width: 33.33%;font-weight:bold;text-align:center;margin-top:5px;margin-bottom:2px;overflow-wrap: break-word;">Packet</p>
      <p style="float:left;width: 33.33%;font-weight:bold;text-align:center;margin-top:5px;margin-bottom:2px;overflow-wrap: break-word;">MRP: 300</p>
   </div>
</div>

I trust this adjustment will be beneficial for your design needs.

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

URLConnection is unable to fetch the entire HTML content

When trying to extract information from a website, I encountered an issue where the content was incomplete if the HTML context was too lengthy. The total length of the retrieved string is approximately 40000, but the count of the string retrieved varies ea ...

JavaScript Brainfuck Compiler

I successfully created a BrainFuck compiler in JavaScript, which functions perfectly with this input: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.--- ...

Creating a stylish diagonal background with CSS has never been easier!

Can CSS be utilized to achieve the design effect depicted in the image below? https://i.sstatic.net/o1k7O.png I am seeking to generate <div> elements with a diagonal split background featuring a solid color on one side and white on the other. ...

enabling the editing of two input fields by clicking on a button

Looking to make two input fields editable with a button click. Once edited, the data from these fields will be sent to separate columns in a database using ajax. Struggling to find a solution through online search. Any advice is appreciated! ...

Error loading CSS file on Google App Engine

I attempted to add a CSS file as a static file in my project just to experiment with how it functions, but encountered difficulties right from the start. The content of the CSS file is: body { background:#00FF00; } In addition, here is my configurat ...

Trigger a notification from one webpage to another (PHP, JavaScript, HTML)

I'm currently working on a website that consists of both a receptionist page and a user page with multiple logins. The receptionist page displays a table listing all logged-in users, including their status (either ready or busy). This table is refresh ...

Tips for sending multiple JSON objects using the ajax() method

As of now, I have successfully implemented a $.getJSON() call to fetch a JSON string from a specific URL. Here is the simplified code that is currently functioning well: $.getJSON("myURL",function(data){ var receivedJSON = data; }); The retrieved JSO ...

Designing a book-style layout using jQuery

I am currently working on a web application that needs to display its data in a book-like format. The structure of the data is as follows: Menu{ String menuName; List<String> subMenu; } To achieve this, I utilized jQuery to dynamically generate ...

The Node.js error message "Module not found"

Currently, I am working through the node.js tutorial on Lynda.com and encountering an issue with the "Error: Cannot find module". Despite having the flight module listed in the package.json file, the error persists. Up until this point, everything has bee ...

Utilize the context API to efficiently share information from the parent to both its children and sibling children

I'm currently working on displaying fetched data in a child component using the context API, but encountering an error in the browser: TypeError: render is not a function The above error occurred in the component: in AppDataList (at App.j ...

Performance of Bloom Effect in Three.js

The frame rate ranges from 50 to 60fps without the bloom effect being applied. renderer.render( scene, camera ); However, once the bloom effect is added, the frame rate drops significantly to just 10fps. camera.layers.set( BLOOM_SCENE ); bloomComposer.r ...

The JSON field is missing from the GET response in mongoose

I am dealing with a JSON document (found in mongolab): { "_id": { "$oid": "566e8673eb862d165fef7171" }, "street": "Dizingof", "buildingNumber": 33, "apartmentNumber": 63, "beds": 3, "owner": { "_id": { ...

Best Practices for Laravel and Vue Collaboration

Currently, I am in the process of developing a straightforward invoice system using Laravel. In order to facilitate editing, I need to create a view that consists of a table where rows can be dynamically added and removed. I initially considered using jQu ...

The concept of immutability is crucial when utilizing a for-in loop to append an object to an array

Within my code, I have a nested loop structure consisting of a for of with a for in loop inside it. This setup retrieves information from a Neo4J database. By utilizing the Object.assign method, I am able to transfer a property from the fetched object into ...

Tips for getting rid of the excess space surrounding an image within a div tag

Placing images inside bootstrap card divs within col divs is my current challenge <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"& ...

Display debugging information in the console using bunyan

child.log.info('info'); child.log.debug('debug'); When running the command: node app.js | bunyan -o short -l debug I am encountering an issue where only INFO messages are displayed and DEBUG messages are not showing up. I want to be ...

The presence of the WordPress admin bar results in extra whitespace on the page, while including

In the process of creating my very first custom WordPress theme, I encountered an issue with whitespace at the top of the body. This was caused by calling wp_head(); in header.php and including the header on each page. To offset this whitespace, I added a ...

Restricting the scope of ngClick in multiple instances with AngularJS

I'm working on developing a directive that can toggle an element open or closed when a specific link is clicked. The issue I'm facing is that, upon clicking one trigger, all instances are being toggled open instead of just the intended one. If y ...

Encountering issues with accessing 'this' items in the confirmation function of Angular Material's $mdDialog

Attempting to utilize the confirm function within an Angular Material $mdDialog in order to clear an array and then log it to the console is proving problematic. There appears to be an issue with accessing 'this' objects, arrays, expressions, and ...

What could be causing the delay in response times from these unpredictable APIs within the Express server?

We are currently experiencing performance issues with our Express.js server and MongoDB database. Randomly, some API requests are taking too long to respond or timing out throughout the day. Our application is in production, hosted on two AWS instances wit ...