Secara default bawaan HTML, setiap tabel akan diatur lebarnya secara otomatis dari besar data yang ada didalam tabel tersebut, namun jika kita ingin ‘memaksakan’ web browser untuk menampilkan tabel dengan lebar tertentu, kita bisa menambahkan sebuah atribut width untuk mengatur lebar tabel. Dapat dilihat bahwa web browser menampilkan tabel sesuai dengan panjang data yang terdapat pada baris terpanjang, dan jika anda mencoba mengecilkan jendela web browser, tampilan tabel akan bergeser menyesuaikan dengan lebar web browser. Inilah tampilan default dari tabel HTML. Mengatur Lebar Tabel Dengan Atribut WidthAtribut width dapat digunakan untuk tag tabel (tag table) maupun untuk tag kolom (tag th, tag td, maupun tag col). Jika diletakkan pada tag table, atribut ini berfungsi untuk mengatur lebar tabel secara keseluruhan. Namun jika diletakkan pada tag th atau td, atribut ini akan berfungsi untuk mengatur lebar kolom. Untuk “memaksa” web browser menampilkan lebar tabel sesuai dengan yang diinginkan, kita dapat menambahkan atribut width pada tag table, seperti contoh berikut: Contoh penggunaan atribut width tabel HTML, tabelwidth.html <!DOCTYPE html> <html> <head> <title>Contoh pemakaian attribut width dalam Tabel HTML</title> </head> <body> <h2>Belajar atribut width dalam Tabel HTML</h2> <table border="1" width="600px"> <caption>------------Lebar Tabel 600px--------------</caption> <tr> <th>Judul 1</th> <th>Judul 2</th> <th>Judul 3</th> </tr> <tr> <td>Baris 1, Kolom 1</td> <td>Baris 1, Kolom 2</td> <td>Baris 1, Kolom 3</td> </tr> <tr> <td>Data yang sangat panjang, sehingga merusak tampilan dari tabel</td> <td>Baris 2, Kolom 2</td> <td>Baris 2, Kolom 3</td> </tr> <tr> <td>Baris 3, Kolom 1</td> <td>Baris 3, Kolom 2</td> <td>Baris 3, Kolom 3</td> </tr> </table> </body> </html> Jika anda mencoba mengubah kembali ukuran web browser, tampilan tabel akan tetap, sesuai dengan nilai dari atribut width. Atribut width dapat berisi nilai fixed (tetap), yakni dalam satuan pixel (misalnya: 400px, 600px), maupun nilai relatif dalam bentuk persen (misalnya: 30%, 60%). Seperti yang terlihat, bahwa lebar dari masing-masing kolom diset melalui nilai atribut width. Sebagai catatan, seandainya total lebar dari seluruh kolom (atribut width pada tag th) melebihi lebar tabel (atribut width pada tag table), maka lebar masing-masing kolom akan ‘disesuaikan’ agar tidak melebihi lebar tabel. Sehingga untuk menghindari tampilan yang tidak diinginkan, pastikan agar total lebar kolom tidak melebihi nilai width tabel. Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string. Approach : We will be using the f-strings to format the text. The syntax of the alignment of the output string is defined by ‘<‘, ‘>’, ‘^’ and followed by the width number. Example 1 : For Left Alignment output string syntax define ‘<‘ followed by the width number.
Right Aligned Text0 Right Aligned Text1 Output : Left Aligned Text Example 2 : For Right Alignment output string syntax define ‘>’ followed by the width number.
Right Aligned Text4
Right Aligned Text7 Right Aligned Text1 Output : Right Aligned Text Example 3 : For Center Alignment output string syntax define ‘^’ followed by the width number.
Centered1
Centered4 Right Aligned Text1 Output : Centered Example 4 : Printing variables in Aligned format Centered6 Centered7 Centered8 Centered9 Left Text Centered Text Right Text0 Centered8 Left Text Centered Text Right Text2 Left Text Centered Text Right Text3 Centered8 Left Text Centered Text Right Text5 Left Text Centered Text Right Text6 Left Text Centered Text Right Text7
Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 380 Right Aligned Text1 Output : Left Text Centered Text Right Text Example 5 : Printing out multiple list values in aligned column look. Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 382 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 383 Centered8 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 385 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 386 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 388 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # here 20 spaces are reserved for the 0Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # here 20 spaces are reserved for the 2# here 20 spaces are reserved for the 3
Centered8 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 385 # here 20 spaces are reserved for the 7Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # here 20 spaces are reserved for the 9Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # particular output string. And the string 1Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # particular output string. And the string 3# here 20 spaces are reserved for the 3
Centered8 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 385 # particular output string. And the string 8Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # particular output string. And the string 8Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # is printed on the left side 2Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 # is printed on the left side 4# here 20 spaces are reserved for the 3
Centered8 Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 385 # is printed on the left side 9Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 print 1Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 print 3Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 387 print 5# here 20 spaces are reserved for the 3 |