- How to Make a Div Vertically Scrollable
- Create HTML
- Add CSS
- Example of making a vertically scrollable using the overflow-x and overflow-y properties:
- Result
- Example of making a vertically scrollable using the overflow property:
- Add scroll to html
- В браузере — как можно увидеть все значения прокрутки?
- Прокрутка «overflow: auto»
- Результат вывода прокрутки в html:
- Прокрутка «overflow: scroll»
- Результат использования прокрутки «overflow: scroll;»
- Использовать горизонтальную или вертикальную прокрутку
- Разберем пример выводи одного типа прокрутки.
- Пример только одной прокрутки — горизонтальной:
- Результат вывода — только горизонтальная прокрутка
- Почему не работает прокрутка?
How to Make a Div Vertically Scrollable
CSS allows us to make a vertically scrollable. It can be easily done by using the overflow property. The overflow property has different values. E.g., overflow: auto; and an axis hiding procedure like overflow-x: hidden; and overflow-y: auto; will make a bar scrollable vertically and horizontally, and the «auto» value will add only a vertically scrollable bar.
For a scrollable bar, use the x and y-axis. Set the overflow-x: hidden; and overflow-y: auto; to automatically hide the horizontal scrollbar and show a vertical scrollbar.
Let’s see an example, where the is vertically scrollable.
Create HTML
body> h2>W3docsh2> div Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when a n unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. div> body>
Add CSS
- Set the background-color, width, and height properties for the element.
- Use the overflow-x property to specify whether the content must be hidden, visible or scrolling horizontally when the content overflows the element’s left and right edges. Set the «hidden» value.
- Use the overflow-y property to specify whether the content must be hidden, visible or scrolling vertically when the content overflows the element’s top and bottom edges. Set the «auto» value.
- Use the text-align property with its «center» value.
div.scroll < background-color: #fed9ff; width: 600px; height: 150px; overflow-x: hidden; overflow-y: auto; text-align: center; padding: 20px; >
Let’s bring the parts together and see the whole code!
Example of making a vertically scrollable using the overflow-x and overflow-y properties:
html> html> head> title>Title of the document title> style> div.scroll < background-color: #fed9ff; width: 600px; height: 150px; overflow-x: hidden; overflow-y: auto; text-align: center; padding: 20px; > style> head> body> h2>W3docs h2> div class="scroll"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. div> body> html>
Result
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ‘Content here, content here’, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ‘lorem ipsum’ will uncover many web sites still in their infancy.
Example of making a vertically scrollable using the overflow property:
html> html> head> title>Title of the document title> style> div.scroll < background-color: #fed9ff; width: 600px; height: 150px; overflow: auto; text-align: justify; padding: 20px; > style> head> body> h2>W3docs h2> div class="scroll"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. div> body> html>
Add scroll to html
За вывод полосы прокрутки отвечает свойство «overflow».
Свойство overflow может принимать несколько значений:
В браузере — как можно увидеть все значения прокрутки?
нажимаем исследовать элемент добавляем свойство overflow и далее можно перебирать.
Прокрутка «overflow: auto»
Перейдем к примерам. использования и вывода прокрутки в html :
Создаем блок div с текстом и стилями(«3 способа css»):
В свойствах заранее подразумеваем, что высота будет меньше предполагаемого текста.
Для прокрутки ставим «overflow: auto»:
как сделать прокрутку html — overflow: auto
как сделать прокрутку html — overflow: auto
как сделать прокрутку html — overflow: auto
как сделать прокрутку html — overflow: overflow: scroll;
как сделать прокрутку html — overflow: auto
как сделать прокрутку html — overflow: auto
как сделать прокрутку html — overflow: auto
Результат вывода прокрутки в html:
Как видим. при использовании «overflow: auto» произошел вывод только вертикальной прокрутки.
Из-за того, что по горизонтали, внутренний размер блока не превышен его внешнего размера!
Перейдем ко второму примеру прокрутки:
Прокрутка «overflow: scroll»
В принципе, по поведению scroll и auto похожи. вы можете потренироваться. заменить scroll на auto и обратно.
Зададим нашему блоку размер «width: 200px;»
Внутри него сделаем второй блок, который будет больше нашего выше приведенного размера «width: 500px;»
И для примера прокрутки используем «overflow: scroll;»
как сделать прокрутку html — overflow: overflow: scroll;
как сделать прокрутку html — overflow: overflow: scroll;
как сделать прокрутку html — overflow: overflow: scroll;
Разместим приведенный код прокрутки ниже:
как сделать прокрутку html — overflow: overflow: scroll;
как сделать прокрутку html — overflow: overflow: scroll;
как сделать прокрутку html — overflow: overflow: scroll;
как сделать прокрутку html — overflow: overflow: scroll;
Результат использования прокрутки «overflow: scroll;»
Мы можем наблюдать на примере выведенной прокрутки, что и вертикальная и горизонтальная прокрутка в примере присутствует!
Использовать горизонтальную или вертикальную прокрутку
К примеру. вам требуется показать только один вид прокрутки. либо горизонтальный:
Либо только вертикальную прокрутку:
Разберем пример выводи одного типа прокрутки.
Соответственно в зависимости от ваших потребностей меняем горизонтальную прокрутку букв «x» либо вертикальную прокрутку букв «y»
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
Пример только одной прокрутки — горизонтальной:
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
как сделать прокрутку html — overflow: overflow-y: hidden; overflow-x: scroll;
Результат вывода — только горизонтальная прокрутка
В приведенном примере мы наблюдаем только одну прокрутку — горизонтальную!
Почему не работает прокрутка?
Интересный вопрос попался:
Почему не работает прокрутка? Одна из банальных причин, почему может «не работать прокрутка» — отсутствие прописанной «высоты!»(height)