前端 css 通用css

If you have just started writing CSS codes it’s highly likely that you are having a hard time making the page responsive or aligning the components properly or positioning the components as you have in your mind. This article is about how to maintain the hierarchy of the page and in turn making CSS more logical. The best way to learn is to open code pen and write the code yourself also and see what happens.

如果您刚刚开始编写CSS代码,则很可能很难使页面响应或正确对齐组件或按照您的想法放置组件。 本文介绍如何维护页面的层次结构,以及如何使CSS更具逻辑性。 最好的学习方法是打开代码笔并自己编写代码,然后看看会发生什么。

MAINTAINING THE HIERARCHY

保持等级

Having a complete picture of how the page is structured is important and <div>is your lifesaver. For example, if you are making a nav bar, its structure will be something like this:

全面了解页面的结构非常重要,而<div>是您的救命稻草。 例如,如果要制作nav bar ,其结构将如下所示:

<div class="nav-bar"> <div class="logo">     <h1>LOGO</h1> </div> <div class="nav-links-container"> <div class="nav-link"> Home </div> <div class="nav-link"> About </div> <div class="nav-link"> Contact </div> </div></div>

This structure will help a lot when you apply CSS on it.

当您在其上应用CSS时,此结构将有很大帮助。

ADDING BACKGROUND COLOR FOR BETTER VISUALIZATION

为更好的可视化添加背景色

Here I have added different background colors to all the components and now you can see clearly how much space each one is taking, how it is aligned wrt its parent container. (Note : I have added few paddings so that you can see more clearly, but you don’t need to do that after you understand the structure)

在这里,我为所有组件添加了不同的背景色,现在您可以清楚地看到每个组件占用了多少空间,如何与其父容器对齐。 (注意:我添加了一些填充,以便您可以更清楚地看到,但是在您了解结构之后就不需要这样做了)

.nav-bar{  background-color:green;  padding:5px;}.logo{  background-color:red;}.nav-links-container{  background-color:blue;  padding: 10px}.nav-link{  background-color:yellow;  margin:2px;}

USING FLEX-BOX

使用FLEX-BOX

Now you know how to structure your nav bar but it doesn’t look quite good. Here comes the FLEX-BOX!!. Flex box is a module that aims at providing a more efficient way to lay out, align and distribute space among items in a container. The display property of any container is set to flex and after doing that all the child components inside that container is arranged in sort of an array either horizontally(default) or verically. Lets see what happens after I do: nav-bar{ display:flex; } (To change the direction to vertical we add flex-direction:column

现在您知道了如何构造导航栏,但是它看起来不太好。 这是FLEX-BOX !!。 Flex box是一个模块,旨在提供一种更有效的方式来布置,对齐和分配容器中各个项目之间的空间。 将任何容器的display属性设置为flex ,然后将容器内的所有子组件以水平(默认)或垂直排列为数组的形式排列。 让我们看看我做完之后会发生什么: nav-bar{ display:flex; } nav-bar{ display:flex; } (要将方向更改为垂直,我们添加flex-direction:column

You can see it is arranged horizontally but the width doesn’t look quite right. For this, we use the property flex .Keep in mind that only the child containers of the container having display set to flex can use the flex property. The flex property defines the ratio of the total width/height the child containers will take. Here we want the logo and nav-links-container to have equal width hence 1:1 . So we set flex:1 in both logo &nav-links-container, .logo,.nav-links-container{ flex:1; } (change the value to 2:1 to see how its affects)

您可以看到它水平排列,但是宽度看起来不太合适。 为此,我们使用属性flex 。请记住,只有将显示设置为flex的容器的子容器才能使用flex属性。 flex属性定义子容器将采用的总宽度/高度的比率。 在这里,我们希望徽标和nav-links-container具有相等的宽度,因此为1:1 。 因此,我们设定flex:1在这两个标志及导航链接容器, .logo,.nav-links-container{ flex:1; } .logo,.nav-links-container{ flex:1; } (将值更改为2:1以查看其影响)

You can set the display property of logo container and nav-links-container to flex to arrange the links horizontly. Lets see the final result:

您可以设置徽标容器和nav-links-container的显示属性,以灵活地水平排列链接。 让我们看一下最终结果:

ALIGNMENT IN FLEX-BOX

柔性盒校准

Now we can focus on logo container and nav-links-container seperately. The logo is way over to the left. In flex box there are two types of alignment, along the main-axis and along the cross-axis. The main-axis is the axis along which the child components is aligned and the other one is cross-axis. To align the child components along main-axis we use justify-content:center . After adding this to the logo container .logo { justify-content : center; }

现在,我们可以分别关注徽标容器和nav-links-container。 徽标在左侧。 在伸缩框中,有两种类型的对齐方式:沿主轴对齐和沿横向对齐。 主轴是子组件对齐所沿的轴线,另一个是交叉轴线。 为了使子组件沿主轴对齐,我们使用了justify-content:center 。 将其添加到徽标容器.logo { justify-content : center; } .logo { justify-content : center; }

We are almost done, but the nav-links are too close. One way to fix this is the align it to the center and add margins to all the individual links. But it can be done more efficiently by using justify-content : space-around There are more values that can be given to justify-content that u can read about yourself. .nav-links-container { justify-content : space-around; }

我们差不多完成了,但是导航链接太近了。 解决此问题的一种方法是将其与中心对齐,并为所有单个链接添加边距。 但是,可以通过使用justify-content : space-around来更有效地完成此操作justify-content : space-around可以有关自己可以读懂自己的justify-content值。 .nav-links-container { justify-content : space-around; }

Now we can focus on the nav-links. First lets align the nav-links along the vertical axis which is the cross-axis here, to do this we use align-items property .nav-link { display: flex ; align-items: center; }

现在,我们可以专注于导航链接。 首先,让导航链接沿垂直轴(此处为交叉轴) align-items ,为此,我们使用align-items属性.nav-link { display: flex ; align-items: center; } .nav-link { display: flex ; align-items: center; }

Remove the background colors to see final result

删除背景色以查看最终结果

You can add shadows, animations, fonts, colors to make it one of a kind. The idea behind the blog was to learn the thought process. Only one thing is left now is to make this bar responsive.

您可以添加阴影,动画,字体,颜色以使其成为一种。 博客背后的想法是学习思考过程。 现在只剩下一件事了,那就是使该栏响应。

Media queries

媒体查询

The @media rule, introduced in CSS2, made it possible to define different style rules for different media types. That means you can have different set of CSS rules based on the screen size, viewport or orientation of the screen. We will focus on the screen size here. Let’s say after the screen size decreases to 800px or less we can say that the device is a mobile phone. We can write new CSS for this device using the following syntax :

CSS2中引入的@media规则可以为不同的媒体类型定义不同的样式规则。 这意味着您可以根据屏幕尺寸,视口或屏幕方向来设置不同CSS规则集。 我们将在这里关注屏幕尺寸。 假设在屏幕尺寸减小到800像素或更小之后,我们可以说该设备是手机。 我们可以使用以下语法为此设备编写新CSS:

@media (max-width:800px){.nav-bar{       flex-direction:column;  }}

BEFORE YOU GO

你走之前

Reading it for the first time you might find it bit tricky to understand. Try writing the code along side and you will understand better.

第一次阅读它可能会很难理解。 尝试一起编写代码,您会更好地理解。

One thing to remember is that the flex property can only be used for the child components of a container having display set to flex and all the others like justify-content align-items flex-direction etc are used for the container having display set to flex.

要记住的一件事是, flex属性只能用于显示设置为flex的容器的子组件,而所有其他属性(如justify-content align-items flex-direction等)都用于显示设置为flex的容器。

SUMMARY

摘要

  1. display:flex To arrange the components horizontally/vertically.

    display:flex用于水平/垂直排列组件。

  2. flex-direction To change the axis along which the components are arranged.

    flex-direction更改组件排列所沿的轴。

  3. flex To set the width of the child components wrt to the parent container.

    flex设置子组件wrt到父容器的宽度。

  4. justify-content To align components along main-axis.

    justify-content沿主轴对齐组件。

  5. align-items To align components along main-axis.

    align-items沿主轴对齐组件。

  6. @media To define different style rules for different media types.

    @media为不同的媒体类型定义不同的样式规则。

Follow me on facebook and instagram and visit my website.

Facebookinstagram上关注我,并访问我的网站

翻译自: https://medium.com/@thebinaryrealm/writing-css-the-easy-way-easy-front-end-49b250f12f9a

前端 css 通用css


http://www.taodudu.cc/news/show-5180047.html

相关文章:

  • shopify 结账_是自我结账的机器邪恶化身
  • win7 命令行工具_7个很棒的命令行工具
  • 微服务解决方案_微服务为您提供正确的解决方案
  • wordpress表格筛选_wpDataTables:WordPress中用于表格和图表的最佳插件
  • ipad safari php readfile mp4,MP4 plays when accessed directly, but not when read through PHP, on iOS
  • html编辑contentbuilder,HTML BUILDER
  • 【kali】29 提权—— 利用漏洞提权
  • 游戏站推荐
  • FineBI 中 逻辑函数 Switch 的使用
  • 关于switchhosts
  • switch未能连接ea服务器,《Apex》无法完成EA账号登入问题,迅游支持Switch联机加速...
  • One Switch 1.1 破解版 Mac 集合一键切换系统各项功能的神奇菜单软件
  • PHP Switch语句写法示例
  • Java 18 Switch语句更强大了
  • android 不能试用switch
  • Switch版初音 mega39去渲染 获得PS4版初音街机效果说明
  • switch好玩吗_Switch今年上半年游戏汇总 原来有这么多选择
  • 小程序switch内部加上文字_Switch每日情报:国行版《健身环大冒险》微信小程序上线...
  • SwitchHosts小工具
  • Hosts 和 SwitchHosts
  • 怎样录制电脑内部发出的声音
  • 大整数乘法---C语言实现
  • 大整数乘法(分治)
  • 大整数乘法(递归+分治法)
  • c/c++ 大整数乘法
  • 【算法/C语言】大整数乘法(分治)
  • PYTHON:大整数乘法(分治法)
  • Java实现大整数乘法
  • 在动作捕捉系统中使用加速度和倾角传感器
  • 姿态估计之2D人体姿态估计 - PifPaf:Composite Fields for Human Pose Estimation

前端 css 通用css_编写CSS简易方法简易前端相关推荐

  1. [css] 在实际编写css中你有遇到过哪些浏览器兼容性的问题?怎么解决的?

    [css] 在实际编写css中你有遇到过哪些浏览器兼容性的问题?怎么解决的? 必用的三个工具PostCSSAutoprefixerBrowserslist 个人简介 我是歌谣,欢迎和大家一起交流前后端 ...

  2. css清除浮动的几种方法_web前端学习路线分享CSS浮动-清除浮动篇

    web前端学习路线分享CSS浮动-清除浮动篇,为什么要清除浮动 这里所说的清除浮动,并不是不要浮动了,而是清除浮动与浮动之间的影响.那么到底会有什么影响呢? 1.高度塌陷 举个例子我们看一下. 我们在 ...

  3. CSS清除浮动的三种方法【前端开发】

    p.s.高产量博主,点个关注

  4. css div快捷键,div+css基础

    Div+css技术 Div是用于存放文字,图片,元素的容器 Css 是用于指定存放在div中的内容如何显示,包括内容的位置和外观(层叠样式表) Html 文件 1 3 4 5 1 3 4 5 1 3 ...

  5. react 改变css样式_web前端入门到实战:编写CSS代码的8个策略,资深开发工程师总结...

    编写基本的CSS和HTML是我们作为Web开发人员学习的首要事情之一.然而,我遇到的很多应用程序显然没有人花时间真正考虑前端开发的长久性和可维护性.我认为这主要是因为许多开发人员对组织CSS / HT ...

  6. css绝对定位如何居中?css绝对定位居中的四种实现方法-web前端教程

    在网页进行css布局时居中是经常需要用到的,其中就有css绝对定位居中,那么,css绝对定位如何实现居中?今天的这篇文章将给大家来介绍关于css绝对定位居中的实现方法. css绝对定位居中的实现方法有 ...

  7. 前端html隐藏元素方式,CSS 隐藏元素的八种方法

    前言 总括: 本文详细讲述了在网页中用CSS隐藏元素的七种方法. 念念不忘,必有回响;有一口气,点一盏灯. 正文 说起隐藏元素我想每一个前端er都能说起几种,但能说全的我想就不是很多了.博主总结了几种 ...

  8. 前端 CSS 变量简介及基本使用方法

    背景 复杂的网站都会有大量的CSS代码,通常也会有许多重复的值. 举个例子,同样一个颜色值可能在成千上百个地方被使用到,如果这个值发生了变化,需要全局搜索并且一个一个替换,效率不高且容易出错. 自定义 ...

  9. php 图片圆角透明,CSS_使用CSS3实现圆角,阴影,透明,CSS实现圆角,阴影,透明的方法 - phpStudy...

    使用CSS3实现圆角,阴影,透明 CSS实现圆角,阴影,透明的方法很多,传统的方法都比较复杂,用CSS3就方便很多了,虽然现在各浏览器对CSS3的支持还不是很好,但不久的将来CSS3就会普及. 1.圆 ...

最新文章

  1. jupyter配置r
  2. linux查找并删除进程,linux中查找并kill一个名为server的进程
  3. python求最大值最小值_Python求可变参数的最大值最小值以及参数个数
  4. 85后转行java,一名85后阿里老程序员的真心话
  5. 学委作业管理系统c语言,c语言大作业-学生信息管理系统.doc
  6. hdfs MapTask类
  7. 【UML】如何画好数据流图基础教程
  8. 全球各国家手机号正则校验
  9. linux安装db2数据库并设置开机自启动
  10. panabit环境搭建
  11. 实现语音视频录制源码分享
  12. MSDB数据库置疑状态的解决方法
  13. ZooKeeper示例 实时更新server列表
  14. CAPI 初探及使用小结(4)
  15. android语音控制歌曲播放,发条 - 支持音乐聚合搜索,歌单导入,语音控制的 APP - Android 应用 - 【最美应用】...
  16. 深入理解蓝牙BLE之“BQB认证”
  17. windows 10 arm架构ISO下载
  18. day05循环结构while循环嵌套控制条件语句方法(函数)
  19. 在Livemedia的基础上开发自己的流媒体客户端 V 0.01
  20. webRTC原理及信令简介

热门文章

  1. python输入城市找省份_python爬虫学习之爬取全国各省市县级城市邮政编码
  2. 第三课 k8s源码学习和二次开发-缓存机制Informers和Reflector组件学习
  3. Python 飞速下载各大平台音乐
  4. GD32F303RCT6PWM及SPWM配置
  5. 金融学习之九——票息剥离法求零息利率
  6. python写动态壁纸_10分钟教你用python更换电脑壁纸
  7. 新鲜出炉的蚂蚁金服面经
  8. IOS中相册的一般处理
  9. 深信服java研发面经_【面经】深信服科技一面
  10. Oracle ebs pon,OracleApps(EBS)模块名称SAP模块名称