Position the image in a responsive manner relative to the text

I've successfully centered my h1 in a div using the following CSS properties:

text-align: center;
line-height: 150px;
vertical-align: top;

Now, I'm facing a challenge where I want to position an image in the top-left corner of the text. I want it to remain fixed in that position regardless of window size changes. However, when I tried using position: absolute / fixed for the image and adjusting the top and left percent values, the image moved relative to the text (h1) when the window size changed.

As a beginner in CSS and HTML, I'm struggling to understand what's happening on the screen and how to solve this issue.

.container {
  margin: auto;
}
.logo {
  height: 150px;
  text-align: center;
  line-height: 150px;
  vertical-align: top;
  font-size: 50px;
  background-image: url(../img/forest.jpeg);
  background-position: 30% 20%;
}
.logo h1 {
  font-family: Coiny-Regular;
  color: skyblue;
}
.logo img {
  position: fixed;
  height: 2.5em;
  top: 7%;
  left: 30%;
}
<div class="container">
  <div class="logo">
    <h1> Example </h1>
    <img src="img/image.png" />
  </div>
</div>

Answer №1

The img:

  • set the style to position: absolute
  • insert it within the h1

The h1:

  • apply display: inline-block;
  • ensure it has position: relative (to make the img's absolute position relative to the h1)

The display: inline-block; on the h1 will adjust its bounding box to match the content, enabling the image's absolute position to remain precise.

.container {
  margin: auto;
}
.logo {
  height: 150px;
  text-align: center;
  line-height: 150px;
  vertical-align: top;
  font-size: 50px;
  background-image: url(../img/forest.jpeg);
  background-position: 30% 20%;
}
.logo h1 {
  font-family: Coiny-Regular;
  color: skyblue;
  position: relative;
  display: inline-block;
}
.logo img {
  position: absolute;
  top: 0;
  left: 70px;
}
<div class="container">
  <div class="logo">
    <h1> Example
      <img src="http://placehold.it/50" />
    </h1>
  </div>
</div>

Answer №2

Based on your question, it seems like you're looking to have text positioned in the center of a div with a floating image in the top left corner that remains fixed even if the text's position changes.

.content {
  width: 400px;
  height: 80px;
  border: 2px solid blue;
  display: inline-block;
  text-align: center;
}
.content img {
  float: left;
  padding: 5px;
}
.content h1 {
  padding-right: 45px;
}
<div class="content">

  <img src="http://www.techinsights.com/uploadedImages/Public_Website/Content_-_Primary/Teardowncom/Sample_Reports/sample-icon.png" height="40px" width="40px">
  <h1>Sample Text</h1>

</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

What is the best way to select an element based on its relationship to another Element object using a selector?

I am currently developing a small library in which I require the ability to select a relative element to the targeted element using the querySelector method. For instance: HTML <div class="target"></div> <div class="relative"></div& ...

Achieving perfect alignment of two elements using flexbox: centering one and aligning the other to the right

Can you help me with aligning two elements to the center and right edge using flex? I am trying to achieve a layout similar to the image. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX https://i.sstatic.net/cvmHX.png CSS Styles .flex{ display: flex; justify- ...

My vanilla JavaScript code isn't running the smooth scroll function as expected

Struggling to incorporate smooth scroll on Safari using Vanilla js. Despite following the documentation to a tee, the implementation is still not functioning as expected. Outlined below is the vanilla.js code: (function() { scrollTo(); })(); function ...

Having trouble with contact form functionality, developed with AJAX, PHP, and HTML5

I am currently in the process of creating a functional contact form for my website to streamline communication with potential clients who are interested in design services. Although I have never developed an operational contact form that sends emails, I c ...

There seems to be an issue with how Gmail is rendering HTML text

I developed a script that automatically sends HTML email messages to users. However, I've encountered an issue with the font color in Gmail. The first message displays correctly, but subsequent messages in the conversation appear in purple. This prob ...

Is there a way for me to remove an uploaded image from the system

Here is an example of my HTML code: <input type='file' multiple/> <?php for($i=0;$i<5; $i++) { ?> <div class="img-container" id="box<?php echo $i ?>"> <button style="display: none;" type="submit" cl ...

Transitioning one element's opacity to zero while concurrently increasing another element's opacity to replace it

There are a total of 3 div elements and 3 links included in this setup. The goal is to only display one div at any given time. Upon clicking on a link corresponding to a different div, the current one should fade out while the selected one fades in seamle ...

Make a div expand to its maximum width

I have utilized bootstrap classes and div elements to arrange the two divs in the layout shown in the image. The upper div, containing icons, does not extend all the way to the edge of the screen like the lower div. What could I be doing wrong? Additionall ...

Guide to importing an external JSON file into a JavaScript-based quiz on an HTML webpage

I am currently diving into the world of JavaScript and trying my hand at creating a quiz. Check out my simple quiz here Here are my specific inquiries: Is there a way to save the questions and answers in an external JSON file? Can I use a different fil ...

The error message "require is undefined for Electron"

I am developing an application that requires access to the file system (fs) module. Despite having nodeIntegration enabled, the renderer is throwing this error: Uncaught ReferenceError: require is not defined Many similar issues I researched suggested tu ...

Can CSS blend with elements beyond the stacking context?

I am currently in the process of constructing a "hero area" for my webpage through a CMS. This hero area will feature a background image, text content, and a couple of button links. My goal is to have the buttons utilize the mix-blend-mode:multiply propert ...

Trouble with CSS positioned image rendering in Internet Explorer? Z-index not solving the issue?

I've been working on a timeline project where the completed sections should have a green background with a check mark image in the foreground. It appears correctly in Firefox, Chrome, and Safari. However, in IE 7 and 8 (and possibly 9), the layout is ...

Footer not positioned at the bottom of the page

I have been experimenting with different ways to keep my footer at the bottom of the page. Initially, I tried using position: static, but the footer ended up above the bottom of the page and the background was visible (similar to the image below). Then, I ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

Setting a background image in vanilla-extract/css is a straightforward process that can instantly enhance

I am brand new to using vanilla-extract/CSS and I have a rather straightforward question. I am attempting to apply a background image to the body of my HTML using vanilla-extract, but I am encountering issues as I keep getting a "failed to compile" error. ...

Place the button inside the form following another block element

Here is the HTML code I am working with: <div class="actions"> <input id="submit-car" name="commit" type="submit" value="Добавить объявление"> </div> and here is a fiddle example: http://jsfiddle.net/348U4/ In ...

What is the best way to duplicate and delete the final child element using jQuery?

Can someone help me troubleshoot this code that is supposed to clone and remove the last child of a ul element? It doesn't seem to be working as expected. $(document).ready(function () { var tmp1 = ''; var tmp = '<ul>< ...

Personalize the bootstrap-vue styling within your Nuxt project

Currently, I am working on a nuxt project and utilizing bootstrap-vue as a styling module for my components. I am unsatisfied with the default styles provided and wish to incorporate some customization. Specifically, I aim to modify the appearance of the ...

What is the best way to include a component within a content-editable div in Vue.js?

Looking for the correct way to add a div inside a content-editable div on a click of a button in vue js. Here's the code I have been experimenting with: var ComponentClass = Vue.extend(AddTag) var instance = new ComponentClass({ propsData: { type: ...

The initial POST request is causing the hidden fields to have incorrect values

I created a user interface where users can log the time spent on various activities using an HTML form. The view displays a list of activities and generates a log time form (from the _LogTime partial view) for each activity. The only information passed to ...