使用 CSS 创建列布局


Web 开发人员可以通过利用 CSS 创建列布局,以一种视觉上美观的方式组织和构建内容。开发人员可以使用column-count属性定义列数,而column-gap属性则控制列之间的距离。此技术最大限度地利用了可用空间并提高了可读性,尤其是在文本较多的网页上。

通过使用 CSS 规则,可以创建一个响应式且灵活的布局,该布局可以轻松适应不同的屏幕尺寸,并在各种设备上提供一致的用户体验。熟练掌握列布局可以提高网站的整体可用性和外观,使其设计更精细和专业。

示例

让我们来看下面的示例,我们将使用 column-count 属性并指定列数。

<!DOCTYPE html>
<html>
<head>
   <style>
      .tutorial {
         column-count: 2;
         color: #DE3163;
         font-family: verdana;
         font-size: 15px;
         background-color: #D5F5E3;
      }
   </style>
</head>
<body>
   <div class="tutorial"> Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. </div>
</body>
</html>

运行以上代码后,它将生成一个输出,其中包含在网页上显示的、分成两列的文本。

示例

考虑另一种情况,我们将使用 column-width 属性来指定最佳宽度。

<!DOCTYPE html>
<html>
<head>
   <style>
      .tutorial {
         column-width: 80px;
         font-family: verdana;
         font-size: 12px;
         color: #DE3163;
      }
   </style>
</head>
<body style="background-color:#E8DAEF">
   <div class="tutorial"> Our Text Library Content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don't force our readers to sign up with us or submit their details either to use our Free Text Tutorials Library. No preconditions and no impediments, Just Simply Easy Learning! We have established a Digital Content Marketplace to sell Video Courses and eBooks at a very nominal cost. You will have to register with us to avail these premium services. </div>
</body>
</html>

运行以上代码后,输出窗口将弹出,显示网页上带有列宽度的文本。

示例

在下面的示例中,我们将使用 column-rule-width 属性来指定列之间的分隔线。

<!DOCTYPE html>
<html>
<head>
   <style>
      .tutorial {
         font-family: verdana;
         color: #DE3163;
         background-color: #D6EAF8;
         column-count: 2;
         column-gap: 50px;
         column-rule-style: solid;
         column-rule-width: 2px
      }
   </style>
</head>
<body>
   <div class="tutorial"> MS Dhoni probably ranks as the third-most popular Indian cricketer ever, behind only Sachin Tendulkar and Virat Kohli. He emerged from a cricketing backwater, the eastern Indian state of Jharkhand, and made it to the top with a home-made batting and wicketkeeping technique, and a style of captaincy that scaled the highs and hit the lows of both conservatism and unorthodoxy. Under Dhoni's leadership, India won the top prize in all formats: leading the Test rankings for 18 months starting December 2009, winning the 50-over World Cup in 2011, and the T20 world title on his captaincy debut in 2007. </div>
</body>
</html>

运行以上代码后,它将生成一个输出,其中包含在网页上显示的、分成多列的文本。

更新于:2024年1月8日

248 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告