Tested on OSX Chrome 45, align-items: center; is working for content, but if you click into the empty editable below, the caret position is not centered until you start typing.
Is the only way to fix this with top/bottom balanced padding or is there a way of getting this to work without pixel shifting? Thanks
[contenteditable="true"] {
border: 1px solid #ddd;
display: flex;
align-items: center;
min-height: 46px;
}
[contenteditable="true"]:focus {
outline: none;
}
<div contenteditable="true"></div>
<div contenteditable="true">content is centered, but caret isn't</div>
As James Donnelly said in the comment, you can try adding line-height to your rules, eg:
[contenteditable="true"] {
border: 1px solid #ddd;
display: flex;
align-items: center;
min-height: 46px;
line-height: 46px;
}
However I'd actually probably go with padding instead. Adding the min-height to your contenteditable element is what's causing the issues between the cursor and the inputted value. Take away the min-height, and the problem is solved. Then your problem becomes "How do I add an even amount of space around this contenteditable element?" - and that's exactly what padding is for :)
So perhaps something more like:
[contenteditable="true"] {
border: 1px solid #ddd;
display: flex;
align-items: center;
padding: 15px 0;
}
Depends on what exactly you're trying to implement though, and whether you really need to fix the height of the div.
just put line-height: 150% for your contenteditable div
but if you want to set font-size, you should Note that you should compute line-height in proportion with font-size to make the caret vertically center
To achieve this effect the element's line-height has to be set to the same pixel value of the element's height. Then on :focus you can change it to a normal line-height. See the snippet below.
[contenteditable="true"] {
border: 1px solid #ddd;
display: flex;
align-items: center;
min-height: 46px;
line-height:46px;
}
[contenteditable="true"]:focus {
outline: none;
}
[contenteditable="true"]:focus:not(:empty) {
line-height:normal;
}
<div contenteditable="true"></div>
<div contenteditable="true">content is centered, but caret isn't</div>
In my opinion you should use padding instead of min-height and line-height. If it's not important to fix the height of that div.
Because of padding it also looks clean.
[contenteditable="true"] {
border: 1px solid #ddd;
display: flex;
align-items: center;
padding: 15px;
}
Related
This is my reference photo on what i would like to achieve. My intention is to have the buttons be the same height regardless of text length and have them be in on line (and stop them from forming this sort of three triangle look below (screenshot below is my existing jsfiddle.
first i tried to set them the same width but the shorter first button does not naturally fit since the other two need an extra space for the words:
.up-promos .top-area .tabs a {
width: 150px;
}
Because of this, I also tried adjusting the height for them to stay the same size however all of them do not align (first button with the least text looks awkward) and the first button somehow moves down. I also tried aligning the text to center and adjusting the margin but the height css probably prevents this change.
.up-promos .top-area .tabs a {
border: 1px solid #fff;
color: #fff;
padding: 10px;
border-radius: 8px;
display: inline-block;
text-align: center;
margin: 0 0 10px 10px;
text-decoration:none;
text-align: center;
width:150px;
height: 30px;
}
Here is the JSFiddle for all of this:
https://jsfiddle.net/b34rsLyu/
I will need some help on these adjustments. Tried what I thought was the solution, hope I can get a guide in the correct fix.
There a few things that can be done.
use flex for .tabs container.
dynamically calculate font size - using font-size: calc( ... )- to prevent overflow for button, or any other container.
if you want to achieve alignment like in in the first screenshot, set a min-width in r-button class and flex-wrap: wrap in .tabs class.
Try using these values, and then modify other classes as you see fit.
.tabs {
display: flex;
align-items: center;
justify-content: center;
background: #eee;
gap: 8px;
flex-wrap: wrap;
}
.r-button {
border: 1px solid black;
border-radius: 8px;
padding: 15px 0px;
word-wrap: break-word;
min-width: 10rem;
font-size: calc(1rem - 0.1vmin);
}
Check the docs for more on css Flex property
You could use display: flex on your container to have better control over how elements behave and flow in relation to each other, here's the simplest solution you can get:
.up-promos .tabs {
display: flex;
}
If you want to learn more about display: flex, because there's tons of things you can tweak on it, I seriously suggest this guide. It's my go-to place to remind myself about all the properties it has as I always fail to remember them.
I'm trying to recreate a math game for a University project with HTML CSS and JS. During the layout, my flexbox items are going outside their container. I gave them a thick border so it is easier to see what is happening. I'm trying to design it first for mobile as I show in the picture below.
See the screenshot here
.container {
display: flex;
flex: 1;
flex-flow: column wrap;
justify-content: center;
align-items: center;
margin: 1em;
border-width: 5px;
border-style: solid;
border-color: red;
}
.flex-item {
align-items: center;
justify-content: center;
border-width: 5px;
border-style: solid;
border-color: rgb(0, 255, 64);
}
Here is the full code in codepen
What I'm doing wrong?
The issue is the gap in your grid. You need to account for the 0.5em padding added on both sides of your element. To do this, you can use the CSS calc method. Set the width of .answer-options to calc(100% - 1em);, 1em being what you get when you add the .5em and .5em on either side of your grid. You're subtracting that 1em from the 100% width.
in .container I changed margin to padding and the problem got resolved.
https://jsfiddle.net/n00q5wdn/
Please refer to fiddle for the output plus I'm linking an image so that the problem may be understood more clearly.
I just want to have the 'hgroup' to be vertically centered.
HTML:
<article id="full-height">
<div class="hgroup">
<h1>Exotic Designs</h1>
<h2>Best Quality</h2>
</div>
</article>
SCSS:
#full-height {
background-color: red;
min-height: 600px;
}
.hgroup {
h1 {
color: white;
font-size: 5em;
font-weight: 800;
line-height: 0.8em;
text-shadow: black 0 0 20px;
text-align: center;
}
h2 {
display: block;
color: white;
width: 60%;
max-width: 200px;
margin: 0 auto;
text-align: center;
border: 1px solid white;
margin-top: 15px;
padding: 10px;
background: rgba(0, 0, 0, 0.5);
font-size: 1.3em;
}
}
The best fit for this kind of problem is flexbox, which uses the Box Alignment spec, specifically created for these situations.
If you set the #full-height-element to display as a flex row container, you can align the contents with align-items (vertically) and justify-content (horizontally).
#full-height {
display: flex;
align-items: center;
justify-content: center;
}
See forked fiddle here.
This will work in most modern browsers, and if you run it through something like Autoprefixer you can get it working in some slightly older browsers as well (IE10, older Safari etc). IE9 and other ancient browsers do not support flexbox, so I recommend a fallback with either the .hgroup element having a fixed margin, or just top aligned etc. If you can get away with a fixed, explicit height of the container, there's another, more hacky and involved solution:
I see you have dabbled with inline-block and vertical-align to try and use the middle keyword for vertical centering. That will only work if you can set a definite height on the #full-height container, and use a "ghost"-element to force the line-height calculation to cover the box, for example:
#full-height {
height: 600px;
text-align: center; /* horizontal centering */
}
/**
* "ghost element", to force the calculation of the
* middle keyword to equal the vertical middle of
* the box.
*/
#full-height:before {
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
/* offset the whitespace generated by inline-block.
May vary with font-size. */
margin: -.25em;
}
.hgroup {
display: inline-block;
vertical-align: middle;
}
See jsbin example for this solution here.
I have a need for my links and buttons to look the same, but I've been unable to vertically align the text within an "a" tag in the same manner as the "button" tag. It is important to note that the tags need to be able to handle multiple lines of text (so line-height will not work).
a,button {
display: inline-block;
-moz-box-sizing: border-box;
width: 150px;
height: 150px;
vertical-align: middle;
border: 1px solid #000;
text-align: center;
}
See the jsfiddle below:
http://jsfiddle.net/bZsaw/3/
As you can see, I can get it to work with a combination of a span tag inside and setting "display:table" to the "a" and setting "display:table-cell" and "vertical-align:middle" to the span, but that doesn't work in IE7.
a,button {
width: 150px;
height: 150px;
border: 1px solid #000;
text-align: center;
}
a {
display: table;
-moz-box-sizing: border-box;
}
a span, button span {
vertical-align: middle;
text-align: center;
}
a span {
display: table-cell;
}
Looking for a simple CSS only solution.
The only reliable way to I've found align text vertically and allow wrapping of the text if it gets too long is with a 2 container approach.
The outer container should have a line height of at least double that specified for the inner container. In your case, that means the following:
a {
width: 150px;
height: 150px;
border: 1px solid #000;
text-align: center;
line-height: 150px;
display: block;
}
a span {
display:inline;
display:inline-table;
display:inline-block;
vertical-align:middle;
line-height: 20px;
*margin-top: expression(this.offsetHeight < this.parentNode.offsetHeight ? parseInt((this.parentNode.offsetHeight - this.offsetHeight) / 2) + "px" : "0");
}
Add float left on the a tag if you want everything inline. Here's the updated example with long text in the A tag too..
http://jsfiddle.net/bZsaw/13/
You can set the line height on the span to whatever you like and if it is less than half of the line height of the parent, it will center AND allow text wrapping if your text exceeds the parent container width. This works on all modern browsers as far as I know.
All answers are not updated,and all of them are basically hacks, you should use new CSS3 features, in this case flexbox
a,button {
-moz-box-sizing: border-box;
width: 150px;
height: 150px;
display:flex;/*CSS3*/
align-items:center;/*Vertical align*/
justify-content:center;/*horizontal align*/
border: 1px solid #000;
}
<span>Testing 1,2,3</span>
<button><span>Testing 1,2,3</span></button>
That should work for your problem, note that align-items and justify-content will behave the opposite if set flex-direction:vertical, default is flex-direction:row.
Feel free to use, all browsers support it caniuse.com/#search=flex
Also check out the free and excellent course flexbox.io/ he is the best teacher at this
Also check out css-grid, also new in CSS3
If your text won't be larger than the width of the box you could set the line-height equal to the height of the box.
line-height:150px;
The cleanest and most consistent way I found is this
display: grid;
place-items: center;
https://jsfiddle.net/j8bktum9/
Use line-height:150px; and display-inline:block;
I have a Button that is a simple anchor tag styled with the following -
.buyBtn{
background:url(../images/buyBtn.png) no-repeat;
padding-top:4px;
width:97px;
height:28px;
margin-top:14px;
}
.buyBtn a{
color:#fff!important;
font-weight:normal!important;
text-decoration:none;
padding-left:27px;
padding-top:12px;
text-shadow:none!important;
}
I'm having problems vertically centering the text within the button, it appears fine in some devices, but off centre in others.
Can anybody recommend a way to fix this or a better solution to achieve the same result?
Cheers
Use line-height to center it vertically. I usually use the same value as its height.
HTML:
<div class="buyBtn">Button</div>
CSS:
.buyBtn{
background:url(../images/buyBtn.png) no-repeat;
width:97px;
height:28px;
display: table-cell;
vertical-align: middle;
}
.buyBtn a{
color:#fff!important;
font-weight:normal!important;
text-decoration:none;
padding-left:27px;
text-shadow:none!important;
}
No need to use padding-top or margin-top for vertical align. Just use display: table-cell; and vertical-align: middle;. Thats it.
Flexbox helped me nail it. Assume you have an excel button (an anchor tag, really).
HTML
<a class="btn-excel" href="/Export/ExportListToExcel">
Export To Excel
</a>
CSS
.btn-excel {
background-color: #32CD32; /*lime green*/
color: #fff;
outline: none;
border-radius: 0.3rem;
text-decoration: none;
width: 5rem;
height: 2.5rem;
/* Flex rules 'em all */
display: flex;
justify-content: center;
align-items: center;
}
.btn-excel:hover {
background-color: #808000; /*olive*/
cursor: pointer;
text-decoration: none;
}
I would use line-height as bchhun as mentioned. Also, padding-top & padding-bottom can help.
You can copy this code and put it as a CSS and adjust the things as you need
.btn-alignment{
width: 88px;
height: 40px;
margin: 0 auto;
padding: 0;
display: inline-block;
line-height: 40px;
margin-top: 9px;
text-align: center;
}
Did you try setting a font-size to your link ? Each browser probably has its own default size, so that may be an hint. Be careful too with padding and width/height, you need to decrease the block size if you add padding 'cause the padding is included in the width. For example, a simple block of 100px width without padding will have a size of 100px : ok. Add a padding-left: 10px; and your block now has a width of 110px. ;)