search
HomeWeb Front-endCSS TutorialTeach you step by step how to use CSS to create a simple and elegant input box

本篇文章给大家带来了关于CSS的相关知识,其中主要介绍了怎么用CSS实现一个简单又高大上的输入框,手把手教你哦~下面一起来看一下吧,希望对需要的朋友有所帮助。

一个商务简约的登陆界面

前几天在逛codepen的时候,发现了一个很有意思的登陆界面,于是就想着自己实现一下,于是就有了这个demo。
By the way, the login interface of the next website will also be changed to this style.

First the previous rendering:

Teach you step by step how to use CSS to create a simple and elegant input box

In the rendering we see two input boxes, one for text input box and one for password input frame.
Since the styles of the two input boxes are roughly the same, we only describe the implementation of the first input box.

1. Input box structure

In fact, this input box consists of two parts:
They are the prompt content of the input box and the input box body.

We put the prompt content of the input box into the label label, and add a for attribute to the label label, with the value of the input box The id attribute value.

In this way, when the user clicks on the prompt statement, the cursor will automatically focus on the input box.

    <div class="user_name">
        <label for="userName" class="userNameTip">请输入您的用户名</label>
        <input type="text" id="userName">
    </div>

So far, the structure of our input box has been set up.

2. Input box style

We first add a relative positioning to the entire big box to facilitate subsequent adjustments to the position of the elements inside. By the way, set the size of the entire box.

    .user_name {
        position: relative;
        width: 400px;
        height: 200px;
    }

Next we will change the style of the input box. After all, such a default box is too ugly.

.user_name{        
    width: 200px;        
    height: 50px;        
    position: absolute;        
    top: 50px;
    left: 30px;        
    font-size: 20px;
    }

Here we first adjust the position of the entire input box, and then set a font size, so that our input box has a basic style.

Let’s start setting the style of the input box:

#userName{    
display: inline-block;    
width: 300px;    
height: 30px;    
color: #0FF;    
font-size: 20px;    
border: 0px transparent;    
border-bottom: 2px solid #fff;    
background-color: rgb(54, 54, 54);
}

Here we set the width, height, font color, font size, border, and background color of the input box.

Because my entire background color is rgb(54, 54, 54), in order to prevent the input box from standing out so much, I set the background color of the input box to match the size of the input box. The background color is the same color.

But this is not enough, because when the input box gets focus, there is a border outside the input box, which will make the input box ugly.

Through the outline property we can set the border style when the input box gets focus.
In the rendering, we can see that when the input box gains focus, there is a blue border below the input box. The width of this border is 2px and the color is #0FF.

We give the input box a style like this:

#userName:focus{    
outline: none;    
border-bottom: 2px solid #0FF;
}

The style of the input box comes out:
Teach you step by step how to use CSS to create a simple and elegant input box

3. Input box prompt

When the input box in the rendering has not gained focus, the prompt statement is in the input box. This is achieved by using absolute positioning. Adjust it to the appropriate position and place the prompt statement in the input box. in.
And the color of the text is white at this time.

.userNameTip{    
position: absolute;    
top: 0px;    
left: 0px;    
font-size: 20px;    
color: #fff;
}

Finally, the style of the entire input box is like this:
Teach you step by step how to use CSS to create a simple and elegant input box

Of course, this is a static input box now without any interaction, we will implement it next The interaction of this input box.

4. Input box interaction

Interaction definitely requires an animation to achieve. Here we find that after the input box gains focus, the prompt text will become smaller, the color will change accordingly, and then move Go to the top of the input box. This is the effect we need to achieve.

After losing focus, we will judge whether there is content in the input box:
If there is content, the animation will not be removed and the animation will remain in the end state; if there is no content, it will be moved. Remove the animation and return to the first acquaintance state.

Then we define an animation:

@keyframes user {    
from{        
top: 0px;        
font-size: 20px;
    }    
    to{        
    top: -20px;        
    font-size: 12px;        
    color: #0FF;
    }
}

Now there is a question. We click on the input box and finally add this animation to the prompt statement of the input box. So how do we add the animation? ?

I use jquery to implement the class operation, that is, using addClass() and in jQuery removeClass() method to achieve.

So before that, we need to write the animation into a class first, and then operate this class through jQuery.

I simply wrote a class here, and then wrote the animation into it:

.userNameTipA{    
animation: user 0.3s linear normal forwards;    
animation-iteration-count: 1;
}

Then we can operate this class through jQuery

$('#userName').focus(function () {
    $('.userNameTip').addClass('userNameTipA');    
    console.log("点击了");
})
$('#userName').blur(function () {    
let val = $('#userName').val();    
if (val) {        
return;
    } else {
        $('.userNameTip').removeClass('userNameTipA');
    }
});

Finally click save and run, and you can see the effect.

Summary

In fact, this demo is still very simple. It uses some positioning to adjust the position of the input box, and then implements animation according to the focus style and defocus style of the input box, and finally jQuery to operate this animation.

Recommended study: "css video tutorial"

The above is the detailed content of Teach you step by step how to use CSS to create a simple and elegant input box. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

css可以取消横向滚动条嘛css可以取消横向滚动条嘛Apr 25, 2022 pm 07:22 PM

css可以取消横向滚动条,只需要给滚动条的父元素添加overflow-x属性,并将属性值设为“hidden”即可,语法为“父元素{overflow-x: hidden;}”;该方法可以设置当内容溢出左或右边缘时不提供滚动机制,直接裁剪内容。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.