1627180320
HTML Toggle Switch | No Javascript (Quick Tutorial). How to build a custom toggle switch element from scratch without using a single line of javascript code. Build a beautiful and modern checkbox design using HTML and CSS.
#html
1663207274
Learn how to create a dark mode switch with HTML, CSS & Vanilla Javascript. When the user clicks on this switch, the theme of the webpage/website toggles between dark and light mode. With this tutorial, you will get a basic idea of how you can add a dark theme option to your website without changing much code or without adding excessive CSS.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dark Mode Toggle</title>
<!--Google Fonts-->
<link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
<!--Stylesheet-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<input type="checkbox" id="toggle">
</div>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Facilis, ab sequi! Ipsum, reprehenderit! Dolor vero sunt corporis ea natus, nulla cum assumenda. Nostrum corporis molestiae corrupti magni. Corporis ducimus ipsam, qui et eveniet nisi excepturi sint dolore, labore velit repellat quia quasi! Repellendus quo magni voluptatem aut odit, totam sequi autem, doloremque minima tenetur placeat debitis reiciendis repudiandae dolore tempore adipisci blanditiis reprehenderit doloribus recusandae esse commodi harum ratione quisquam?
</p>
<script src="script.js"></script>
</body>
</html>
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
padding: 30px;
}
.container{
width: 100%;
height: 40px;
position: relative;
margin-bottom: 30px;
}
#toggle{
-webkit-appearance: none;
appearance: none;
height: 40px;
width: 75px;
background-color: #15181f;
position: absolute;
right: 0;
border-radius: 20px;
outline: none;
cursor: pointer;
}
#toggle:after{
content: "";
position: absolute;
height: 30px;
width: 30px;
background-color: #ffffff;
top: 5px;
left: 7px;
border-radius: 50%;
}
p{
font-family: "Open Sans",sans-serif;
line-height: 35px;
text-align: justify;
}
.dark-theme{
background-color: #15181f;
color: #e5e5e5;
}
.dark-theme #toggle{
background-color: #ffffff;
}
.dark-theme #toggle:after{
background-color: transparent;
box-shadow: 10px 10px #15181f;
top: -4px;
left: 30px;
}
document.getElementById("toggle").addEventListener("click", function(){
document.getElementsByTagName('body')[0].classList.toggle("dark-theme");
});
#html #css #javascript
1680087863
In this article, I have shown you how to create Day and Night Mode Toggle using HTML CSS and JavaScript. Earlier I shared with you the design of many more types of CSS Toggle Switch. This is the first time we have created a Day and Night Mode design using toggle design.
Now, different types of websites use dark and light themes. This kind of feature undoubtedly enhances the quality and user satisfaction of the website. Various websites like YouTube, Facebook have introduced such dark mode features. If you want, you can easily create such designs with the help of HTML CSS, and JavaScript.
In the following tutorial, I have shown how to create a dark mode toggle. There is no reason to worry if you are a beginner. Here is a complete step-by-step tutorial for you. Here is an explanation of each code used.
Here JavaScript is just two lines and the rest is a little bit HTML and CSS. If you have an idea about basic HTML CSS and JavaScript then you can easily create a project (Day and Night Mode JavaScript) by following this tutorial.
Below is a demo that will help you learn how it works. Here you will find the required source code.
On the first, I have defined an area that will contain the contents. Then I created the toggle button which will change the dark and light mode. Then I added all the tests using the paragraph below.
I have created an area for this project using the code below. This area cannot be seen because the background color was not used. However, it will contain all the information. The width of this area 500px.
<div class=”all”>
</div>
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
padding: 30px;
}
.all{
width: 500px;
margin: 40px auto;
}
Now I have made one of these switches that can be used to switch from dark to light mode and from light to dark mode. Checkboxes are used to install such switches. Similarly, I have taken the help of the check box using input.
<div class=”container”>
<input type=”checkbox” id=”toggle”>
</div>
.container{
width: 100%;
height: 40px;
margin-bottom: 20px;
position: relative;
}
Button width: 75px, height: 40px and background-color I used black.
#toggle{
-webkit-appearance: none;
appearance: none;
height: 40px;
width: 75px;
background-color: #15181f;
position: absolute;
right: 0;
border-radius: 20px;
outline: none;
cursor: pointer;
}
Now I have created a button in it using CSS’s “: after” tag. If you watch the demo you will understand that there is a button in the Toggle switch.
The following codes have been used to create it. I saw the button equal in length and height and used border-radius: 50% to make it completely round.
#toggle:after{
content: “”;
position: absolute;
height: 30px;
width: 30px;
background-color: #ffffff;
top: 5px;
left: 7px;
border-radius: 50%;
}
Now I have added all the tests in the paragraph tags. This box does not have a specific height, it will determine its own height based on the amount of content. However, box-shadow has been used which will determine its size.
<p>
Lorem ipsum dolor sit amet …. ratione quisquam?
</p>
p{
font-family: “Open Sans”,sans-serif;
line-height: 35px;
padding: 10px;
text-align: justify;
box-shadow: 0 0 20px rgba(0,139,253,0.25);
}
Now I have added what will change during Dark Mode. What we have added above is for light mode only. I have added here what will change when the light mode is converted to dark mode. Then I will link these codes to Suez using JavaScript.
First indicates the background color and the color of the text. When you turn on the dark mode the background color of the text will be black and the text color will be white. This will change the background color of the switch from black to white.
.dark-theme{
background-color: #15181f;
color: #e5e5e5;
}
.dark-theme #toggle{
background-color: #ffffff;
}
.dark-theme #toggle:after{
background-color: transparent;
box-shadow: 10px 10px #15181f;
top: -4px;
left: 30px;
}
Remember that you will not get the result shown in your picture in this step. This can be seen after adding JavaScript. But here I have given the image to understand what will change after using the css code.
Using a little bit of JavaScript I have linked the CSS codes of the dark mode added above in the switch. Using the click method here I have indicated that “dark-theme” will work when you click on “toggle”.
document.getElementById(“toggle”).addEventListener(“click”, function(){
document.getElementsByTagName(‘body’)[0].classList.toggle(“dark-theme”);
});
Hopefully, the above tutorial has helped you to know how I created this Day and Night Mode Toggle project using HTML CSS, and JavaScript.
If you have any problems you can let me know by commenting. Below is the source code for creating this day-night toggle button that you can download.
Original article source at: https://foolishdeveloper.com/
1625652623
In this era of technology, anything digital holds a prime significance in our day-to-day life. Hence, developers have submerged themselves to create a major impact using programming languages.According to Statista, HTML/CSS holds the second position (the first being Javascript), in the list of most widely-used programming languages globally (2020).Interested to learn this language? Then head on to this tutorial and get to know all about HTML! Plus we have added numerous examples such that you can learn better! So happy learning!
html for beginners
#html #html-for-beginners #html-tutorials #introduction-to-html #learn-html #tutorials-html
1680134940
在本文中,我向您展示了如何使用 HTML CSS 和 JavaScript 创建日夜模式切换。之前我和大家分享了更多类型的CSS Toggle Switch的设计。这是我们第一次使用拨动设计创建日夜模式设计。
现在,不同类型的网站使用深色和浅色主题。这种功能无疑提高了网站的质量和用户满意度。YouTube、Facebook 等各种网站都引入了这种黑暗模式功能。如果需要,您可以借助 HTML CSS 和 JavaScript 轻松创建此类设计。
在下面的教程中,我展示了如何创建暗模式切换。如果您是初学者,则无需担心。这是一个完整的分步教程。以下是对使用的每个代码的解释。
这里的 JavaScript 只是两行,其余的是一些 HTML 和 CSS。如果您了解基本的 HTML CSS 和 JavaScript,那么您可以按照本教程轻松创建一个项目(日夜模式 JavaScript)。
下面是一个演示,可帮助您了解其工作原理。在这里您可以找到所需的源代码。
首先,我定义了一个包含内容的区域。然后我创建了切换按钮,它将改变黑暗和光明模式。然后我使用下面的段落添加了所有测试。
我使用下面的代码为这个项目创建了一个区域。由于未使用背景颜色,因此看不到该区域。但是,它将包含所有信息。该区域的宽度为 500px。
<div class=”all”>
</div>
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
padding: 30px;
}
.all{
width: 500px;
margin: 40px auto;
}
现在,我制作了其中一个开关,可用于从暗模式切换到亮模式以及从亮模式切换到暗模式。复选框用于安装此类开关。同样,我已经使用输入的复选框的帮助。
<div class=”container”>
<input type=”checkbox” id=”toggle”>
</div>
.container{
width: 100%;
height: 40px;
margin-bottom: 20px;
position: relative;
}
按钮宽度:75px,高度:40px 和背景色我用的是黑色。
#toggle{
-webkit-appearance: none;
appearance: none;
height: 40px;
width: 75px;
background-color: #15181f;
position: absolute;
right: 0;
border-radius: 20px;
outline: none;
cursor: pointer;
}
现在我已经使用 CSS 的“:after”标签在其中创建了一个按钮。如果您观看演示,就会明白Toggle 开关中有一个按钮。
以下代码已用于创建它。我看到按钮的长度和高度相等,并使用 border-radius: 50% 使其完全呈圆形。
#toggle:after{
content: “”;
position: absolute;
height: 30px;
width: 30px;
background-color: #ffffff;
top: 5px;
left: 7px;
border-radius: 50%;
}
现在我已经在段落标签中添加了所有测试。这个盒子没有具体的高度,它会根据内容的多少来决定自己的高度。但是,已使用 box-shadow 来确定其大小。
<p>
Lorem ipsum dolor sit amet …. ratione quisquam?
</p>
p{
font-family: “Open Sans”,sans-serif;
line-height: 35px;
padding: 10px;
text-align: justify;
box-shadow: 0 0 20px rgba(0,139,253,0.25);
}
现在我已经添加了在暗模式期间会发生变化的内容。我们上面添加的内容仅适用于灯光模式。我在这里添加了当亮模式转换为暗模式时会发生什么变化。然后我将使用 JavaScript 将这些代码链接到 Suez。
首先表示背景颜色和文字颜色。当您打开深色模式时,文本的背景颜色将为黑色,而文本颜色将为白色。这会将开关的背景颜色从黑色更改为白色。
.dark-theme{
background-color: #15181f;
color: #e5e5e5;
}
.dark-theme #toggle{
background-color: #ffffff;
}
.dark-theme #toggle:after{
background-color: transparent;
box-shadow: 10px 10px #15181f;
top: -4px;
left: 30px;
}
请记住,您不会在此步骤中获得图片中显示的结果。这可以在添加 JavaScript 后看到。但是这里我已经给出了图像,以了解使用css代码后会发生什么变化。
使用一点点 JavaScript,我已经链接了上面在开关中添加的暗模式的 CSS 代码。使用此处的点击方法,我已经指出当您点击“切换”时,“深色主题”将起作用。
document.getElementById(“toggle”).addEventListener(“click”, function(){
document.getElementsByTagName(‘body’)[0].classList.toggle(“dark-theme”);
});
希望以上教程可以帮助您了解我是如何使用 HTML CSS 和 JavaScript创建这个日夜模式切换项目的。
如果您有任何问题,可以通过评论告诉我。下面是创建这个昼夜切换按钮的源代码,您可以下载。
文章原文出处:https: //foolishdeveloper.com/
1680138660
В этой статье я показал вам, как создать переключатель дневного и ночного режима с помощью HTML CSS и JavaScript. Ранее я поделился с вами дизайном еще многих видов CSS Toggle Switch . Это первый раз, когда мы создали дизайн дневного и ночного режима с использованием переключателя.
Сейчас разные типы сайтов используют темную и светлую темы . Такая функция, несомненно, повышает качество и удовлетворенность пользователей веб-сайтом. Различные веб-сайты, такие как YouTube, Facebook, представили такие функции темного режима. Если вы хотите, вы можете легко создавать такие дизайны с помощью HTML, CSS и JavaScript.
В следующем уроке я показал, как создать переключатель темного режима . Нет причин для беспокойства, если вы новичок. Вот полное пошаговое руководство для вас. Вот объяснение каждого используемого кода.
Здесь JavaScript — это всего две строки, а остальное — это немного HTML и CSS. Если у вас есть представление об основах HTML CSS и JavaScript, вы можете легко создать проект (дневной и ночной режим JavaScript), следуя этому руководству.
Ниже приведена демонстрация, которая поможет вам узнать, как это работает. Здесь вы найдете необходимый исходный код.
В первом я определил область, которая будет содержать содержимое. Затем я создал кнопку-переключатель, которая будет менять темный и светлый режимы . Затем я добавил все тесты, используя абзац ниже.
Я создал область для этого проекта, используя приведенный ниже код. Эта область не видна, потому что цвет фона не использовался. Тем не менее, он будет содержать всю информацию. Ширина этой области 500px.
<div class=”all”>
</div>
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
padding: 30px;
}
.all{
width: 500px;
margin: 40px auto;
}
Теперь я сделал один из этих переключателей, который можно использовать для переключения из темного в светлый режим и из светлого в темный режим. Флажки используются для установки таких переключателей. Точно так же я воспользовался помощью флажка, используя ввод.
<div class=”container”>
<input type=”checkbox” id=”toggle”>
</div>
.container{
width: 100%;
height: 40px;
margin-bottom: 20px;
position: relative;
}
Ширина кнопки: 75 пикселей, высота: 40 пикселей и цвет фона, который я использовал черный.
#toggle{
-webkit-appearance: none;
appearance: none;
height: 40px;
width: 75px;
background-color: #15181f;
position: absolute;
right: 0;
border-radius: 20px;
outline: none;
cursor: pointer;
}
Теперь я создал в нем кнопку, используя тег CSS «: after». Если вы посмотрите демо, то поймете, что в тумблере есть кнопка .
Для его создания использовались следующие коды. Я увидел кнопку одинаковой длины и высоты и использовал border-radius: 50%, чтобы сделать ее полностью круглой.
#toggle:after{
content: “”;
position: absolute;
height: 30px;
width: 30px;
background-color: #ffffff;
top: 5px;
left: 7px;
border-radius: 50%;
}
Теперь я добавил все тесты в теги абзаца. Это поле не имеет определенной высоты, оно будет определять свою собственную высоту в зависимости от количества содержимого. Однако используется box-shadow, который определяет его размер.
<p>
Lorem ipsum dolor sit amet …. ratione quisquam?
</p>
p{
font-family: “Open Sans”,sans-serif;
line-height: 35px;
padding: 10px;
text-align: justify;
box-shadow: 0 0 20px rgba(0,139,253,0.25);
}
Теперь я добавил, что изменится во время Dark Mode . То, что мы добавили выше, предназначено только для легкого режима. Я добавил сюда, что изменится, когда светлый режим будет преобразован в темный режим. Затем я свяжу эти коды с Suez с помощью JavaScript.
Сначала указывается цвет фона и цвет текста. Когда вы включаете темный режим, цвет фона текста будет черным, а цвет текста будет белым. Это изменит цвет фона переключателя с черного на белый.
.dark-theme{
background-color: #15181f;
color: #e5e5e5;
}
.dark-theme #toggle{
background-color: #ffffff;
}
.dark-theme #toggle:after{
background-color: transparent;
box-shadow: 10px 10px #15181f;
top: -4px;
left: 30px;
}
Помните, что на этом шаге вы не получите результат, показанный на картинке. Это можно увидеть после добавления JavaScript. Но здесь я привел изображение, чтобы понять, что изменится после использования кода css.
Используя немного JavaScript, я связал коды CSS темного режима, добавленные выше, в переключатель. Используя здесь метод щелчка, я указал, что «темная тема» будет работать, когда вы нажмете «переключить».
document.getElementById(“toggle”).addEventListener(“click”, function(){
document.getElementsByTagName(‘body’)[0].classList.toggle(“dark-theme”);
});
Надеюсь, приведенный выше учебник помог вам узнать, как я создал этот проект переключения дневного и ночного режимов с использованием HTML CSS и JavaScript.
Если у вас есть какие-либо проблемы, вы можете сообщить мне, комментируя. Ниже приведен исходный код для создания этой кнопки переключения «день-ночь», которую вы можете скачать.
Оригинальный источник статьи: https://foolishdeveloper.com/