-
Hajipur, Bihar, 844101
In CSS, alignment refers to positioning elements horizontally or vertically in a container.
There are different alignment techniques depending on whether you're aligning:
Text
Block elements
Inline elements
Flexbox/Grid items
Use the text-align
property to align inline content like text, inline images, or inline-block elements inside a block container.
text-align: left | right | center | justify;
<p class="centered-text">This paragraph is centered.</p>
.centered-text {
text-align: center;
}
Use vertical-align
to align inline or inline-block elements vertically relative to the line box.
vertical-align: baseline | top | middle | bottom | text-top | text-bottom;
<img src="icon.png" class="v-align"> Text next to icon
.v-align {
vertical-align: middle;
}
To center a block element (like <div>
) horizontally:
margin: auto
(with fixed width).center-block {
width: 300px;
margin: 0 auto;
}
.parent {
display: flex;
justify-content: center;
}
Flexbox provides powerful alignment options:
display: flex;
justify-content: center; /* horizontal */
align-items: center; /* vertical */
.container {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
}
CSS Grid uses these properties:
justify-items: start | center | end;
align-items: start | center | end;
Q1. Align text to the right inside a <div>
.
Q2. Center a heading inside its container.
Q3. Vertically align an icon next to text.
Q4. Horizontally center a block element with fixed width.
Q5. Center text both horizontally and vertically using Flexbox.
Q6. Create three buttons spaced evenly using Flexbox.
Q7. Align a grid item to the bottom inside a grid container.
Q8. Justify a paragraph to make both edges aligned.
Q9. Use text-align
to center an image inside a block.
Q10. Vertically center two inline-block elements.
Q1: Which property aligns text horizontally?
Q2: Which value of text-align centers the text?
Q3: How do you vertically center content in a flex container?
Q4: Which property aligns block elements horizontally?
Q5: What is the default value of text-align?
Q6: Which value justifies text to both sides?
Q7: Which vertical-align value places the element in the middle?
Q8: What is the use of align-items in Flexbox?
Q9: Which property centers items horizontally in Flexbox?
Q10: How do you align an inline element vertically with vertical-align?