Centered div is getting cut off at the top? - html

Here's the page I'm working on: http://en08-phx.stablehost.com/~news/test.html
This is the code I'm using to center the div:
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 70%;
height: auto;
padding: 20px;
background-color:rgba(255, 255, 255, 0.2);
color: white;
text-align: center;
border-radius:5px;
border:2px solid rgba(255, 255, 255, 0.5);
}
Whenever I decrease the width of my browser or use a mobile phone, the top of the page starts to get cut off.
I want the div to be centered regardless of the size of the browser width. However, if the browser's height is too small, I'd prefer to just add a margin of 10px to the top/bottom and make sure all the text shows.
What exactly am I doing wrong here?

The issue with this positioning technique (top 50% minus translateY -50%) is that it aligns itself based its own height. When the viewport squishes the container taller than the viewport it remains centered with the top and bottom getting cut off. If you're able to use flexbox I recommend flexbox (http://caniuse.com/#search=flex).
Wrap the container you want always centered in another container such as centered-wrapper and apply flexbox to center it:
.centered-wrapper {
display: flex;
flex-direction: column;
justify-content: center;
min-height: 100%;
}
The min-height is very important here. If your div doesn't stretch to the page's height it'll fill and center its child. If it is then it'll just keep expanding avoiding the scenario that you have as well. If you're box is relative in height to the whole page then you'll also need to set the height of your page for this to work:
html,
body {
height: 100%;
}
You don't need any centering done to your centered div, just the visual styles.
Here's a sample of it in action: https://jsfiddle.net/vfc9n7p2/

body {
display: flex;
flex-direction: column;
justify-content: center;
}
div {
/* position: absolute; */
/* left: 50%; */
/* top: 50%; */
/* transform: translate(-50%, -50%); */
width: 70%;
height: auto;
padding: 20px;
background-color: rgba(255, 255, 255, 0.2);
color: white;
text-align: center;
border-radius: 5px;
border: 2px solid rgba(255, 255, 255, 0.5);
margin: 0 auto;
}
this will work fine for you.

<br clear="all">
This has worked for me. Just put this before your content element like div.

Related

Is there any way to set multiplication margin in css?

I have a container an I need to put a box in the middle, but I cant get a static height to parent because its going to change. because of that my box cant be set in the middle of the page. I need some help
#container {
display : flex;
align-items: center;
justify-content: center;
}
Keep this styling on your container box(parent box). This will place the inner div in the center and maintain the same when the height of the container is changed too.
Use this
#container {
display : flex;
align-items: center;
justify-content: center;
}
if this doesn't work
try to set certain width & height of box to be placed in middle and follow according to parent container size.
For example: if the parent container measures 100% of width and width of box is 30% ; then give a
margin-left : 35% ;
Similarly set the height and give margin-top.
Hope this method may help you out.
Sorry if this doesn't work .
.parent {
position:relative;
float: left;
margin: 5px;
width: 180px;
height: 180px;
border:1px solid #DDD;
}
.box {
position: absolute;
background: #1695A3;
color:#FFF;
text-align: center;
width: 40px;
height: 40px;
line-height: 40px;
}
.box3 {
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div class="parent">
<div class="box box3">3</div>
</div>

Center CSS X button

I have bee trying to do a close button using CSS in order to close a tag.
I have done this using a compination of a pattern described here: Patterns for Closebuttons, and a css only pattern for creating an X Close Button using CSS.
Now to my question:
I can't figure out why the X is not entierly centered.
If I change the placement to 50% of whatever width I'm using for the lines it seemes fine, thus I come to the conclusion it has to have something to do with the width of the lines.
I'm writing here to see if someone could explain this to me.
'HTML
<button type="button" aria-label="Close">
<span aria-hidden="true" class="close"></span>
</button>
'CSS
button {
display: flex;
justify-content: center;
align-items: center;
font-weight: 700;
padding: 0px;
background-color: rgb(192,192,192);
border: none;
color: white;
text-decoration: none;
height: 150px;
width: 150px;
border-radius: 50%;
}
button:hover {
background-color: rgb(146, 146, 146);
cursor: pointer;
}
.close {
position: relative;
/* right: 10px; */
width: 60%;
height: 60%;
}
.close:before, .close:after {
position: absolute;
content: ' ';
height: 100%;
width: 20px;
background-color: rgb(255, 255, 255);
}
.close:before {
transform: rotate(45deg);
}
.close:after {
transform: rotate(-45deg);
}
I created a codepen for simplicity: Codepen link
Just add flexbox to the .close class and you're ready to go
.close {
position: relative;
width: 100%;
height: 60%; // 100% means that both lines will take the entire space
display: flex;
align-items: center;
justify-content: center;
}
The width of the .close:before, .close:after CSS rule is actually letting both lines "expand" horizontally, starting from the center.
But, as they are being rotated, their starting position is not the same as if they were not rotated, so that's why their alignment becomes non-centered. You can inspect the item and you will see that the actual box has an empty space, which is the "rotated width" empty space.
Consider a x = 0 coordinate (0px from CSS box left margin) which is the horizontal starting point of the CSS box; rotating elements will make content to start at x = ? (usually half of the content width)
You could solve this issue also by setting a negative margin-left with its value being half the width, but using flexbox will require much less maintenance (just imagine working with UI/UX guys telling you to change its dimensions)
Add This CSS left:50%; margin-left:-10px; on .close:before, .close:after
.close:before, .close:after {
position: absolute;
content: ' ';
height: 100%;
width: 20px;
background-color: rgb(255, 255, 255);
left:50%;
margin-left:-10px;
}
Hello there try to add this to your span class .close:
display: flex;
justify-content: center;
align-items: center;
You should not use right or left when your attribute is relative.

How to get text input that has been rotated by 90 degrees to take up full height of div

I can't seem to work out how to get my input to take up the whole height of the parent div when its rotated (i.e. I want some one to be able to type as much text in as will fit in the parent div.
I've tried changing both the width and height values of the input to 100% and it doesn't help.
I have it working when the input is not rotated, so there must be some sort of trick I am missing?
HTML
.Yaxis-Label-Container {
grid-area: Yaxis-Label;
display: flex;
align-items: center;
justify-content: end;
width: 100px;
height: 1000px;
}
#yaxisLabel {
width: 100%;
height: 29px;
margin: 0 0 0 5px;
background: transparent;
color: #000000;
border: none;
outline: none;
font-size: 16px;
text-align: center;
transform: rotate(-90deg);
}
<div class="Yaxis-Label-Container" >
<input
type="text"
autocomplete="off"
placeholder="Yaxis"
id="yaxisLabel"
/>
</div>
I've ran into this problem as well. All you have to do is use viewport width(vw) and viewport height(vh)
input {
width: 100vw;
height: 100vh;
}
or just try using an id selector if for some reason that selector won't work
#input {
width: 100vw;
height: 100vh;
}
*You won't use 100vw and 100vh you'd have to adjust it to fit the div element, but I can't get it to work any other way.
With your html and css and rotating the div instead of the input it will become this css
.Yaxis-Label-Container {
border: 1px solid red; /*remove this line, for test only*/
grid-area: Yaxis-Label;
display: flex;
align-items: center; /* input will center in width 1000px */
width: 1000px;
height: 100px;
margin-top: calc(0.5 * (1000px - 100px)); /* half of width minus height */
transform: rotate(-90deg);
}
#yaxisLabel {
box-sizing: border-box;
width: 100%;
height: 29px;
margin: 0;
background: transparent;
color: #000000;
border: 1px solid transparent;
outline: none;
font-size: 16px;
text-align: center;
}
<div class="Yaxis-Label-Container" >
<input
type="text"
autocomplete="off"
placeholder="Yaxis"
id="yaxisLabel"
/>
</div>
In the end I did what bron suggested but found that the yaxis label appeared to be shifted to the left as a result of the rotation
Therefore I had to implement a javascript solution to fix its position on the x co-ordinate
I added a listener to the co-ordinates of the parent div. Everytime it moved (i.e. screen resize) I had to calculate the distance required needed to shift the label element left or right such that it appeared in the right place

creating perfect square with inline-block

I'm wondering if there's a way to create a perfect square using display: inline-block The reason is that I want to place it right next to a text e.g.
.legend {
display: inline-block;
color: rgba(0, 0, 0, 0);
width: 1em;
background-color: lightblue;
}
<div>
<div class="legend">
d
</div>
<div style="display: inline-block">
Some legend
</div>
</div>
Right now it still looks kinda rectangular.
You can simply specify the div's height, too. Consider the following:
/* The container needs to be relatively positioned */
.container {
position: relative;
}
/* The legend is absolutely positioned, but in relation to its
* container.
* We also apply a common trick to place it at the vertical center of
* its parent: position the top bound at 50% of the parent's height.
* then transform the position to move it up by 50% of its own height.
*/
.legend {
display: inline-block;
color: rgba(0, 0, 0, 0);
width: 1em;
height: 1em;
background-color: lightblue;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/* This div is decisive for the whole thing's height.
* Since the legend is positioned in an absolute way, we need to make
* room for it by moving this div to the right (margin-left)
*/
.legend-text {
margin-left: 1em;
padding: 5px;
display: inline-block;
}
<div class="container">
<div class="legend">
d
</div>
<div class="legend-text">
Some legend
</div>
</div>
Its very simple. add height to .legend
.legend {
display: inline-block;
color: rgba(0, 0, 0, 0);
width: 1em;
height: 1em;// just add this line
background-color: lightblue;
}

Height Responsive Horizontal Image Gallery, Fixed Header & Footer

I'm trying to make a horizontal scroll gallery for a portfolio of photography on my website, but I want the images to be responsive to height (to fit varying screen sizes). To try and do this I have used the unit: vh and this is causing me problems.I have a position:fixed header and footer so they always stay on the screen while you scroll through the gallery. With the CCS I have used this means as the screen gets smaller, the images go underneath the header & footer rather than constantly staying inbetween them.
I have seen a website with an ideal horizontal gallery very similar to what I am trying to achieve. You can check out the website here. On the linked website the images always seem to stay equidistant from the header and footer.When inspecting the element it looks like they're using tables, which I understood to be a big no, no. Is this how they are achieving this effect on the gallery?
I've linked a JS Fiddle to a very basic version of my design so you can see what I've done so far.
JS Fiddle: https://jsfiddle.net/pmh9zvta/1/
Basically, in a sentence I'm asking how I can achieve the same effect as the example website in the link.
Robin,
Hmm...so vh can actually achieve a pretty similar effect. Your example images are rather extreme, though (1500x100).
Check out this fiddle I made (using your code as a base):
https://jsfiddle.net/Benihana77/5xw21tvc/
*,
*:before,
*:after {
box-sizing: inherit;
}
html {
height: 100%;
box-sizing: border-box;
position: relative;
}
body {
position: relative;
margin: 0;
padding-bottom: 100px;
min-height: 100%;
}
#header {
width: 100%;
padding: 10px;
margin-right: auto;
margin-left: auto;
position: fixed;
background-color: #fff;
background: rgb(255, 255, 255);
/* Fall-back for browsers that don't support rgba */
background: rgba(255, 255, 255, 0.92);
text-align: center;
z-index: 1;
}
#gallery-wrapper {
position: relative;
padding-top: 60px;
overflow-x: scroll;
}
#gallery-wrapper img {
height: 70vh;
width: auto;
}
#footer {
font-family: Corda-Light;
font-size: 14px;
color: #333;
width: 100%;
padding: 5px;
padding-top: 13px;
padding-bottom: 8px;
padding-left: auto;
padding-right: auto;
position: absolute;
bottom: 0;
background-color: #efefef;
text-align: center;
background-color: #fff;
background: rgb(255, 255, 255);
/* Fall-back for browsers that don't support rgba */
background: rgba(255, 255, 255, 0.9);
z-index: 1;
}
/* Navigation Bar Styling */
.nav {
border-bottom: 1px solid #ccc;
border-width: 1px 0;
list-style: none;
margin: 0;
padding: 0;
padding-top: 5px;
padding-bottom: 5px;
text-align: center;
}
.nav li {
display: inline;
}
.nav a {
display: inline-block;
padding: 10px;
}
/* Horizontal Gallery Styling */
ul.gallery-row {
white-space: nowrap;
}
ul.gallery-row li {
list-style: none;
display: inline;
}
/* Footer Styling */
.footer {
list-style: none;
margin: 0;
padding: 0;
text-align: center;
}
.footer img:hover {
opacity: 0.6;
filter: alpha(opacity=60);
}
Main changes
Added a wrapper around your content for better management (within JSFiddle and out).
Changed your footer to be positioned absolutely, along with a host of other changes that allow it to stick to the bottom until your Viewport is too short. Then it gets pushed down like a normal footer. This keeps your content from going behind the footer.
Made the "gallery-wrapper" with "overflow-x:scroll". I'm personally not a fan of side-scrolling galleries, but if your heart is set on it, this will keep the side-scrolling contained to this block, and no your entire website (in turn obviating the need for a "fixed" footer).
Chose some more realistic image dimensions to work with, and a shorter vh (70).
Regarding your example, as best as I can tell, they're using Javascript to rewrite the height of the "scrollHolder" container DIV. So their solution is not CSS-only, instead using JS to read the height of the browser and adjust the height accordingly.
I'd also say their approach is flawed, as it doesn't scale properly to browser width. On a thinner screen, you can only see zoomed-in pieces of each image.
So, in addition to the above changes, I'd recommend:
Setting media-queries at an appropriate browser width (say 760) so that your images become scaled by browser width, not height (so vw, not vh).
This might require some special "min-height" settings in order to keep your tall images from becoming toooo tall, and short images from becoming little munchkins.