Oracle Content Management 提供段落元件內響應式表格的 CSS 範例,在行動裝置上顯示時可啟用資料列堆疊處理。
響應式表格可根據螢幕大小調整表格以有效地顯示內容。例如,5 欄表格可在網頁上以水平方式正確地顯示,但在電話上檢視時,資料可能最好以堆疊形式呈現。請注意,響應式表格需要有標頭資料列才能正常運作。
在下列產生的 HTML 中,請注意,已對具有符合資料欄標頭文字值的每個表格儲存格新增資料標籤屬性。
<thead> <tr> <th scope="col">ACCOUNT</th> <th scope="col">DUE DATE</th> <th scope="col">AMOUNT</th> <th scope="col">MINIMUM</th> <th scope="col">PERIOD</th> </tr> </thead> <tbody> <tr> <td data-label="ACCOUNT">Visa</td> <td data-label="DUE DATE">04/16/2020</td> <td data-label="AMOUNT">$3,090</td> <td data-label="PERIOD">03/09/2020 - 04/08/2020</td> </tr>
每個儲存格都有該屬性之後,TD 會在套用下面的 CSS 規則時彼此堆疊。
.scs-paragraph:not(.scs-paragraph-edit) table td { border-bottom: 1px solid #ddd; display: block; text-align: right; } .scs-paragraph:not(.scs-paragraph-edit) table td::before { content: attr(data-label); float: left; font-weight: bold; }
請注意,下方的媒體規則會查詢螢幕大小,且只有在螢幕大小小於 767 像素時才會生效:
@media screen and (max-width: 767px) {
使用 CSS 讓插入的表格成為響應式表格的程式碼範例位於所提供 StarterTheme 的預設 design.css 中。如果從 StarterTheme 建置網站,插入至段落槽位的表格預設為響應式表格。若要插入表格:
完成時,請切換回檢視模式,並選取螢幕寬度小於 767 像素的檢視選項來預覽結果。您必須處於檢視模式,因為表格在編輯模式時不能以響應式的方式運作。
如果您想要在從不同主題建置網站時使用響應式表格,則需要將 StarterTheme design.css 中的程式碼複製到您目前使用之主題的 design.css 檔案。
/** * An example CSS of how to render a table responsively. * It enables stacking of row data on mobile devices. * Only do this for view mode (not for edit mode). * * On each cell rendered, it adds a user-defined attribute * 'data-label' with value matching the column header text. */ @media screen and (max-width: 767px) { .scs-paragraph:not(.scs-paragraph-edit) table { border: 0; } .scs-paragraph:not(.scs-paragraph-edit) table caption { font-size: 1.3em; } .scs-paragraph:not(.scs-paragraph-edit) table thead { border: none; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; } .scs-paragraph:not(.scs-paragraph-edit) table tr { border-bottom: 3px solid #ddd; display: block; margin-bottom: .625em; } .scs-paragraph:not(.scs-paragraph-edit) table td { border-bottom: 1px solid #ddd; display: block; text-align: right; } .scs-paragraph:not(.scs-paragraph-edit) table td::before { content: attr(data-label); float: left; font-weight: bold; } .scs-paragraph:not(.scs-paragraph-edit) table td:last-child { border-bottom: 0; } }