I am trying to overlay two pieces of text, directly on top of one another to create a layered effect, whilst also trying to vertically centre them in the parent. To vertically centre, I am using a ghost pseudo element as outlined in this post, which I find to be the most reliable method when centring in a parent whose height is variable.
As you can see in the fiddle below, the .bg-text element is vertically centered, but the .text-wrapper element is forced to sit below the parent, so it appears this method of vertically centring does not allow for more than one centered element?
figure {
position: relative;
overflow: hidden;
float: left;
width: 100%;
height: 200px;
background: red;
}
figure::before {
content: "[BEFORE]";
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
/* Background text */
.bg-text {
display: inline-block;
vertical-align: middle;
width: 80%;
color: green;
text-align: center;
}
/* Text */
.text-wrapper {
display: inline-block;
vertical-align: middle;
width: 80%;
text-align: center;
color: blue;
}
<figure>
<div class="bg-text">BACKGROUND TEXT</div>
<div class="text-wrapper">
<h3>Overlay This</h3>
<h4>And this!</h4>
</div>
</figure>
FIDDLE
Flexbox and absolute positioning can do that:
* {
margin: 0;
padding: 0;
}
figure {
position: relative;
height: 200px;
background: red;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
/* Background text */
.bg-text {
width: 80%;
color: green;
text-align: center;
}
/* Text */
.text-wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 255, 0, 0.25);
width: 80%;
text-align: center;
color: blue;
}
<figure>
<div class="bg-text">BACKGROUND</div>
<div class="text-wrapper">
<h3>Overlay This</h3>
<h4>And this!</h4>
</div>
</figure>
Vertical-align:middle works in table-celll as you not puts. Use this in your style may it's help you
.bg-text {
color: green;
display: table-cell;
text-align: center;
vertical-align: middle;
width: 80%;
}
.text-wrapper {
color: blue;
display: table-cell;
text-align: center;
vertical-align: middle;
width: 80%;
}
figure {
background: red none repeat scroll 0 0;
float: left;
height: 100%;
overflow: hidden;
position: relative;
width: 100%;
}
Related
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
It's hard to ask this question without a visual reference, so I included a picture below (as well as a code snippet). I'm trying to achieve two things:
right-align the blue <span> circle inside the yellow <p> box
keep the text centered in the <p> box, independent of the blue circle
This is my code:
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px
}
<p class='box'>This is centered</p>
<p class='box'>This is not<span class='circle'></span></p>
I'm not super familiar with HTML, but I tried doing align-self: right for the circle, but nothing happened. Not sure what to do.
Here is a picture of what I'm trying to achieve:
just float:right and add margin to center
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block;
float: right;
margin: 5px;
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px;
box-sizing: border-box;
}
.box2 {
padding-left: 30px; /* circle width (20px) + circle margin-left (5px) + margin-right (5px) = 30px */
}
<p class='box'>This is centered</p>
<p class='box box2'>This is not<span class='circle'></span></p>
You can use absolute position.
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block;
/* added */
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 10px;
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px;
position: relative; /* should be relative */
}
<p class='box'>This is centered</p>
<p class='box'>This is not<span class='circle'></span></p>
To center the text vertically and horizontally:
.circle {
height: 20px;
width: 20px;
background-color: blue;
border-radius: 50%;
display: inline-block;
/* added */
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 10px;
}
.box {
background-color: yellow;
height: 30px;
width: 500px;
text-align: center;
margin: 10px;
position: relative; /* should be relative */
/* add these to center text vertically and horizontally */
display:flex;
align-items:center;
justify-content: center;
}
<p class='box'>This is centered</p>
<p class='box'>This is not<span class='circle'></span></p>
I'm trying to vertically center an image in a div in an <a> tag but I can't get the CSS to vertically center the image for the life of me.
div {
height: 7rem;
width: 90%;
text-align: center;
margin: 1.25rem auto;
padding: 0 5.5rem 0 5.5rem;
font-size: .8rem;
border-style: solid;
border-width: .08rem;
}
div * {
position: relative;
top: 50%;
transform: translateY(-50%);
}
div img {
width: 4rem;
}
div div {
margin: 0 auto;
width: 4rem;
height: 100%;
}
<div class="footer">
<div id="footerhome">
<a href="https://www.google.com">
<img src="http://images.clipartpanda.com/smiley-face-transparent-background-tumblr_mdv6nltwdB1qdic05o1_500.gif" />
</a>
</div>
</div>
This is what I'm currently working with:
http://codepen.io/SamanthaBae/pen/LRomdr
I've tried the solution of making the image the background image of the div it's contained in and inserting the <a> tag in between, but it doesn't seem to work as a link, even though I can get it to vertically align:
I have checked here and here as resources, but have not been able to apply their methods in this case.
You should to replace this line:
.footer * {
position: relative;
top: 50%;
transform: translateY(-50%);
}
With this code:
.footer #footerhome, .footer a {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.footer a {
display: block;
}
Your problem start when you align all child to their parents.
There are multiple possibilities for centering images vertically.
Other possible solutions can be found here
I used display:flexbox here.
#footerhome {
display: flex;
align-items: center;
}
Here's a codepen
http://codepen.io/Maharkus/pen/ozRyNK
There is one old hack for this if you don't want to deal with flexbox. In short, you should implement table-like semantics for your mark-up with display: table and display: table-cell rules:
.footer {
display: table;
}
#footerhome {
display: table-cell;
vertical-align: middle;
}
UPD
Following your updated code:
#footerhome {
display: table;
}
a {
display: table-cell;
vertical-align: middle;
height: // some fixed height
}
What you were going for was something like this. You'll notice I've added the positioning styles to the <img> again.
div {
height: 7rem;
width: 90%;
text-align: center;
margin: 1.25rem auto;
padding: 0 5.5rem 0 5.5rem;
font-size: .8rem;
border-style: solid;
border-width: .08rem;
}
div div {
position: relative;
top: 50%;
transform: translateY(-50%);
margin: 0 auto;
width: 4rem;
height: 100%;
}
div img {
width: 4rem;
position: relative;
top: 50%;
transform: translateY(-50%);
}
<div class="footer">
<div id="footerhome">
<a href="https://www.google.com">
<img src="http://images.clipartpanda.com/smiley-face-transparent-background-tumblr_mdv6nltwdB1qdic05o1_500.gif" />
</a>
</div
But if I were you I'd do something like this, which uses flexbox and calls the elements by their classes/ids
#footerhome,
.footer {
box-sizing: border-box;
padding: 0 5.5rem;
border-style: solid
}
.footer {
height: 7rem;
width: 90%;
margin: 1.25rem auto;
font-size: .8rem;
border-width: .08rem
}
#footerhome {
width: 4rem;
height: 100%;
display: flex;
justify-content: center;
margin: 0 auto;
align-items: center;
border-width: 0 .08rem
}
#footerhome img {
width: 4rem
}
<div class="footer">
<div id="footerhome">
<a href="https://www.google.com">
<img src="http://images.clipartpanda.com/smiley-face-transparent-background-tumblr_mdv6nltwdB1qdic05o1_500.gif" />
</a>
</div>
</div>
Here is my jfiddle - fiddle, everything is perfect here but only the probelm is when you minimise the window the image goes down , need to fit that image in vertically center which is not happening now
HTML:
<div class="left_panel">
<div class="slide-frame">
<img src="http://www.w3schools.com/bootstrap/paris.jpg">
</div>
</div>
CSS:
.left_panel {
border: 1px solid #ddd;
border-collapse: collapse;
bottom: 0;
left: 0;
position: absolute;
right: 300px;
top: 0;
background:#ddd;
}
.left_panel .slide-frame::before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
.left_panel .slide-frame{
height: 100%;
text-align: center;
width: 100%;
}
.left_panel .slide-frame img {
display: inline-block;
height: auto;
max-height: 100%;
max-width: 100%;
vertical-align: middle;
width: auto;
}
The reason for this behaviour is, that the :after element is an inline element. That causes a little gap between the image and that element. With setting the fon-size to zero, you remove that gap:
.left_panel {
font-size: 0;
}
Good article about that topic on CSS-Tricks. This solution is preferable, because you aren't using text. With text, there are other approaches to remove the space between inline elements.
Please check this for vertical and horizontal div without using height. https://jsfiddle.net/hardyrajput/myuqm5x8/2/
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 40%;
height: 50%;
padding: 20px;
color: white;
text-align: center;
}
use this
.left_panel .slide-frame {
height: 100%;
text-align: center;
width: 100%;
display: inline-table;
}
just add display property
I need your help,
How would I go about amending the HTML or CSS markup below to have the text that is in my custom dialog box, to be vertically centered in the white space. Here is a snapshot of the problem:
and the expected result:
Here is the CSS:
#wrapper {
height: 100px;
width: 500px;
bottom: 50%;
right: 50%;
position: absolute;
}
#container {
background: #FFF;
left: 50%;
padding: 10px;
top: 50%;
margin: 0;
padding: 0;
height: 100%;
border: 2px solid rgb(100,139,170);
height: 100%;
position: relative;
}
.topbar {
cursor: pointer;
color: white;
background: rgb(100,139,170);
padding: 4px;
font-weight: bold;
}
#Text {
width: 100%;
height: 100%;
text-align: center;
}
Here is the HTML:
<div id="wrapper">
<div id="container">
<div style="float:left;" class="topbar">Custom Dialog Box</div><div class="topbar" style="text-align: right;">Close</div>
<div id="Text">This is some sample text that will appear here</div>
</div>
</div>
Fiddle is: https://jsfiddle.net/vc5xL1vy/
You are going to need to set the container to display: table; and set all its children to display: table-cell but the table header to display: table-caption. I have made a few other modification to your header to a single div wrapper. JSFiddle: https://jsfiddle.net/1s45cdvs/1/
CSS
#wrapper {
height: 100px;
width: 500px;
bottom: 50%;
right: 50%;
position: absolute;
}
#container {
background: #FFF;
left: 50%;
padding: 10px;
top: 50%;
margin: 0;
padding: 0;
height: 100%;
border: 2px solid rgb(100,139,170);
height: 100%;
position: relative;
display:table;
}
.topbar {
cursor: pointer;
color: white;
background: rgb(100,139,170);
padding: 4px;
font-weight: bold;
display: table-caption;
}
#Text {
text-align: center;
display: table-cell;
vertical-align: middle;
}
HTML
<div id="wrapper">
<div id="container">
<div class="topbar">
<div style="float:left;" >Custom Dialog Box</div><div style="text-align: right;">Close</div>
</div>
<div id="Text">This is some sample text that will appear here</div>
</div>
</div>
There's multiple ways to do this but they all require you to set a pixel value for something. Padding, line-height, etc...
You can set the container display as table-cell to act like a table element would, then set width and height of the text container so that the element knows where the middle is. Using % never works for vertical aligning. Pain in the CSS.
#Text {
width: 500px;
height: 75px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
https://jsfiddle.net/bgbfpbta/
So basically I need to center (vertically) text, but the height is variable, since it can be on multiple lines, how can I do this?
What I've tried:
.box {
text-align: center;
vertical-align: center;
}
.box {
position: absolute;
top: 50%;
text-align: center;
left: 50%;
}
.box {
margin: 50% auto;
text-align: center;
}
I don't know if this is even possible or if I have to correct it with JS.
Use display:table-cell
.box {
text-align: center;
vertical-align: middle;
background:black;
color:white;
height:200px;
display:table-cell;
width:450px
}
DEMO - Single line
DEMO - Multi line
Here you go, works vertically and horizontally.
HTML:
<div class="area">
<div class="bubble">
<p>To look best, text should really be centered inside this bubble both vertically and horizontally.</p>
</div>
</div>
CSS:
.area {
width: 300px;
height: 300px;
background: url(../images/abe-bg.png) no-repeat;
position: relative;
}
.bubble {
position: absolute;
left: 93px;
top: 21px;
width: 135px;
height: 84px;
display: table;
}
.bubble p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
DEMO