What is the best way to align an element in the middle of a div when there is another element in

I am trying to center an element within a div while another element is positioned nearby. Using text-align: center gets the element close to where I want it, but not exactly due to the presence of another element. My goal is to have the logo element centered perfectly while keeping the social element floating to the right.

#header {
  width: 100%;
  height: auto;
  text-align: center;
  background-color: #ffffff;
}
#logo {
  width: 30%;
  margin: 10px auto;
  display: inline-block;
  text-align: center;
}
#logo img {
  width: 100%;
  height: auto;
  margin: auto;
}
#social {
  width: 15%;
  display: inline;
  float: right;
}
#social img {
  width: 25%;
  display: inline-block;
}
<div id="header">
  <div id="logo">
    <img src="images/final/logo2.png">
  </div>
  <div id="social">
    <img src="images/final/fb.png">
  </div>
</div>

Answer №1

Take a look at this demo

If you want to center the logo div no matter what the surrounding social link div looks like, you'll need to use absolute positioning for the logo div.

To ensure that the absolutely positioned logo div is centered, add left: 0; right: 0; in your CSS.

The updated CSS code should be:

CSS

#logo {
    width: 30%;
    margin: 10px auto;
    position: absolute;
    left: 0;
    right: 0;
}

Answer №2

Consider including the following code snippet:

 #logo {
   position: absolute;
   left: 0;
   right: 0;
   margin-left: auto;
   margin-right: auto;

}

If #logo had a width of 100px, you could also use the following alternative positioning: position: absolute; left: 50%; margin-left: -50px;

Answer №3

Make the social icons centered by adding center tag and removing float:right

    #header {
      width: 100%;
      height: auto;
      text-align: center;
      background-color: #ffffff;
    }
    #logo {
      width: 30%;
      margin: 10px auto;
      display: inline-block;
      text-align: center;
    }
    #logo img {
      width: 100%;
      height: auto;
      margin: auto;
    }
    #social {
      width: 15%;
      display: inline;
    }
    #social img {
      width: 25%;
      display: inline-block;
    }
    <div id="header">
    <center>     
    <div id="logo">
        <img src="images/final/logo2.png">
      </div>
      <div id="social">
        <img src="images/final/fb.png">
      </div>
    </center>     
    </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

Navigate through each element with a specific class name in a block of HTML code using

Currently, I am dealing with an HTML string that is being retrieved through AJAX. Let's assume it looks like this: var htmlString = '<div class="post"></div><div class="post"></div>'; I want to find a way to iterat ...

Transferring information from Node.js to HTML with the help of EJS template engine

Below is the Server Side code I am using: app.set('view engine', 'html'); app.engine('html', require('ejs').renderFile); app.use(express.static('views')); app.use(bodyParser.urlencoded({extended:true})); a ...

Creating visually appealing layouts with CSS floats and divs while ensuring responsiveness

As I work on bringing my vision to life, I'm experimenting with a mix of floats and divs, along with using a responsive image for the background and text. There's quite a bit involved in this process. 1. To start off, check out this example with ...

Sending an array through HTML select using Jquery

Is this scenario possible: I have implemented an Ajax call to fetch results from a SQL query. The data is retrieved in an array format, which I then convert to JSON and output at the end of a PHP script that the Ajax function calls. Next, for each row in ...

The default page length for datatables cannot be applied at this time

I am having trouble with setting the pageLength for a table in this code. Can someone assist me with this issue? $(document).ready( function () { $('#Campaign_Overview').DataTable({ "pageLength": 5, "lengthMenu": [5, 10, 25, 50] }); ...

What's the best way to ensure that all the numbers I input into my calculator app are effectively combined when I press the buttons?

For my very first coding project, I decided to create a calculator app. Despite the abundance of tutorials available online, I wanted to challenge myself by breaking down the problem into smaller steps on my own. However, I've encountered an issue wh ...

svg xlink attribute not functioning properly when embedded as svg object within html

Within the SVG, I have multiple A elements linking to various pages. When the SVG is loaded into a browser as a standalone, the links function properly. However, when it is embedded as an object within an HTML page, the links do not work. It seems like th ...

When the HTML code is rendered in a web browser, the CSS styles are not being

I am utilizing adminer within a docker container. When testing services, I utilize the URL mydomain.tld. In order to interact with this container directly, one option is to expose and map a port, such as mapping port 8081 to the adminer port 8080. Access ...

Designing draggable tags similar to those on LinkedIn, incorporating a parent div element containing an SVG image and text

Lately, I've been exploring the world of CSS and Angular. Can someone give me a jumpstart on using CSS to design an element like the one shown in this https://i.stack.imgur.com/vcR3Z.png image? Essentially, this outer tag consists of a div element th ...

Model OBJ-based color palette

I am looking to dynamically assign colors based on the height of my model, which is loaded as a .obj file. The goal is to have colors transition from green to red based on height, with green representing the minimum point and red representing the maximum ...

What is the process for printing the CSS stylesheet for PPI::HTML highlight?

PPI::HTML has done an impressive job of formatting the HTML highlighting for my Perl code, similar to the example on CPAN. However, I am facing challenges in making the output usable without the appropriate CSS styles. Unfortunately, I am unsure of how to ...

The error message indicates a BadRequestKeyError: KeyError was raised with the specific key 'nume_pacient' missing

When the button is pressed, I need to extract the text from my input with the name "nume_pacient", pass it to a Python code, and use it in a database query. The results should then be displayed on the same page. HTML PAGE: {% extends "base.html" %} {% blo ...

Using JavaScript to open a document file in Chrome at launch

Is it possible to automatically open a doc file stored on my C:// drive when a link is clicked on my HTML page? Here is the code I have been using: <a href="C://mytestfile.doc">Click Me To launch the DOC FILE</a> Unfortunately, instead of op ...

Is it possible to have a PHP variable embedded within an HTML link tag?

<?php $message = ' <a href="' . $link . '">here</a> '; ?> My goal is to include a clickable link in the $message variable for use in the mail() function. However, despite setting $link with the desired link string, it ...

Unresponsive Vanilla Bootstrap Navbar Links

I recently started working with the vanilla version of Bootstrap to create a universal template for a friend's websites. Although I have previous experience with Bootstrap and have never encountered any issues, I am now facing a problem. I want to cla ...

Guide to stacking blocks on top of each other

How can I create even spacing between blocks of different heights? For instance, in the image below, you can see that block 2 should be positioned directly under block 1. https://i.stack.imgur.com/RnYbP.png https://i.stack.imgur.com/LHvlz.png ...

Is there a glitch in the Bootstrap v4 Navbar?

Encountering an issue with the default navbar example from Bootstrap's official website. The navbar is supposed to be full-sized and collapse when the screen size decreases. However, after implementing the code into my project, it appears as always co ...

Tips for incorporating shared attributes into unique server controls derived from diverse parent classes

I am in search of a Server Control (referred to as FormLayout in this instance) that can dynamically render child controls based on their attributes. The objective is to have the ability to input something similar to the following: <custom:FormLayout r ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

React Application - The division is positioned centrally when run on local host, however, it does not retain its center alignment

I'm encountering an issue with deploying my create-react-app on github pages. The first image showcases my deployed app on github pages, but the image is not properly centered on the page as intended. https://i.stack.imgur.com/rOciT.jpg On the othe ...