text-align PropertyThe text-align property controls the horizontal alignment of text or inline content inside an element.
<p>, <h1>, <div>.<p style="text-align: left;">This text is left aligned.</p>
<p style="text-align: center;">This text is centered.</p>
<p style="text-align: right;">This text is right aligned.</p>
<p style="text-align: justify;">
This paragraph is justified. All lines will stretch to equal width.
</p>
float Propertyfloat moves an element to the left or right of its container. Other inline elements like text will wrap around the floated element.
<img src="pic.jpg" style="float: left; width: 150px;" alt="Sample Image" />
<p>This paragraph will wrap around the image that is floated to the left.</p>
Observation: The image is pushed to left, and paragraph text starts beside it on the right, wrapping neatly.
<div style="float: left; width: 100px; margin: 10px; background: lightblue;">
Box 1
</div>
<div style="float: left; width: 100px; margin: 10px; background: lightgreen;">
Box 2
</div>
Observation: The two colored boxes appear side by side horizontally within the container.
clear: both; which stops elements from flowing next to floats.Example:
<div style="clear: both;"></div>
Observation: This empty block ensures subsequent content starts below the floated elements.
| Property | Purpose | Common Values |
|---|---|---|
| text-align | Aligns text horizontally | left, right, center, justify |
| float | Moves an element left/right and allows wrapping | left, right, none |
float and clearUnderstand how floated elements affect layout and how clearing works.
<div> boxes with float: left each of width 100px and margin 10px.<div>s without clearing.<div style="clear: both;"></div> after the boxes.Scenario: You want the heading of a page to stand out centered on top.
text-align value will you use?text-align: center; to center the heading horizontally on the page.Scenario: You need to place a small image on the right side of a paragraph so that text wraps on left.
float: right; on the image element, so it moves to right and text wraps to its left.Scenario: After floating two boxes left, the footer content is overlapping with the boxes.
<div style="clear: both;"></div> before footer to move it below floated boxes.Scenario: You want a paragraph to have clean edges on both sides by stretching words evenly.
text-align value should you select?text-align: justify; to stretch lines so left and right edges align evenly.Scenario: You have floated several small buttons left to make a toolbar aligned horizontally.
clear: both; after the buttons to avoid wrapping issues.