Adjust distance between underline and text using css - css

I want to add some space between the text and the underline. But when I try to add some border on the bottom it occupies the 100% width of my resolution.
So it looks like this:
Here's my css:
h1 {
font-size: 24pt;
color: #279839;
position: relative;
text-decoration: none;
padding-bottom: 5px;
border-bottom: 1px solid #279839;
}
My page is multilingual so the border bottom should be the same width of the text.
Can you help me with this?

text-underline-offset
<h1 style="text-decoration:underline; text-underline-offset:.25em;">text</h1>

You could add display: inline-block; to the <h1> or you add a inline element (like a span) inside the h1 ...
h1 {
text-align: center;
}
h1 span {
font-size: 24pt;
color: #279839;
padding-bottom: 5px;
border-bottom: 1px solid #279839;
}
<h1><span>hello</span></h1>
<h1><span>hello world</span></h1>
<h1><span>hello world and univers</span></h1>

Put a span tag inside the h1
<h1 class="the-h1"><span class="the-span">商品</span></h1>
the css
.the-h1 {
text-align: center;
}
.the-span {
display: inline-block;
font-size: 24pt;
color: #279839;
position: relative;
text-decoration: none;
padding-bottom: 5px;
border-bottom: 1px solid #279839;
}

If you don't want to wrap that by some other tag then use transform to align h1 tag at center of page and change it's display to inline-block this applies to only one h1 tag,
h1 {
font-size: 24pt;
color: #279839;
position: relative;
text-decoration: none;
padding-bottom: 5px;
border-bottom: 1px solid #279839;
display: inline-block; /*Add this*/
left: 50%; /*Add this*/
transform: translate(-50%, 0); /*Add this*/
}
<h1>Hello</h1>

Step1: You need to make H1 display:inline-block; so that the border remain according to the width of text instead of window width.
Step2: In Order to provide space you can use css pseudo element
h1 {
font-size: 24pt;
color: #279839;
position: relative;
text-decoration: none;
padding-bottom: 5px;
display:inline-block;
position:relative;
margin: 0 0 10px;
}
h1:after {
content:'';
position: absolute;
left:0;
right:0;
bottom:0;
height:1px;
background: #279839;
display: block;
}

Your h1 tag is a block element by default, so it makes sense that the border-bottom goes through the whole width. You would need to change the display property of your headline to achieve the wished result.
h1 {
display: inline-block; /* most solid one; best choice */
display: initial; /* most safe one can easily be overwritten */
display: inline-flex; /* could be useful if people using flex-grids */
}

h1 {
display:Block;
width: 25%
position:relative;
margin-right:auto;
margin-left:auto;
font-size: 24pt;
color: #279839;
position: relative;
text-decoration: none;
padding-bottom: 5px;
border-bottom: 1px solid #279839;
}

Related

How to centralise a button div

I want to centralise a button or a tag within the line it is on (by themselves).
I've written this CSS which makes it yellow, puts a border around it etc but it is always left aligned. How do I centralise it?
a.butt, button.butt {
border: 1px solid #ffff01;
background-color: transparent;
height:50px;
cursor:pointer;
padding-left: 40px;
padding-right: 40px;
padding-top: 10px;
padding-bottom: 10px;
text-decoration: none;
text-align: center;
color: #ffff01;
}
Thanks for your help
several ways to do this, but maybe
a.butt, button.butt {
border: 1px solid #ffff01;
background-color: transparent;
height:50px;
cursor:pointer;
padding-left: 40px;
padding-right: 40px;
padding-top: 10px;
padding-bottom: 10px;
text-decoration: none;
text-align: center;
color: #ffff01;
display: block;
margin-left: auto;
margin-right: auto;
width: ??px;
}
will do the trick.
You can do it by adding text-align:center; to parent <div>
Example here https://jsfiddle.net/uy4u781d/
margin-left:auto;
margin-right:auto;
width:150px; //width as you want
display:block;

z-index property doesn't work on hover menu links

I made a menu bar, and custom effect on hover. I wanted the link text to be on top layer. Problem is that when I hover the link in menu, the triangle overlays the text as shown in example, even though I set the z-index of a link to 999.
<div id="menu">
<ul>
<li>yyyyyy</li>
<li>ppppppp</li>
<li>ggggggg</li>
<li>jjjjjjjj</li>
</ul>
Jsfiddle:
https://jsfiddle.net/z5zLL1kn/
#menu{
height:50px;
background-color:#fff8dc;
border-bottom:1px #ff8888;}
ul {
list-style-type: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
height:50px;
background-color: #fff8dc;
width:450px;
}
li { float: left; }
li a {
font-family: 'Quicksand', sans-serif;
font-size: 16px;
font-weight: 400;
line-height: 44px;
display: block;
color: #ff1636;
text-align: center;
border-bottom:1px #ff8888;
text-decoration: none;
height:49px;
position: relative;
padding-left:15px;
padding-right:15px;
z-index:100;
}
li a:hover:after {
content: "";
display: block;
border: 10px solid #fff8dc;
border-bottom-color: #ff8888;
position: absolute;
bottom: 0px;
left: 50%;
margin-left: -10px;
margin-bottom:1px;
}
Your problem is that the ::after pseudo element is considered a part of the a element, and so changing the z-index of the anchor, will also apply to the pseudo element.
A quick solution would be to move the arrow pseudo element to the list item instead of the link.
Working jsFiddle
li {
position: relative;
}
li a {
position: relative;
z-index:2;
}
li:hover::after {
position: absolute;
}
Another solution:
As Roko C. Buljan mentioned in the comments, a more straight-forward solution can be to build the arrow pseudo element properly (the second border color needs to be transparent, instead of the background's color:
li a:hover:after {
border: 10px solid transparent;
border-bottom-color: #ff8888;
}
Working jsFiddle

Weird CSS behavior - Diagonal border - Why is the border edge not straight?

I want to add a white gap between menu elements but Im encountering a weird problem. See this jfiddle: http://jsfiddle.net/ERYat/1/
Here is the CSS code:
/* a styling */
ul#menu-menu-services a {
display: block;
font-size: 20px;
color: #000;
text-decoration: none;
padding: 5px;
border-bottom: 2px solid #fff;
border-left-style: solid;
border-left-width: 3px;
border-left-color: #000;
}
/* li fix */
ul#menu-menu-services li {
margin: 0;
padding: 0;
border: none;
}
/* Sub Menu */
ul#menu-menu-services li ul.sub-menu {
display: block;
margin-left: 0px;
}
ul#menu-menu-services li ul.sub-menu li a {
padding-left: 15px;
font-size: 14px;
}
I can't figure out why is the border diagonal on the left. Anyone knows?
Borders come together like this:
||
||______
|/______
You should use margin-bottom instead of border-bottom fiddle:
ul#menu-menu-services a {
display: block;
font-family: 'Droid Sans', arial, serif;
font-size: 20px;
color: #000;
text-decoration: none;
padding: 5px;
margin-bottom: 2px;
border-left-style: solid;
border-left-width: 3px;
border-left-color: #000;
}
And if you need a white line, consider using :after:
ul#menu-menu-services a { position: relative; }
ul#menu-menu-services a:after {
content: '';
width: 100%;
position: absolute;
height: 2px;
background: #fff;
left: 0;
bottom: -2px;
}
It's because it's drawing the corner of the two borders. Try changing your bottom border to something other than white and you'll see more clearly what it's doing.
To get rid of this effect, you need to get rid of the bottom border.
If you need the gap that the bottom border is currently giving you, you could use padding-bottom or margin-bottom instead.

Add an "underline" to a box with CSS

I want to achieve something like this by CSS:
I'm a novice with CSS.
My questions:
How can I add a green line to the bottom as below? Will I have to add a small div under the div containing the text and set its background to green? I do know there are many ways to do it but I just want to learn the best practice.
Is this font Arial?
You can either add the div at the bottom as you described, or you can use a border. In either case you'll have some adjustment of heights to do. No big deal.
http://jsfiddle.net/PQgH3/2
div {
width: 50%;
padding-top: 10px;
font-size: 24px;
text-align: center;
font-family: arial, sans-serif;
}
.followers {
background-color: #777;
float: right;
height: 75px;
color: #ccc;
}
.following {
background-color: #555;
float:left;
height: 70px;
color: #ccc;
border-bottom: 5px solid lime;
}
<div class="followers">Followers</div>
<div class="following">Following</div>
I don't have the eye to say whether that font is Arial. I can say that it's a similar sans-serif font if it isn't.
Use CSS sprite sheets. They will help you achieve this effect using images. Generally when you do the markup for the menu use UL and LI tags, then style appropriately for the functionality. Then set it to change the background sprite when the mouse is over then li using the :hover selector. I recommend creating the sprite sheet as an exact image of what you want all the default menu buttons to look like (spanning horizontally). Then do another version below it on the same image that has the look of the hover version. You can repeat this process for any other versions you need like active, disabled, etc. Just make sure you offset the Y value of the background position for each version. Such as this:
li { background-position: 0px 0px; }
li:hover { background-position: 0px -100px; }
li:active { background-position: 0px -200px; }
Check out this article for a bit more information regarding the markup as well as the design aspect:
http://line25.com/tutorials/how-to-create-a-css-menu-using-image-sprites
Edit:
If you don't want to do sprite sheets I have a jsFiddle of pure css3 way of doing it here:
http://jsfiddle.net/uBhKF/
HTML Markup:
<ul>
<li>FOLLOWING</li>
<li>FOLLOWERS</li>
</ul>
CSS3:
ul {
width: 100%;
padding: 0px;
margin: 0px;
list-style-type: none;
float: left;
}
li {
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
float: left;
width: 50%;
height: 50px;
display: block;
margin: 0px;
background-color: #444;
border-left: 1px dotted #DDD;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
font-size: 24px;
text-align: center;
line-height: 50px;
color: #888;
}
li:first-of-type {
border-left: none;
}
li:hover {
color: #55E000;
border-bottom: 5px solid #55E000;
background-color: #333;
}
But I couldn't get the font-family right. :(
use this
.header
{
margin-left: auto;
margin-right: auto;
height: 102px;
background-color: #F0F0F0;
width:500px
}
.header .header-first
{
float: left;
width: 216px;
border-bottom: 3px solid #3CA2DF;
}
.header .header-last
{
width: 216px;
float:right;
}
The Font is not Arial for sure, i believe its calibri, and try this code for you solution
<html>
<body>
<div style="border-bottom: 3px solid #00ff00;
background:#000;
height: 50px;
width:400px;
color:#00ff00;
text-align:center;">
FOLLOWERS
</div>
</body>
</html>
Also try this
<html>
<head>
<style>
td
{
width:400px;
background:#000;
color:#fff;
text-align:center;
height: 100px;
font-size:20px;
}
td:hover
{
text-decoration: underline;
border-bottom: 3px solid #00ff00;
color:#00ff00;
}
</style>
</head>
<body>
<table style="margin:0 auto;">
<tr>
<td>Following</td>
<td>Followers</td>
</tr>
</table>
</body>
</html>

CSS layout - footer is overlapping the content of page

I am new to CSS, I found an layout online, but I have a little problem customizing it.
When the content of mainContent div is "higher" then the left menu, everything is working ok - but when the mainContent is smaller, it looks like this:
The div order is visible through Firefox WebDevelopper addon. The .css file is as follows:
body {
padding: 0px;
margin: 0px;
font-family: Arial, Helvetica, sans-serif;
}
#container {
padding-right: 20px;
padding-left: 20px;
}
#header {
}
#mainMenu {
border: 1px solid #808080;
font-size: 0.8em;
}
#pageBody {
position: relative;
}
#sidebar {
width: 250px;
padding: 5px 10px 10px 10px;
float: left;
border: 1px solid #808080;
font-size: 0.8em;
margin-top: 5px;
margin-right: 10px;
min-height: 500px;
}
.mainContent {
text-align: justify;
min-width: 400px;
min-height: 500px;
margin-left: 285px;
}
#footer {
font-size: 0.8em;
border-top-style: solid;
border-top-width: 1px;
border-top-color: #CCCCCC;
text-align: center;
color: #CCCCCC;
background-color: #333333;
padding-top: 10px;
padding-bottom: 10px;
margin: 0;
/*clear: both;*/
}
#footer p {
margin-top: 0px;
margin-bottom: 5px;
}
#footer a {
color: #CC3300;
text-decoration: none;
}
#footer a:hover {
color: #FF9900;
}
Can I please ask for some guidance in repairing this layout? I would like to acheive something similar to:
The exact position is not important, I just want the footer to be placed below the pageBody div.
I would be grateful for any help.
I don't know what your html looks like, but it looks like you could try inserting a clearing div just before the footer... btw, why is the 'clear: both;' commented out in the footer css rule?
code to insert just before the footer div:
<div style="clear: both;"/>
Not positive whether it will help, but if I understand your problem correcty, this is my best guess!
Try changing pageBody to:
#pageBody {
float: left;
clear: both;
}

Resources