如何使线成为伪元素?

时间:2018-07-31 21:05:11

标签: html css web frontend

所以,我被困在这一个上。在下面的图片中,您可以看到我该怎么办。但是,我不太了解如何在div之间划出那些交叉线。

picture

这是我到目前为止完成的代码: https://codepen.io/kaeses_developer/pen/wxyYdb

我认为我必须为.menu-item--large创建一个:before和:after伪元素。

.menu-item--large {
    flex-basis: 100%;
    font-size: 36px;
    background: none;
    margin: 0 50%;
}

但是我该怎么做呢?还是有其他不同的制作方法?

3 个答案:

答案 0 :(得分:0)

您确实可以使用伪元素,但也可以使用背景图像(可以使用渐变色和currentcolor)。

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

body {
  margin: 0;
  height: 100%;
  background-image: url(https://i.pinimg.com/originals/a4/6c/6d/a46c6d2f5104ea715af56e5c60d57eac.jpg);
}

.menu-container {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin: 0 25%;
}

.menu-item {
  background-color: rgba(0, 0, 0, 0.2);
  flex: 1 1 25%;
  font-size: 20vw;/* demo purpose */
  text-align: center;
  margin: 10px;
  color: #fff;
  position: relative;
}
/* add pseudo where needed to draw a line */
.menu-item:not(.menu-item--large)+.menu-item:not(.menu-item--large):before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  border-left: solid 2px;
  margin-left: -10px;
}

div.menu-item.menu-item--large {
  min-width: 100%;
  font-size: 36px;
  /* extra to draw lines */
  background: linear-gradient( to right, transparent 0.75em, currentcolor 0.75em, currentcolor calc(50% - 1em), transparent calc(50% - 1em), transparent calc(50% + 1em), currentcolor calc(50% + 1em), currentcolor calc(100% - 0.75em), transparent calc(100% - 0.75em)) 50% 50% no-repeat;
  background-size: 100% 2px;
  /* end extra */
  margin: 0 50%;
}

.menu-item:hover {
  opacity: 0.4;
  color: #000;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1">

<div class="menu-container">
  <div class="menu-item">
    <i class="far fa-list-alt"></i>
  </div>
  <div class="menu-item">
    <i class="fas fa-user"></i>
  </div>
  <div class="menu-item menu-item--large">
    <i class="fas fa-sign-in-alt"></i>
  </div>
  <div class="menu-item">
    <i class="fas fa-plus"></i>
  </div>
  <div class="menu-item">
    <i class="fas fa-pencil-alt"></i>
  </div>
</div>

答案 1 :(得分:0)

对于垂直线,我认为您不需要使用伪元素。再增加一些div。我们称它们为.vertical-line

.vertical-line {
  border-right: 1px solid rgba(255,255,255,0.5);
  margin: 10px 0;
}

对于水平线,我们将使用伪元素:

.menu-item--large:before {
  content: '';
  border-bottom: 1px solid rgba(255,255,255,0.5);
  position: absolute;
  display: inline-block;
  left: 30px;
  right: calc(50% + 30px);
  top: 50%;
  z-index: -1;
}

.menu-item--large:after {
  content: '';
  border-bottom: 1px solid rgba(255,255,255,0.5);
  position: absolute;
  display: inline-block;
  right: 30px;
  left: calc(50% + 30px);
  top: 50%;
  z-index: -1;
}

以上30px的数字是基于我增加.menu-item的边距的,因此它看起来更像是您附加的图像。我还向{.1容器中添加了position: relative;,以使绝对定位有效。 https://codepen.io/mlissne/pen/XBZybZ

答案 2 :(得分:0)

在这里,我根据需要使用了伪元素,并提出了这个codepen,首先进行检查,这是下面的代码。

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

body {
	margin: 0;
	height: 100%;
	background-image: url(https://i.pinimg.com/originals/a4/6c/6d/a46c6d2f5104ea715af56e5c60d57eac.jpg);
}

.menu-container{
	display: flex;
	justify-content: center;
	flex-wrap: wrap;
	margin: 0 25%;
}

.menu-item {
	background-color: rgba(0,0,0,.2);
	flex: 1 1 25%;
	font-size: 250px;
	text-align: center;
	margin: 10px;
	color: #fff;
}

.menu-item--large {
	flex-basis: 100%;
	font-size: 36px;
	background: none;
	margin: 0 50%;
}

.menu-item:hover > i {
	opacity: .4;
	color: #000;
}

@media only screen and (min-width: 720px) {
  .menu-item.menu-item--with-v-divider {
    position: relative;
    margin-right: 50px;
  }
  
  .menu-item.menu-item--with-v-divider:after {
    content: '';
    display: inline-block;
    position: absolute;
    right: -30px;
    height: 100%;
    width: 1px;
    background-color: #e5e5e5e5;
  }
  
  .menu-item.menu-item--with-h-dividers {
    position: relative;
    margin: 20px 0;
  }
  
  .menu-item.menu-item--with-h-dividers:after,
  .menu-item.menu-item--with-h-dividers:before {
    content: '';
    position: absolute;
    width:  325px;
    height: 1px;
    top: 20px;
    background-color: #e5e5e5;
  }
  
  
  .menu-item.menu-item--with-h-dividers:after {
    right: 15px;
  }
  .menu-item.menu-item--with-h-dividers:before {
    left: 15px;
  }
}
<!DOCTYPE html>
<html>
<head>
	<title>menu 2</title>
	<link rel="stylesheet" href="menu-2.css">
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
	<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
	<div class="menu-container">
		<div class="menu-item menu-item--with-v-divider">
			<i class="far fa-list-alt"></i>
		</div>
		<div class="menu-item">
			<i class="fas fa-user"></i>
		</div>
		<div class="menu-item menu-item--large menu-item--with-h-dividers">
			<i class="fas fa-sign-in-alt"></i>
		</div>
		<div class="menu-item menu-item--with-v-divider">
			<i class="fas fa-plus"></i>
		</div>
		<div class="menu-item">
			<i class="fas fa-pencil-alt"></i>
		</div>		
	</div>
</body>
</html>