CSS3 transform:translate produces inaccurate results - html

I was trying to position the right edge of a div element 300px from the center of the page with the following code:
HTML:
<div id="content">
<div id="login">
<!-- login area -->
</div>
</div>
CSS:
#content {
width: 100%;
position: relative;
top: 0px;
}
div#login {
position: absolute;
text-align: right;
right: 50%;
transform: translate(300px, 0px);
}
However, strangely, this results in the div being moved to the right by 374px.
Is there anything I forgot to think about?
Or is there a better way to reach the same result?

Another solution without using transform -
use right: calc(50% - 300px);
See this JSFiddle first
Now check this fiddle
don't forget to widen the jsfiddle result window to see the result properly
Hope this works as you want. If this doesn't work then please feel free to share it, also check if another CSS is overriding some styles of the div, I doubt this is happening.

Well, as it turned out, the problem was not CSS, but Windows 10 settings. Be careful when measuring screenshots in GIMP while having set the general display size at 125%...
After that, all solutions worked fine, thanks!

Related

How to make diagonal divs like this?

I was searching for diagonal divs and I didn't find anything that can help me. I have this project to do and I need to do this.
This
And This
I want to do exactly like that but I only find posts with horizontal diagonal divs..
Thanks, regards.
Edit: The second image link its the continuation from above.
Try this easiest solution on the internet for making polygon shapes
You can use a CSS property clip_path to produce a shape of any type.
-webkit-clip-path: polygon(0 33%,100% 10%,100% 60%,0 85%); This line represents that we are drawing polygon which has four points and specifying the
location of each point in terms of x and y. You can specify the
position in terms of px, % or any unit terminology
CSS Code
#header{
background-color:green;
height:350px;
width:100%;
-webkit-clip-path: polygon(0 33%,100% 10%,100% 60%,0 85%);
}
And
HTML Code
<html>
<head>
</head>
<body>
<div id="header">
</div>
</body>
</html>
And check this link, where I've written the same code just to illustrate the clip-path functionality, to jsfiddle
For further and details Explanation check these links:
https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path
https://css-tricks.com/almanac/properties/c/clip/
let me know if u didn't understand anything. I'll be more than happy to help you.
I don't know nothing about diagonal divs but u can try to cover parts of img's that you need with
.cover {
position: absolute;
top: -25px; /* or differenet position that fit on every section*/
left: 0;
z-index: 1;
width: 100vw;
height: 50px; /* or different height that you need*/
transform: rotate(-10deg); /* or different angle*/
background-color: white;
}
<div class="cover"></div>
And the same thing on bottom of section.
Be sure to set overflow and position on every section:
overflow: hidden;
position: relative;

set div center and make it fixed

How to set a div in the center of the screen/parent div, and make it fixed, which will ignore the width changes after it has been placed in the center? I have a div which contain a table, it looks like this:
I am not sure whether the outer div is necessary or not. I want my table to be placed in center, and fixed, which it will ignore its width changes. Result below is what I get:
As you can see, the table moves left when its size changes to remain the table in center, I want to prevent this, any idea?
This is what I have so far:
.main
{
position: relative;
width: 600px;
left: calc(50% - 300px);
border: 1px solid #000;
}
.table
{
margin: 0 auto;
}
<div class="main">
<table class="table">
<tr><td>Username: </td><td><input type="text"></td><td>ErrorMessage</td></tr>
<tr><td>Password: </td><td><input type="text"></td><td>ErrorMessage</td></tr>
</table>
</div>
Try using this style inside the div, it works for me and you may use help text instead of them inside a table.
<div style="text-align:center">
<input/><br/><input/><br/><button/>
</div>
Change you position to fixed
position:fixed;
and re-align other divs that might have been rearranged with top bottom left right
Why the use of a table for lay-out? Tables should be used for data output or HTML mails (correct me if I'm wrong but that's what I know).
Also you can use this code to vertical and horizontal center a specific element (keep in mind this is absolute but please feel free to play around with this):
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
Regarding the fact you want the error messages not to inflict the width of the form. I would advice using position absolute.
I've wrote an example for you see https://jsfiddle.net/bdwte97g/
Hope this helps.
Remove margin: 0 auto; from .table
and put this lines
`.table{
position: relative;
left: 45%;
}`

HTML / CSS: exception in Google Chrome

sorry if the question title is weak, i can't quite sum my problem up into one snappy tagline...
I'm working on a website (using Joomla) and i've had to insert a DIV serving as a sidebar on the right side of the page. in order for it to be displayed "above" (or "over", i mean on the z-axis) the regular page content, i'm using a negative margin on the left side of it, covering the whole width of it, so it will simply float to the right and sit there, which works fine in ff and IE.
Since i've rarely ever run into issues with Chrome that were fine in IE, i didn't bother to check until quite late:
Now i see that in Chrome, the div is just sitting below (at the bottom of) the regular content; despite the "inline" display-types and the negative margin.
Now I've tried ridiculous things to make it work, but for some reason it just won't.
Can someone tell me how i can get it to work in Chrome?
HTML:
<div class="cframe">
<div class="content">
...
</div>
<div class="sideright">
...
</div>
</div>
CSS:
div.cframe {
display: table;
vertical-align: top;
}
div.content {
display: inline-table;
width: 751px;
padding: 60px;
}
DIV.sideright {
width: 200px;
float: right;
display: block;
position: relative;
top: 320px;
margin: 0px 0px 0px -200px;
}
...this is what i'm stuck with right now, it's all quite ugly.
[link to live-page removed as the solution has already been applied]
(The sidebar is the div classed sideright, and contains a module titled Archiv)
Thank you in advance
Change the div.content css to:
div.content {
display: inline;
float: left;
}
You're using float, but then setting the position to relative. You should remove the relative part of your css for the siderright and it should fix the issue
Edit: even better you should change the position to absolute.
Set your container div to position:relative and then position:absolute your sidebar in relation to that.
.cframe {
display: table;
vertical-align: top;
position: relative;
}
.sideright {
width: 200px;
position: absolute;
top: 320px;
right: 0;
}
I didn't test the answers above but I take their word that they worked. However, your question caught my eye, because I thought you were looking for a browser hack.
There are ways that you can tell an element to behave differently on a specific browser. This happens sometimes across browsers and the best way is to hack each individual browser and give them specific instructions. For chrome, of course you'll have to use a webkit.
This would be an easy example of the syntax to follow:
<p>TEST</p>
p {color:green;}
#media screen and (-webkit-min-device-pixel-ratio:0) {
p {color:red;}
}
Try the DEMO in several browsers and notice how only chrome will display it in red

How can I have two columns with a combined width of 100% ?

They say, that a single picture can explain more than a tousand words, so here's my "more": http://www.imagebanana.com/view/hcqsz5fs/cols.png
My goal is to have the columns as shown on the image, with them together having 100% body width.
And my fiddle is here: http://jsfiddle.net/c2JH3/ (note that this is just a mockup of my current work).
How can I achieve this?
A Quick Note
In your comments, you are saying that you can't use a background image because the height is variable.
The way to fix this is using multiple images, and tell them to repeat or not to repeat on different parts of the page. But I'll go over this after I answer your question directly.
Short Answer
To get 100% body width, you'll want to use percentages (%) on for your width rules. Like this:
#left {
width: 60%;
}
#right {
width: 40%;
}
Fixing Some Problems
One problem you are bound to encounter when you have content that passes the bottom of the screen. In this case, you need to tell the divs to stay side by side.
This should do the trick:
#left {
position: absolute;
top: 0;
left: 0;
width: 60%;
}
#right {
position: absolute;
top: 0;
right: 0;
width: 40%;
}
Back to the Note
Believe me, you don't want to be using percentages on your widths. It makes sizing and scaling extremely hard to design nicely, and changes that you try to make in the future probably won't work without a complete redesign of your css.
Like I was saying before, you want to use multiple images. You'll have background image on the body tag that - going by the design you provided - has the gray-to-orange fade in it. The css would look like this:
body {
background-image: url('path/to/header.jpg') no-repeat;
}
You would then have a wrapper div like the one you already have, that holds the content and such. Inside the wrapper you have:
A header (the logo and navbar), which would have no background (so you can see the body background).
A featured section which holds that really big image in the middle. You can use negative margins to get it centered.
A subnav section for those images in the middle. This would have it's own background image that has a matching part of the background of the body so that it appears to flow in as the image does.
A content section that holds all of the content of the page. This would have an image repeating vertically to look continuous.
#content {
background-image: url('path/to/slice.jpg') repeat-y;
}
A subfooter section that has the curved part of the page (that gray-to-orange curve at the bottom).
And finally a footer section that has all the stuff on the very bottom.
You can use the same structure on the inner pages, you would just use different images and spacing to change the look of the page.
To Sum Up
You will never, ever need to have a 100% width for your wrapping div. (I say this to generalize, there are certain styles that use this, but they aren't the same kind of design).
What you should always try to do first is create images for the body, header, content, and footer sections that create the look you want.
Then have your wrap be a set width in pixels that will stay in the center of the page, while the margins increase and reveal more of the background image.
Have fun and good luck with your design!
I don't know how to use fiddle. But this worked out fine for me.
I just used the background colors and borders to properly show the differences
<!doctype html>
<html>
<head>
<title>xxx</title>
<style>
body{
margin: 0;
}
#wrap {
width: 100%;
}
#left {
width: 600px;
float: right;
border: 1px solid #000000;
}
#right {
width: 350px;
border: 1px solid #000000;
}
#container_left {
width: 55%;
float: left;
background: red;
}
#container_right {
width: 45%;
float: right;
background: blue;
}
</style>
</head>
<body>
<div id="wrap">
<div id="container_left">
<div id="left">
<p>Content</p>
</div>
</div>
<div id="container_right">
<aside id="right">
<p>Sidebar</p>
</aside>
</div>
</div>
</body>
Cheers!
Thank you all for your answers and ideas. They were helpful and I did learn something new (my biggest 'thank you' goes to #Jon for a really great, great post). But, since I can't solve this neither with percents nor backgrounds (since my design is a little more complicated that the one provided), I made my way with jQuery. To sum up, here's my mockup fiddle.
Note: sometimes you'll need to change left 125 to 126, just to make sure both ends meet.

CSS: Standard (dynamic) way to centralize an element in the y-axis

my question is more or less self-explanatory, I am trying to find a standard dynamic way to centralize an element in the y-axis, much like the:
margin: auto;
For the x-axis. Any ideas?
I am talking about the following piece of code, empty page, align one image in the center.
<div id="main" style="display: block;">
<img style="margin: auto; display: block;"
src="http://www.example.com/img.jpg" />
</div>
Any help will be appreciated! :)
Just give up and use tables on this one, with vertical-align: middle. You can get away with just a single-row, single-cell table without feeling too guilty (I sleep like a baby about it). It's not the most semantic thing in the world, but what would you rather maintain, a tiny one celled table, or figuring out the exact height and doing absolute positioning with negative margins?
If you know the height of the element that you're trying to center, you can do this:
img {
display: block;
height: 500px;
position: absolute;
top: 50%;
margin-top: -250px; /* 50% of your actual height */
}
I know only one way for that:
#mydiv {
position: fixed;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
margin-top: -50px;
margin-left: -50px;
}
This is for x and y axis - but width/height and margins have to be changed for every element. I hate it :-)
Additionally you get problems if the element is larger than the browser-window.
The best known method is to use absolute positioning. You set the top amount to 50% and then set a margin top of minus half of the element.
#main {
position: relative;
}
#main img {
position: absolute;
top: 50%;
margin-top: -(half your image height)px;
}
Here is a variation using vertical-align
http://jsfiddle.net/audetwebdesign/r46aS/
It has a down side in that you need to specify a value for line-height that will also define the height of the containing element that acts like the viewport (outlined in blue).
Note: You may be able to get around the window height issue by setting a height to the body or html element (100%) but you would need to try it out (see 3rd reference).
However, the good thing is that you don't have to do some math based on the dimensions of the image.
Here are some references related to vertical alignment:
http://css-tricks.com/what-is-vertical-align
http://blog.themeforest.net/tutorials/vertical-centering-with-css
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
and sometimes I have to remember the basics so I reread:
http://www.w3.org/TR/CSS21/visudet.html
This may not solve OP's problem, but may be useful in other contexts.
Using #menu img { vertical-align: middle; } in my style sheet works great for the latest versions of FireFox, Opera, Safari and Chrome, but not in IE9. I have to manually add style="vertical-align: middle" to every line of img code. For example:
<li><a href="../us-hosts.php">
<img src="../images/us-button.png" width="30" height="30"
alt="US Hosts" style="vertical-align: middle;"/> US Hosts</a>
</li>
Try this css:
margin-top:auto;
margin-bottom:auto;
display:block;