Place an image on top of a div, aligning it both horizontally and vertically in the center of the div

Looking for a way to achieve this using only CSS rather than JavaScript. I have a horizontal line (div) and would like to center an image both vertically and horizontally on the line.

I attempted to do this with JavaScript, but encountered issues.

Here is some sample HTML code:

<style type="text/css">
#container {
    background-color:#F00;
    height:5px;
    width: 77%;
    margin: 0 auto;
}
</style>

<script>
var logo = document.getElementById('logo');
var container = document.getElementById('container');
var margin = (container.clientHeight - logo.clientHeight) / 2;
logo.style.marginTop = margin + "px";
logo.style.marginBottomn = margin + "px";
</script>

<br><br><br>
<div id="container">
<img src="images/logo_footer.jpg" width="229" height="75"  id="logo" />
</div>

Answer №1

To style the element using CSS, include margin-left: auto; and margin-right: auto; for centered alignment. For the top and bottom margins, specify margin-top: amountpx; and adjust as needed.

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

Experience the simplistic magic of the Vue.js/Vuefire/Firebase app world, though it struggles with reading values

Transitioning to Vue.js from SQL programming has been a bit of a challenge, but I'm getting there. Currently, I am using vuefire according to the specifications and even manually inserting the correct key from my Firebase database with one record and ...

Is it possible to incorporate an external SCSS rule without it showing up in the final output?

Assuming there is an scss file with predefined rules such as .btn {..} or .btn-primary... I aim to create my own rules by building upon the existing ones .my-button { @extend .btn @extend .btn-primary } without directly adding the .btn and .btn-prim ...

Combine array in MongoDB containing nested documents

I need assistance with querying my MongoDB database: Specifically, I want to retrieve data that is nested within an array and filter it based on a specific key within the nested structure. The example document looks like this: [ { "name": ...

IE Script loading

There is a script that I have, it appends in the document like so: window.d = document s = d.createElement('script') s.setAttribute('type','text/javascript') s.setAttribute('src',options.url) d.getElementById ...

Using React in ES5 to create a button that triggers two separate functions with a

I have encountered an issue with two React classes that both contain a render function returning forms with buttons. In class A, I had to import class B in order to use the form from class B. The problem: Whenever I click the inner button, the outer butto ...

Errors encountered while using AngularJS HTTP GET request

I have encountered an issue with my Angular Js app when attempting to retrieve json data from my web server (which is created using codeigniter). It seems that while other urls provide the expected json data response, mine does not. Here is the Angular js ...

Having issues with validating a form using Yup for a Checkbox input?

My form is built using mui, formik, and yup. If the input fields are empty (e.g. "surname") after clicking the submit button, an error is displayed. However, the issue arises when the checkbox for Terms of Service isn't checked as no error shows up. ...

Tips on wrapping a div snugly around an image while also adjusting the image size to fit within the content area if it is larger than the content area

Currently working on developing an image viewer that has specific requirements to meet. Here they are: The content area is defined as a grid-area. If the image is larger than the content area, it must be contained without stretching. If the image is smal ...

.then function not functioning properly in Axios DELETE request in a React project

I am currently facing an issue with calling a function to update the array of notes after deleting a note from the database. The function causing the error is called deleteNote, and the function I intend to call within the .then promise is getNotes. Here i ...

Traversing within an entity

I came across something confusing to me within the for loop: var info = { "full_name" : "John Doe", "title" : "FrontEnd", "links" : { "blog" : "JohnDoe.com", "facebook" : "http://facebook.com/JohnDoe", "youtube" : "http://www.youtube. ...

I wish to trigger the function when the button with the ID "add_city" is clicked instead of it being activated upon pressing the Enter key as it currently is

Is it possible to trigger the function by clicking a button with the id "add_city", rather than just pressing Enter? function createCity(stateId, newCity, event) { if(event.keyCode == 13 || $(this).attr('id') === 'add_city') { i ...

I am currently in the process of cross-referencing the tags that have been retrieved with those that have been selected or created. If a tag does not exist

I have a collection of tags in an object, and I want to dynamically add new tags before submitting the form. Despite using vue watch, it doesn't seem to be working for me. Here is the code snippet: data() { return { blog: { blog_ti ...

Similar to the functionality of $.fn.load but without the need for jQuery

My goal is to dynamically load Jade content into a specific div when a button is clicked. After researching how to achieve this with jQuery, I found several posts recommending the use of the following code snippet: $('#div').load('/somePage& ...

Error encountered when attempting to upload CSV file through HTML form using POST method in Django framework

I have developed a straightforward application that has a single page where users can input a list of zip codes and receive a file containing zip codes within a specific radius of the original list. However, I am facing an issue with the function in my vi ...

The ForbiddenError has struck again, this time wreaking havoc in the realms of Node.js, Express.js

I am currently adapting this GitHub sample application to utilize Express instead of KOA. However, I am encountering an Access Denied issue when the / route in Express attempts to load the index.html. What specific modifications are required in the code be ...

The TypeScript factory design pattern is throwing an error stating that the property does not

While working with TypeScript, I encountered an issue when trying to implement the factory pattern. Specifically, I am unable to access child functions that do not exist in the super class without encountering a compiler error. Here is the structure of my ...

Axios: Actions taken by users prior to logging in

After successfully sending "EnvioLogin" with the correct email/password, I am able to obtain the access token from "/login", but I encounter an issue when trying to access "/users" after that. Upon inspecting in my browser, I found these image links: https ...

Using Material UI's ProgressBar to create a countdown feature in a React

I am working on a feature where pressing a lock button changes the isReserved status of an item to true. This action also creates two new states in Firebase Firestore, namely startDate and expiryDate. The expiryDate is set to be 72 hours after the startDat ...

How to use the handleChange() function in React with various state properties

Explaining a bit about the handleChange function, which takes the name of the state element to be associated with it. Is there a specific reason why it has to be structured like this: handleInputChange(property) { return e => { this.setSta ...

Creating HTML Divs with Equal Heights

I have written this JavaScript code, but I feel like there might be some redundancy. Does anyone have suggestions on how to improve and optimize this code? var boxHeights = []; $('.box').each(function() { boxHeights.push( $(this).outerHeight ...