Center align multiple lines of text in a button using CSS - html

I'm trying to create a button in CSS with an image as a background.
So far I have been able to get it to work, as long as the Text is only one line.
Now I'm trying to get it to work with a two line button, but it is either braking the layout or like in the jsfiddle not showing the second line.
Here is my CSS section:
#font-face {
font-family: 'easy_street_eps';
src: url('http://www.fontsaddict.com/fontface/easy-street-eps.ttf');
}
#gallery {
list-style: none;
list-style-type: none;
}
#gallery a {
text-decoration: none;
color: #FFF;
}
#gallery a:hover { text-decoration:underline;}
#gallery ul { -webkit-padding-start: 0px; }
#gallery li {
padding: 0 20px 0 0;
display: inline;
background:url("http://imageshack.com/a/img843/2751/gidy.png") no-repeat scroll 0% 0%;
font-family: "easy_street_eps";
font-size:35px;
color: #FFF;
text-align: center;
text-decoration: none;
cursor: pointer;
border: none;
min-width: 200px;
min-height: 140px;
float:left;
line-height : 140px;
padding-top: 4.25px;
transition: all 0.5s ease 0s;
-webkit-transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-o-transition: all 0.5s ease 0s;
}
.gallery2lb {
line-height: 20px;
width: 100px;
max-height: 140px;
}
Here is the jsfiddle: http://jsfiddle.net/2vMPs/
Any help is very much appreciated.
thanks,
Alex

Demo
Defining the parent <li> as display: table and the element <a> itself with vertical-align:middle & display:table-cell, and removing the other unwanted styles as the this will take care of issue.
CSS
li {
display: table;
}
a {
display: table-cell;
vertical-align: middle;
}
#font-face {
font-family:'easy_street_eps';
src: url('http://www.fontsaddict.com/fontface/easy-street-eps.ttf');
}
#gallery {
list-style: none;
list-style-type: none;
}
#gallery a {
text-decoration: none;
color: #FFF;
}
#gallery a:hover {
text-decoration:underline;
}
#gallery ul {
-webkit-padding-start: 0px;
}
#gallery li {
/*padding: 0 20px 0 0;*/
/* display: inline; */
background:url("http://imageshack.com/a/img843/2751/gidy.png") no-repeat scroll 0% 0%;
font-family:"easy_street_eps";
font-size:35px;
color: #FFF;
text-align: center;
text-decoration: none;
cursor: pointer;
border: none;
min-width: 200px;
min-height: 150px;
float:left;
/* line-height : 140px;
padding-top: 4.25 px; */
transition: all 0.5s ease 0s;
-webkit-transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-o-transition: all 0.5s ease 0s;
}
.gallery2lb {
/* line-height: 20px; */
width: 100px;
max-height: 140px;
}
HTML
<div style="text-align: center;">
<div id="gallery">
<ul>
<li><a title="Candyland" href="#">Candyland</a>
</li>
<li class="gallery2lb"><a title="Diese Seite befindet sich noch im Aufbau" href="#">Alice in Wonderland</a>
</li>
<li><a title="Arielle" href="#">Arielle</a>
</li>
<li><a title="Rainbow" href="#">Rainbow</a>
</li>
</ul>
</div>
</div>

You can use em to adjust the font-size on your parent container where the text resides.
#gallery a {
text-decoration: none;
color: #FFF;
font-size: 0.6em;
}
You can also use instead of font-size. vh is CSS new viewport sized typography.
font-size:4.2vh;
Another technique, however, not consistent (for all buttons is) to change this on #gallery li:
line-height : 30px;
padding-top: 30px;
This will give you something like this:

You can't vertically center align multiline text with line-height. Instead, if ancient browser support is not an issue, you can use css3 flexible boxes for centering by adding the following to <li>
display: flex;
justify-content: center;
align-items: center;
Updated fiddle
If older browser support is a requirement you can go with the display:table method mentioned in this answer

Related

Why is the first item moving to the left when I put the mouse over it?

I am new to programming and I wanted to make a navbar where a name is on the left of the navbar and the other navigation items are on the right. I put transfer:scale() effect on the items and all of them on the right side are working good, but the one on the left is moving to the left when the animation is happening. Pls help me how to fix it, so the navigation is not stretched to the left so it works like on the right side.
nav {
display: flex;
padding: 30px;
overflow: hidden;
justify-content: flex-start;
background-color: lightblue;
}
nav a {
line-height: 19px;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: ease-in-out;
transition-delay: 0s;
text-decoration: none;
color: black;
font-family: "Goudy Old Style";
padding: 0px 14px 0 14px;
}
nav a:not(#logo) {
text-align: center;
}
nav a:hover {
color: #b3d0ff;
transform: scale(1.1);
}
<nav>
<a id="logo" style="flex:1" href="index.html">Hidden Logo</a>
EN
CONTACT
PORTFOLIO
ABOUT
HOME
</nav>
Sorry for the messy code.
From MDN:
The scale() CSS function defines a transformation that resizes an element on the 2D plane. Because the amount of scaling is defined by a vector, it can resize the horizontal and vertical dimensions at different scales. Its result is a data type.
With this said, you have flex: 1; on the first child meaning the amount of scaling is increased because of the horizontal dimensions at different scales.
Solution:
Remove flex: 1; and use margin-left: auto; on the second a as an alternative.
nav {
display: flex;
padding: 30px;
overflow: hidden;
justify-content: flex-start;
background-color: lightblue;
}
nav a {
line-height: 19px;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: ease-in-out;
transition-delay: 0s;
text-decoration: none;
color: black;
font-family: "Goudy Old Style";
padding: 0px 14px 0 14px;
}
nav a:nth-child(2) {
margin-left: auto;
}
nav a:not(#logo) {
text-align: center;
}
nav a:hover {
color: #b3d0ff;
transform: scale(1.1);
}
<nav>
<a id="logo" href="index.html">Hidden Logo</a>
EN
CONTACT
PORTFOLIO
ABOUT
HOME
</nav>
Because of the flex it made the item long so the side of it went to the left when scaling. My solution was to use float instead of flex.
nav {
padding: 30px;
overflow: hidden;
background-color: lightblue;
}
nav a {
line-height: 19px;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: ease-in-out;
transition-delay: 0s;
text-decoration: none;
color: black;
font-family: "Goudy Old Style";
padding: 0px 14px 0 14px;
float: right;
}
nav a:not(#logo) {
text-align: center;
}
nav a:hover {
color: #b3d0ff;
transform: scale(1.1);
}
<nav>
<a id="logo" style="float:left; " href="index.html">Hidden Logo</a>
EN
<!--
CONTACT
PORTFOLIO
ABOUT
-->
HOME
</nav>
after
Change:
nav a:not(#logo) {
text-align: center;
}
nav a:hover {
color: #b3d0ff;
transform: scale(1.1);
}
To:
#logo {
text-align: left;
}
nav a:hover {
color: #b3d0ff;
transition: 0.5s;
font-size: 20px;
}
With transform you are moving the logo. I think with transition and font-size you can have what you want. I hope it helps

Space on the right end of menu bar

I'm new to HTML and CSS, and this is my first project.
I have this "space" near the end of the navbar and it forces the next link to break into a new line. Also, I cannot find a solution that removes the gap between the background image and the navbar no matter the screen size.
I'm not able to pinpoint what is causing these problems, and I'm sure it's just some petty mistake.
navbar http://prntscr.com/g0xi6z
.topnavhome nav {
position: relative;
display: block;
margin: 0px;
background-color: #333333;
overflow: hidden;
text-align: justify;
font-size: 0px;
min-width: 500px;
width: 100%;
}
.topnavhome:after {
content: '';
display: inline-block;
width: 100%;
}
.topnavhome .li {
display: inline-block;
}
.topnavhome nav li a {
float: left;
display: block;
padding: 12px;
width: 180px;
margin-bottom: -1.5px;
color: #f2f2f2;
border-left: solid 3px #333333;
text-align: center;
text-decoration: none;
font-family: 'Lato', sans-serif;
font-size: 20px;
-webkit-transition: background .3s linear;
-moz-transition: background .3s linear;
-ms-transition: background .3s linear;
-o-transition: background .3s linear;
transition: background .3s linear;
}
.topnavhome nav a:hover {
background-color: #046A78;
color: white;
border-left: solid 3px #79CBD6;
-moz-transition: background-color 0.01s;
-webkit-transition: background-color 0.01s;
-o-transition: background-color 0.01s;
transition: background-color 0.01s;
}
<ul class="topnavhome" id="myTopnav">
<nav>
<li>Home</li>
<li>Network Security</li>
<li>Passwords</li>
<li>Firewalls</li>
<li>Encryption</li>
<li>Biometric Devices</li>
<li>References</li>
</nav>
</ul>
Fix Error:
* {
box-sizing: border-box;
}
More css for setting nav use :
.topnavhome {
padding: 0;
}
And define height for nav and a element.
body, ul {
margin:0;
}
* {
box-sizing: border-box;
}
.topnavhome {
padding: 0;
}
.topnavhome nav {
position: relative;
display: block;
margin: 0px;
background-color: #333333;
overflow: hidden;
text-align: justify;
font-size: 0px;
min-width: 500px;
width: 100%;
height: 90px;
}
.topnavhome:after {
content: '';
display: inline-block;
width: 100%;
}
.topnavhome li {
display: inline-block;
}
.topnavhome nav li a {
float: left;
display: block;
padding: 12px;
width: 180px;
margin-bottom: -1.5px;
color: #f2f2f2;
border-left: solid 3px #333333;
text-align: center;
text-decoration: none;
font-family: 'Lato', sans-serif;
font-size: 20px;
-webkit-transition: background .3s linear;
-moz-transition: background .3s linear;
-ms-transition: background .3s linear;
-o-transition: background .3s linear;
transition: background .3s linear;
height: 90px;
}
.topnavhome nav a:hover {
background-color: #046A78;
color: white;
border-left: solid 3px #79CBD6;
-moz-transition: background-color 0.01s;
-webkit-transition: background-color 0.01s;
-o-transition: background-color 0.01s;
transition: background-color 0.01s;
}
<ul class="topnavhome" id="myTopnav">
<nav>
<li>Home</li>
<li>Network Security</li>
<li>Passwords</li>
<li>Firewalls</li>
<li>Encryption</li>
<li>Biometric Devices</li>
<li>References</li>
</nav>
</ul>
About a "space" near the end of the navbar which forces the next link to break into a new line, you have this problem cause there is not enough place to fit in, you need to use "#media queries", so that when you would reduce width it would fit nicely. For example on my screen I do not have such problem
Here are a link for good examples of using #media-query
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
And next, you have your <ul> height greater than <nav> in it. And the reason why expanding height of nav wouldn't help is that you have content after your <ul>, which is taking a gap place after nav.
just delete that line:
.topnavhome:after {
content: ''; <------- delete this one
display: inline-block;
width: 100%;
}
you have fixed width of 180px to .topnavhome nav li a which making the li to stack down as they doesn't fit in the container width.
Try using flex which will equally divide the width to li
nav { display: flex;}
nav li { flex: 1;}
Please try below code.
Here your css is not working well it's due to all li element's are not fitting in container due to this reason displaying in new line.
Just add below classes.if it's already their then update the properties.
ul#myTopnav {
-webkit-padding-start: 5px;
}
Here i have only made changes in width & padding.
.topnavhome nav li a {
padding: 12px 6px 12px 6px;
width: 172px;
}
One more thing is used #media query if it's not fitting in small screen or larger screen.
Hope this helps.

How can I stop other text moving down on my Nav bar on hover?

I'm trying to make the text on my nav bar grow in size on hover, I've managed to do this but when I do this the other text links move down slightly and push the others to the right (except the end one)
here's my code:
/* -----------------------------------------------------------------*/
html, body {
margin:0;
padding:0;
}
.header {
margin-top:-7px;
background-color:#333333;
height:70px;
}
.container {
max-width: 940px;
margin: 0 auto;
padding: 30px 10px;
}
.nav {
list-style-type: none;
margin: 0;
padding: 0px 0;
color:white;
}
.nav li {
color: #fff;
display: inline;
font-family: 'Raleway', sans-serif;
font-weight: 600;
font-size: 15px;
margin-right: 25px;
text-transform: uppercase;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease;
}
/* NAV PROPERTIES */
.nav li:hover{
cursor: pointer;
font-size:18px;
text-shadow:0px 0px 5px white;
margin-top: 10px;
padding: 6px;
}
Define a line-height on .nav li
.nav li {
line-height: 10px;
}
https://jsfiddle.net/gnd6q0sp/1/
You are changing the size and margins of the list items...of course the other links move...that's what is supposed to happen.
One option is to just visually scale the list item using a transform instead.
.nav li:hover{
transform:scale(1.1);
cursor: pointer;
text-shadow:0px 0px 5px white;
}
JSfiddle Demo
Note: I am not recommending this option, I think there are better methods available to you.

How can I make the background of an li the same length as the text in it?

I have the following HTML:
<div id="resultList">
<ul>
<li class="product"> Hevea fopspeentje Kroon
</li>
<li class="product"> BabyBjörn Babybord met babylepel 2-Pack
</li>
<li class="product"> Janod Scooter Vanilla
</li>
<li class="product"> Numi-Med Medicijnspeen met doseerschijf Wit (6-18 mnd)
</li>
</ul>
</div>
and CSS:
#resultList {
max-width: 557px;
margin-top: 13px;
padding: 20px;
border: 4px solid #c7c6c6;
border-radius: 4px;
background: #ffffff;
}
#resultList a {
padding: 8px 14px;
color: #072f49;
text-decoration: none;
display: block;
}
#resultList ul li {
margin-bottom: 10px;
color: #072f49;
list-style: none;
background: #f5f5f5;
display: inline-block;
-webkit-transition: background 0.3s ease;
-moz-transition: background 0.3s ease;
-o-transition: background 0.3s ease;
transition: background 0.3s ease;
}
#resultList .product {
width: 100%;
display: inline-block;
}
and the jsFiddle
I would like each product do be on a different line. That is why I set #resultList .product to width:100; However, I would like the background of the #resultList ul li to only be as long as the text itself.
Is there a way to do this?
Move the background-color to the <a> element inside the <li>.
http://jsfiddle.net/NicoO/7nR7T/4/
#resultList ul li {
margin-bottom: 10px;
list-style: none;
display: block;
}
#resultList .product A {
color: #072f49;
background: #f5f5f5;
display: inline-block;
}
add background css code for a and remove background in li. Demo
#resultList a {
background:#F5F5F5;
color: #072F49;
display: block;
float: left;
padding: 8px 14px;
text-decoration: none;
}
#resultList ul li {
margin-bottom: 10px;
list-style: none;
display: inline-block;
}
You can make the width:inherit instead of setting to 100% in #resultList .product.
Updated fiddle. DEMO.

Responsive navigation, can't define width/height in percent

So I'm trying to build a responsive navigation menu that'll scale with media queries.
The problem is (and it's a stupid one I can't work out) that I can't get the a tags to appear as block level elements(I think).
Here's the HTML:
<div class="navbuttons">
<ul>
<li>Home</li>
<li>About</li>
<li>Expertise</li>
<li>Capabilities</li>
<li>Case Studies</li>
<li>Contact</li>
</ul>
</div> <!-- end div navbuttons -->
And the CSS:
.navbuttons {
position: relative;
height: 100px;
width: 50%;
background-color: blue;
float: left;
transition:all .2s linear;
-o-transition:all .2s linear;
-moz-transition:all .2s linear;
-webkit-transition:all .2s linear;
}
.navbuttons ul {
list-style: none;
margin:0;
padding:0;
float: left;
}
.navbuttons li {
float:left;
}
.navbuttons li a {
background: #444;
display: block;
float: left;
line-height: 100px;
height: 100%;
text-align: center;
text-decoration: none;
text-transform: uppercase;
width: 16.5%;
}
Yet, if I use px to define width/height, I can get it looking roughly how I want (though it obviously isn't fluid, like I need).
.navbuttons {
position: relative;
height: 100px;
width: 50%;
background-color: blue;
float: left;
transition:all .2s linear;
-o-transition:all .2s linear;
-moz-transition:all .2s linear;
-webkit-transition:all .2s linear;
}
.navbuttons ul {
list-style: none;
margin:0;
padding:0;
float: left;
}
.navbuttons li {
float:left;
}
.navbuttons li a {
background: #444;
display: block;
float: left;
line-height: 100px;
height: 100px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
width: 100px;
}
Where am going wrong?
The A tags are indeed block level once you specify display: block; on them, however, the 16.5% is calculated from their natural width (i.e. their 100% width based on the inner text).
Depending on your needs, you may want to remove the width parameter and simply add padding. See: http://jsfiddle.net/bLS6B/. This way, the width is dependent on the navigation text, not some preset width. Alternatively, you can specify 120% which has a similar effect.