Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center content next to a floated element and relative to the containing element?

I have markup that looks like this

<div>
  <h1 style="text-align:center;" >Heading 1</h1>
  <img style="float:left;" src="logo.gif"/>
  <h1 style="text-align:center;" >Heading 2</h1>
</div>
<div>
  Content goes here
</div>

The problem is that heading 2 is centered relative to the remainder of space after the image, and not to the whole div, so its not centered on the page.

If I remove the img from the flow with position:absolute, it does not push down the content and instead overlaps it.

like image 540
Matt Avatar asked Nov 18 '10 10:11

Matt


People also ask

How do you center a floated element in CSS?

The CSS float property is used to set or return the horizontal alignment of elements. But this property allows an element to float only right or left side of the parent body with rest of the elements wrapped around it. There is no way to float center in CSS layout.

How do I center text in a float?

To center the text using float we can use margin-left or margin-right and make it 50% , like below. You can learn more about the uses of Float here.

How do you center an element within another element?

Use the "inline-block" value of the display property to display the inner <div> as an inline element as well as a block. Set the text-align property on the outer <div> element to center the inner one. This property only works on inline elements.

How do I center a floated div?

You can set float div center by using margin property. you does not need to use float property but you can use margin property and set left or right margin value auto by this way.


2 Answers

One way is to add a right padding to the div with the size of the logo:

<div style="padding-right: 50px;">
  <img style="float:left;" src="logo.gif"/>
  <h1 style="text-align:center;" >Heading</h1>
</div>
<div>
  Content goes here
</div>

Another way is to remove the heading from the flow. This only works on the assumption that the logo height is bigger than the heading height. Beware also that image and heading could overlap.

 <h1 style="position: absolute; width: 100%; text-align:center;">
    Heading
</h1>
<img style="float:left;" src="logo.gif"/>
<div style="clear:both;">
  Content goes here
</div>
like image 82
littlegreen Avatar answered Sep 21 '22 12:09

littlegreen


Solved it through trial and error. I don't know why but in my testing it only works if width is set between 12 and 80%.

So it seems "h1" is a block element, and text-align does not center block elements, it only centers inline elements inside it, which explains the "centered off-center" behavior. So it turns out the answer is the same answer to the question "how do you center a block element?"

<div>
    <h1 style="text-align:center;">Heading 1</h1>
    <img style="float:left;" src="logo.gif"/>
    <h1 style=" 
             text-align:center;
             margin-left:auto;
             margin-right:auto;
             width:50%;
      ">Heading 2</h1>
</div>
<div style="clear:both;">
  Content goes here
</div>
like image 39
Matt Avatar answered Sep 21 '22 12:09

Matt