2016-11-17 113 views
2

您將如何繪製一條對角線,該對角線總是具有11°的角度並固定每端的圓角。它將被用作圖像的重疊,如下面的例子:帶有固定角度的對角線與css中的曲線

enter image description here

灰色區域應該是在上面,和藍色。將所述圖像。

理想情況下,可以通過純CSS或data:image/svg + xml作爲背景圖像進行樣式化。

也許類似於在圖像頂部使用::after選擇器。或者甚至可能是內容div上的::before選擇器將被添加到它。例如類似的東西:

<div class="card"> 
    <div class="header"> 
    <img src="#" /> 
    </div> 
    <div class="content"> 
    <h1>Title</h1> 
    <p>Body copy</p> 
    </div> 
</div> 

<style> 
    .content:before { 
    content: ''; 
    display: block; 
    width: 100%; 
    } 
</style> 
+0

那麼你的建議解決方案有什麼問題? – Turnip

+0

如果你可以使用SVG很好。但對於CSS + HTML來說,這很難實現,因爲你需要傾斜轉換,並且你會遇到文本問題等。你是否在圖層上嘗試了一個圖像? –

回答

4

您可以部署::before::after僞元素。

每個僞元件使用的組合:

  • absolute positioning
  • border
  • border-radius
  • box-shadow
  • transform: skewY

div { 
 
display:inline-block; 
 
position: relative; 
 
width: 256px; 
 
height: 140px; 
 
margin-right:32px; 
 
background: url('/my-image.jpg') no-repeat rgb(15,150,196); 
 
overflow: hidden; 
 
} 
 

 
div::before, div::after { 
 
content: ''; 
 
position: absolute; 
 
left: 0; 
 
display: block; 
 
width: calc(100% - 12px); 
 
height: 100%; 
 
border-width: 3px 6px; 
 
border-style: solid; 
 
border-color: rgb(178,178,178); 
 
border-radius: 9px; 
 
box-shadow: 36px -36px 0 36px rgb(178,178,178), -2px 2px 0 2px rgb(178,178,178); 
 
transform: skewY(11deg); 
 
} 
 

 
div::before { 
 
bottom: 50%; 
 
} 
 

 

 
div::after { 
 
top: 50%; 
 
box-shadow: -36px 36px 0 36px rgb(178,178,178), 2px -2px 0 2px rgb(178,178,178); 
 
} 
 

 
.with-image { 
 
background: url('https://images.pexels.com/photos/115045/pexels-photo-115045.jpeg?w=940&h=650&auto=compress&cs=tinysrgb') no-repeat 0 0/256px 140px rgb(15,150,196); 
 
} 
 

 
.with-image::before, .with-image::after { 
 
border-color: rgb(0,127,0); 
 
box-shadow: 36px -36px 0 36px rgb(0,127,0), -2px 2px 0 2px rgb(0,127,0); 
 
} 
 

 
.with-image::after { 
 
box-shadow: -36px 36px 0 36px rgb(0,127,0), 2px -2px 0 2px rgb(0,127,0); 
 
}
<div></div> 
 
<div class="with-image"></div>

+1

這似乎很好。做得好 –

1

我認爲這段代碼可以幫助你達成你所要做的。

.container{ 
 
    width:300px; 
 
    margin-top:30px; 
 
} 
 
.imagecontainer{ 
 
    width:100%; 
 
    height:200px; 
 
    background:blue; 
 
    transform: skew(0deg, 11deg); 
 
    border-radius:10px; 
 
    margin-bottom:4px; 
 
}
<div class="container"> 
 
    <div class="imagecontainer"></div> 
 
    <div class="imagecontainer"></div> 
 
    <div class="imagecontainer"></div> 
 
</div>

+0

這也會扭曲圖像。我會想象,這不是OP想要的:https://jsfiddle.net/f5Lj3u8t/ – Turnip

+0

哦,是的!你是對的。在這種情況下,試試這個https://jsfiddle.net/f5Lj3u8t/2/ –