惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
Schneier on Security
The Hacker News
The Hacker News
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
O
OpenAI News
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Latest news
Latest news
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Vertical and Horizontal Lines
Ahmad Shadeed · 2019-06-07 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

While working on my new website design, I needed to add two lines. One of them is horizontal, and the other is vertical.

Notice them in the below design mockup:

Both of them need to expand from left to right, and top to bottom. In this article, I will discuss the various ways to build them in CSS.

Vertical Line

It’s starting from the top to the end of the page. It should have a height that is equal to the page height. In other words, it should be dynamic.

Option 1: CSS Grid

I thought about CSS Grid for this use case. I divided the page into 12 columns and ordered the header and section as below.

<header>..</header>
<section>..</section>
<section>..</section>
<section>..</section>
body {
  display: grid;
  grid-template-columns: 134px repeat(11, 1fr);
}

header {
  grid-column: 1 / 13;
}

section {
  grid-column: 2 / 13;
}

With that, here is the current result as inspected in Firefox Grid tool.

Let’s add the line!

The first thought I got is to add a pseudo element for the <body> element and make it expand with grid-row.

body:before {
  content: "";
  width: 2px;
  grid-column: 1 / 2;
  grid-row: 1 / -1;
  /*Other styles*/
}

Unfortunately, this didn’t work as expected. The line is not expanding from top to bottom.

It turned out that grid-row: 1 / -1 works only with implicit grids. Which means, I should define the number of rows manually. In my case, this won’t work as the number of sections should be dynamic and not tied to fixed amount of rows.

In order to fix this, I need to use a hack similar to the flex-grow: 9999 one. It will be like grid-row: 1 / 50. Where 50 is a big number for the rows, which mostly won’t be reached.

See the Pen Vertical Line - 1 by Ahmad Shadeed (@shadeed) on CodePen.

Option 2: CSS Gradients

It’s possible to add a gradient to the body element and have a 1px stop color to represent the line.

body {
  background: linear-gradient(
    to right,
    transparent 80px,
    #cbcbcb 0,
    #cbcbcb 81px,
    transparent 0,
    transparent 100%
  );
}

What I don’t like about this solution is that it’s not 100% robust. If I need to change the width of the line, the start and end stop values should be tweaked. The same applies to its color.

Whereas in the CSS Grid solution, changing the width and color is easier and straightforward.

See the Pen Vertical Line - 2 by Ahmad Shadeed (@shadeed) on CodePen.

Option 3 - Fixed Positioning

By using position: fixed, it’s possible to achieve the same result without any side-effects (At least for my use case).

body:before {
  content: "";
  position: fixed;
  left: 80px;
  top: 0;
  bottom: 0;
  background: #cbcbcb;
  width: 1px;
}

See the Pen Vertical Line - 3 by Ahmad Shadeed (@shadeed) on CodePen.

However, at the end of the day, you should pick the solution that works best for you. A solution might be right for your design, but it could be wrong for mine.

Horizontal Line

Next, I’m going to build the horizontal line that is expanded from the left to right above the navigation items.

For this one, I will use the same concept as the vertical line, but it will be a pseudo element added to the <header> element.

header {
  display: inherit;
  grid-template-columns: inherit;
  /* The inherit keyword is meant to inherit the grid styles from the body element */
}

header:before {
  content: "";
  height: 1px;
  background: #b9b9b9;
  grid-column: 1 / 13;
  margin-top: 16px;
}

See the Pen Horizontal Line - 1 by Ahmad Shadeed (@shadeed) on CodePen.

The End

I hope you found the article useful. I try to always think about multiple solutions and weigh them depending on the use case I have.

Did I miss anything? Please feel free to ping me on @shadeed9. Thank you for reading.