Divide diagonal mosaic in CSS

Asked

Viewed 1,285 times

3

I need to create a menu with diagonal div that when you pass the mause above for color and each image contains a link to a different category.

The problem is that I can’t create a diagonal div, and I wanted to find a solution to the problem just using html and css if possible.

How can I create a mosaic menu like this?

In case just making geometric shapes does not work for me as it has the background image.

I need to distort the size of the Divs to look like the image.

Some people are saying that it is duplicate and even voting against my post, so I will show that the problem is not so simple. The problem is much deeper than the geometric shapes by which in the scheme of border do not make a background image. Just add a background color.

And by the skew you can put a background-image, but I can’t make the images have the right angle, because it distorts both sides of the angle.

#trapezoid { 
  border-top: 200px solid transparent;
  border-left: 0px solid white;
  border-right: 40px solid white;
  width: 150px;
  float: left;

  background-image: url('http://pongnamuroms.weebly.com/uploads/1/3/3/6/13369650/144214_orig.png');
  opacity: 0.7;
  background-size: calc(100% + 100px);
}

#trapezoid2 { 
  border-bottom: 200px solid transparent;
  border-left: 40px solid white;
  border-right: 0px solid white;
  width: 150px;
  margin-left: -34px;
  float: left;

  background-image: url('http://www.androidspin.com/wp-content/uploads/2015/05/flow_aquablue.png');
  opacity: 0.7;
  background-size: calc(100% + 100px);
}

#trapezoid2:hover{
  opacity: 1;
}

#trapezoid:hover{
  opacity: 1;
}
<div id="trapezoid">

</div>
<div id="trapezoid2">
</div>

<div id="trapezoid3">

</div> 

inserir a descrição da imagem aqui

  • 1
  • 1

    @I even thought about voting for them as the same thing, but with a background image it changes a little. The techniques mentioned there work for solid colors. Border and Skew are problematic with background images. SVG is a possibility, but as it has things written inside, it is not enough to make mere polygons. I come to think that there is a very different answer here than the links indicated.

2 answers

4


follows an example using SVG

var trapezio1 = document.getElementById("trapezio1");
var trapezio2 = document.getElementById("trapezio2");

trapezio1.addEventListener("click", function () {
  alert("Parque");
});

trapezio2.addEventListener("click", function () {
  alert("Praia");
});
.trapezio {  
  stroke:black;
  stroke-width:1;
  filter:url("#desaturate")
}

#trapezio1 {
  fill:url(#img1);        
}

#trapezio2 {
  fill:url(#img2);
}

.trapezio:hover  {
  filter: none;
  cursor: pointer;
}
<svg viewBox="0 0 455 250" >
  <polygon id="trapezio1" class="trapezio" points="  0,0 250,0 200,250   0,250" />
  <polygon id="trapezio2" class="trapezio" points="255,0 455,0 455,250 205,250" />      
  <defs>
    <pattern id="img1" width="1" height="1">
      <image xlink:href="http://crossorigin.me/http://www.travelandleisure.com/sites/default/files/field/image/local-experts-shanghai-most-beautiful-parks.jpg" 
             width="250" height="250" />
    </pattern>
    <pattern id="img2" width="1" height="1">
      <image xlink:href="http://crossorigin.me/http://www.travelandleisure.com/sites/default/files/field/image/local-experts-bangkok-best-beaches.jpg" 
             width="250" height="250"/>
    </pattern>
  </defs>
  <filter id="desaturate">
    <feColorMatrix type="matrix" 
                   values="0.3333 0.3333 0.3333 0 0
                           0.3333 0.3333 0.3333 0 0
                           0.3333 0.3333 0.3333 0 0
                           0      0      0      1 0"/>
  </filter>
</svg>

  • Interesting, this solution falls like a glove to the author of the topic. Very good!

  • I made a change to use viewbox instead of viewport, so it is easier to manipulate the svg size and maintain the proportions.

  • It has how to put the images in grayscale, and when pass over color?

  • @Felipejorge, I applied a filter to put the gray scale image.

3

You may be interested in the css3 skew method. Let’s say you can "skew" your div according to your preferences:

I have my div:

<div id="divteste">olá mundo</div>

And my style sheet:

div {
    width: 300px;
    height: 100px;
    background-color: yellow;
    border: 1px solid black;
}

div#divteste {
    -ms-transform: skew(20deg,10deg); /* IE 9 */
    -webkit-transform: skew(20deg,10deg); /* Safari */
    transform: skew(20deg,10deg); /* Standard syntax */
}

Example in Jsfiddle: https://jsfiddle.net/sab09dfq/2/

More about the skew method: http://www.w3schools.com/css/css3_2dtransforms.asp

Already change the color when passing the mouse, you want to know more about Hover. It can be done both in css, as in js.

More about the selector Hover: http://www.w3schools.com/cssref/sel_hover.asp

  • Could you put background image in your examples to validate the technique? A problem in your fiddle that I noticed early on is the distortion of the text.

  • Hello Bacco, in fact the skew will distort the entire contents of the div, including the background image. There is a way around this if you apply the inverted skew in the div text for example: https://jsfiddle.net/7qhgaw6u/

  • Missing the display:block on . text - Anyway, I think that this solution will not serve the case of the author, because we could not do "unskew" in the background. This skew solution had already been presented in another question, but had no image in the requirements: http://answall.com/q/100110/70 - An alternative would be to make a clip in div, but is accepted by few browsers. http://caniuse.com/#search=clip-path

  • Serve serves, but in case the author will have to create a pseudo element containing the image, as in this topic: http://stackoverflow.com/questions/18289643/how-do-i-unskew-background-image-in-wedske-layer-css Fiddle with the distortion-free image in the skew-applied div.: http://jsfiddle.net/qNW8j/177/ http://jsfiddle.net/qNW8j/177/

  • If you post something in this line you can value the answer a bit. By the way, you can use the snippets of Sopt itself if you want, pro demo stay in the answer. The good of the pseudo-element is that there you can already put the image and the text, to take the distortion together. Even so, you’ll have to do some tricks in the case of trapezoidal images.

  • 1

    But can’t we leave one of the angles straight and other crooked as in the example of images? Because in the case of the example you gave the two sides distort, but in the images above it is sometimes only a side that has to distort.

  • The skew works with the x and y axes, you can control both Transform: (10deg, -25deg); In the end Bacco may be right, this is not the most appropriate solution for your problem. And if you cut out the Divs backgrounds and instead of working the div shape, simply overwrite the Divs with the cut-out backgrounds via z-index?

  • The serious problem the Hover, that it would not be right for all Divs, because a div would overwrite the other.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.