How do I center an element within a div with another element nearby?

RedDeadVolvo

I need to center an element within a div while there is another element nearby. When I use text-align center, it places the element close but it's not going exactly where I'd like due to another element nearby. I want the logo element to center exactly while the social element floats 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>

Lal

See this fiddle

To make the logo div centralize irrespective of the div taht holds the social link, you'll have to make the logo div absolute positioned.

Now, to make the absolute positioned logo div centered, you'll have to add left:0;right:0 to the CSS.

The revised CSS is as follows

CSS

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I center this div element?

From Dev

How to center a table element within a div?

From Dev

Center a div element within body

From Dev

How do I center a div in another div?

From Dev

How do you dynamically center a div inside another div element relative to content?

From Dev

How do I center an absolutely positioned element?

From Dev

How do I automatically center a fixed element?

From Dev

How do I center an absolutely positioned element?

From Dev

Center only one element within a div

From Dev

Center align a p element within a div or td

From Dev

Center only one element within a div

From Dev

How do I center an anchor element both vertically and horizontally in a floating div?

From Dev

How to center text in a div element?

From Dev

How do I unwrap this element and append it to another?

From Dev

How do I center this horizontal list within a fixed div?

From Dev

How do I position a <div> element after another class or div which isn't accessible to me

From Java

How can I center an absolutely positioned element in a div?

From Dev

How can I center text inside a div element using CSS

From Dev

How can I center a div element no matter what the resolution

From Dev

How do I keep an element from pushing another element in CSS?

From Dev

How do I center an inline-block element of variable width?

From Dev

How do I vertically center a pseudo element image in CSS?

From Dev

How do I make an element appear in the center of a screen from a link?

From Dev

Place element adjacent to another that is not within the same div-element

From Dev

How do I get an element within a table cell?

From Dev

How do I setup columns in a < td > element within an HTML < table >?

From Dev

How do I insert a node as first element within a for loop?

From Dev

How to center an element based on the parent div?

From Dev

How to center a fontawesome icon inside a div element

Related Related

  1. 1

    How do I center this div element?

  2. 2

    How to center a table element within a div?

  3. 3

    Center a div element within body

  4. 4

    How do I center a div in another div?

  5. 5

    How do you dynamically center a div inside another div element relative to content?

  6. 6

    How do I center an absolutely positioned element?

  7. 7

    How do I automatically center a fixed element?

  8. 8

    How do I center an absolutely positioned element?

  9. 9

    Center only one element within a div

  10. 10

    Center align a p element within a div or td

  11. 11

    Center only one element within a div

  12. 12

    How do I center an anchor element both vertically and horizontally in a floating div?

  13. 13

    How to center text in a div element?

  14. 14

    How do I unwrap this element and append it to another?

  15. 15

    How do I center this horizontal list within a fixed div?

  16. 16

    How do I position a <div> element after another class or div which isn't accessible to me

  17. 17

    How can I center an absolutely positioned element in a div?

  18. 18

    How can I center text inside a div element using CSS

  19. 19

    How can I center a div element no matter what the resolution

  20. 20

    How do I keep an element from pushing another element in CSS?

  21. 21

    How do I center an inline-block element of variable width?

  22. 22

    How do I vertically center a pseudo element image in CSS?

  23. 23

    How do I make an element appear in the center of a screen from a link?

  24. 24

    Place element adjacent to another that is not within the same div-element

  25. 25

    How do I get an element within a table cell?

  26. 26

    How do I setup columns in a < td > element within an HTML < table >?

  27. 27

    How do I insert a node as first element within a for loop?

  28. 28

    How to center an element based on the parent div?

  29. 29

    How to center a fontawesome icon inside a div element

HotTag

Archive