0

0

CSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)

碧海醫心

碧海醫心

发布时间:2024-11-30 09:51:28

|

683人浏览过

|

来源于dev.to

转载

感谢大家对上一篇文章的评论,这确实意义重大。我希望你能从这篇文章中学到一两件事。

在本文中,我们将探讨 css 中的两个基本概念——定位和布局。定位和布局是创建具有视觉吸引力和功能性的网页的核心。掌握这些概念可以让您制作出增强用户体验的响应式设计。最后,您将了解如何使用这些技术像专业人士一样构建您的网页。

- 定位和布局

css 定位控制元素在网页上的定位或放置方式。如果适用,定位会受到顶部、底部、左侧和右侧偏移值的影响。有 5 个主要的 css position 值;

1。静态: 默认情况下,所有 html 元素都是静态定位的。这仅仅意味着元素不变,不移动,并且不受上、下、左、右偏移值的影响。

立即学习前端免费学习笔记(深入)”;

2。相对: 元素相对于其正常位置定位。

3。绝对: 元素相对于其最近的祖先(父级)或视口定位。

4。已修复: 元素相对于视口定位并在滚动期间保持固定。

5。粘性: 粘性定位允许元素根据滚动位置和偏移值上、下、左、右在相对位置和固定位置之间切换。

下面是解释 css 定位的插图。

CSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)

这是帮助使插图栩栩如生的代码。欢迎自行复制修改。




    
    
    css positioning
    


    

static position

this div has a static position. it is positioned relative to its normal position, which means it will not be affected by the positioning properties of other elements.

relative position

this div has a relative position. it is positioned relative to its normal position, and it can be moved using the top, bottom, left, and right properties.

absolute position

this div has an absolute position. it is positioned relative to the nearest positioned ancestor, which means it will be affected by the positioning properties of other elements.

fixed position

this div has a fixed position. it is positioned relative to the viewport, which means it will stay in the same place even when scrolling.

sticky position

this div has a sticky position. it is positioned relative to the nearest positioned ancestor, but it will not be affected by the positioning properties of other elements. it will stay in its original place until it crosses a specified threshold.

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body{
    font-family: arial, sans-serif;
    background-color: #f2f2f2;
    display: grid;
    place-content: center;
    min-height: 100vh;
}

.container{
    width: 100%;
    max-width: 1200px;
    height: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    border: 1px solid red;
    gap: 20px;
    padding: 20px;
}

.static{
    background-color: #ccc;
    padding: 20px;
    border: 1px solid black;
    width: 300px;
    position: static;

}

.relative{
    background-color: #ccc;
    padding: 20px;
    border: 1px solid black;
    width: 300px;
    position: relative;
    top:30px;
    right: 30px;

}

.absolute{
    background-color: #ccc;
    padding: 20px;
    border: 1px solid black;
    width: 300px;
    position: absolute;
    top: 30px;
    right: 100px;
}

.fixed{
    background-color: #ccc;
    padding: 20px;
    border: 1px solid black;
    width: 300px;
    position: fixed;
    bottom: 0;
    right: 0;
}

.sticky{
    background-color: #ccc;
    padding: 20px;
    border: 1px solid black;
    width: 300px;
    position: sticky;
    top: 0;
    right: 0;
}

— 暂停,深呼吸,然后继续!!—
- css 布局

MagickPen
MagickPen

在线AI英语写作助手,像魔术师一样在几秒钟内写出任何东西。

下载

1。 flexbox:这是一种一维布局方法,用于在单轴(水平和垂直)上布局项目。

flexbox 的特点

  • display: flex - 这会为容器创建一个弹性框。
  • align-items: center - 这控制容器的垂直对齐。
  • justify-content: space- between - 这控制容器的水平对齐方式。
  • 间隙:在不需要边距的情况下增加弹性项目之间的间距。

这是一个简单导航栏的前后对比




    
    
    navigation bar using css flexbox
    


    



* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
}

li {
  list-style: none;
}
a {
  text-decoration: none;
  color: white;
}
nav {
  background-color: #333;
  color: #fff;
  padding: 10px;
}

ul {
  display: flex;
  align-items: center;
  gap: 2rem;
}

结果:

CSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)

CSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)

2。网格:这是一种用于创建行和列的二维布局方法。

特点

  • display: grid - 这会为容器创建一个网格。
  • grid-template-columns/grid-template-rows - 这定义了容器的行或列。
  • repeat(2, 1fr) - 这将创建 2 个等宽列。
  • 间隙:10px-增加网格项目之间的间距。

这是我在 unsplash 上找到的一些猫照片的前后对比。



  
    
    
    css grid using cat photos
    
  
  
    
@@##@@ @@##@@ @@##@@ @@##@@
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
}

.container {
  margin: 0 auto;
  width: 950px;
  padding: 1rem;
}

.cat-photos {
  padding: 2rem;
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* Creates 2 equal-width columns */
}

img {
  border: 5px solid white;
  border-radius: 10px;
  width: 250px;
  height: 250px;
}

结果:

cute cat

cute cat

比较表

feature flexbox grid
axis one-dimensional two-dimensional
alignment horizontal/vertical rows and columns
best for navigation bars layouts like dashboards
flexibility better for small components better for page layouts

定位和布局是css的基础。了解何时以及如何使用它们不仅会让您的造型体验变得更加轻松,而且更加愉快和高效。虽然本文将带您开始使用 flexbox 和 grid,但我很快就会发布更深入的指南,探索它们的高级功能、提示和技巧。请继续关注!

这就是掌握css基础知识的总结!我希望你喜欢阅读这篇文章,就像我喜欢写它一样。但在我们分别之前,我很想听听你的消息:

您的项目更喜欢哪种 css 布局方法 - flexbox 还是 grid?为什么

欢迎在下面的评论中分享您的想法。

再见了!!!!

cute catcute catCSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)CSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)

相关专题

更多
css
css

css是层叠样式表,用来表现HTML或XML等文件样式的计算机语言,不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

510

2023.06.15

css居中
css居中

css居中:1、通过“margin: 0 auto; text-align: center”实现水平居中;2、通过“display:flex”实现水平居中;3、通过“display:table-cell”和“margin-left”实现居中。本专题为大家提供css居中的相关的文章、下载、课程内容,供大家免费下载体验。

262

2023.07.27

css如何插入图片
css如何插入图片

cssCSS是层叠样式表(Cascading Style Sheets)的缩写。它是一种用于描述网页或应用程序外观和样式的标记语言。CSS可以控制网页的字体、颜色、布局、大小、背景、边框等方面,使得网页的外观更加美观和易于阅读。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

752

2023.07.28

css超出显示...
css超出显示...

在CSS中,当文本内容超出容器的宽度或高度时,可以使用省略号来表示被隐藏的文本内容。本专题为大家提供css超出显示...的相关文章,相关教程,供大家免费体验。

537

2023.08.01

css字体颜色
css字体颜色

CSS中,字体颜色可以通过属性color来设置,用于控制文本的前景色,字体颜色在网页设计中起到很重要的作用,具有以下表现作用:1、提升可读性;2、强调重点信息;3、营造氛围和美感;4、用于呈现品牌标识或与品牌形象相符的风格。

757

2023.08.10

什么是css
什么是css

CSS是层叠样式表(Cascading Style Sheets)的缩写,是一种用于描述网页(或其他基于 XML 的文档)样式与布局的标记语言,CSS的作用和意义如下:1、分离样式和内容;2、页面加载速度优化;3、实现响应式设计;4、确保整个网站的风格和样式保持统一。

603

2023.08.10

css三角形怎么写
css三角形怎么写

CSS可以通过多种方式实现三角形形状,本专题为大家提供css三角形怎么写的相关教程,大家可以免费体验。

559

2023.08.21

css设置文字颜色
css设置文字颜色

CSS(层叠样式表)可以用于设置文字颜色,这样做有以下好处和优势:1、增加网页的可视化效果;2、突出显示某些重要的信息或关键字;3、增强品牌识别度;4、提高网页的可访问性;5、引起不同的情感共鸣。

389

2023.08.22

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 0.8万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.9万人学习

CSS教程
CSS教程

共754课时 | 18.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号