Is it possible to align a rotated text to center?
For example here:
ill try using:
.accordion div.image .title {
bottom: -15px;
color: #fff;
display: block;
font-size: 16px;
max-height: 200px;
position: absolute;
transform: rotate(270deg);
transform-origin: 0 0 0;
white-space: normal;
left: 20px; // this is only the way to center it from left corner }
But when the text has a line break, i have no chance to align it to the center.
Any ideas?
Here is one approach (proof of concept) that involves using the translate function of the transform property and display: table-cell.
First, use display: inline-block on the parent container to get a shrink-to-fit to the image size (or else specify fixed dimensions if you can).
You then define an absolutely positioned container to contain the label. I fixed the width and height (this makes things easier) and then rotated and translated the box to center it within the parent.
Finally, I used vertical-align: middle in the nested table-cell container to allow multiple lines of text to remain centered.
If you are adding this to a pre-existing HTML/CSS structure (accordion?), you need to make the CSS very specific so that you don't break other CSS styling.
You may still need to make some adjustments depending on how you want the bottom of the label to be positioned and so on.
.image {
position: relative;
border: 1px dashed blue;
display: inline-block;
}
.image img {
display: block;
}
div.cell {
display: table-cell;
text-align: left;
vertical-align: middle;
}
div.image .title {
font-size: 16px;
white-space: normal;
color: #fff;
display: table;
border: 1px dotted blue;
position: absolute;
bottom: 0%;
left: 0%;
transform: rotate(270deg) translate(50%,50%);
transform-origin: 50% 50% 0;
width: 300px;
height: 100px;
margin-left: -50px;
}
<div class="image">
<img src="http://www.placehold.it/300x400">
<div class="title"><div class="cell">The title text
that can span two lines or more in a box<div></div>
</div>
Related
I've got a portfolio-like page, displaying images in a grid with a title on top of it. The title is set to inline-block and has a background color, width depends on the length on the title.
Whenever a title becomes too long to fit within the parent article it wraps to a second row; no problem.
But why does the auto width result in 100% now?
.content{
background: pink;
width: 33%;
float: left;
height: 200px;
overflow: hidden;
position: relative;
}
.title{
position: absolute;
bottom: 10%;
left: 0;
text-align: center;
}
h2{
display: inline-block;
font-family: Arial;
background: rgba(255,255,255,0.6);
}
Example:
https://jsfiddle.net/6qtw7duf/
Ok..let's explain this in simple term
I know there are several similar questions answered here, but I can not seem to get this working.
I have two parent divs - one is like a frame with a border and padding, the second is a solid black background, and the third is where a transparent image will actually be placed. I need the two parent divs to expand their height based on the image's height.
I have this working for the div with the black background, but I can't get the parent div with the border to expand it's size:
Here is the fiddle: http://jsfiddle.net/vpdj4kst/
#builder_container {
width: 100%;
/*overflow: auto;*/
position: relative;
padding: 8px;
border: 1px solid #ccc;
margin-bottom: 15px;
display: inline-block;
clear: both;
}
#builder_contents {
background: #000;
width: 100%;
height: 100%;
position: relative;
display: block;
}
.builder_img {
width: 100%;
height: auto;
position: absolute;
}
<div id="builder_container">
<div id="builder_contents">
<img class="builder_img" src="image.png" />
</div>
</div>
This is because you have set the image to position: absolute; which will take it out of the flow causing the parent elements to act as if it wasn't there.
Elements that are positioned relatively are still considered to be in
the normal flow of elements in the document. In contrast, an element
that is positioned absolutely is taken out of the flow and thus takes
up no space when placing other elements.
Position (https://developer.mozilla.org/en-US/docs/Web/CSS/position)
Remove position: absolute; from .builder_img and the parent containers will react to its height.
#builder_container {
width: 100%;
/*overflow: auto;*/
position: relative;
padding: 8px;
border: 1px solid #ccc;
margin-bottom: 15px;
display: inline-block;
clear: both;
}
#builder_contents {
background: #000;
width: 100%;
height: 100%;
position: relative;
display: block;
}
.builder_img {
width: 100%;
height: auto;
}
<div id="builder_container">
<div id="builder_contents">
<img class="builder_img" src="http://coolspotters.com/files/photos/1036167/adidas-st-girls-straw-hat-profile.png" />
</div>
</div>
I have a jsfiddle here - http://jsfiddle.net/j4zvyz7h/1/
I need a overlay the full width and height of the page and text to be dead center.
I can do this with display: table; but I need height: 100%; on the html and body.
This is causing problems in the actual code.
Is it possible to have the 100% overlay with centered text without display: table;
I have an example here - http://jsfiddle.net/j4zvyz7h/2/
I can center it horizontally but not vertically.
Is it possible to center the text horizontally in the second example.
html, body{
height: 100%;
}
.block{
background: rgba(230, 97, 97, 0.4);
display: table;
width: 100%;
height: 100%;
text-align: center;
}
.text{
display: table-cell;
vertical-align: middle;
}
You can also do this:
.text{
position: absolute;
top: 50%;
left: 0;
right: 0;
}
http://jsfiddle.net/j4zvyz7h/5/
You would have to adjust the margins a bit to perfectly center it though. A negative margin-top with values exactly half of your element's height will do it.
The 'correct' way of achieving this is using display: flex;
JS Fiddle
.box {
display: flex;
align-items: center;
justify-content: center;
background-color: brown;
height: 350px;
}
.box > p {
font-family: sans-serif;
color: yellow;
}
<div class="box">
<p>Centered content goes here</p>
</div>
EDIT:
Demo for full screen centered content
You could use a combination of absolute positioning and transforms. Here's an example forked from your code.
https://jsfiddle.net/dwkn5563/
.text{
position:absolute;
top:50%;
left:50%;
transform:translateY(-50%) translateX(-50%);
}
I was wrote this source code just for example, I was manual enter padding-top 90px for h2 tag for example what i want; but when remove padding text is not centered vertical. This is not problem when i know bluebox div height but some times this is 200px, some times 900px.
.bluebox
{
width: 400px;
background-color: blue;
height: 200px;
}
.bluebox h2
{
font-family: Arial;
font-size: 10pt;
text-align: center;
padding-top: 90px;
}
<div class="bluebox"><h2>Hi i am a text, now I am only horizontal centered<h2></div>
Demo: http://jsfiddle.net/5UJWa/
.bluebox {
width: 400px;
background-color: blue;
height: 200px;
position: relative; /* allow absolute positioning within */
}
.bluebox h2 {
font-family: Arial;
font-size: 10pt;
text-align: center;
position: absolute; /* positioning */
top: 50%; /* 50% from the top (half way) */
margin-top: -5pt; /* bring it back up half the height of your text size */
width: 100%; /* to allow for the text align */
}
Example at http://jsfiddle.net/zTPgh/1/ - Change the height of the container and run or update to see it in action.
You can play with display: table-cell;.
Your new CSS:
.bluebox {
width: 400px;
background-color: blue;
height: 150px;
display: table-cell;
vertical-align: middle;
}
.bluebox h2 {
font-family: Arial;
font-size: 10pt;
text-align: center;
}
Check out the illustration on jsFiddle.
See my tutorial here which will vertically align and center text and images. DON'T rely on line-heights as you'll have huge gaps between lines of text. http://www.andy-howard.com/verticalAndHorizontalAlignment/index.html
I have Create one demo for vertical image center and text also i have test on firefox ,chrome,safari, internet explorer 9 and 8 too.
It is very short and easy css and html, Please check below code and you can find output on screenshort.
HTML
<div class="frame">
<img src="capabilities_icon1.png" alt="" />
</div>
CSS
.frame {
height: 160px;
width: 160px;
border: 1px solid red;
white-space: nowrap;
text-align: center; margin: 1em 0;
}
.frame::before {
display: inline-block;
height: 100%;
vertical-align: middle;
content:"";
}
img {
background: #3A6F9A;
vertical-align: middle;
}
For aligning an element vertically center, I have used css3 calc() function. It's perfectly working in all the latest browsers including ie11 and edge.
Check it live here https://jsfiddle.net/ashish_m/ebLxsxhk/
.calcTest { width: 250px; height: 250px; border:1px solid #e0e0e0; text-align: center; }
.calcTest .calcTestInner { width: 50px; height: 50px; background: #e0e0e0;
margin: 0 auto; margin-top: calc(50% - 25px); vertical-align: top; }
<div class="calcTest">
<div class="calcTestInner">
Hello Folks
</div>
</div>
My .cent class is for <li> elements. I want to center the text of the <li> both horizontally and vertically. text-align:center; takes care of horizontal centering, but the vertical centering isn't working. What's the CSS trick for this?
.cent{
height:20px;
width:20px;
text-align:center;
vertical-align: middle;
}
Use line-height property.
Set it equal to height of element, and text will be vertically centered.
.cent{
height: 20px;
width: 20px;
text-align: center;
/*vertical-align: middle;*/
line-height: 20px;
}
What is its parent?
.parent {
position: relative;
}
.cent {
position: absolute;
top: 50%;
left: 50%;
margin-left: -10px;
margin-top: -10px;
}
Or if you want vertical-align: middle to work, set display: table-cell.
Try to set the style for li tag, margin and padding tags are will help you
Like,
# set default
li
{
margin: 0;
padding: 0;
}
after write the style for your class cent, it will be affect the li tag properly.