Use links in geometric shapes

Asked

Viewed 175 times

2

I have the following code:

.menu .trapezioinvertido{
	float:left;
	border-top: 70px solid #c1c1c1;
	border-left: 30px solid transparent;
	border-right: 15px solid transparent;
	height: 0;
	width: 120px;
	color:#000;
	text-align: center;
	line-height:0px;
list-style-type:none;

}
<ul class="menu">
	<li><a class="trapezioinvertido" href="#">Link</a></li>
</ul>

I have a menu where each item will be of a different geometric shape, this is in the li’s.

I want the text "Link" to stay exactly inside the trapeze, which I can do?

  • You want the whole trapeze to have the href, or just the Link?

  • Whatever. If it’s not harder, I’d rather Link just keep href.

  • Anyone can answer?

  • The tricky thing is that your element has no height (height:0), then you’ll have to use absolute position as Randrade suggested.

  • Even if I assign a height the text does not align in the center because the shape is made with border. Has some solution?

  • That’s what I said, and Randrade’s response is a solution.

Show 1 more comment

2 answers

6

Use the Randrade proposal or change its implementation.

There is no way to center the link without the position is absolute because its element has no height defined, you have chosen to do the visual part with edges and there are problems of this approach. Try to answer: How to vertically center a text on a height-free element?

I would still bet on SVG. Create the link with the tag <text> and group the elements (geometric figure and link) with the tag <g>:

svg { fill: #1dd2af }
svg text { fill: #fff }
svg text:hover { fill: #333 }
<svg width='200' height='100'>
  
  <g id='figura-com-texto'>
    <polygon points='100,50 0,0 200,0 150,150' />
    <a xlink:href='/'>
      <text x="70" y="30">StackOverflow</text>
    </a>
  </g>
  
</svg>

  • 2

    +1 for the use of svg. I would also choose to go that way.

  • I would also choose

  • 1

    Lightweight with animation support and full svg+1 control.

5

You can put the class trapézioInvertido in a div, and call your list within it.

.trapezioinvertido{
	float:left;
	border-top: 70px solid #c1c1c1;
	border-left: 30px solid transparent;
	border-right: 15px solid transparent;
	height: 0;
	width: 120px;
	color:#000;
    line-height:0px; /* Remover esta linha caso quera mais de 1 item na lista ou aumentar o line-height*/
list-style-type:none;

}

.lista{
    position: absolute;   
    top:2%;
}
<div class="trapezioinvertido">
    <ul class="lista">
        <li><a href="#">Link</a></li> 
        <!--<li><a href="#">Link1</a></li> -->
    </ul>
</div>

I created the class lista defining the position as absolute and his position.

Edit

To add to a list, you should add position: relative in the list and change the position of each <a href="#">, in this way:

.trapezioinvertido{
    padding: 10px;
	border-top: 70px solid #c1c1c1;
	border-left: 30px solid transparent;
	border-right: 15px solid transparent;
	height: 0;
	width: 120px;
	color:#000;
	text-align: center;
	line-height:0px;
    list-style-type:none;
    position: relative;
}

li a.link{
    position: absolute;
    bottom: 60px;
    left: 50px;
}
<ul>
	<li class="trapezioinvertido" ><a href="#" class="link">Link</a></li>
    <li class="trapezioinvertido" ><a href="#" class="link">Link2</a></li>
    <li class="trapezioinvertido" ><a href="#" class="link">Link3</a></li>
</ul>

To better understand the positions, see this response.

  • Won’t that get in the way of the code and the page? I refer to the Absolute position and make a menu with Divs instead of li’s and as usual. 'Cause I’ll have other items on the menu that will have other forms, not just this.

  • 1

    @Tiagop. C You have not explained what the menu would be for. If each li is a geometric shape, this is not the best approach, and if so, edit your question with this information, I elaborate a more appropriate answer.

  • I edited the question @Randrade

  • @Tiagop. C See if this issue suits you.

  • Turns out I’m gonna have to put another menu item with another shape on the side, then it won’t work!

  • 1

    @Tiagop. C Edit your question and put all the necessary information you need, so I can help you.

Show 1 more comment

Browser other questions tagged

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