Home  >  Article  >  Web Front-end  >  7 CSS Hacks Every Developer Should Know

7 CSS Hacks Every Developer Should Know

WBOY
WBOYforward
2023-08-29 14:53:02603browse

每个开发者都应该知道的 7 个 CSS Hack

CSS is the abbreviation of Cascading Style Sheets. It is used to make visually appealing websites. Using it will make the process of creating effective web pages easier.

The design of the website is crucial. It improves the aesthetics and overall quality of the website by facilitating user interaction. While it is possible to create a website without CSS, styling is required because no user wants to interact with a boring, unattractive website. In this article, we discuss 7 CSS hacks that every developer will need at some point in their web design journey.

Create responsive images using CSS

Using various techniques known as responsive images, the correct image can be loaded for the device's resolution, orientation, screen size, network connection, or page layout. Images should not be stretched by the browser to fit the page layout, and downloading images should not take too long or use too much network traffic. This improves user experience as images load quickly and are clear to the human eye. To create responsive images, always write the following syntax −

img{
   max-width: 100%;
   height: auto;
}

The simplest technique to create photos with high resolution is to set their width and height values ​​to half their actual size.

Place the content of the element in the center

If you want to center align the content of any element, there are multiple ways to do it. The simplest ones are mentioned below.

Position attributes

Use the CSS position property to center the content using the following syntax:

element{
   position: absolute;
   left: value;
   top: value;
}

Example

<!DOCTYPE html>
<html>
<head>
   <style>
      h1{
         text-align: center;
      }
      div{
         position: absolute;
         left: 45%;
      }
   </style>
</head>
<body>
   <h1> Position property </h1>
   <div> This is an example. </div>
</body>
</html>

Use
tags

Content to be centered should be written within the

tag. The entire content will then be center aligned.

Use text-align attribute

If the content you want to center align only contains text, you can simply use the textalign attribute.

text-align: center; 

Use universal selector

CSS asterisk (*) selector, also known as CSS universal selector, is used to select or match all elements or parts of elements of the entire web page at once. Once selected, you can use any CSS custom properties to style them accordingly. It matches any type of HTML element like ,

,

Universal selectors are actually used to style every element in a web page. Often, maintaining a specific style format for an entire page is difficult because of defaults set by browsers. It enables developers to prepare default styles for web pages. The most common use is to style all elements of a web page together.

grammar

*{
   Css declarations
}

Example

<!DOCTYPE html>
<html>
<head>
   <style>
      *{
         color: green;
         text-align: center;
         font-family: Imprint MT shadow;
      }
   </style>
</head>
<body>
   <h1>Css Universal Selector</h1>
   <h2>This is an example. It shows how to style the whole document altogether.</h2>
   <div>
      <p class = "para1"> This is the first paragraph. </p>
      <p class= "para2"> This is the second paragraph </p>
   </div>
</body>
</html>

Override CSS styles

Usually, we use CSS classes to override CSS styles. However, if you want to specify that a specific style must be applied to an element, then use !important.

grammar

element{
   property: value !important;
}

Example

<!DOCTYPE html>
<html>
<head>
   <style>
      h2 {
         color: blue;
      }
      .demo {
         color: red !important;
      }
   </style>
</head>
<body>
   <h2> This is an example #1 </h2>
   <h2> This is an example #2 </h2>
   <h2> This is an example #3 </h2>
   <h2 class= "demo"> This is an example #4 </h2>
   <h2> This is an example #5 </h2>
</body>
</html>

Scroll behavior

Good and efficient user experience is the most critical factor in web design. There is no point in making a website if your website is not user friendly. To ensure a smooth user experience, you should add a smooth scrolling effect to your website.

scroll-behaviour Properties enable developers to specify the behavior of the page during scrolling.

html{
   scroll-behaviour: smooth;
}

Add media queries and make the layout responsive

When a media type matches the type of device on which the document is displayed, a media query with that media type will be used to apply styles to the content.

@media (max-width: 100px){
   {CSS rules….
   }
}

If your website needs to be viewed on different devices, it is best to use viewport units. It ensures that content automatically resizes according to the viewport.

  • vw Viewport Width

  • vh ——Viewport height

  • v minutes Minimum viewport

  • v max Maximum viewport

CSS 弹性盒

一个CSS Flexbox是一个包含多个flex元素的容器。这些flex元素可以根据需要排列成行或列。Flex项目是flex容器的子元素,它是其父元素。使用CSS flexbox可以使每个元素具有精美和吸引人的外观。

display:flex帮助开发者让每一个组件都显得合适、可爱。它通过对齐元素的子元素将它们排列成行或列。

它将父元素中的子元素对齐到行或列中。

示例

<!DOCTYPE html>
<html>
<head>
   <style>
      .flex-container {
         display: flex;
         flex-direction: row;
         flex-wrap: nowrap;
         background-color: #097969;
         align-items: center;
         justify-content: center;
         width: 60%;
      }
      .demo1, .demo2, .demo3, .demo4 {
         background-color: yellow;
         height : 50px;
         width: 90%;
         margin: 10px;
         padding: 12px;
         font-size: 17px;
         font-weight: bold;
         font-family: verdana;
         text-align: center;
         align-items: center;
         color: brown;
      }
      .demo1{
         order: 1;
      }
      .demo2{
         order: 4;
      }
      .demo3{
         order: 2;
      }
      .demo4{
         order: 3;
      }
   </style>
</head>
<body>
   <h1>Order of Flex Items</h1>
   <p>The following list of flex elements has them in an ordered arrangement thanks to the order property:</p>
   <div class="flex-container">
      <div class= "demo1" > This </div>
      <div class= "demo2"> example </div>
      <div class= "demo3"> is </div>
      <div class= "demo4"> an </div>
   </div>
</body>
</html>

The above is the detailed content of 7 CSS Hacks Every Developer Should Know. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete