What is the best way to center my image on a webpage without having it overlap with the text using HTML and CSS?

I'm struggling to center and make my image responsive without causing text overlap. How can I resolve this issue?

Check out the problem here

div.shadow {
    position: absolute;
    max-width: 45%;
    max-height: 45%;
    top: 50%;
    left:50%;
    overflow: visible;
}
img.logo {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    margin-top: -50%;
    margin-left: -50%;
}
header {
    text-align: center;
    color: black;
    text-decoration: none;
    font-size: 40px;
    font-family: 'existencelight';
    position: fixed;
    top: 0px;
    left: 0px;
    padding: 10px;
    margin: 0px;
    width: 100%;
}

                
<header>
  <h1>Welcome to Nepali Kitchen</h1>
</header>
<div class="shadow"><img class="logo" src="bg3.jpg" /></div>

Answer №1

If you have set your div to position absolute, you can easily adjust the top value as needed.

div.shadow {
 position: absolute;
 max-width: 45%;
 max-height: 45%;
 top: 200px;  /* example using a fixed pixel value */
 left:50%;
 overflow: visible;
}

Alternatively, you could try using

 position: relative;

Answer №2

Consider using that image as a background instead of placing it within the text.

header {
  text-align: center;
  color: black;
  text-decoration: none;
  font-size: 40px;
  font-family: 'existencelight';
  position: fixed;
  top: 0px;
  left: 0px;
  padding: 40px;
  margin: 0px;
  width: 100%;
  background: url('http://kenwheeler.github.io/slick/img/fonz1.png') center top no-repeat;
  background-size: contain;
}
<header>
  <h1>Welcome to Nepali Kitchen</h1>
</header>

You could also place the image behind the text by adjusting the z-index property.

div.shadow {
  position: absolute;
  max-width: 45%;
  max-height: 45%;
  top: 50%;
  left: 50%;
  overflow: visible;
}

img.logo {
  position: relative;
  max-width: 100%;
  max-height: 100%;
  margin-top: -50%;
  margin-left: -50%;
  z-index: -1;
}

header {
  text-align: center;
  color: black;
  text-decoration: none;
  font-size: 40px;
  font-family: 'existencelight';
  position: fixed;
  top: 0px;
  left: 0px;
  padding: 10px;
  margin: 0px;
  width: 100%;
}
<header>
  <h1>Welcome to Nepali Kitchen</h1>
</header>
<div class="shadow"><img class="logo" src="http://kenwheeler.github.io/slick/img/fonz1.png" /></div>

Answer №3

The reason for this issue lies in how your elements are positioned.

If you desire a fixed header, your content must be shifted down by the height of your header. To achieve this, enclose your content within a container and assign it a margin-top equal to the height of your header.

header {
    position: fixed;
    height: 100px;
}

.content-container {
    position: relative;
    margin-top: 100px; 
}

Here is an example of how your HTML structure should look:

<header></header>
<div class="content-container">
</div>

Set the position of your content-container as relative. For centering items, you can utilize flexbox or apply margin: 0px auto;.

Positioning relative means that it's positioned relative to other elements.

Furthermore, here are some suggestions for optimizing your code:

  • Consider using em or rem units for font-size
  • Avoid prefixing your classes with the element (e.g., div.shadow -> .shadow, img.logo -> .logo)
  • Organize your CSS according to the CSS Box Model for cleaner and more readable code.

Your CSS code could follow this format:

.class {
    // Positioning properties
    position: relative;
    top: 0;
    right: 0;
    z-index: 1;

    // Dimensions
    width: 500px;
    height: 500px;

    // Margin
    margin: 0px auto;

    // Border styling
    border: 1px solid blue;

    // Padding
    padding: 2em 0;

    // Styling for content
    color: #676766;
    background: blue;
}

Answer №4

Why the need for such complicated CSS code? Achieving the same result can be done with simpler coding techniques.

<style>
div.shadow {
    width: 100%;
    float: left;
    text-align: center;
}
img.logo {

}
header {
    text-align: center;
    color: black;
    text-decoration: none;
    font-size: 40px;
    font-family: 'existencelight';
    width: 100%;
    float: left;
}
</style>

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

Combining LESS with cultural designations

Currently working on a multi-lingual website project and facing the challenge of switching out CSS based on different languages. I am passing the culture code into the page and exploring ways to use it to reference specific selectors for each language. H ...

How to effectively use graph paper with both major and minor grids in the background?

Looking to create a 100px by 100px image in Inkscape with major lines every 100px and minor lines every 10px. This will help verify viewport size when used as a repeating background. What's the best approach for this? Thinking of using #888888 for ma ...

Is it possible to use CSS to target the nth-child while also excluding certain elements with

How can I create a clear break after visible div elements using pure CSS? Since there isn't a :visible selector in CSS, I attempted to assign a hidden class to the div elements that should be hidden. .box.hidden { background: red; } .box:not(.hid ...

What specific element is being targeted when a directive injects a ViewContainerRef?

What specific element is associated with a ViewContainerRef when injected into a directive? Take this scenario, where we have the following template: template `<div><span vcdirective></span></div>` Now, if the constructor for the ...

Shifting the position of personalized tabs using Jquery

I have created some basic HTML/CSS tabs and I want to move to the next tab by clicking a link at the bottom of every tab. HTML <ul class="tabs"> <li class="active"> <a href="#">Explainer (2mins)</a> <div class=" ...

Ways to stop button from wrapping inside a div

While working on a search page, I encountered an issue where the submit button overlaps with the text input when zooming in due to lack of space. Is there a solution to prevent this overlapping and keep the layout intact? HTML <input type="text> &l ...

invoking a function from an attached HTML file using Angular

How can I invoke a function in Angular using inner HTML? I attempted the following: cellTemplateFunc(cellElement, cellInfo){ var subContainer = document.createElement('div'); subContainer.innerHTML = "<a href='javascript:v ...

Tips for customizing the appearance of dayMouseover in Fullcalendar

While working on my Angular application, I encountered an issue with the Fullcalendar plugin. Specifically, I want to dynamically change the cell color when hovering over a time slot in AgendaWeek view (e.g. 7.30am-8.00am). I am striving for something like ...

The Google Maps V3 API map is not loading completely

I'm currently in the process of developing an application that will display maps showcasing local dining establishments. Although the map does appear, it's taking an excessively long time to load and never fully loads as expected. My deadline i ...

Sloped Divider on the Upper Edge of the Page

I'm currently in the process of developing a new website, and I'm looking to create a unique design for the main navigation bar on the homepage. Here is the ideal layout that I have in mind: https://i.stack.imgur.com/pc8z4.png While I understan ...

Maintain a fixed element and enable the rest of the elements to scroll as the mobile browser address bar collapses while scrolling upwards

Currently facing a challenge where I want the background image to remain static while the address bar and content underneath scroll up. The image occupies 90% of the view height, and although I've prevented it from resizing and jumping when the addres ...

Using JavaScript or JQuery, move an image from one location to another by removing it and adding

Firstly, feel free to check out the JSFiddle example My task involves moving an image with the class "Full" after a div with the class "group-of-buttons" using JavaScript. Here is the snippet of HTML code: <img src="http://i.telegraph.co.uk/multimedia ...

Is there a way to specifically target the MUI paper component within the select style without relying on the SX props?

I have been experimenting with styling the Select MUI component using the styled function. I am looking to create a reusable style and move away from using sx. Despite trying various methods, I am struggling to identify the correct class in order to direct ...

Having trouble running the npm script due to a multitude of errors popping up

I have encountered an issue while trying to run an npm script. I added the script in the `package.json` file multiple times, but it keeps showing errors. I am currently working on building a website and require npm sass for it. However, when I try to run t ...

Getting filenames from a directory using PHP

I'm attempting to retrieve the folder name from my computer using PHP code. After researching on Stack Overflow, I came across the following information: <input type="file" name="folder" webkitdirectory directory multiple/> Directory Chooser i ...

Challenges arise when trying to coordinate scrolling movements between a canvas element and the remaining content on the webpage

I'm currently stuck on a project involving a Three.js canvas with OrbitControls for interactive 3D visualization. The main issue I'm facing is the lack of synchronization between scrolling within the canvas and the rest of the page content. Whil ...

Jquery is failing to handle multiple inputs

I am currently working on a system that can handle multiple Yes/No questions. However, every time I try to submit the form, I encounter an error in the console that I cannot identify, and no data is returned from the query. Previously, I used an Array to s ...

Text that is arranged vertically and adjusts its size and layout

I'm currently facing a challenge with vertical text display Check out my plunker What I'm attempting to accomplish is vertical text where: If a short text is placed inside the vertical text div, it should be aligned in the middle of the div I ...

centered text positioned perfectly on a dynamically adjusting background

Seeking help to center text both vertically and horizontally within a circle background link that needs to be responsive. Setting the line-height equal to the height of the link is not an option, here's my code: html <a class="Previous" href="#"& ...

Even when applying a class, the CSS link:hover style will only affect the initial occurrence

I've hit a wall on this issue and can't seem to find a solution. I styled a few links using classes, but it seems that only the first link is accepting the :hover and :visited state styling. I made sure to use classes to style all of them. Not su ...