How to create link with hover showing sides traces

3

What I want to do

I want to create a menu and for each link I want to apply a hover effect. When the user hovers the mouse will show strokes on the sides of the link.

Image Example

Follow here as I'm trying to do. When I hover over "home" I want these 2 lines to appear next. I tried to use <hr> or <span> and did not succeed.

What I've tried

<li>
    <a href="" title="Home"><p><span>__</span>home</p></a>
</li>

I tried to put the span tag next to the p, but the paragraph took up 100 of the width, so it was on another line, so I tried to put that span next to the paragraph, so it looks good, but looks good on the "floor" .

Update

<li>
    <a href="" title="Home">
        <div class="traco"></div>
        <div class="textoMenu">home</div>
        <div class="traco"></div>
</li>

<li>
    <a href="" title="Empresa">
        <div class="traco"></div>
        <div class="textoMenu">a empresa</div>
        <div class="traco"></div>
</li>   

CSS

.traco {
    width: 20px;
    height: 1px;
    background-color: transparent;
    float: left;
    margin-top: 8px;                            
}

.textoMenu{
    .colorFonte(@corTodasLetras);
    text-align: center;
    font-family: Georgia;
    font-size: 14px;
    .marginReduzida(0,10px);
    float: left;
}

Now I just need to find a way to center these links.

    
asked by anonymous 02.09.2014 / 15:19

6 answers

2

The answer from Fernando is basically correct. Some properties have been added to allow line size adjustment:

ul > li > a {
    display: inline-block;
    background-color: #ccc;
    text-align: center;
    position: relative;
    padding-left: 50px;
    padding-right: 50px;
}

    ul > li > a:hover::after {
        content: "";
        display: inline-block;
        border-top: solid 1px black;
        width: 50px;
        height: 1px;
        top: 50%;
        position: absolute;
        right: 1px;
    }

    ul > li > a:hover::before {
        content: "";
        display: inline-block;
        border-top: solid 1px black;
        width: 50px;
        height: 1px;
        top: 50%;
        position: absolute;
        left: -1px;
    }

Here goes the JSFiddle with the simulation.

    
02.09.2014 / 16:31
4

I have prepared a version where you can control various features of the trace and other elements with CSS, so as not to be limited to the width of the "minus sign" or other characters, and have precise control over the design. >

The advantage of this solution is that you do not need to enter any foreign characters in the HTML content, definitely separating the content from the visual.

HTML:

<div id="menu">
    <a href="#">
        <span class="traco"></span>
        <span class="link">HOME</span>
    </a>
    <a href="#">
        <span class="traco"></span>
        <span class="link">CONTATO</span>
    </a>
</div>

CSS:

#menu a {
   display:block;
   position:relative;
   float:left;                       /* somente se o menu for horizontal */
   overflow:auto;
   text-decoration:none;
   color: #422;
   background-color: #422;
}

#menu a span.link {
   position:relative;
   display:block;
   margin: 0 20px;                   /* Largura do traço */
   padding: 0 4px;                   /* Espaço entre o traço e as letras */
   z-index: 10;
   color: #ffe;
   background-color: #422;
}

#menu a span.traco {
   display:block;
   position:absolute;
   width:100%;
   height: 50%;
   z-index: 5;
   border-bottom: 1px solid #422;   /* Espessura do traço */
}

#menu a:hover span.traco {
   border-color: #ffe;              /* Cor do traço */
}
  

See working at JS Fiddle

    
02.09.2014 / 15:55
2

I would solve this problem using Pseudo CSS Classes and Pseudo Elements . Using the pseudo: : hover , :: after , and :: before .

Would create a rule similar to this:

ul > li > a:hover::after{    
    content : '-';
}

ul > li > a:hover::before{    
    content : '-';
}

And would format the content as needed;

Follow a online example .

Example 2 , controlling the positioning of the stroke.

To set the row size you can add more "-" in the content property, and decrease the space between the letters with the css property letter-spacing , making a continuous stroke effect is an option. Follow here an example .

    
02.09.2014 / 15:53
1

I understood, from what I saw, you'll have to have two strokes even, it might be with float: left, something like this:

<div class="traco"></div>
<div class="divDomeio"></div>
<div class="traco"></div>

Do not use span to create a new class.

    
02.09.2014 / 15:38
0

You can do with spaces and line-through in hover , like this:

HTML:

<ul>
    <li>
        <a href="" title="Home"><span>&nbsp;&nbsp;&nbsp;&nbsp;</span>home<span>&nbsp;&nbsp;&nbsp;&nbsp;</span></a>
    </li>
</ul>

CSS:

a span {
    display:inline-block;
    padding: 10px;
}

a:hover span {
    text-decoration:line-through;
}

See working in JSFiddle

    
02.09.2014 / 16:04
0

First of all, I thank all those who came forward to help me. I chose the @OnoSendai answer using pseudo-elements.

Code

CSS

ul > li > a {
    display: inline-block;
    background-color: #ccc;
    text-align: center;
    position: relative;
    padding-left: 50px;
    padding-right: 50px;
}

ul > li > a:hover::after {
    content: "";
    display: inline-block;
    border-top: solid 1px black;
    width: 50px;
    height: 1px;
    top: 50%;
    position: absolute;
    right: 1px;
}

ul > li > a:hover::before {
    content: "";
    display: inline-block;
    border-top: solid 1px black;
    width: 50px;
    height: 1px;
    top: 50%;
    position: absolute;
    left: -1px;
}

HTML

<ul>
    <li><a href="" title="Home">home</a></li>
    <li><a href="" title="A Empresa">a empresa</a></li>
</ul>
    
02.09.2014 / 17:02