Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App

CSS Forms Styling

Last Updated : 01 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

CSS Form is used to create interactive form for user. CSS provides many ways to set the style.

Try It:

Basic
Modern
Outlined
Dark

There are many CSS properties available which can be used to create and style HTML forms to make them more interactive, some of which are listed below:

CSS Forms Styling Examples

1. Attribute Selector:

The attribute type of the input form can take a variety of form depending on user's choice. It could be anything out of the possible types like text, search, url, tel, email, password, date pickers, number, checkbox, radio, file etc. User needs to specify type while creating a form.

Example:

HTML
<!DOCTYPE html>  <html>      <head>          <style>          body{              background-color:green;          }          </style>      </head>       <body>          <center>              <b>Is Geeksforgeeks useful ?</b>              <form>                  <input type="radio" name="useful" value="yes" checked>                  Yes <br>                  <input type="radio" name="useful" value="def_yes">                  Definitely Yes              </form>          </center>      </body>  </html>                      

Output:

Consider another example where the input type is simply a text:

HTML
<!DOCTYPE html>  <html>  <head>      <style>          body{              background-color:green;          }      </style>  </head>   <body>      <center>      <form>          <b>Do you find Geeksforgeeks helpful?</b>              <br>          <input type="text" name="info"><br>      </form>      </center>  </body>  </html>                      

Output:

2. Styling the Width of Input:

The width property is used to set the width of the input field. Consider the below example where the width is set to be 10% of the entire screen.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;          }                   body{              background-color:green;          }      </style>  </head>   <body>      <center>      <form>          <b>Do you find Geeksforgeeks helpful?</b>              <br>          <input type="text" name="info"><br>      </form>      </center>  </body>  </html>                 

Output:

3. Add Padding in Inputs:

The padding property is used to add spaces inside the text field. Consider the below example:

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;              padding: 12px;          }                   body{              background-color:green;          }      </style>  </head>   <body>      <center>          <form>              <b>Do you find Geeksforgeeks helpful?</b><br>              <input type="text" name="info"><br>          </form>      </center>  </body>  </html>                  

Output:

4. Set Margin for Inputs:

The margin property is used to add space outside the input field. It is helpful when there are many inputs. Consider the example below with two inputs and observe the space (margin) between them.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;              margin: 8px;          }                   body{              background-color:green;          }      </style>  </head>   <body>      <center>      <form>          <b>Mention two topics that you liked on Geeksforgeeks</b>              <br>          <input type="text" name="info"><br>          <input type="text" name="info"><br>      </form>      </center>  </body>  </html>     

Output:

5. Adding Border and Border-radius:

The border property is used to bring change in the size and color of the border whereas border-radius property is used for adding rounded corners. Consider the below example where a 2px solid red border is created with a border radius of 4px.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;              margin: 8px;              border: 2px solid red;              border-radius: 4px;          }                   body{              background-color:green;          }      </style>  </head>   <body>      <center>          <form>              <b>                  Mention two topics that you liked on                  Geeksforgeeks              </b>              <br>              <input type="text" name="info"><br>              <input type="text" name="info"><br>          </form>      </center>  </body>  </html>   

Output:

Note: User can also have border at any particular side and remove others or have all borders of different color. Consider the below example where user want border only on top(blue color) and bottom(red color).

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;              margin: 8px;              border: none;              border-top: 3px solid blue;              border-bottom: 3px solid red;          }                   body{              background-color:green;          }      </style>  </head>   <body>      <center>          <form>              <b>                  Mention two topics that you liked on                  Geeksforgeeks              </b>              <br>              <input type="text" name="info"><br>              <input type="text" name="info"><br>          </form>      </center>  </body>  </html>                      

Output:

6. Adding Color to text and Background:

The color property is used to change the color of the text in the input and the background-color property is used to change the color of the background of the input field. Consider the below example where color of text is black and background color is set to green.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;              margin: 8px;              border: none;              background-color: green;              color: black;          }                   body{              background-color:white;          }      </style>  </head>   <body>      <center>          <form>              <b>                  Mention two topics that you liked                  on Geeksforgeeks              </b>              <br>              <input type="text" name="info"><br>              <input type="text" name="info"><br>          </form>      </center>  </body>  </html>       

Output:

7. Focus Selector:

When we click on the input field it gets an outline of blue color. You can change this behaviour by using :focus selector. Consider the below example where user wants a 3px solid red outline and green background when focused.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width:10%;              margin: 8px;              color: black;          }                   input[type=text]:focus {          border: 3px solid red;          background-color: green;          }                   body{              background-color:white;          }      </style>  </head>   <body>      <center>      <form>          <b>              Mention two topics that you liked              on Geeksforgeeks          </b>          <br>          <input type="text" name="info"><br>          <input type="text" name="info"><br>      </form>      </center>  </body>  </html>  

Output:

8. Adding images in the Input form:

The background-image property can be used to put an image inside the input form and it can be positioned using background-position property and user can also decide whether to repeat or not. Consider the example below with image in background with no-repeat mode.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width: 20%;              background-image: url('search.png');              background-position: 10px 10px;              background-repeat: no-repeat;              padding: 12px 20px 12px 40px;          }                   body{              background-color:white;          }      </style>  </head>   <body>      <center>      <form>          <b>Search on Geeksforgeeks</b><br>          <input type="text" name="info" placeholder="Search.."><br>      </form>      </center>  </body>  </html>   

Output:


9. Transition Property:

The transition property can be used over the input field to bring change in the size of the field by specifying the relaxed width and focused width along with the time period for which operation will take place. Consider below example where relaxed input field width is 15% which when focused changes to 30% in 1 second.

HTML
<!DOCTYPE html>  <html>  <head>      <style>          input{              width: 15%;              -webkit-transition: width 1s ease-in-out;              transition: width 1s ease-in-out;          }                   input[type=text]:focus {              width: 30%;              border:4px solid blue;          }                   body{              background-color:green;          }      </style>  </head>   <body>      <center>      <form>          <b>Search on Geeksforgeeks</b><br>          <input type="text" name="info" placeholder="Search.."><br>      </form>      </center>  </body>  </html>                    

Output: When Relaxed-

When focused-



P

piyushpilaniya98
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • CSS
  • Technical Scripter 2018
  • CSS-Advanced

Similar Reads

    Using the Tabindex Attribute in Navigation Bars with HTML & CSS
    The tabindex attribute is used to specify the order in which elements receive focus when the "tab" key is pressed. This allows for improved accessibility and control over the focus order for interactive elements on a webpage.What is tabindex?The tabindex attribute controls whether an element can be
    2 min read
    Advanced Selectors in CSS
    Selectors are used for selecting the HTML elements in the attributes. Some different types of selectors are given below: Adjacent Sibling Selector: It selects all the elements that are adjacent siblings of specified elements. It selects the second element if it immediately follows the first element.
    5 min read
    Advance CSS Layout with Flexbox
    Flexbox is a powerful CSS layout model that simplifies the creation of flexible and responsive layouts. It allows you to align and distribute space among items within a container, making complex designs easier to manage.Flexbox is particularly useful for building responsive designs that adapt seamle
    4 min read
    CSS 2D Transforms
    A transformation in CSS is used to modify an element by its shape, size and position. It transforms the elements along the X-axis and Y-axis. There are 6 main types of transformation which are listed below:translate()rotate()scale()skewX()skewY()matrix()We will implement all these functions & wi
    5 min read
    CSS 3D Transforms
    CSS 3D transforms are used to manipulate HTML elements in three dimensions by rotating them along the x-axis, y-axis, and z-axis. There are three main types of transformation which are listed below:rotateX(): Rotates an element around its X-axis.rotateY(): Causes rotation around the Y-axis.rotateZ()
    3 min read
    CSS Media Queries
    CSS Media Queries are used to apply CSS styles according to the screen size.Media queries detect device features like screen width, height, and resolution.Breakpoints define the screen sizes where the design needs to change.They ensure a smooth, user-friendly experience across all devices.Syntax:@me
    4 min read
    CSS Pagination
    Pagination is the process of dividing the document into pages and providing them with numbers. Types of Pagination: There are many types of pagination in CSS. Some of them are given below: Simple PaginationActive and Hoverable PaginationRounded Active and Hoverable ButtonsHoverable Transition Effect
    7 min read
    CSS Gradients
    CSS gradients allow you to create smooth transitions between two or more colors, making your web elements visually appealing. Each gradient type blends colors in different ways, helping you enhance your designs. Learning how to use them will give you more control over your site's appearance.Types of
    7 min read
    CSS Shadow Effect
    The shadow effect property in CSS is used to add shadows to text and images in HTML documents. This enhances the visual appeal and depth of your web elements, making your design more engaging. Text ShadowThe text-shadow property in CSS is used to display text with a shadow. This property defines the
    2 min read
    CSS Animations
    CSS animations control the movement and appearance of elements on web pages. We can animate HTML elements without using JavaScript.Use @keyframes to define the animation steps.Apply animations with properties like animation-name and animation-duration.Control the animation flow using animation-timin
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences