I want to center the "view" buttons on my Writings and Projects page, on portrait-oriented mobile devices. However, I can't quite get this to work. Sub-optimally, I've been using translateX as a bandaid solution; however, this doesn't get the job done on multiple screen widths. Here is my current code:
<div class="image-button-wrapper">
<div class="image-button sqs-dynamic-text">
<div class="image-button-inner">
View
</div>
</div>
</div>
#media screen and (max-width: 414px) {
.image-button-wrapper {
max-width: 150px;
transform: translateX(56%);
}}
So far, none of the centering tricks I can think of have proven effective: margin: 0 auto;, text-align: center;, etc. Does anybody have a clue how to fix what should be a simple issue?
Thanks in advance!
Tyler
I think the issue isn't that it's difficult to do, but in that there so many different ways of doing it, depending on the situation, it's hard to know which to reach for.
For example for horizontal unknown width:
use this :
.image-button-wrapper {
width:100%;
height:100vh;
position:relative
}
.image-button-inner {
width: 100%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
position: absolute;
left: 50%;
padding: 20px;
resize: both;
overflow: auto;
max-width: 150px;
text-align: center;
}
<div class="image-button-wrapper">
<div class="image-button sqs-dynamic-text">
<div class="image-button-inner">
View
</div>
</div>
</div>
For Vertical use this :
.image-button-wrapper ,.image-button.sqs-dynamic-text{
width:100%;
height:100vh;
position:relative
}
.image-button-inner {
width: 100%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
position: absolute;
top: 50%;
resize: both;
overflow: auto;
max-width: 150px;
text-align: center;
display:table;
vertical-align:center;
}
<div class="image-button-wrapper">
<div class="image-button sqs-dynamic-text">
<div class="image-button-inner">
View
</div>
</div>
</div>
and for both use :
.image-button-wrapper ,.image-button.sqs-dynamic-text{
width:100%;
height:100vh;
position:relative
}
.image-button-inner {
width: 100%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
position: absolute;
left: 50%;
top:50%;
padding: 20px;
resize: both;
overflow: auto;
max-width: 150px;
text-align: center;
}
<div class="image-button-wrapper">
<div class="image-button sqs-dynamic-text">
<div class="image-button-inner">
View
</div>
</div>
</div>
To know more this is why transform:translate(-50%) is used :
Related
What I'm trying to achieve, once I reduce the size of the screen and some elements starts to move to a second line, keep them organized to the left, instead of centered (based on the number of elements on that row). This image might help
And this is my actual code, what I'm doing wrong?
#teamBoxesWrapper {
position: relative;
background-color: ;
width: 1200px;
height: auto;
left: 50%;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-ms-transform: translateX(-50%);
-o-transform: translateX(-50%);
display: flex;
flex-flow: row wrap;
justify-content: space-between;
}
#teamUser {
width: 250px;
/* 20% of the viewport width */
height: 250px;
background-color: ;
margin: auto;
cursor: pointer;
margin-bottom: 20px;
position: relative;
}
.teamUser4 {
background-image: url('../images/empleado4.jpg');
background-position: center center;
background-size: cover;
}
<div id="teamBoxesWrapper">
<div id="teamUser" class="teamUser4">
<div id="teamUserPopup4" class="teamUserDetails">
<div id="teamUserTextAligner">
<h3 class="teamUserText1">Manuel Brenes</h3>
<hr class="userHr" />
<h3 class="teamUserText2">Graduado Social y
<br>Derechos Laborales</h3>
</div>
</div>
</div>
</div>
On your code try replacing your justify-content: space-between for center;
I'm trying to vertically align the first div at the center of the browser and everything below it below that div.
I may be able to wrap these two divs in another div and centering that div may work, but I can't change this html structure and have to achieve with two divs only. The first div is a dynamic container where different html will be displayed. The second div is static.
.center {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
<div class="center">
I'm at the center of the Browser!
</div>
<div>I'm just below the center!</div>
Add width and margin: auto 0 to the css class and apply it to the div elements.
like this:
.center {
margin: 0 auto;
width: 200px;
}
<div class="center">
I'm at the center of the Browser!
</div>
<div class="center">I'm just below the center!</div>
This should work (I added the color only to make it easier to see the result)
.center {
width:750px;
margin-left: auto;
margin-right: auto;
background-color:Red;
}
<div class="center">
I'm at the center of the Browser!
</div>
<div>I'm just below the center!</div>
Summary:
You can use this code that is in the jsfiddle.
I wrote two classes named .center-x and .center-y. You can use these classes when you want to center elements by x and y axes.
The code:
.box-1 {
background: #00adef;
padding: 5px 10px;
}
.box-2 {
background: #ccc;
padding: 5px 10px;
position: absolute;
top: 100%;
width: 170px;
}
.center-y {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.center-x {
position: absolute;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
}
.center-x.center-y {
-webkit-transform: translateY(-50%) translateX(-50%);
-ms-transform: translateY(-50%) translateX(-50%);
transform: translateY(-50%) translateX(-50%);
}
<div class="box-1 center-y center-x">
I'm at the center of the Browser!
<div class="box-2 center-x">I'm just below the center!</div>
</div>
And if you want to center by vertical, just remove the .center-x class from .box-1. Else if you want to center by horizontal, just remove the .center-x class from .box-1.
If the browser supports viewport units, you can use this way:
.center {
position: relative;
left: 0;
margin-top:50vh;
}
See it working: http://jsfiddle.net/fgpqkrr4/
For some reason my vertical alignment code is blurring some but not all child elements in Chrome and Safari.
Whats causing it is the translateY(-50%), if I remove this then blurriness is gone however the vertical centring effect is lost.
/* #group Center all the things */
.center-wrapper {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
.center-wrapper .center {
position: relative;
overflow: hidden;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
/* This fixes the blurred buttons but breaks centering
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transform: translateZ(0);*/
}
/* #end */
Tried and tested methods such as below work but they break the vertical centring:
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transform: translateZ(0);
Faced the same issue while trying to position buttons in the middle, but the button appeared on hover of the parent element and each time text inside was randomly cut.
The solution is:
.positioned-button {
transform: translateY(-50%) scale(1);
filter: blur(0);
line-height: 1;
}
Breaks nothing, fixes Chrome :)
The only way around this blurry issue, from what I could see was to change the vertical alignment method and use display: table instead. Here's how I did it:
1) Keep HTML markup the same
<div class="center-wrapper">
<div class="center">
<p>Centered content here</p>
</div>
</div>
2) Change CSS to the following:
/* #group Center all the things */
.center-wrapper {
min-height: 100%;
padding: 0;
display: table;
width: 100%;
}
.center-wrapper .center {
display: table-cell;
vertical-align: middle;
}
/* #end */
You can see the display table in action here.
.center-wrapper {
min-height: 200px;
padding: 0;
display: table;
width: 100%;
background: black;
}
.center-wrapper .center {
display: table-cell;
vertical-align: middle;
color: white;
}
.center-wrapper .center div {
height: 40px;
background: red;
width: 50%;
margin: 0 auto;
}
<div class="center-wrapper">
<div class="center">
<div>Centered content here</div>
</div>
</div>
I know this issue has been discussed a lot and i have read over the other questions and answers but i have not been able to solve this issue. I am using bootstrap and i want to center a div which works in chrome and firefox however in explorer the content is on the right side of the screen. I am unsure of what approach to take in order to correct the position. The css for my page is:
html,
body {
height: 100%;
width: 100%;
margin: 0 auto;
background-color: white;
font-family: "Verdana", Geneva, sans-serif;
}
.sRed {
color: black;
}
u {
color: red;
}
.container {
position: relative;
height: 14rem;
}
.jumbotron {
position: absolute;
top: 50%;
left: 50%;
-moz-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
background-color: white;
}
.fa-exclamation-triangle {
color: red;
padding-right: 10px;
}
<body>
<div class="jumbotron vertical-center">
<div class="container">
<h1><center><i class="fa fa-exclamation-triangle fa-lg"></i><u><span class="sRed">Title</span></u></center></h1>
<center>
<h3>Main Content.</h3>
</center>
</div>
</div>
</body>
I have included a fiddle Here. Thank you for any help and suggestions
It's because you are missing normal transform property (and -ms for old browsers)
http://jsfiddle.net/tvc4tv9L/2/
-moz-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
Try adding transform: translate(-50%, -50%) and -ms-transform: translate(-50%, -50%) to .jumbotron for IE9+.
Another way to do this:
html,
body {
height: 100%;
width: 100%;
margin: 0 auto;
background-color: white;
font-family: "Verdana", Geneva, sans-serif;
}
.sRed {
color: black;
}
u {
color: red;
}
.container {
position: relative;
height: 14rem;
}
.jumbotron {
position: absolute;
top: 50%;
left: 50%;
width:300px;
height:200px;
margin:-100px 0 0 -150px;
}
.fa-exclamation-triangle {
color: red;
padding-right: 10px;
}
<body>
<div class="jumbotron vertical-center">
<div class="container">
<h1><center><i class="fa fa-exclamation-triangle fa-lg"></i><u><span class="sRed">Title</span></u></center></h1>
<center>
<h3>Main Content.</h3>
</center>
</div>
</div>
</body>
Never ever use the <center> tags. Use the css equivalent instead: text-align: center;
You forgot the -ms- prefixed translate property.
If you don't care about support for IE9 or lower: You can use flexbox for this as well.
html, body {
height: 100%;
width: 100%;
}
body {
display: flex;
background: black
}
.jumbotron {
margin: auto;
background: white;
}
.container {
text-align: center;
}
<body>
<div class="jumbotron vertical-center">
<div class="container">
<h1>Title</h1>
<h3>Main Content.</h3>
</div>
</div>
</body>
I have a situation like this: http://jsfiddle.net/uqhwt1wj/
HTML:
<div class="activity_rounded">
<img class="image" src="http://i.imgur.com/059cOzT.png?1" />
</div>
CSS:
.activity_rounded{
width: 165px;
height: 165px;
border-radius: 165px;
-moz-border-radius: 165px;
overflow: hidden;
margin: 0 auto;
position: relative;
margin-bottom: 30px;
background: #FFDE15;
}
.image{
max-width: 226px;
height: auto;
position: absolute;
left: 50%;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-ms-transform: translateX(-50%);
-o-transform: translateX(-50%);
}
It works all good in all browsers, however in safari it seems like overflow: hidden ignores border radius of the block and hides overflow only for full div block(square). Tried to Google around, but haven't seen any solutions with horizontal centering that would work properly in my case.
Any suggestions, links or comments would help a lot.
in this case cross-browser solution that I decided to use was: using image as background image of a div instead of wrapping image element inside a div.
So now code looks like this(DEMO):
HTML:
<div class="activity_rounded"></div>
CSS:
.activity_rounded{
width: 165px;
height: 165px;
border-radius: 165px;
-moz-border-radius: 165px;
overflow: hidden;
margin: 0 auto;
position: relative;
margin-bottom: 30px;
background: url('http://i.imgur.com/059cOzT.png?1') no-repeat center #FFDE15;
}
Hope this helps to anyone as well.