Keep width when using letter-spacing on hover - css

I have a some basic button styles where on :hover I add the letter-spacing property:
.btn {
display: inline-block;
text-align: center;
background-color: transparent;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
font-size: 1rem;
border-radius: 0.25rem;
cursor: pointer;
transition: all 0.2s ease;
}
.btn-primary {
background-color: #8065F1;
color: #FFFFFF;
}
.btn-large {
border-radius: 32px;
box-shadow: 0 2px 80px 0 rgba(74, 74, 74, 0.23);
padding: 0.25rem 3rem;
font-size: 1.5rem;
text-transform: uppercase;
}
.btn:hover {
letter-spacing: 4px;
}
<button type="button" class="btn btn-primary btn-large">Lorem</button>
Is there a way that width doesn't expand? Like adding min/max-width? However the problem is that button elements can contain different string length:
.btn {
display: inline-block;
text-align: center;
background-color: transparent;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
box-shadow: 0 2px 80px 0 rgba($grey, 0.23);
font-size: 1rem;
border-radius: 0.25rem;
cursor: pointer;
transition: all 0.2s ease;
}
.btn-primary {
background-color: #8065F1;
color: #FFFFFF;
}
.btn-large {
border-radius: 32px;
-webkit-box-shadow: 0 2px 80px 0 rgba(74, 74, 74, 0.23);
box-shadow: 0 2px 80px 0 rgba(74, 74, 74, 0.23);
padding: 0.25rem 3rem;
font-size: 1.5rem;
text-transform: uppercase;
min-width: 240px;
}
.btn:hover {
letter-spacing: 4px;
}
<p>I need this "effect" (I added some min-width):</p>
<button type="button" class="btn btn-primary btn-large">Lorem</button>
<p>However it won't work for larger strings</p>
<button type="button" class="btn btn-primary btn-large">Lorem Ipsum</button><br><br>
<button type="button" class="btn btn-primary btn-large">Lorem Ipsum Dolor</button>
I know I can use JS and append the elements fixed width to it, however I'm looking for a CSS solution - if there is one?

On idea to approximate this is to duplicate the text considering a hidden one that has already the letter-spacing and another one on the top that you animate to fill the space already defined by the hidden text:
Here is an idea by making the text color the same as background:
.btn {
display: inline-block;
text-align: center;
background-color: transparent;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
font-size: 1rem;
border-radius: 0.25rem;
cursor: pointer;
transition: all 0.2s ease;
margin-bottom:10px;
}
.btn-primary {
background-color: #8065F1;
color: #FFFFFF;
}
.btn-large {
border-radius: 32px;
box-shadow: 0 2px 80px 0 rgba(74, 74, 74, 0.23);
padding: 0.25rem 3rem;
font-size: 1.5rem;
text-transform: uppercase;
}
.btn::before {
content:attr(data-text);
position:absolute;
left:0;
right:0;
text-align:center;
letter-spacing: initial;
color:#fff;
transition: all 0.2s ease;
}
.btn {
letter-spacing: 4px;
color:#8065F1;
position:relative;
}
.btn:hover::before {
letter-spacing: 4px;
}
<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem">Lorem</button></div>
<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem Ipsum">Lorem Ipsum</button></div>
<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem Ipsum Dolor">Lorem Ipsum Dolor</button></div>
Another one using opacity and both pseudo element in case the background is not a solid color:
.btn {
display: inline-block;
text-align: center;
background-color: transparent;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
font-size: 1rem;
border-radius: 0.25rem;
cursor: pointer;
transition: all 0.2s ease;
margin-bottom:10px;
}
.btn-primary {
background: linear-gradient(#8065F1,purple);
color: #FFFFFF;
}
.btn-large {
border-radius: 32px;
box-shadow: 0 2px 80px 0 rgba(74, 74, 74, 0.23);
padding: 0.25rem 3rem;
font-size: 1.5rem;
text-transform: uppercase;
}
.btn {
position:relative;
font-size:0;
}
.btn::before {
content:attr(data-text);
position:absolute;
left:0;
right:0;
text-align:center;
letter-spacing: initial;
font-size: 1.5rem;
transition: all 0.2s ease;
}
.btn::after {
content:attr(data-text);
letter-spacing: 4px;
opacity:0;
font-size: 1.5rem;
}
.btn:hover::before {
letter-spacing: 4px;
}
<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem">Lorem</button></div>
<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem Ipsum">Lorem Ipsum</button></div>
<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem Ipsum Dolor">Lorem Ipsum Dolor</button></div>

How about turning off box-sizing, then using a percentage for your padding? It should work well no matter what the size of your contents is, but my numbers are probably wrong so you'll need to adjust the numbers to make it accurate.
.wrapper{
display:inline-block;
}
button{
box-sizing: content-box;
padding: 0 10.4%;
border: none;
width: 100%;
font-size:13px;
}
button:hover{
letter-spacing:0.1em;
padding:0;
}
.outer-wrapper{
}
<div class="outer-wrapper">
<div class="wrapper">
<button class='button'>Small Text</button>
</div>
</div>
<div class="outer-wrapper">
<div class="wrapper">
<button class='button'>Much much bigger text</button>
</div>
</div>
<div class="outer-wrapper">
<div class="wrapper">
<button class='button'>Small TextSmall TextSmall TextSmall TextSmall Text</button>
</div>
</div>
Edit: Well, it's...more or less working, except that it turns out to be impossible. No matter how finely I tune it, I'm getting problems because of differences in browser rendering of sub-pixels. (The browser apparently rounds rem differently than it does %, which means this is going to be impossible to do with pure CSS.)

Related

How i can do an underline effect in my menu?

I'm practicing my CSS, so after few tutorials I started to create a simple website. I want to create something like this:
How can I achieve that effect? I was looking for an answer in the internet and I just found only solutions with fancy animations and underline effects, but I couldn't find solution similar to my problem. I only achieved something like this:
This is my code
.button {
background-color: white;
border: none;
color: black;
text-align: center;
text-decoration: none;
cursor: pointer;
margin: 0;
}
.button:hover {
background-color: white;
border-bottom: #55415f 4px solid;
color: #55415f;
text-align: center;
text-decoration: none;
cursor: pointer;
margin: 0;
}
.r_menu {
display: flex;
justify-content: flex-end;
border-bottom: black 3px solid;
gap: 20px;
margin: 0;
}
<div class="r_menu">
<button class="button">Home page</button>
<button class="button">Style demo</button>
<button class="button">Full width</button>
<button class="button">Portfolio</button>
<button class="button">Gallery</button>
<button class="button">Dropdown</button>
</div>
There is a gap and 'movement' on hover because the border is going from none to 4px.
A straightforward way round this is to give a border all the time, it's just that you make it transparent when there isn't a hover. That way there is nothing to move on the hover, the space is already there.
Note: this snippet also makes the long border less wide just to make it a little more like the first image in the question.
.button {
background-color: white;
border: none;
border-bottom: transparent 4px solid;
color: black;
text-align: center;
text-decoration: none;
cursor: pointer;
margin: 0;
}
.button:hover {
background-color: white;
border-bottom: #55415f 4px solid;
color: #55415f;
text-align: center;
text-decoration: none;
cursor: pointer;
margin: 0;
}
.r_menu {
display: flex;
justify-content: flex-end;
border-bottom: black 1px solid;
gap: 20px;
margin: 0;
}
<div class="r_menu">
<button class="button">Home page</button>
<button class="button">Style demo</button>
<button class="button">Full width</button>
<button class="button">Portfolio</button>
<button class="button">Gallery</button>
<button class="button">Dropdown</button>
</div>

Input field and button side by side in center

I have an input field and a button that I want to have horizontally and vertically centered (without transform) side by side in the middle. I can easily fix the centering part, but to place the input field and the button side by side has proven to be much harder. For whatever reason, the button is placed lower than the input field.
The HTML and CSS:
body {
background: #000;
text-align: center;
}
.hd {
font-size: 3.9rem;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
color: rgb(250, 250, 250);
margin-bottom: 30px;
}
.content {
margin: 0;
position: relative;
margin-top: 20vh;
}
.form-control, .btn {
display: inline-block;
padding: 10px 25px;
font-size: 1rem;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
border-radius: 0;
-webkit-appearance: none;
}
.form-group {
display: inline-block;
}
.form-control {
border: 0;
float: left;
width: 250px;
}
*:focus {
outline: none;
}
.form-controlt:focus{
outline: none;
}
.btn {
background-color: lightgreen;
border: 0;
background-color: #ff8e41;
-o-transition: all .3s;
transition: all .3s;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
font-style: normal;
font-weight: 600;
position: relative;
border: 0;
box-sizing: border-box;
color: #fff;
text-align: center;
margin: 0 auto;
-webkit-appearance: button;
cursor: pointer;
}
.btn::-moz-focus-inner {
border: 0;
}
.btn[type=submit]:active {
background: #ed7a18;
box-shadow: 0 1px 0 0 hsla(0,0%,100%,.27),inset 0 1px 0 0 #bc5f10;
}
.btn[type=submit]:hover {
background-color: #ffa353;
-o-transition: all .3s;
transition: all .3s;
}
<div class="content">
<div class="hd">Text</div>
<form action="8dy9s8hsd9.php" method="POST">
<div id="name-group" class="form-group">
<input type="text" class="form-control">
</div>
<button type="submit" class="btn btn-success">Submit<span class="fa fa-arrow-right"></span></button>
</form>
</div>
Put the input and the button elements inside the same div with .form-group class.
body {
background: #000;
text-align: center;
}
.hd {
font-size: 3.9rem;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
color: rgb(250, 250, 250);
margin-bottom: 30px;
}
.content {
margin: 0;
position: relative;
margin-top: 20vh;
}
.form-control, .btn {
display: inline-block;
padding: 10px 25px;
font-size: 1rem;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
border-radius: 0;
-webkit-appearance: none;
}
.form-group {
display: inline-block;
}
.form-control {
border: 0;
float: left;
width: 250px;
}
*:focus {
outline: none;
}
.form-controlt:focus{
outline: none;
}
.btn {
background-color: lightgreen;
border: 0;
background-color: #ff8e41;
-o-transition: all .3s;
transition: all .3s;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
font-style: normal;
font-weight: 600;
position: relative;
border: 0;
box-sizing: border-box;
color: #fff;
text-align: center;
margin: 0 auto;
-webkit-appearance: button;
cursor: pointer;
}
.btn::-moz-focus-inner {
border: 0;
}
.btn[type=submit]:active {
background: #ed7a18;
box-shadow: 0 1px 0 0 hsla(0,0%,100%,.27),inset 0 1px 0 0 #bc5f10;
}
.btn[type=submit]:hover {
background-color: #ffa353;
-o-transition: all .3s;
transition: all .3s;
}
<div class="content">
<div class="hd">Text</div>
<form action="8dy9s8hsd9.php" method="POST">
<div id="name-group" class="form-group">
<input type="text" class="form-control">
<button type="submit" class="btn btn-success">Submit<span class="fa fa-arrow-right"></span></button>
</div>
</form>
</div>
Change:
<div id="name-group" class="form-group">
<input type="text" class="form-control">
</div>
<button type="submit" class="btn btn-success">Submit<span class="fa fa-arrow-right"></span></button>
To:
<div id="name-group" class="form-group">
<input type="text" class="form-control">
<button type="submit" class="btn btn-success">Submit<span class="fa fa-arrow-right"></span></button>
</div>
body {
background: #000;
text-align: center;
}
.hd {
font-size: 3.9rem;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
color: rgb(250, 250, 250);
margin-bottom: 30px;
}
.content {
margin: 0;
position: relative;
margin-top: 20vh;
}
.form-control, .btn {
display: inline-block;
padding: 10px 25px;
font-size: 1rem;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
border-radius: 0;
-webkit-appearance: none;
}
.form-group {
display: inline-block;
}
.form-control {
border: 0;
float: left;
width: 250px;
}
*:focus {
outline: none;
}
.form-controlt:focus{
outline: none;
}
.btn {
background-color: lightgreen;
border: 0;
background-color: #ff8e41;
-o-transition: all .3s;
transition: all .3s;
font-family: Open Sans,Segoe UI,Arial,Verdana,Tahoma,sans-serif;
font-style: normal;
font-weight: 600;
position: relative;
border: 0;
box-sizing: border-box;
color: #fff;
text-align: center;
margin: 0 auto;
-webkit-appearance: button;
cursor: pointer;
}
.btn::-moz-focus-inner {
border: 0;
}
.btn[type=submit]:active {
background: #ed7a18;
box-shadow: 0 1px 0 0 hsla(0,0%,100%,.27),inset 0 1px 0 0 #bc5f10;
}
.btn[type=submit]:hover {
background-color: #ffa353;
-o-transition: all .3s;
transition: all .3s;
}
<div class="content">
<div class="hd">Text</div>
<form action="8dy9s8hsd9.php" method="POST">
<div id="name-group" class="form-group">
<input type="text" class="form-control">
<button type="submit" class="btn btn-success">Submit<span class="fa fa-arrow-right"></span></button>
</div>
</form>
</div>

Bootstrap with navbard fixed dropdown is hidden

I have this problem in bootstrap.
I have made a navbar wich is fixed on top, and the dropdown does work on computer. But when I use mobile view the dropdown does not show.
I have search this and other sites for solution, but none of them have worked for me. The problem seems to be overflow:auto; in mobile view.
Here is my code:http://www.bootply.com/tMdIqM9xSL
HTML:
<div class="row mynav">
<div class="col-md-12 nopadding">
<span class="logo glyphicon glyphicon-home" aria-hidden="true"><label>Hjem</label></span>
<div class="btn-group"><span class="logo glyphicon glyphicon-user" data-toggle="dropdown" aria-hidden="true"><label>Din profil</label><span class="caret"></span></span>
<ul class="dropdown-menu " role="menu">
<li>Login
</li>
<li>My profile
</li>
<li>Add
</li>
<li>Favorites
</li>
<li>Logout
</li>
</ul>
</div>
<span class="logo glyphicon glyphicon-stats align-bottom" aria-hidden="true"><label>Stats</label></span>
<span class="logo glyphicon glyphicon-envelope" aria-hidden="true"><label>Contact us</label></span>
<span class="logo glyphicon glyphicon-search pull-right" data-toggle="collapse" href="#m_showsearch" aria-hidden="true"><label>Search</label></span>
</div>
<div id="m_showsearch" class="panel-collapse collapse container-fluid">
<div class="col-md-4 floatnone">
<div class="panel panel-default search-panel">
<div class="panel-heading">
<h4 class="panel-title"><a data-toggle="collapse" href="#collapseFag">Subject</a></h4>
</div>
<div id="collapseFag" class="panel-collapse collapse in">
<div class="panel-body">
<p> </p><p> </p><p> </p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-1 text-center floatnone">
<button type="button" class="searchbutton btn btn-primary btn-lg pull-center" onclick="findhelp();"><span class="glyphicon glyphicon-search" data-toggle="dropdown" aria-hidden="true"></span>
</button>
<br>
</div>
CSS:
.maincontainer {
padding: 0px;
}
.mynav {
border-bottom: 1px solid rgb(38, 70, 83);
background-color: #FFF;
text-align: left;
box-shadow: 0px 2px 2px #AAAAAA;
z-index: 10;
position: fixed;
width: 100%;
}
#spaceontop {
margin-bottom: 60px;
}
#m_showsearch {} a[title2]:hover:after {
font-size: 10px;
font-weight: thin;
border: 1px solid #000;
background-color: rgb(38, 70, 83);
content: attr(title2);
padding: 10px;
size: 5px;
color: #FFF;
position: absolute;
left: 50%;
top: 50px;
}
.dropdown-menu {
border-radius: 0px;
border: 1px solid rgb(38, 70, 83);
}
.dropdown-menu li a:hover {
background-color: #51b098;
color: #fff;
}
.panel {
box-shadow: 3px 3px 3px 0px;
border: 1px solid #51b098;
border-radius: 0px;
}
.search-panel {
border-radius: 3px;
box-shadow: 0px 0px 0px 0px;
}
.search-panel.panel-heading {
box-shadow: 0px 0px 0px 0px;
}
.panel>.panel-heading {
background-color: #FFF;
/*#51b098;*/
//box-shadow: 0px 2px 2px 0px;
border-radius: 0px;
font-size: 100%;
text-transform: uppercase;
font-family: 'Lato', sans-serif;
}
.panel>.panel-headinga a {
font-size: 20px;
font-weight: bold;
color: #51b098;
}
.panel-body {
// border:1px solid #51b098;
}
.btn-group {
vertical-align: initial;
}
.logo {
font-size: 20px;
height: 60px;
padding: 18px;
padding-left: 20px;
padding-right: 20px;
vertical-align: middle;
border-top: 3px solid transparent;
-webkit-transition: background 0.2s, color 0.2s;
-moz-transition: background 0.2s, color 0.2s;
-ms-transition: background 0.2s, color 0.2s;
-o-transition: background 0.2s, color 0.2s;
transition: background 0.2s, color 0.2s;
text-align: center;
}
.logo:hover {
//background-color:rgb(38, 70, 83);
color: orange;
//text-shadow:-1px -1px #000;
border-top: 3px solid rgb(38, 70, 83);
-webkit-transition: background 0.5s, color 0.2s;
-moz-transition: background 0.5s, color 0.2s;
-ms-transition: background 0.5s, color 0.2s;
-o-transition: background 0.5s, color 0.2s;
transition: background 0.5s, color 0.2s;
}
.logo label {
font-size: 12px;
padding: 14px 0px 14px 0px;
margin: 5px;
font-family: Lato;
text-transform: uppercase;
padding: 0px;
}
.btn,
.btn:hover,
.btn:focus {
//background-color:rgb(38, 70, 83);
padding: 20px;
//color:#FFF;
padding: 10px;
border-radius: 0px;
border: 0px;
}
.panel-default>.panel-heading {
background: transparent;
}
#media(max-width:1000px) {
.logo label:last-child {
display: none;
//visibility:hidden;
}
.logo {
height: 100%;
}
}
#media(max-width:768px) {
.logo label {
display: none;
padding: 20px 15px 15px 0px;
}
.logo {
font-size: 16px;
padding-left: 10px;
padding-right: 10px;
padding: 10px;
}
.div_headline {
padding: 5px;
text-align: center;
width: 100%;
}
.div_headline:after {
display: none;
}
.floatnone {
padding: 0px;
}
#m_showsearch {
padding: 1px;
}
.mynav {
width: 100%;
margin-bottom: 0px;
max-height: 100%;
overflow: auto;
}
.dropdown-menu {
}
}
As I said I have tried every solution I have found, but none those I found worked.
EDIT:
I need the overflow to make the search-button work. When I press the searchbutton a div appears. And the search-div may be large so I need to be able to scroll it.
Just remove overflow: auto. It should work.
.mynav {
width: 100%;
margin-bottom: 0px;
}

Style link as submit button

I have a link and a submit button styled with the same .button class. Is there an easy way to make them look exactly the same (to have a same height)?
body .button {
text-align: center;
line-height: 15px;
background: #3333CC;
border-color: #5033CC;
border-radius: 4px;
color: #fff;
font-size: 11px;
border: 1px solid;
padding: 4px 7px 4px 7px;
cursor: pointer;
min-width: 90px;
text-decoration: none;
display: inline-block;
}
<form>
<a class="button" href="http://www.example.org">Link as button</a>
<input class="button" type="submit" value="button as button"/>
</form>
EDIT: I just found out that they look the same in Google Chrome, but not in Firefox.
In this example, they do have the same height (25pixels). You can always set a height in the CSS.
One thing you missed is changing the font-family.
In this example I've added one just to Helvetica, but that will make them more similar.
You can set box-sizing:border-box, set a solid height (25px in this example) and then change display:inline-block; to float:left;
body .button {
text-align: center;
line-height: 15px;
background: #3333CC;
border-color: #5033CC;
border-radius: 4px;
color: #fff;
font-size: 11px;
border: 1px solid;
padding: 4px 7px 4px 7px;
cursor: pointer;
min-width: 90px;
text-decoration: none;
float:left;
font-family:'Helvetica';
box-sizing:border-box;
height:25px;
}
<form>
<a class="button" href="http://www.example.org">Link as button</a>
<input class="button" type="submit" value="button as button"/>
</form>

How to work with hyperlink and submit buttons together in Foundation Zurb CSS

I really like the Option Button found in the ZURB building block library here: http://zurb.com/building-blocks/option-button
However, I am almost always working with 1 x normal hyperlink button and a submit button. I'm finding this is causing me headaches wherever I try to have neat rows of buttons that should look the same. What additional CSS would get this 'Option Button' to work as desired when working with an input submit class ?
My HTML is pretty straightforward and looks like this:
<form name="article_selection" action="single-article-view.php" method="post">
<div class="row">
<div class="medium-6 columns">
<div class="wrapper">
<label>READ MORE</label>
Read on another website
<input type="hidden" value="<?php echo $article_ID; ?>" name="article_ID" />
<input type="submit" class="inside" name="<?php echo $article_ID; ?>" value="Read on this website" />
</div>
</div>
</div>
</form>
In this scenario the 2nd half of the option button is malformed.
I can't update an image to display due to reputation restrictions (seriously?)
<img src="http://i96.photobucket.com/albums/l185/indoanalytics/Button_Problem_zpszhgpvzbt.png" border="0" alt=" photo Button_Problem_zpszhgpvzbt.png"/>
The CSS as per the building blocks option button (linked to above) is:
.wrapper {
overflow: hidden;
display: inline-block;
border: 2px solid #008cba;
border-radius: 999px;
text-align: center;
width: 100%;
background: #fff;
margin-top: 50px;
}
.wrapper:hover {
border: none;
background-color: #008cba;
transition: background-color 0.3s ease;
}
.wrapper:hover .inside {
display: block;
font-size: 16px;
padding: 22px;
float: left;
}
.wrapper:hover .inside:hover {
transition: background-color 0.3s ease;
background-color: rgba(0, 0, 0, 0.1);
}
.wrapper:hover label {
display: none;
}
.wrapper .inside {
display: none;
color: #fff;
width: 50%;
z-index: 9999;
}
.wrapper label {
padding: 16px;
color: #008cba;
font-size: 16px;
font-weight: bold;
}
Update: I also have separate button stylings before the above in the my CSS which may be causing the problem.
.topic-button {
border-style: solid;
border-width: 1px;
font-family: inherit;
font-weight: bold;
line-height: 1;
position: relative;
text-decoration: none;
text-align: center;
-webkit-border-radius: 3px;
border-radius: 3px;
background: #009fd9;
color: #fff;
box-shadow: rgba(255, 255, 255, .4) 0 1px 0 inset;
border-color: #008cbf;
font-size: 12px;
padding: 5px 6px;
margin-top: 2px;
cursor: pointer;
}
.menu-topic-button {
width:99%;
background: #444444;
border:none;
font-family: inherit;
font-weight: bold;
line-height: 1;
position: relative;
text-decoration: none;
text-align: center;
color: white;
font-size: 14px;
padding: 2px;
margin-top: 2px;
cursor: pointer;
}
I have attempted applying CSS from the above to:
.wrapper button[type="submit"] {}
.wrapper input[type="submit"] {}
For both I have copied the .inline CSS from the zurb building blocks as well as tried resetting CSS values back to zero in both, but no luck.
Any help greatly appreciated.

Resources