Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float: left; ambiguity - CSS

Tags:

html

css

Lets take two images as an example

With float: left;

enter image description here

Without float: left;

enter image description here

HTML

<h6><span>Sign Up</span></h6>

CSS

h6:before {
   content: url("images/arrow.png");
   padding-right: 8px;
   float: left; // Here lies problem
}

Question

Why without float: left; text (Signup) goes down ? What's science behind this ?

like image 278
Junaid Avatar asked May 13 '15 09:05

Junaid


People also ask

What does float left do?

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).

How do I make a div float right?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do I put an image left to the right in HTML?

You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.


1 Answers

The default display mode of a pseudo element is display: inline; and the default vertical alignment is vertical-align: baseline;. This means that the image will be aligned to the baseline of the text.

When you float an element its display mode becomes display: block; and it is removed from the document flow. Vertical alignment is no longer a factor and in this case the top edge of the span is now aligned with the top edge of the floated pseudo element.

As Luis P. A. and Paulie_D allude to in the comments, changing the vertical alignment will allow for the non-floated pseudo element to be aligned to the middle of the text:

h6:before {
  content: url("http://placehold.it/20x20");
  display: inline-block;
  padding-right: 8px;
  vertical-align: middle;
}
h6 span {
  vertical-align: middle;
}
<h6><span>Sign Up</span></h6>
like image 177
Hidden Hobbes Avatar answered Sep 20 '22 08:09

Hidden Hobbes