This question already has answers here:
Horizontally & Vertically centering 2 divs within two side by side 50% width columns
(1 answer)
2 side by side divs centered?
(3 answers)
Align 2 divs side by side in a parent
(4 answers)
Closed 4 years ago.
How can I put two centered divs next to each other, with some padding in between? I've tried display: inline but that doesn't seem to work.
.my-container {
position: relative;
text-align: center;
color: red;
width: 20%;
margin: auto;
}
/* Centered text */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-moz-border-radius: 50%;
border-radius: 50%;
background-color: blue;
}
<div class="my-container">
<img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
<div class="centered">text</div>
</div>
<div class="my-container">
<img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
<div class="centered">text 2</div>
</div>
(Note: I'm also looking for a way to make the blue text-elipse a circle, but that's a different question I suppose.)
Use display:inline-block in your .my-container to align them side by side...
Also you will need to wrap your .my-container divs into a wrapper div with text-aign:center to align the inner inline items to center
.wrapper {
text-align: center;
}
.my-container {
position: relative;
text-align: center;
color: red;
width: 20%;
margin: auto;
display: inline-block;
}
/* Centered text */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-moz-border-radius: 50%;
border-radius: 50%;
background-color: blue;
}
<div class="wrapper">
<div class="my-container">
<img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
<div class="centered">text</div>
</div>
<div class="my-container">
<img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
<div class="centered">text 2</div>
</div>
</div>
You should add display: inline-block; to my-container class.
.my-container {
position: relative;
text-align: center;
color: red;
width: 20%;
margin: auto;
display: inline-block;
}
/* Centered text */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-moz-border-radius: 50%;
border-radius: 50%;
background-color: blue;
}
<div class="my-container">
<img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
<div class="centered">text</div>
</div>
<div class="my-container">
<img src="https://demo.keypasco.com/res-1.2.2/img/User_ring.png" style="width:100%;">
<div class="centered">text 2</div>
</div>
Related
I know there are many ways to vertically center a div inside another div, but what I'm experiencing problems on is a way to vertically center a div inside an image element.
Here's the minimum code I require:
HTML:
<img class="star" src="star.png"></img>
<div class="vCenter">Text to be vertically centered in the image.</div>
CSS:
.star {
position: absolute; /* I need this value for another thing in my project */
}
Also, the height of the image is in percentage.
Wrap the image in another element
Move the position: absolute to the wrapper element
Center the text in the wrapper element, let the image expand the wrapper element
.wrapper {
position: absolute;
top: 30px;
left: 10px;
display: flex;
justify-contents: center;
align-items: center;
}
.wrapper img {
/* debug */
background: yellow;
width: 300px;
height: 300px;
}
.wrapper .vCenter {
position: absolute;
left:50%;
max-width: 100%;
transform: translateX(-50%);
}
<div class="wrapper">
<img class="star" src="star.png" />
<div class="vCenter">Text to be vertically centered in the image.</div>
</div>
Try this way:
<div class="container">
<img class="star" src="star.png"></img>
<div class="centered-div">Text to be vertically centered in the image.</div>
</div>
.container {
position: relative;
}
.star {
display: block;
margin: auto;
width: 100%;
}
.centered-div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
code solution:https://stackblitz.com/edit/web-platform-5qivc2?file=index.html
referred link:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_text
<div class="container">
<img src="https://via.placeholder.com/250" alt="Snow" style="width:100%;">
<div class="centered">Centered</div>
</div>
.container {
position: relative;
text-align: center;
color: white;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 20px;
color: red;
}
You may add your image and text to a container set to position:absolute
This should do the job ;-)
.container {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
// just add the desired position to this container
}
.star {
position: absolute;
}
.vCenter {
z-index: 1;
}
<div class="container">
<img class="star" src="star.png" />
<div class="vCenter">Text to be vertically centered in the image.</div>
</div>
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
.right {
float: right;
border: 3px solid #73AD21;
}
<div>
<div class="right">
<p>testtesttest.</p>
</div>
<div style="text-align: center;">
<h2>Middle</h2>
<p>blablabla</p>
</div>
</div>
I want that the Middle is centered and the other box is on the right. In my code the Middle is not centered. How can I do that?
I hope this is what you are expecting.
.text {
text-align: center;
background-color: red;
}
.container {
position: relative;
}
.right {
position: absolute;
right: 0;
width: fit-content;
text-align: center;
top: 50%;
transform: translateY(-50%);
border: 3px solid #73AD21;
}
<div class="container">
<div class="right">
<p>testtesttest.</p>
</div>
<div class="text">
<h2>Middle</h2>
<p>blablabla</p>
</div>
</div>
U mean to have it look something like this ?
But this has BIG drawback, that if caption is too long it will overflow the div on the right.
.main {
position: relative;
}
.right {
//float: right;
position: absolute;
right: 0;
border: 3px solid #73AD21;
}
<div class="main">
<div class="right">
<p>testtesttest.</p>
</div>
<div style="text-align: center;">
<h2>Middle</h2>
<p>blablabla</p>
</div>
</div>
This question already has answers here:
How to center align img wrapped in SPAN tag?
(5 answers)
How to center the center circle?
(4 answers)
Closed 3 years ago.
How to center image in <span> tag?
<div class="col-1">
<span class="dot">
<h:graphicImage library="imgs" name="campus.png" width="25" height="25" style="align-content-center"/>
</span>
</div>
dot css
.dot {
height: 75px;
width: 75px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
}
Don't worry about h:graphicImage, it is a JSF tag which translates to <img> in html. Any html class can be applied to it.
I am using Bootstrap 4.3.
Try Display Flex and align items and justify content to center.
.dot {
height: 75px;
width: 75px;
background-color: #bbb;
border-radius: 50%;
display: flex;
align-items:center;
justify-content:center;
}
.dot {
display: block;
margin-left: auto;
margin-right: auto;
}
<div class="container">
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-4"><img class="dot" src="campus.png"/></div>
<div class="col-sm-4"></div>
</div>
I gave this a try, I couldn’t comment so I had to code this. I used position: absolute. Hope this helps!
.dot {
height: 75px;
width: 75px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
}
.campus {
position: absolute;
top: 11.5%;
left: 8%;
transform: translate(-50%, -50%)
}
<div class="col-1">
<span class="dot">
<img src="https://pngimage.net/wp-content/uploads/2018/05/campus-png-5.png" width="25" height="25" style="align-content-center" class="campus"/>
</span>
</div>
I have searched for similar questions however unfortunatley the left:50% soultion does not work here.
I have a container (.leftLanding) with a relative postion, inside this I have a div with an absolute position (.imageCenter) which I would like to center horizontally. Adding left: 50% doesn't actually center it however as the container has a with of 85% I also tried 42.5% but this didn't work either.
I've removed all unnecessary code.
HTML:
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG1.png">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG3.png">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG2.png">
</div>
</div>
CSS:
.leftLanding {
display: flex;
position: relative;
width: 85%;
margin-left: 3%;
transition: all 0.5s ease;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
}
If you add this rule, where flex: 1 tells the flex items (in this case the first div and the last img) to take all the available space (and since they are 2 they share it 50/50)
.leftLanding div:first-child,
.leftLanding img{
flex: 1;
}
And the use left: 50%, transform: translate(-50%) like this it will work
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translate(-50%);
border: 1px solid gray;
}
Added borders on the two so it clearly shows
.leftLanding {
display: flex;
position: relative;
width: 85%;
margin-left: 3%;
transition: all 0.5s ease;
border: 1px solid gray;
}
.leftLanding div:first-child,
.leftLanding img{
flex: 1;
}
.leftLanding div:first-child {
background: lightblue;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translate(-50%);
border: 1px solid gray;
}
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="http://placehold.it/150/f00">
</div>
</div>
To center an absolutely positioned element horizontally, use a combination of left: 50%; transform: translateX(-50%); and it will center it relative to it's closest non-static positioned ancestor.
Though I'm not sure what you're trying to do with this layout, so not sure if that's really what you're looking for, but added some borders/background colors to show the children are centered horizontally.
I have a container (.leftLanding) with a relative postion, inside this I have a div with an absolute position (.imageCenter) which I would like to center horizontally.
This will center .imageCenter in .leftLanding.
.leftLanding {
display: flex;
position: relative;
width: 85%;
transition: all 0.5s ease;
background: #aaa;
border: 1px solid blue;
}
.imageCenter {
position: absolute;
width: 25%;
height: 30%;
align-self: center;
z-index: 100;
left: 50%;
transform: translateX(-50%);
background: #eee;
border: 1px solid red;
}
<div id="landing-images">
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG1.png">
</div>
<div class="rightLanding right">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG3.png">
</div>
<div class="leftLanding left">
<div class="imageCover">
</div>
<div class="imageCenter">
Test
</div>
<img class="landingImage" src="assets/landingIMG2.png">
</div>
</div>
Try to set the .imageCenter to width:100% and style display:block margin:auto to the img tag
.imageCenter {
position: absolute;
width: 100%;
height: 30%;
align-self: center;
z-index: 100;
}
img{
display: block;
margin: auto;
}
I have a set of divs whose dimensions are set by percentage:
<div class="parent">
<div class="x20">content 1</div>
<div class="x4">gutter</div>
<div class="x20"> content 2 </div>
<div class="x4">gutter</div>
<div class="x20"> content 3 </div>
<div class="x4">gutter</div>
<div class="x20"> content 4 </div>
<div class="x4">gutter</div>
</div>
.parent {
position: absolute;
left: 50%;
top: 50%;
width: 100%;
height: 100%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
}
.x20 {
height: 20%;
border: 2px solid #f0f;
vertical-align: text-bottom text-align: center;
position: relative
}
.x4 {
height: 4%;
border: 2px solid #ccc;
}
How can I get the "content" to be bottom aligned?
Flexbox method can solve this with two lines of code.
display: flex and align-items: flex-end on the content element.
.parent {
position: absolute;
left: 50%;
top: 50%;
width: 100%;
height: 100%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
}
.x20 {
height: 20%;
border: 2px solid #f0f;
vertical-align: text-bottom text-align: center;
position: relative;
display: flex; /* Add to initiate the flex box layout */
align-items: flex-end; /* Add to align the content to cross-axis end */
}
.x4 {
height: 4%;
border: 2px solid #ccc;
}
<div class="parent">
<div class="x20">content 1</div>
<div class="x4">gutter</div>
<div class="x20">content 2</div>
<div class="x4">gutter</div>
<div class="x20">content 3</div>
<div class="x4">gutter</div>
<div class="x20">content 4</div>
<div class="x4">gutter</div>
</div>
try adding like
.x20 p {
position:absolute;
bottom: 0px;
}
and then including
<p> content 1</p>
that works for me..
http://codepen.io/anon/pen/qObBKQ
You could try the following :
HTML
<div class="x20">
<div class="x10"> content 1</div>
</div>
CSS
.x20 {
height: 20%;
border: 2px solid #f0f;
position: relative
}
.x10 {
position: absolute;
bottom: 0;
left: 50%;
}
Add another div inside your content container and position that absolute should solve your problem.
See the following JSFIDDLE - LINK
try:
.x20 {
height: 20%;
border: 2px solid #f0f;
vertical-align: bottom;
text-align: center;
display: inline-block ;
}
.x4 {
height: 4%;
border: 2px solid #ccc;
display: inline-block ;
vertical-align: bottom ;
}