如何制作卡车/货车的后视镜形状?

时间:2014-12-08 09:52:28

标签: css html5 css3 css-shapes

.circle {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: yellow;
  position: absolute;
  z-index: 20;
  border: 1px solid #AAAAAA;
  box-shadow: 0 0 8px 2px yellow;
}
.roof {
  width: 280px;
  height: 80px;
  background-color: gray;
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
  left: 0;
  z-index: -2;
  position: relative;
}
.windscreen {
  width: 260px;
  height: 75px;
  background: rgb(41, 137, 216);
  /* Old browsers */
  background: -moz-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(10%, rgba(41, 137, 216, 1)), color-stop(22%, rgba(170, 170, 170, 1)), color-stop(35%, rgba(41, 137, 216, 1)), color-stop(50%, rgba(170, 170, 170, 1)), color-stop(65%, rgba(41, 137, 216, 1)), color-stop(78%, rgba(170, 170, 170, 1)), color-stop(90%, rgba(41, 137, 216, 1)), color-stop(100%, rgba(41, 137, 216, 1)));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* IE10+ */
  background: linear-gradient(135deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#2989d8', endColorstr='#2989d8', GradientType=1);
  /* IE6-9 fallback on horizontal gradient */
  margin-left: 10px;
  box-shadow: inset 0 0 0 2px #000;
  /*not sure of this*/
}
.roof-bonnet {
  height: 320px;
  margin-top: -100px;
}
#numberplate {
  height: 40px;
  width: 190px;
  background-color: white;
  bottom: 10px;
  position: relative;
  margin-top: -45px;
  margin-left: 44px;
  border: 2px solid black;
  font-size: auto;
  text-align: center;
}
.circle-headlight {
  margin-top: -55px;
  background-color: white;
  box-shadow: 0 0 8px 2px white;
}
.right-headlight {
  margin-left: 238px;
}
<div class="circle"></div>
<div class="circle" style="margin-left:240px;"></div>


<div class="roof"></div>

<div class="windscreen"></div>

<div class="roof roof-bonnet"></div>
<div class="circle circle-headlight"></div>
<div class="circle circle-headlight right-headlight"></div>
<div id="numberplate">REG PLATE</div>

我一直在尝试用CSS生成卡车/卡车,但是生成侧镜时遇到了一些困难,我认为这主要是由于我position的设计/使用不当。

我目前有上面的代码,它产生基本的卡车形状(不完全是完整的)。

我想补充一下:

         +-------+
|        |        \
|        |         |
|       /|         |
|      //|         |
|     // |         |
|    // /|         |
|   // //|         |
|  // // |_________/
--- +//
-----+  
|
|

到任何一方。

如果没有“打破”当前笔,有人会有任何建议吗?


到目前为止,我尝试使用以下方法生成此形状:

#wing-mirror {
    margin-top:40px;
    width: 100px;
    height: 50px;
    background: red;
    position: relative;
    transform: rotate(-90deg);
    border-radius:20px;
}

#wing-mirror:after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 10px solid red;
    border-left: 29px solid #fff;
    border-right: 29px solid #fff;
    width: 42px;
    height: 0;
}

经过多次尝试,并撰写此问题后,


我创造了这个'后视镜',但由于我正在使用特定的值,将这个'下一个'放在我的卡车上,对齐方式中断,因此没有任何'发动机罩/挡风玻璃/车顶正确对齐,无论我在哪里把这个后视镜放在我的HTML中。

我完成的后视镜看起来像这样:

#wing-mirror {
  margin-top: 40px;
  width: 100px;
  height: 50px;
  background: gray;
  position: relative;
  transform: rotate(-90deg);
  border-radius: 20px;
}
#wing-mirror:after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 10px solid gray;
  border-left: 29px solid #fff;
  border-right: 29px solid #fff;
  width: 42px;
  height: 0;
}
.border-div {
  width: 75px;
  background-color: gray;
  height: 10px;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  transform: rotate(45deg);
  margin-left: 30px;
}
.border-div-top {
  width: 50px;
  background-color: gray;
  height: 10px;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  transform: rotate(45deg);
  margin-left: 50px;
  margin-top: -20px;
}
<div id="wing-mirror"></div>
<div class="border-div"></div>
<div class="border-div-top"></div>

但是我怎么能把这个附在我的整车上?因为我尝试的所有东西都会导致'跳跃'元素(它们不会“粘在”卡车上)并且它们没有正确对齐元素的一侧(看起来不是卡车的左侧或右侧) ,可能是因为执行不好)。

2 个答案:

答案 0 :(得分:16)

  

免责声明:我不相信这种方法最适合创建您所追求的形状。根据您的使用情况,您应该查看SVG或普通的png / jpeg。

我为每个镜子添加了一个元素,并在镜像臂的.windscreen div上使用带有边框的倾斜伪元素。

为了定位镜子,我将position:relative;添加到.windscreen div并将镜像作为该div的子项插入。通过这种方式,您可以使用绝对定位将镜子相对于挡风玻璃左右放置。

<强> DEMO

body{
    padding-left:100px;
}
.circle {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: yellow;
  position: absolute;
  z-index: 20;
  border: 1px solid #AAAAAA;
  box-shadow: 0 0 8px 2px yellow;
}
.roof {
  width: 280px;
  height: 80px;
  background-color: gray;
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
  left: 0;
  z-index: -2;
  position: relative;
}
.windscreen {
    position:relative;
  width: 260px;
  height: 75px;
  background: rgb(41, 137, 216);
  /* Old browsers */
  background: -moz-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(10%, rgba(41, 137, 216, 1)), color-stop(22%, rgba(170, 170, 170, 1)), color-stop(35%, rgba(41, 137, 216, 1)), color-stop(50%, rgba(170, 170, 170, 1)), color-stop(65%, rgba(41, 137, 216, 1)), color-stop(78%, rgba(170, 170, 170, 1)), color-stop(90%, rgba(41, 137, 216, 1)), color-stop(100%, rgba(41, 137, 216, 1)));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* IE10+ */
  background: linear-gradient(135deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#2989d8', endColorstr='#2989d8', GradientType=1);
  /* IE6-9 fallback on horizontal gradient */
  margin-left: 10px;
  box-shadow: inset 0 0 0 2px #000;
  /*not sure of this*/
}
.roof-bonnet {
  height: 320px;
  margin-top: -100px;
}
#numberplate {
  height: 40px;
  width: 190px;
  background-color: white;
  bottom: 10px;
  position: relative;
  margin-top: -45px;
  margin-left: 44px;
  border: 2px solid black;
  font-size: auto;
  text-align: center;
}
.circle-headlight {
  margin-top: -55px;
  background-color: white;
  box-shadow: 0 0 8px 2px white;
}
.right-headlight {
  margin-left: 238px;
}
.windscreen:after{
    content:'';
    position:absolute;
    left:100%; bottom:15px;
    width:10px; height:20px;
    transform:skewX(-30deg);
    border-bottom:12px solid #000;
    border-right: 12px solid #000;
    z-index:-1;
}
.windscreen:before{
    content:'';
    position:absolute;
    right:100%; bottom:15px;
    width:10px; height:20px;
    transform:skewX(30deg);
    border-bottom:12px solid #000;
    border-left: 12px solid #000;
    z-index:-1;
}
.mirror{
    position:absolute;
    left:100%; top:20px;
    margin-left:19px;
    height: 30px;
    width:20px;
    background:#000;
}

.mirror:before, .mirror:after{
    content:'';
    position:absolute;
    width:100%;
    box-sizing:border-box;
    bottom:100%;
    border-color: #000 transparent;
    border-width: 0 5px 10px 0;
    border-style:solid;
}
.mirror:after{
    bottom:auto;
    top:100%;
    border-width: 7px 3px 0 0;
}
.mirrorL{
    position:absolute;
    right:100%; top:20px;
    margin-right:19px;
    height: 30px;
    width:20px;
    background:#000;
}

.mirrorL:before, .mirrorL:after{
    content:'';
    position:absolute;
    width:100%;
    box-sizing:border-box;
    bottom:100%;
    border-color: #000 transparent;
    border-width: 0 0 10px 5px;
    border-style:solid;
}
.mirrorL:after{
    bottom:auto;
    top:100%;
    border-width: 7px 0 0 3px;
}
<div class="circle"></div>
<div class="circle" style="margin-left:240px;"></div>


<div class="roof"></div>

<div class="windscreen"><div class="mirror"></div><div class="mirrorL"></div></div>

<div class="roof roof-bonnet"></div>
<div class="circle circle-headlight"></div>
<div class="circle circle-headlight right-headlight"></div>
<div id="numberplate">REG PLATE</div>

答案 1 :(得分:14)

这样的东西?

我强烈建议您使用相对定位的父母来装载您的卡车。然后,我将使用基于百分比的单位来确定元素的大小和位置,以便父级的宽度和高度可以改变,但是内部的形状可以改变。

该示例使用相对定位的父级,以便所有子元素可以相对于父级而不是主体定位。我还将所有内容都更改为基于百分比的宽度和位置,这样当您更改父级的宽度和高度时,卡车将在其内部缩放。

#lorry2 {
	position: relative;
	width: 260px;
	height: 320px;
	float: left;
}
#lorry2 .roof {
	height: 20%;
	background-color: gray;
	border-top-left-radius: 50%;
	border-top-right-radius: 50%;
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
}
#lorry2 .windscreen {
	height: 20%;
	background: rgb(41, 137, 216);
	/* Old browsers */
	background: -moz-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
	/* FF3.6+ */
	background: -webkit-gradient(linear, left top, right bottom, color-stop(10%, rgba(41, 137, 216, 1)), color-stop(22%, rgba(170, 170, 170, 1)), color-stop(35%, rgba(41, 137, 216, 1)), color-stop(50%, rgba(170, 170, 170, 1)), color-stop(65%, rgba(41, 137, 216, 1)), color-stop(78%, rgba(170, 170, 170, 1)), color-stop(90%, rgba(41, 137, 216, 1)), color-stop(100%, rgba(41, 137, 216, 1)));
	/* Chrome,Safari4+ */
	background: -webkit-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
	/* Chrome10+,Safari5.1+ */
	background: -o-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
	/* Opera 11.10+ */
	background: -ms-linear-gradient(-45deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
	/* IE10+ */
	background: linear-gradient(135deg, rgba(41, 137, 216, 1) 10%, rgba(170, 170, 170, 1) 22%, rgba(41, 137, 216, 1) 35%, rgba(170, 170, 170, 1) 50%, rgba(41, 137, 216, 1) 65%, rgba(170, 170, 170, 1) 78%, rgba(41, 137, 216, 1) 90%, rgba(41, 137, 216, 1) 100%);
    /* W3C */
    filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#2989d8', endColorstr='#2989d8', GradientType=1);
	/* IE6-9 fallback on horizontal gradient */
	box-shadow: inset 0 0 0 2px #000;/*not sure of this*/
	position: absolute;
	top: 20%;
	left: 4%;
	right: 4%;
	z-index: 5;
}
#lorry2 .bonnet {
    background-color: gray;
	border-top-left-radius: 50%;
	border-top-right-radius: 50%;
	height: 86%;
	position: absolute;
	top: 14%;
	left: 0;
	right: 0;
}
#lorry2 #numberplate {
	height: 10%;
	background-color: white;
	bottom: 10px;
	position: relative;
	border: 2px solid black;
	font-size: auto;
	text-align: center;
	position: absolute;
	top: 80%;
	left: 15%;
	right: 15%;
}
#lorry2 .headLight {
	position: absolute;
	top: 81%;
	left: 1%;
	width: 13%;
	padding-top: 13%;
	height: 0;
	background-color: white;
	font-size: auto;
	text-align: center;
	z-index: 1;
	border-radius: 50%;
	box-shadow: 0 0 8px 2px #fff;
}
#lorry2 .headLight.right {
	left: auto;
	right: 1%;
}
#lorry2 .fullBeam {
	position: absolute;
	top: 3%;
	left: 1%;
	width: 13%;
	padding-top: 13%;
	height: 0;
	background-color: yellow;
	font-size: auto;
	text-align: center;
	z-index: 1;
	border-radius: 50%;
	box-shadow: 0 0 8px 2px yellow;
}
#lorry2 .fullBeam.right {
	left: auto;
	right: 1%;
}
#lorry2 .wingMirror.left {
	position: absolute;
	width: 15%;
	height: 0;
	padding-top: 15%;
	top: 25%;	
	left: -11%;
}
#lorry2 .wingMirror.right {
	position: absolute;
	width: 15%;
	height: 0;
	padding-top: 15%;
	top: 25%;
	right: -11%;
}
#lorry2 .wingMirror.right .barBottom {
	position: absolute;
	bottom: 0;
	left: 0;
	background: #828282;
	width: 42%;
	height: 20%;
}
#lorry2 .wingMirror.right .barDiagonal {
	position: absolute;
	bottom: 23%;
	left: 20%;
	background: #828282;
	width: 62%;
	height: 20%;
	-moz-transform: rotate(-60deg);
	-webkit-transform: rotate(-60deg);
	-o-transform: rotate(-60deg);
	-ms-transform: rotate(-60deg);
	transform: rotate(-60deg);
}
#lorry2 .wingMirror.right .mirror {
	position: absolute;
	bottom: 15%;
	left: 58%;
	height: 100%;
	width: 60%;
	background: #828282;
	-webkit-border-radius:  0 30% 30% 0;
	border-radius: 0 30% 30% 0;
}
#lorry2 .wingMirror.left .barBottom {
	position: absolute;
	bottom: 0;
	right: 0;
	background: #828282;
	width: 42%;
	height: 20%;
}
#lorry2 .wingMirror.left .barDiagonal {
	position: absolute;
	bottom: 23%;
	right: 20%;
	background: #828282;
	width: 62%;
	height: 20%;
	-moz-transform: rotate(60deg);
	-webkit-transform: rotate(60deg);
	-o-transform: rotate(60deg);
	-ms-transform: rotate(60deg);
	transform: rotate(60deg);
}
#lorry2 .wingMirror.left .mirror {
	position: absolute;
	bottom: 15%;
	right: 58%;
	height: 100%;
	width: 60%;
	background: #828282;
	-webkit-border-radius: 30% 0 0 30%;
	border-radius: 30% 0 0 30%;
}
<div id="lorry2">
	<div class="fullBeam left"></div>
	<div class="fullBeam right"></div>
	<div class="roof"></div>
	<div class="windscreen"></div>
	<div class="bonnet"></div>
	<div class="headLight left"></div>
	<div class="headLight right"></div>
	<div id="numberplate">REG PLATE</div>
	<div class="wingMirror left">
		<div class="barBottom"></div>
		<div class="barDiagonal"></div>
		<div class="mirror"></div>
	</div>
	<div class="wingMirror right">
		<div class="barBottom"></div>
		<div class="barDiagonal"></div>
		<div class="mirror"></div>
	</div>
</div>