It works fine on Desktop because I use width: 100%. But on Mobile I do width: 50% and try to center it.
I have tried setting the img.youtube-thumb to margin-left: auto; and margin-right: auto; I've also tried setting the images parent element to text-align: center; and having the image displayed as inline-block, and that doesn't work either.
I'm not sure what else to try, as it seems that every S.O answer I run into provides one of the two above solutions.
Here's a CodePen but I'm not sure how valuable it'll be since I cant seem to get it exact without pasting the entire project into the pen. http://codepen.io/anon/pen/yOEQKg
Here's the HTML for one of the images:
<section class="container-fluid">
<div class="section group">
<div class="col span_6_of_12">
<div class="thumbnail project">
<h3>Some title</h3>
<div class="youtube-container">
<div class="youtube-player" data-id="ZMJP-FFzaZk">
<div>
<img class="youtube-thumb" src="//i.ytimg.com/vi/ZMJP-FFzaZk/hqdefault.jpg">
<div class="play-button"></div>
</div>
</div>
</div>
<div class="caption">
<p>blablablablabla</p>
</div>
</div>
</div>
my CSS code:
/* inside media query */
.youtube-player {
position: absolute;
text-align: center;
width: 100%;
height: 100%;
}
/* inside media query*/
img.youtube-thumb {
bottom: 0;
display: inline-block;
margin-left: auto;
margin-right: auto;
max-width: 50%;
width: 50%;
height: 100%;
position: absolute;
}
.project {
text-align: center;
}
/* for entire site */
.youtube-container {
position: relative;
padding-bottom: 300px; /* 16:9 */
padding-top: 25px;
height: 0;
}
/* for entire site */
div {
text-align: left;
}
/* 12 Grid Column Setup??? */
/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
}
img.youtube-thumb {
bottom: 0;
display: inline-block;
margin-left: auto;
margin-right: auto;
max-width: 50%;
width: 50%;
height: 100%;
position: absolute;
}
into this:
img.youtube-thumb {
top: 0;
display:block;
left:0;
right:0;
margin:auto;
max-width: 50%;
width: 50%;
height: 100%;
position: absolute;
}
Hope it helps
Related
I am having a really hard time with getting this image centered.
I have tried the following:
margin: 0 auto;
display: block;
text-align: center;
I really do not want to use the left command because it isn't working in my mobile setting. I just want a fixed property that will work everywhere and I won't have to add it again.
Why is this image not centering?
#section3-container {
position: absolute;
top: 25%;
text-align: center;
width: 80%;
margin: 0 10%;
}
.approach-tablet {
bottom: 0;
position: relative;
/*left: 50%;*/
height: 200px;
width: auto;
}
.approach-tablet img {
display: block;
margin: 0 auto;
}
<div id="section3-container">
</div>
<img src="/examples/imgs/tablets.png" alt="tablets" class="approach-tablet">
I had also tried the below but it still doesn't work.
.approach-tablet {
bottom: 0;
position: absolute;
/*left: 50%;*/
}
img.approach-tablet {
display: block;
margin: 0 auto;
text-align: center;
}
I need the position: absolute to position the div where I am wanting it to go. It sits on the bottom of the page. Regardless, the image isn't centering with what is in there.
As indicated in this SO answer, an element that is positioned absolutely cannot be centered using the margin: 0 auto method and you would have to resort to other options.
One option would be to use left: 50% and then use transform: translateX(-50%) to get it back to the center. The left: 50% offsets the image 50% from the left edge of the page (but this alone will not center the image because the image's left edge is at page center). The translateX(-50%) moves the image to the left by half of the image's width and thus would result in the image's center being at page center.
This should work in all modern browsers (including mobile) as the browser support is good.
As can be seen from the snippet (view it in normal mode and full page mode), no special tweaking is needed for it to be responsive.
Note: Though you had stated that you don't want to use left property in the question, I understand based on your comment that the reason was that mobile support is needed and be responsive.
#section3-container {
position: absolute;
top: 25%;
text-align: center;
width: 80%;
margin: 0 10%;
}
.approach-tablet {
bottom: 0;
position: absolute;
left: 50%;
height: 200px;
transform: translateX(-50%);
}
<div id="section3-container">
</div>
<img src="http://optimumwebdesigns.com/fullPage.js/examples/imgs/tablets.png" alt="tablets" class="approach-tablet">
Please use below code
<div id="section3-container">
<img src="http://optimumwebdesigns.com/fullPage.js/examples/imgs/tablets.png" alt="tablets" class="approach-tablet">
</div>
CSS
#section3-container {
margin: 0 auto;
text-align: center;
width: 100%;
}
.approach-tablet {
height: 200px;
margin: 0 auto;
text-align: center;
width: auto;
}
Your image is outside of the div. If you put it inside, it centers
#section3-container {
position: absolute;
top: 25%;
text-align: center;
width: 80%;
margin: 0 10%;
}
.approach-tablet {
bottom: 0;
position: relative;
/*left: 50%;*/
height: 200px;
width: auto;
}
.approach-tablet img {
display: block;
margin: 0 auto;
}
<div id="section3-container">
<img src="http://optimumwebdesigns.com/fullPage.js/examples/imgs/tablets.png" alt="tablets" class="approach-tablet">
</div>
Just add another div, html is all about divs:
#section3-container {
position: absolute;
top: 25%;
text-align: center;
width: 80%;
margin: 0 10%;
}
.approach-tablet {
bottom: 0;
position: relative;
/*left: 50%;*/
height: 200px;
width: auto;
}
.approach-tablet img {
display: block;
margin: 0 auto;
}
#section4-container {
text-align: center;
width: 100%;
}
<div id="section3-container">
</div>
<div id="section4-container">
<img src="http://optimumwebdesigns.com/fullPage.js/examples/imgs/tablets.png" alt="tablets" class="approach-tablet">
</div>
I do have looked for this answer without success. I found some ways that you can do this but those looked really weird. (Like margin: -9999px;) So what I'm trying to do is center this image in my container. The overflow and width/heigh properties work as they should, but you're supposed to see the center of the image instead.
Thanks in advance!
The Html:
<section class="photo-grid-static">
<div class="photo-grid-container"><img src="Images and videos/odesza1.jpg"/></div>
<div class="photo-grid-container"></div>
<div class="photo-grid-container"></div>
</section>
The Css:
.photo-grid-static {
height: 300px;
width: 100%;
display: block;
position: relative;
background: black;
padding: none;
}
.photo-grid-container {
float: left;
width: calc(100% / 3);
height: 100%;
line-height: 100%;
margin: 0;
margin-right: 0;
padding: 0;
display: inline-block;
position: relative;
left: 0;
right: 0;
background: red;
}
.photo-grid-container img {
height: 100%;
overflow: hidden;
position: relative;
}
<section class="photo-grid-static">
<div class="photo-grid-container" style="background-image: url('videos/odesza1.jpg')"></div>
<div class="photo-grid-container"></div>
<div class="photo-grid-container"></div>
</section>
.photo-grid-container {
/* other styles */
background-position: center center;
background-size: cover;
}
If thats what you mean?
Ill do a mock up in a second.
https://jsfiddle.net/7zw0ej89/
I'm experimenting with a logo in css. I build this with different div's.
The logo contains two parts, the right and the left part. Each part contains two div's with the 2d- front material and one with the 3d extrusion parts. These last two are each split up into an upper and a lower div.
I made the logo bigger than it is supposed to so I translated the outer div to the correct size. I wasn't able to set the measurements of that outer div to automatically wrap the translated div container, so I did this manually.
I want to add this css logo into a horizontal navigation list, one of the item's needs to contain this logo.
I'm breaking my head on how to align this div in the center of my list item. I searched for it but I can't seem to find a descent solution. Can someone put me in the right direction ? It would be much appreciated !
---> I gave the div main-nav-logo tbe same size as my list item, now I want to accomplish that I get the logo div in the center of the main-nav-logo div ...
.main-nav-logo {
position: relative;
width: 200px;
/* for testing purpose */
height: 144px;
/* for testing purpose */
text-align: center;
/* for testing purpose */
margin-left: auto;
/* for testing purpose */
margin-right: auto;
/* for testing purpose */
float: left;
/* for testing purpose */
overflow: auto;
/* for testing purpose */
padding: 0px;
display: table;
/* for testing purpose */
clear: both;
/* for testing purpose */
}
.logo {
position: relative;
top: -110px;
width: 300px;
height: 300px;
display: block;
margin-left: auto;
/* for testing purpose */
margin-right: auto;
/* for testing purpose */
-moz-transform: scale(0.351);
-webkit-transform: scale(0.351);
-o-transform: scale(0.351);
-ms-transform: scale(0.351);
transform: scale(0.351);
}
.left-cap {
position: relative;
height: 100%;
width: 100%;
left: 0;
}
.right-cap {
position: relative;
height: 100%;
width: 100%;
top: -100%;
left: 0;
}
.left-cap-front {
position: relative;
height: 100%;
width: 100%;
z-index: 3;
display: inline-block;
}
.left-cap-extrusion {
position: relative;
height: 100%;
width: 100%;
top: -102%;
z-index: 2;
}
.right-cap-front {
position: relative;
height: 100%;
width: 100%;
z-index: 3;
}
.right-cap-extrusion {
position: relative;
height: 100%;
width: 100%;
top: -100%;
z-index: 2;
}
.left-cap-front-upper {
position: absolute;
}
.left-cap-front-lower {
position: absolute;
}
.left-cap-extrusion-upper {
position: absolute;
}
.left-cap-extrusion-lower {
position: absolute;
}
.right-cap-front-upper {
position: absolute;
}
.right-cap-front-lower {
position: absolute;
}
.right-cap-extrusion-upper {
position: relative;
}
.right-cap-extrusion-lower {
position: relative;
}
<div class="main-nav-logo">
<div class="logo">
<div class="left-cap">
<div class="left-cap-front">
<div class="left-cap-front-upper">
</div>
<div class="left-cap-front-lower">
</div>
</div>
<div class="left-cap-extrusion">
<div class="left-cap-extrusion-upper">
</div>
<div class="left-cap-extrusion-lower">
</div>
</div>
</div>
<div class="right-cap">
<div class="right-cap-front">
<div class="right-cap-front-upper">
</div>
<div class="right-cap-front-lower">
</div>
</div>
<div class="right-cap-extrusion">
<div class="right-cap-extrusion-upper">
</div>
<div class="right-cap-extrusion-lower">
</div>
</div>
</div>
</div>
</div>
Hoping for some insight ... (Also if it means that I will have to rewrite the whole thing)
Thanks in advance !
A screenshot from the above code in JSFiddle gives me :
logo
I am able to center my elements horizontally, but not vertically. I found couple of questions on stackoverflow regarding the same issue but the suggestions in those didn't work for some reason. Any suggestions? This is what I am going for: http://i62.tinypic.com/n4gzcw.png
Here is what I have so far:
<style>
.container {
width: 100%;
height: 100%;
background-color: red;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.datetime {
width: 50%;
margin-left: auto;
margin-right: auto;
margin-top: 15%;
background-color: white;
text-align: center;
}
.main {
width: 100%;
margin-left: auto;
margin-right: auto;
margin-top: 3%;
background-color: yellow;
text-align: center;
}
.left {
width: 25%;
float: left;
}
.left img {
width: 50%;
height: 50%;
max-width: 80px;
max-height: 80px;
}
.mid {
width: 50%;
text-align: center;
float: left;
padding-top: 5%;
font-size: 1em;
}
.right {
width: 25%;
float: left;
}
.right img {
width: 50%;
height: 50%;
max-width: 80px;
max-height: 80px;
}
.stadium {
width: 50%;
margin-left: auto;
margin-right: auto;
background-color: white;
text-align: center;
}
</style>
<div class="container">
<div class="datetime">26 August 2014<br />12:00pm</div>
<div class="main">
<div class="left"><img src="http://dejaniskra.comze.com/ManchesterUnitedUpcomingFixture/logos/mkdons.png" /></div>
<div class="mid">MK Dons vs Manchester United</div>
<div class="right"><img src="http://dejaniskra.comze.com/ManchesterUnitedUpcomingFixture/logos/manchesterunited.png" /></div>
</div>
<div class="stadium">Stadium:mk - Milton Keynes</div>
</div>
You can wrap your content with .container-inner and add styles:
.container-inner{
height: 150px;
position: absolute;
top: 50%;
margin-top: -75px;
width: 100%;
}
you can find the working example here:
http://jsfiddle.net/Lkfdxm13/
Creating a partial CSS layout and then attempting to adapt it to another strategy can be fraught with problems. My suggestion would be to create a clean set of html wrappers with only background colours and work on employing one of the solutions that you've found, for example How to verticaly align into the center of the content of a div with defined width/height?. Once you have a working layout then you can move your content in.
If you come across a problem that you can't solve then you can post a specific CSS question and the answers will probably be a lot more forthcoming.
Okay so I have been working on implementing the 'holy grail'-style layout for my website, so far it's pretty close but I noticed two things I want to fix.
The goal is a 'sticky' footer with the page length expands with the browser window height, a header, and 3 columns. 2 fixed columns on the left and right side, and a fluid column in the middle.
The issues I am having are that right now, my center 'fluid' column doesn't seem to be acting like I expected. Basically I want the fixed columns to always be fully shown, with the center column filling the remaining horizontal space. But the center column is taking up a lot of room and making it so that I have to scroll to view the right column (see image below). Also, the 'text-align: center' code doesn't appear to be centering text within the viewable area of the center column. Any help appreciated!
image: http://i.imgur.com/FPuSiIu.png
html:
<html>
<head>
<link type="text/css" rel="stylesheet" href="test.css" />
</head>
<body>
<div id="header">
<p>Header</p>
</div>
<div id="container">
<div id="center">
<p>Content</p>
</div>
<div id="left">
<p>Content</p>
</div>
<div id="right">
<p>Content</p>
</div>
</div>
<div id="footer">
<p>Footer</p>
</div>
</body>
</html>
css:
* {
margin: 0;
}
#container {
width:100%;
}
#header {
text-align: center;
background: #5D7B93;
height: 95px;
padding: 5px;
position: fixed;
top: 0;
width: 100%;
z-index: 15;
}
#center{
text-align: center;
margin-top: 105px;
background: red;
position: relative;
float: left;
width: 100%;
height: 100%;
}
#left {
height: 100%;
width: 150px;
text-align:center;
background:#EAEAEA;
margin-top: 105px;
margin-left: -100%;
overflow: scroll;
position: relative;
float: left;
}
#right {
position: relative;
height: 100%;
width: 150px;
margin-right: -100%;
margin-top: 105px;
background: blue;
text-align: center;
float: left;
}
#footer {
text-align:center;
background: #5D7B93;
height:25px;
padding:5px;
position: fixed;
bottom: 0;
width: 100%;
}
No need to float. Just position: absolute the sidebars and give the center div fixed margin on both sides.
JSFiddle
CSS
#container{
position: relative;
}
#left, #right {
width: 200px;
height: 100%;
position: absolute;
top: 0;
}
#left {
left: 0;
}
#right {
right: 0;
}
#center {
margin: 0 200px;
}
i've done this on my layout and it works fine for me
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container{
display: inline-flex;
width: 100%;
height: 100%;
background: lightblue;
}
#left {
width: 240px!important;
min-width: 240px!important;
background: red;
height: 100%;
}
#right {
width: 400px!important;
min-width: 400px!important;
background: red;
height: 100%;
}
#center {
background: blue;
width: 100%;
min-width: 600px;
height: 100%;
}