- How TO — Tabs
- London
- Paris
- Tokyo
- Create Toggleable Tabs
- Example
- London
- Paris
- Tokyo
- Example
- Example
- Fade in Tabs:
- Example
- Show a tab by default
- Example
- Close a tab
- Example
- London
- Табуляция в HTML: 4 способа сделать отступ
- Основные способы сделать табуляцию
- Примеры. Табуляция в HTML
- How do I make a tabbed view in HTML?
- 9 Answers 9
How TO — Tabs
Tabs are perfect for single page web applications, or for web pages capable of displaying different subjects:
London
London is the capital city of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
Create Toggleable Tabs
Step 1) Add HTML:
Example
London
London is the capital city of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
Create buttons to open specific tab content. All elements with class=»tabcontent» are hidden by default (with CSS & JS). When the user clicks on a button — it will open the tab content that «matches» this button.
Step 2) Add CSS:
Style the buttons and the tab content:
Example
/* Style the tab */
.tab overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
>
/* Style the buttons that are used to open the tab content */
.tab button background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
>
/* Change background color of buttons on hover */
.tab button:hover background-color: #ddd;
>
/* Create an active/current tablink class */
.tab button.active background-color: #ccc;
>
/* Style the tab content */
.tabcontent display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
>
Step 3) Add JavaScript:
Example
function openCity(evt, cityName) <
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with and hide them
tabcontent = document.getElementsByClassName(«tabcontent»);
for (i = 0; i < tabcontent.length; i++) tabcontent[i].style.display = "none";
>
// Get all elements with and remove the class «active»
tablinks = document.getElementsByClassName(«tablinks»);
for (i = 0; i < tablinks.length; i++) tablinks[i].className = tablinks[i].className.replace(" active", "");
>
// Show the current tab, and add an «active» class to the button that opened the tab
document.getElementById(cityName).style.display = «block»;
evt.currentTarget.className += » active»;
>
Fade in Tabs:
If you want to fade in the tab content, add the following CSS:
Example
.tabcontent <
animation: fadeEffect 1s; /* Fading effect takes 1 second */
>
/* Go from zero to full opacity */
@keyframes fadeEffect from
to
>
Show a tab by default
To open a specific tab on page load, use JavaScript to «click» on the specified tab button:
Example
Close a tab
If you want to close a specific tab, use JavaScript to hide the tab with a click of a button:
Example
London
London is the capital city of England.
x
Tip: Also check out How To — Vertical Tabs.
Табуляция в HTML: 4 способа сделать отступ
По умолчанию браузеры игнорируют последовательности из нескольких пробелов, учитывая только первый. Однако, в некоторых случаях есть необходимость сделать отступ в HTML документе.
Отдельного символа табуляции в HTML не существует, тем не менее, даже без специального кода или знака табуляции, есть много способов достичь нужного результата.
Основные способы сделать табуляцию
Способы сделать табуляцию в HTML:
- Используя CSS свойство margin-left .
- С помощью спецсимвола неразрывный пробел .
- Несколькими пробелами внутри тега .
- Задать блоку CSS свойство white-space и использовать пробелы.
Примеры. Табуляция в HTML
Способ 1: Делаем отступ, например, 50 пикселей от левого края с помощью свойства CSS margin-left .
Способ 2: Используем специальный символ HTML — неразрывный пробел. Каждый nbsp; равен одному пробелу и не будет игнорироваться браузером. Однако неразрывные пробелы не переносятся на следующую строку. Это следует учитывать, если табуляция делается как отступ внутри текста.
Способ 3: Пишем текст внутри тега . По умолчанию, браузеры не игнорируют пробелы внутри этого тега. Подробнее в статье: Тег HTML предварительно отформатированный текст.
Способ 4: Меняем у блока правило учета пробелов через CSS свойство white-space .
Каждый из этих способов будет работать. Какой больше подойдет в конкретном случае — решать вам. Мне в своей практике доводилось использовать все 4 способа табуляции, приведенные выше. Чаще всего использую CSS отступы и неразрывные пробелы.
How do I make a tabbed view in HTML?
When clicking on tab A, show content for tab A. Click on tab B, show content for tab B, and so on. What’s the most simple and compatible way of constructing a HTML snippet? I don’t mean to use any libraries here, so none of jQuery or any other libraries.
9 Answers 9
If you want to roll your own tab control, you could do something like this:
According to w3.org/TR/2000/WD-DOM-Level-1-20000929/…, there are 12 possibilities. Checking for 1 mainly keeps us from getting the attribute and text nodes that may belong to the tab page divs.
Here is a list of different types of tabs plus tutorials on how to build them
TabTastic is a good guide — it is accessible, and (when JavaScript is not available) fails very gracefully.
If you want to implement your own tab view, just do it like this:
Take a look at an example such as this (courtesy of a Google search for ‘tabbed view javascript’).
You can obviously use this with a little customisation, but it’s instructive to take it apart and determine what it’s doing. It’s basically enabling or disabling using the display style and setting it to block or none
If «simple and compatible» are your only two criteria, I’d suggest the jQuery UI Tabs plugin. Cross-browser, and a cinch to implement.
Depending on your ambitions, it could simply be a matter of an unordered list and a number of s (tab contents). Then a simple JavaScript could — by means of getElementById() — set the display property for all the s: none for all except the current.
Alternatively, you could have a look at this.
Edit: Not the only one linking to the jQuery site, it seems 🙂
The jQuery tabs widget works completely on the browser side — content for all tabs are sent on every request, or you could write JavaScript code that uses Ajax to load the tab contents dynamically.
But it might not be appropriate for your use. Consider if you need to control the tabs server-side (that is, a click on a tab sends a new page request to the server — the server constructs HTML that has the visual appearance of tabs).