Width and Height

Width and Height అంటే ఏమిటి?

CSSలో width property ఒక element ఎంత వెడల్పుగా ఉండాలో నిర్ణయిస్తుంది. height property ఆ element ఎంత ఎత్తుగా ఉండాలో నిర్ణయిస్తుంది.

ఈ propertiesను ఉపయోగించి card, box, image container, form area వంటి elements పరిమాణాన్ని నియంత్రించవచ్చు.

Syntax

.box {
  width: 320px;
  height: 150px;
}

width మరియు height valuesకు సాధారణంగా px, %, vw, vh వంటి units ఉపయోగిస్తాం. auto ఉపయోగిస్తే browser content లేదా parent element ఆధారంగా పరిమాణాన్ని నిర్ణయిస్తుంది.

ఎలా పనిచేస్తాయి?

  • width: 300px; element వెడల్పును 300 pixelsగా చేస్తుంది.
  • height: 120px; element ఎత్తును 120 pixelsగా చేస్తుంది.
  • width: 80%; parent element వెడల్పులో 80 శాతం తీసుకుంటుంది.
  • height: auto; contentకు సరిపోయే ఎత్తును browser నిర్ణయిస్తుంది.

Fixed size కావాల్సిన card లేదా bannerకు pixels ఉపయోగించవచ్చు. Parent sizeకు అనుగుణంగా మారే layoutకు percentage లేదా viewport unit ఉపయోగించవచ్చు.

గమనించాల్సిన విషయం

Elementలోని text నిర్ణయించిన height కంటే ఎక్కువగా ఉంటే text బయటకు కనిపించవచ్చు లేదా layoutలో ఇబ్బంది రావచ్చు. అందువల్ల fixed heightను జాగ్రత్తగా ఉపయోగించాలి. Content పరిమాణం మారవచ్చని అనుకుంటే height: auto; తరచుగా ఉపయోగకరంగా ఉంటుంది.

Simple Explanation

ఒక boxను ఇంట్లోని గది లాగా ఊహించండి. width గది వెడల్పు, height గది ఎత్తు. CSSలో valueతో పాటు unit కూడా ఇవ్వాలి. ఉదాహరణకు width: 300px; అంటే box వెడల్పు 300 pixels. height: 120px; అంటే box ఎత్తు 120 pixels.

Real-Life Example

ఒక product cardను తయారు చేస్తున్నప్పుడు ప్రతి card ఒకే పరిమాణంలో కనిపించాలి. అందుకోసం cardకు width: 280px; మరియు height: 180px; ఇవ్వవచ్చు. ఇలా చేయడం వల్ల cards ఒకే రీతిగా కనిపిస్తాయి.

Code Examples

Width మరియు Heightతో Box పరిమాణం

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Width and Height</title>
  <style>
    .card {
      width: 320px;
      height: 160px;
      background-color: #e8f1ff;
      border: 2px solid #2563eb;
      padding: 20px;
    }

    .card h2 {
      margin-top: 0;
      color: #123b75;
    }
  </style>
</head>
<body>
  <section class="card">
    <h2>CSS Card</h2>
    <p>This card has a fixed width and height.</p>
  </section>
</body>
</html>

ఈ ఉదాహరణలో .card elementకు 320px width మరియు 160px height ఇచ్చాం. Browser ఆ sectionను నిర్దిష్ట పరిమాణంలో చూపిస్తుంది. Padding మరియు border వల్ల లోపల contentకు స్థలం కూడా ఉంటుంది.

Browserలో లేత నీలం background, నీలం border ఉన్న card కనిపిస్తుంది. Card యొక్క CSS width 320 pixels మరియు height 160 pixelsగా ఉంటుంది. Cardలో CSS Card అనే heading మరియు ఒక paragraph కనిపిస్తాయి.

Line-by-Line Explanation

.card {
ఇది card class ఉన్న elementను select చేస్తుంది. అందువల్ల ఈ rules ఆ elementకు మాత్రమే వర్తిస్తాయి.
width: 320px;
Card యొక్క widthను 320 pixelsగా నిర్ణయిస్తుంది. Browser cardకు ఈ వెడల్పును ఇస్తుంది.
height: 160px;
Card యొక్క heightను 160 pixelsగా నిర్ణయిస్తుంది. Content కోసం cardలో నిర్ణయించిన ఎత్తు ఉపయోగించబడుతుంది.
background-color: #e8f1ff;
Card backgroundకు లేత నీలం రంగు ఇస్తుంది. ఇది width లేదా heightను మార్చదు; cardను కనిపించేలా చేస్తుంది.
border: 2px solid #2563eb;
Card చుట్టూ 2 pixels మందమైన border ఇస్తుంది. Border card యొక్క బయట భాగంలో కనిపిస్తుంది.
padding: 20px;
Card లోపల content మరియు border మధ్య 20 pixels ఖాళీ ఇస్తుంది. అందువల్ల text borderకు అంటుకోకుండా కనిపిస్తుంది.
}
ఇది .card rule blockను ముగిస్తుంది.


ఈ HTML sectionకు card class ఇస్తుంది. అందువల్ల .card CSS rules ఈ sectionకు వర్తిస్తాయి.

CSS Card


Cardలో ఒక headingను చూపిస్తుంది. ఇది card యొక్క నిర్ణయించిన పరిమాణం లోపల కనిపిస్తుంది.

This card has a fixed width and height.


Cardలో వివరణాత్మక textను చూపిస్తుంది. ఈ content card యొక్క width మరియు heightలోపల ఉంచబడుతుంది.

Try It

Try this CSS code:

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Try Width and Height</title>
  <style>
    .box {
      width: 280px;
      height: 140px;
      background-color: #fff3cd;
      border: 2px solid #d39e00;
      padding: 16px;
    }
  </style>
</head>
<body>
  <div class="box">
    <h2>Try It</h2>
    <p>Change the width and height values.</p>
  </div>
</body>
</html>

Try It Yourself »

Practice

Practice: ప్రాక్టీస్: .profile-card యొక్క widthను 360pxగా, heightను 180pxగా మార్చండి. Browserలో card పరిమాణం ఎలా మారిందో గమనించండి.

Hint: width: 240px; స్థానంలో width: 360px; మరియు height: 120px; స్థానంలో height: 180px; రాయండి.

Common Mistakes

విద్యార్థి width: 300; అని unit లేకుండా రాస్తాడు.
Pixels కోసం width: 300px; అని unit తప్పనిసరిగా రాయాలి. Zero valueకు unit అవసరం లేకపోవచ్చు, కానీ ఇతర valuesకు unit ఇవ్వడం సరైన పద్ధతి.
విద్యార్థి heigth అని property పేరు తప్పుగా రాస్తాడు.
సరైన property పేరు height. CSS property spelling తప్పుగా ఉంటే browser ఆ ruleను అమలు చేయదు.
Fixed heightలో ఎక్కువ text ఉంచి text మొత్తం కనిపిస్తుందని అనుకుంటాడు.
Content height కంటే ఎక్కువైతే textకు స్థలం సరిపోకపోవచ్చు. Content మారే అవకాశం ఉంటే heightను తగినంతగా పెంచండి లేదా height: auto; ఉపయోగించండి.
Width లేదా heightను తప్పు selectorకు apply చేస్తాడు.
HTML elementలో ఉన్న class పేరు మరియు CSS selector పేరు ఒకేలా ఉన్నాయో తనిఖీ చేయండి. ఉదాహరణకు class="profile-card" కోసం .profile-card ఉపయోగించాలి.

Interview Questions

CSSలో width property ఉపయోగం ఏమిటి?

Answer: width property ఒక element యొక్క వెడల్పును నిర్ణయిస్తుంది. ఉదాహరణకు width: 300px; elementను 300 pixels వెడల్పుగా చేస్తుంది.

CSSలో height property ఉపయోగం ఏమిటి?

Answer: height property ఒక element యొక్క ఎత్తును నిర్ణయిస్తుంది. ఉదాహరణకు height: 150px; elementకు 150 pixels ఎత్తు ఇస్తుంది.

width మరియు height valuesకు unit ఎందుకు అవసరం?

Answer: Browser valueను ఎలా కొలవాలో తెలుసుకోవడానికి unit అవసరం. px, %, vw, vh వంటి units ఉపయోగించవచ్చు.

Exam Questions

ఒక box వెడల్పును 400 pixelsగా, ఎత్తును 200 pixelsగా చేయడానికి CSS రాయండి.

Answer:

.box {
width: 400px;
height: 200px;
}

height: auto; అంటే ఏమిటి?

Answer:

Browser content లేదా అందుబాటులో ఉన్న layout ఆధారంగా element heightను స్వయంగా నిర్ణయిస్తుంది.

ఒక elementకు width: 50%; ఇచ్చినప్పుడు సాధారణంగా దాని అర్థం ఏమిటి?

Answer:

Element తన parent element యొక్క వెడల్పులో 50 శాతం వెడల్పు తీసుకుంటుంది.

Quick Revision

  • width element యొక్క వెడల్పును నిర్ణయిస్తుంది.
  • height element యొక్క ఎత్తును నిర్ణయిస్తుంది.
  • Pixels ఉపయోగించినప్పుడు value తర్వాత px రాయాలి.
  • width మరియు heightను class selector ద్వారా HTML elementకు apply చేయవచ్చు.
  • Content మారే అవకాశం ఉన్నప్పుడు fixed heightను జాగ్రత్తగా ఉపయోగించాలి.
  • auto valueను browser స్వయంగా పరిమాణాన్ని నిర్ణయించడానికి ఉపయోగించవచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top