CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Text Align


The text-align property in CSS is used to specify the horizontal alignment of text within its container. This property can be applied to block-level elements to control how the inline content, such as text and images, is aligned. It offers several values to left-align, right-align, center, or justify the text.


Example 1: Aligning Text to the Left

/* This CSS rule targets a paragraph element with the class "left-align" */
.left-align {
  text-align: left; /* Aligns the text to the left side of its container */
  border: 1px solid #ccc; /* Adds a border for visual clarity */
  padding: 10px; /* Adds some space inside the border */
}

Explanation: The text-align: left; declaration forces the text inside any element with the class .left-align to be aligned to the left edge of its parent container. This is the default alignment for text in most web browsers for left-to-right languages.


Example 2: Aligning Text to the Right

/* This rule applies to any paragraph with the class "right-align" */
.right-align {
  text-align: right; /* Aligns the text to the right side of its container */
  border: 1px solid #ccc; /* Visual border to show the container's boundaries */
  padding: 10px; /* Provides internal spacing */
}

Explanation: By setting text-align: right;, the text within the .right-align class is pushed to the right boundary of its container. This is often used for stylistic effect or for languages that are read from right to left.


Example 3: Centering Text

/* This CSS targets paragraphs with the class "center-align" */
.center-align {
  text-align: center; /* Centers the text horizontally */
  border: 1px solid #ccc; /* A border to visualize the element's box */
  padding: 10px; /* Inner spacing for better readability */
}

Explanation: The text-align: center; value horizontally centers the text within its containing element. This is a very common technique for headings and titles to create a symmetrical and focused look.


Example 4: Justifying Text

/* This rule is for paragraph elements with the class "justify-align" */
.justify-align {
  text-align: justify; /* Stretches the lines so that each line has equal width */
  border: 1px solid #ccc; /* Border to illustrate the container's edges */
  padding: 10px; /* Padding for spacing */
}

Explanation: text-align: justify; makes the text spread out to fill the container's width, so both the left and right edges of the text are aligned. This creates a clean, block-like appearance for paragraphs of text.


Example 5: Using text-align with start

/* This CSS rule targets a div with the class "start-align" */
.start-align {
  text-align: start; /* Aligns text to the start based on writing direction */
  direction: rtl; /* Sets the writing direction to right-to-left for this example */
  border: 1px solid #ccc; /* Visual aid to see the container */
  padding: 10px; /* Spacing inside the container */
}

Explanation: The text-align: start; value is direction-sensitive. For left-to-right languages, it behaves like left, and for right-to-left languages (as simulated here with direction: rtl;), it behaves like right.


Example 6: Using text-align with end

/* This rule applies to any div with the class "end-align" */
.end-align {
  text-align: end; /* Aligns text to the end based on writing direction */
  direction: ltr; /* Sets the writing direction to left-to-right */
  border: 1px solid #ccc; /* Border for visualization */
  padding: 10px; /* Internal spacing */
}

Explanation: Similar to start, text-align: end; is also direction-sensitive. In a left-to-right context, it aligns text to the right. If the text direction were right-to-left, it would align the text to the left.


Example 7: Aligning an Image with text-align

/* This CSS rule targets a div with the class "image-align" */
.image-align {
  text-align: center; /* Centers inline-level content, including images */
  border: 1px solid #ccc; /* A border to show the div's boundaries */
  padding: 10px; /* Adds some space around the image */
}

Explanation: The text-align property isn't just for text. It can align any inline-level element within a block container. In this case, text-align: center; is used to horizontally center an image within a div.