I really need help on this: cracking me for 2nd day already. I have the following code:
* {
margin: 0;
padding: 0;
font: 16px Verdana, Arial, sans-serif;
color: #444;
line-height: 1.5rem;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
text-decoration: none;
}
.inlbtn {
width: 2rem;
height: 2rem;
display: table;
text-align: center;
font-weight: bold;
color: #666;
border: 1px solid #939393;
}
.plus {
font-size: 1.5rem;
font-weight: bold;
display: table-cell;
vertical-align: middle;
}
.plus:before {
content: "+";
}
<div class='inlbtn'><span class='plus'></span></div>
It basically has a div and span inside with a "+" symbol. The horizontal alignment seems fine, but the vertical is a little shifted down. How to make it perfectly centered vertically?
I played around with the code and it seems the code under * is the culprit, but why?
Here's fiddle http://jsfiddle.net/jk34josq/2/
You do everything right, I always use the same method. The problem is that this line
content: "+";
is a piece of text, so it automatically has top margin inside of the line-height preserved for the capital letters (and + symbol is not the one); the margin value could also be different depending on the font.
As a proof try the following:
content: "A";
This looks centered.
What you can do to avoid this behavior:
Negative margin / top property
Use image instead of text
Maybe play with reducing the line-height property but I have doubts about this method
I would use only a single HTML element, since there is no need for using an extra element nor a :before pseudo class:
<div class='inlbtn'>+</div>
Then I would use display: inline-block, instead of table.
As mentioned by Simon in his answer, the + character is smaller than A. But instead of using negative margins or paddings, I would alter the line-height:
.inlbtn {
width: 2rem;
height: 2rem;
font-size: 1.5rem;
line-height: 1.5rem;
display: inline-block;
text-align: center;
font-weight: bold;
color: #666;
border: 1px solid #939393;
}
Updated Fiddle
Try like this: Demo
.inlbtn {
width: 2rem;
height: 2rem;
display: block;
text-align: center;
font-weight: bold;
color: #666;
border: 1px solid #939393;
}
.plus {
font-size: 1.5rem;
font-weight: bold;
display: inline-block;
vertical-align: middle !important;
}
.plus:before {
content:"+";
display: inline-block;
}
Related
I am about halfway through completing a course for learning HTML and CSS, the first time I've ever tried programming, so pardon the (probably) simple problem I have.
I am creating navigation tabs for a fictional website, the tabs being "Menu", "Nutrition", "Order", and "Locations". As you can see, each tab would be a different size because the content varies. I am trying to make a border for each tab, so that the borders would be the same height and width for each one, effectively lining up as four congruent rectangles with words inside of them. The code in HTML for this part that I am working with is:
<nav>
<span>MENU</span>
<span>NUTRITION</span>
<span>ORDER</span>
<span>LOCATIONS</span>
</nav>
The code I currently have for CSS that would effect this is:
* {
box-sizing: border-box;
}
,
nav span {
display: block;
font-size: 16px;
font-weight: 100;
letter-spacing: 2px;
margin: 13px 0px;
}
and
nav a {
color: #666666;
border: 1px solid rgb(202, 202, 202);
padding: 1px;
margin: 0px 3px;
}
This is the resulting visual
How would I make it so that the borders for each tab would be congruent and in line with each other?
* {
box-sizing: border-box;
}
nav span {
display: block;
font-size: 16px;
font-weight: 100;
letter-spacing: 2px;
margin: 13px 0px;
text-align: center;
}
nav a {
color: #666666;
border: 1px solid rgb(202, 202, 202);
padding: 1px;
margin: 0px 3px;
text-decoration: none;
display: inline-block;
}
https://codepen.io/stargroup0075/pen/MWwLxMd
You want to
Give the elements with the borders (<a />) display: block;, which causes them to occupy the maximum horizontal space available
Give a container of the blocks (<nav />) display: inline-block;, which makes the container shrink-wrap its content
Give the parent of <nav /> (in your HTML example there is no parent, so I assume it's <body />) text-align: center;, which centers the <nav />, like with your original code.
The resulting CSS would look something like this:
* { box-sizing: border-box }
body { text-align: center } /* Centers child inline content */
nav { display: inline-block } /* Shrink-to-fit content */
nav span {
display: block;
font-size: 16px;
font-weight: 100;
letter-spacing: 2px;
margin: 13px 0px;
}
nav a {
display: block; /* Take up maximum horizontal space */
color: #666666;
border: 1px solid rgb(202, 202, 202);
padding: 1px;
margin: 0px 3px;
}
I recently created a website and after I created a button with a <a> on it, the text kept aligning it self to the bottom of the text.
How do i make the text align to the center horizontally.
I have tried adding this code: "display:table-cell; vertical-align:middle;" to the button's CSS, and I have tried changing the position with these code snippets: "relative, fixed, static etc." But none of them changed the horizontal position of the text.
.button {
background-color: #171717;
/* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 30px;
height: 10px;
}
a {
text-decoration: none;
font-family: bebasNeue;
font-size: 20px;
vertical-align: center;
text-align: center;
}
<center>
<button class="button">Buy Now!</button>
</center>
As commented, do not use <center> but text-align or display:flex/grid/table behavior and margin. Also a clickable element is not made to hold another clikable element. Use <button> or <a> . button is a form element and could be tricky to restyle.
example of what you could do :
.button {
/* what seems to trouble you */
padding: 15px 32px;
font-size: 20px;
/* okay so far, but height is half of font-size !! */
height: 10px;
/*Your reset*/
background-color: #171717;
border: none;
color: white;
font-size: 16px;
border-radius: 30px;
text-decoration: none;
/* Now try the layout reset and use flex behavior */
display: inline-flex;
align-items: center;
/* ==> because items too big will overflow from the container */
}
a {
box-sizing: border-box;
}
body {
text-align: center
}
<button class="button">Test me! (form element) </button>
Test me! (hyperlink)
How about adding style="position:absolute" in the button tag?
The problem appears to be – quite apart from the use of obsolete and invalid HTML – that you've defined a height of 10px, with a font-size of 16px and padding of 15px (top and bottom). That sum doesn't add up, the height would require:
16px + 2*(15px) = 46px
to contain the text in the centre. To center the text, though I'm replacing the <a> with a <span> for the purposes of validity, you could simply remove the height declaration:
.button {
background-color: #171717;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 30px;
}
span {
text-decoration: none;
font-family: bebasNeue;
font-size: 20px;
vertical-align: center;
text-align: center;
}
<div class="centered">
<button class="button"><span>Buy Now!</span></button>
</div>
I've this list of buttons
button {
background-color: grey;
color: #fff;
border: none;
border-radius: 2px;
box-shadow: 1px 1px 0 0.8px #C0CBD1;
height: 30px;
margin: 0;
padding: 0;
position: relative;
width: 30px;
font: 500 16px/36px sans-serif;
}
.special {
font-size: 30px;
}
<button>A</button>
<button>B</button>
<button class="special">C</button>
Now what I've done is that the special button has a bigger font-size. The weird thing is that increasing the font-size moves this button up. I guess this is all very logic but cannot find the explanation (which should help me to fix this of course!)
The explanation is that buttons are inline-element, and the text in the button determines the vertical alignment.
The default vertical alignment for inline elements is to place the bottom of characters on the base line of the text. If you look at the buttons in your example, you see that the bottom of the characters line up exactly.
If you add some text around the buttons, you see that the text of the buttons aligns with the text outside the buttons: http://jsfiddle.net/Guffa/q640e8sc/4/
If you specify a different verical alignment for the buttons, they will line up differently. If you for example use vertical-align: middle;, the buttons will line up at the center of the characters, so the edges of the buttons will line up: http://jsfiddle.net/Guffa/q640e8sc/5/
Another way to avoid that alignment is to make the buttons block elements, for example using float: left;. That makes the buttons line up, but it of course make the buttons react differently to surrounding elements: http://jsfiddle.net/Guffa/q640e8sc/6/
Use vertical-align:
button {
background-color: grey;
color: #fff;
border: none;
border-radius: 2px;
box-shadow: 1px 1px 0 0.8px #C0CBD1;
height: 30px;
margin: 0;
padding: 0;
position: relative;
width: 30px;
font: 500 16px/36px sans-serif;
display: inline-block;
vertical-align: top;
}
.special {
font-size: 30px;
display: inline-block;
vertical-align: middle;
}
<button>A</button>
<button>B</button>
<button class="special">C</button>
And to align the text in the middle, you may use line-height.
button {
background-color: grey;
color: #fff;
border: none;
border-radius: 2px;
box-shadow: 1px 1px 0 0.8px #C0CBD1;
height: 30px;
margin: 0;
padding: 0;
position: relative;
width: 30px;
font: 500 16px/36px sans-serif;
display: inline-block;
vertical-align: top;
line-height: 16px;
}
.special {
font-size: 30px;
display: inline-block;
vertical-align: middle;
line-height: 30px;
}
<button>A</button>
<button>B</button>
<button class="special">C</button>
<button>D</button>
<button>E</button>
I am having an issue that I hope someone will be able to help me out with.
I have and h2 element that i have given a width, so that long headlines break into two lines. I then want to add a border-bottom to the text, not the block element, and so i have wrapped the text inside the h2 element in a span that i apply the border to. Like this:
<h2><span>My headline that breaks into two lines</span><h2>
My css is like this:
h2 {
width: 455px;
height:170px;
text-align: center;
font-size: 67px;
line-height: 90px;
padding: 0;
margin: 0;
font-weight: normal;
}
h2 span {
border-bottom: 2px solid blue;
}
Everything about works fine, EXCEPT i can't get my border to get any closer to the text. No matter what values i put in line-height and padding, the border seems to be stuck about the 30px below the text. Does anyone have any clever thoughts as to what I can do? I feel like I have tried every combination possible.
Thank you very much in advance.
html :
<div class="demo">
<h2>
<span>My headline that breaks into two lines</span>
</h2>
</div>
css:
.demo {
width: 455px;
height:170px;
text-align: center;
}
h2 {
border-bottom: 1px solid red;
display : inline;
line-height: 90px;
padding: 0;
margin: 0;
font-weight: normal;
}
h2 > span {
font-size: 67px;
line-height: normal;
}
demo http://codepen.io/gauravsam/pen/PZQwNz
add padding-bottom:10px; demo
h2 {
width: 455px;
height:170px;
text-align: center;
font-size: 67px;
line-height: 90px;
padding: 0;
margin: 0;
font-weight: normal;
}
h2 span {
border-bottom: 2px solid blue;
padding-bottom:10px;/*add this*/
}
in my example you will see that by making line-height on the h2 relative, the span rests just under the descender (e.g. the letter y), is the border now closer to the baseline than you had it?
h2 {
text-align: left;
width: 400px;
font-size: 40px;
font-weight: normal;
margin: 0;
padding: 0;
line-height: inherit;
}
span {
border-bottom: 2px solid blue;
}
as you can see on this JSFiddle and on picture below, I'm struggling to get the signature placed as I want in my banner (I want it at the bottom right of the blue banner without interfering with the quote text alignment). The issue that I have is that with my current code the second line of the text is not perfectly aligned horizontally to the middle of the block (there's more space to the right than to the left).
How could I fix this and have full control on the signature position?
Many thanks,
HTML:
<div class="block blueback center">
<h2 class="white">dfjdsjdklj dsjfdslfjldsjf ldsjfdlsjflkdsfjdlskjf dljfdslfjkldsj dljfsdljfdklsj lkdjflkdsjdlks dsfjsdlkfjkls dsjflkdsfjdkl</h2><p class="signature">John Dupont</p>
</div>
CSS:
.block {
display: block;
margin-right: auto;
margin-left: auto;
clear: both;
box-sizing: border-box;
padding-left: 20px;
padding-right: 20px;
width: 100%;
overflow: hidden;
}
h2 {
color: #2165CB;
font-weight: 600;
font-size: 18px;
}
.white { color: #fff;
letter-spacing: 1px;
font-style: italic;
font-weight: 300;
display: inline;
}
.center {
vertical-align: middle;
text-align: center;
padding-bottom: 5px;
padding-top: 5px;
}
.signature {
display: inline;
margin-top: 20px;
margin-bottom: 0px;
padding-top: 0px;
font-size: 12px;
color: #fff;
float: right;
font-weight: 700;
}
.blueback {
background: #0064C4;
}
They're interfering with each other because they're both set to be inline display. Floating the signature to the right starts on the same line as the quote. Thus the float ends up reducing the amount of width the quote perceives that it has, and a visually off-center center is the result.
I am not sure why inline is necessary. You could leave them both as block display and then align the quote's text to the right.
CSS:
.white {
color: #fff;
letter-spacing: 1px;
font-style: italic;
font-weight: 300;
padding: 0;
margin: 0;
}
.signature {
font-size: 12px;
color: #fff;
text-align: right;
font-weight: 700;
padding: 0;
margin: 0;
}
JSFiddle adjustment
If you compare it with yours, it looks visually the same (except the quote text is now centered).
You can use negative margins:
.signature{
margin-right: -20px;
}
But I would move the padding from the outer div to be margins of inner h2 and used margin-left on class signature:
.block {
display: block;
margin-right: auto;
margin-left: auto;
clear: both;
box-sizing: border-box;
width: 100%;
overflow: hidden;
}
h2 {
color: #2165CB;
font-weight: 600;
font-size: 18px;
margin-left: 20px;
margin-right: 20px;
}
.signature{
margin-left: <put here>
}
You also need to define float: left; for your white class as you have defined float: right; for your .signature and other things you can manage such as margins and paddings.
demo