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

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

在本文中,我们将探讨 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 布局

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

axisone-dimensionaltwo-dimensionalalignmenthorizontal/verticalrows and columnsbest fornavigation barslayouts like dashboardsflexibilitybetter for small componentsbetter for page layouts

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

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

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

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

再见了!!!!

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

以上就是CSS 并不难(你只是缺少这些基础知识)- 掌握基础(第 2 部分)的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2845847.html

(0)
上一篇 2025年3月10日 12:43:28
下一篇 2025年3月10日 12:44:44

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论