Trying to make an H1 larger than the viewport so it partially sits hidden off the screen horizontally without prompting x-axis scroll. The body and container width is set to 100% so currently the H1 just breaks to the next line when it gets wider than the viewport. Any idea's?
This was my interpretation of your question, the use of vh units makes the font-size size to the viewport.:
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#biggee {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
overflow: hidden;
font-size: 50vh;
line-height: 15vh;
white-space: nowrap;
}
<div id="biggee">
<h1>This is HUGE</h1>
</div>
A possible solution is to set a height equal to a single line of your h1 on the container.
Example:
.container {
width: 100%;
overflow:hidden;
height: 690px;
}
Fiddle: http://jsfiddle.net/7pyxtjah/
So if a single line of your h1 is 60px in height, make the height 60px.
One way (of presumably, many) is:
h1 {
/* width of the parent element: */
width: 100%;
/* not particularly relevant */
padding: 0;
margin: 0;
/* large font-size to increase the chance of the text
extending out of the view-port: */
font-size: 600%;
/* again, to increase the chance of the text exceeding
the view-port: */
letter-spacing: 400%;
/* to prevent scroll-bars: */
overflow: hidden;
/* preventing line-breaks, to stop wrapping: */
white-space: nowrap;
}
<h1>This is the header</h1>
Unless you meant, literally, to have the <h1> element larger than the view-port, in which case:
html, body {
margin: 0;
padding: 0;
/* to prevent the user scrolling to see the extent of the text
'below the fold': */
overflow-y: hidden;
}
h1 {
/* vh: 1% of the viewport's height,
this sets font-size to 150% of the viewport's height: */
font-size: 150vh;
line-height: 1.4;
margin: 0;
padding: 0;
overflow: hidden;
white-space: nowrap;
}
<h1>This is the header</h1>
References:
CSS relative lengths and units (including vh/vw).
Related
I'm using a CSS based image-map, which I want to display correctly whatever the size of the browser window. Of course there are actually multiple links.
My HTML ...
<div id="sitemap" >
<img src="img.jpg" class="center"/>
<a href="url1.html" id='id1'></a>
</div
And the CSS ...
#sitemap img{
max-width: 100vw;
max-height: 100vh;
position: relative;
}
#sitemap a {
display: block;
position: absolute;
}
#sitemap a:hover {
background: rgba(255, 255, 0, 0.5);
border-radius: 20px;
}
a#archive {
top: 48%;
margin-left: 14%;
width: 20%;
height: 15%;
}
This works great in a tall, narrow browser, but when the browser window is wider than it is tall, the percentages consider the dead space in the blank sidebars. How can I make the percentages consider only the actuall image?
So you know the reason.
This is because of the div(id=sitemap)'s width.
How about this one?
#sitemap {
/* for debug background-color: red; */
/* make sure the div width only size of contents */
display: inline-flex;
/* You set position relative to "img", but it semmed doesn't work because it isn't a parentāchild relationship */
position: relative;
}
#sitemap img{
max-width: 100vw;
max-height: 100vh;
/* position: relative; */
}
a#archive {
/* I think it's good enough setting two properties, unless you aren't particular about the details. */
top: 10%;
left: 10%;
}
Assume the parent is relative, the child (style-x) is absolute. I used top 50%, left 25% to center the child.
I wish to actually center the child, so I set transform: translate(-50%, -50%). I am unsure if this is centered, so I double check by deleting that line and adding 'margin-top: -55px;' (half of the height), and 'margin-left: -45px;' (half of the width).
These two lines position my element in slightly different locations, yet this is different from my model of CSS. What's going on?
body {
height: 100%;
width: 100%;
margin: 0 auto;
}
#main {
overflow: auto;
height: 64vh;
width: 38vw;
margin: 0 auto;
margin-top: 10%;
position: relative;
border: 1vh solid black;
overflow: hidden;
}
#style-x {
/*Why doesn't translate(-50%, -50%) give me
the same position as setting the margin top and
left to half of the width and height?*/
width: 90px;
height: 110px;
/*
transform: translate(-50%, -50%);*/
margin-top: -55px;
margin-left: -45px;
position: absolute;
top: 50%;
left: 25%;
padding: 2%;
text-align: center;
background: green;
}
#left-col {
float: left;
width: 4%;
height: 101%;
margin-left: 46%;
background: black;
}
#right-col {
float: left;
width: 4%;
height: 101%;
margin: 0 auto;
margin-left: 0;
background: black;
}
<body>
<section id='main'>
<div id='style-x'>X</div>
<div id='left-col'></div>
<div id='right-col'></div>
</section>
</body>
Here's my Codepen if you'd like a visualization.
http://codepen.io/sentedelviento/pen/ORyqzv
There is no problem in your method. Both will try to center based on the values you provide.
The margin method fails cos you aren't using a Box Sizing method like so.
box-sizing: border-box
This results in all your elements to be larger than the height and width specified. Without this, you are telling the browser to add any padding or border to both width & height.
And so your larger element shifts when using using the margin method.
You've set a 2% padding on style-x, and a width of 38vw on #main. When using margins to center things, you would need to account for these varying values.
When you set a percentage padding, its calculated based on the width of the containing block.
The transform method on the other hand, uses the bounding box of the containing block and has no problem centering a larger element.
I'd suggest you include this box-sizing on main and style-x if using the margin method. You could just use
*, after, before {
box-sizing: border-box;
}
This gives better control over dimensions across all elements.
some times in wrapped by width div needs to set for an element background to full width, so I set it in an pseudo element, but descktop browser, when page is long height adds 16px for vertical scrooll bar to viewport, so I calculate it
by calc (see below).
Here is Example
HTML:
<div class="wrapped">
<h1>100vw background in wrapped</h1>
<div class="fullbg">
some body text, images, etc here
</div>
</div>
CSS
html, body { margin: 0; padding: 0; }
body { height: 100%; width: 100%; }
div { position: relative; }
*,*:before,*:after { box-sizing: border-box;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box;
}
.wrapped {
width: 70%;
margin: 0 auto;
height: 150vh; /* simulate long heigh */
}
.fullbg {
height: 5em;
/* some styles here*/
}
.fullbg:before {
content: "";
bottom: 0;
display: block;
background: rgba(85, 144, 169, 0.7);
position: absolute;
width: 100vw;
right: 50%;
margin-right: -50vw; /* work for short page or mobile browser*/
margin-right: calc( -50vw + 8px ); /* work for desctop long page */
top: 0;
z-index: -1;
}
I looked answer at
Prevent 100vw from creating horizontal scroll
Difference between Width:100% and width:100vw?
and others questions,
but do not find real true universal css solution for this
as a temporary solution may be an js, like this:
var scrollbarWidth = ($(document).width() - window.innerWidth);
but I think it not the best solution, and now I not figured out how to use it with a pseudo considering that to scroll width can vary.
ps. no one overflow: hidden!
The scrollbar can be targeted specifically.
Check this out for the fix in chrome and safari
http://codepen.io/anon/pen/dXgmbZ
Key CSS:
.element::-webkit-scrollbar {
width: 0 !important;
}`
The codepen is just your example with the chrome fix. If you'd like to see a more robust solution, check out this JSFiddle:
http://jsfiddle.net/E78q3/
The idea behind this is just clipping out the scroll bar with absolute positioning and hiding container/wrapper overflow. Simple, clever, yet effective.
Further Reading:
https://blogs.msdn.microsoft.com/kurlak/2013/11/03/hiding-vertical-scrollbars-with-pure-css-in-chrome-ie-6-firefox-opera-and-safari/
I'm trying to center a div with an unknown height.
I can't find a solution that allows scroll to the top of the div when the viewport height is less than the div height.
HTML
<div>
<p>This will be hidden when <br />
window_height < div_width</p>
<br />
<br />
<br />
How to make it scroll to the top?
</div>
CSS
body {
background: grey;
}
p{
background: green;
}
div {
position: absolute;
left: 50%;
top: 50%;
box-sizing: border-box;
transform: translate(-50%, -50%);
max-width: 500px;
width:100%;
height: 700px; /* Unknown*/
padding: 20px;
background: red;
color: white;
text-align: center;
}
http://codepen.io/Koopa/pen/GpypdX
Thanks
The reason you can't scroll to the top of the div is because the transform property with negative values positions the div off-screen on smaller screens.
In this demo transform is disabled:
http://codepen.io/anon/pen/wKpMyM
Also, when you apply absolute positioning to an element you take it out of the normal flow of the document. This means it is ignored by its container. Hence, the body and html element have zero height.
In this demo the body has a green border (which is totally collapsed):
http://codepen.io/anon/pen/RWxrod
To make your layout work, you can give the body a minimum height (so it can expand along with the div) and, instead of centering with absolute positioning, use a flexbox.
CSS
html { height: 100%; } /* necessary for percentage heights to work */
body {
background: grey;
border: 10px solid green; /* for demo purposes */
min-height: 100%; /* allow body to expand with children */
display: flex; /* establish flex container */
justify-content: center; /* center div horizontally, in this case */
align-items: center; /* center div vertically, in this case */
}
p {
background: green;
}
div {
/* REMOVE
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%); */
box-sizing: border-box;
max-width: 500px;
width:100%;
height: 700px; /* Unknown*/
padding: 20px;
background: red;
color: white;
text-align: center;
}
DEMO: http://codepen.io/anon/pen/OyzMvV
Note that flexbox is supported by all major browsers, except IE 8 & 9.
DEMO
Sometimes I'll create a square (or any rectangle, really) that will respect its ratio at any size using a method similar to this method.
What I want:
to prevent the square extending outside of the viewport on devices with a small height
i.e. mobile phone in landscape
Proposed solution
limit width of square to a percentage of viewport height using max-width: 90vh
expect ratio to be respected
CSS
.square {
position: relative;
height: 0;
padding-bottom: 100%;
overflow: hidden;
}
.square-inner {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.mw { max-width: 90vh;} /* solution, but makes things break */
HTML
<div class="square mw">
<div class="square-inner"></div>
</div>
What should happen
in viewports with small heights, the square should be a max width of 90% of the viewport height
What actually happens:
when viewport height is less than width of square:
width is constrained as per vh value
height is calculated from width of square had it not been constrained to vh
we get a vertically long rectangle
The spec says that the relative value is calculated from the 'containing block', which to me seems as though it should be the current width of the container.
Browser behaviour:
Chrome 29.0.1547.65: as described
Firefox 23.01: as described
Opera: does not respect vh at all Not validated with Opera 16+
Am I interpreting the spec incorrectly, or is this a possible bug in implementation by browser vendors?
The problem is in using both lengths in % and vh.
Try this:
Working Example
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: sans-serif;
font-weight: 100;
}
.neat {
width: 50%;
max-width: 600px;
min-width: 320px;
margin: 0 auto;
}
.col {
float: left;
padding: 2rem;
width: 90vh; /*** Important bit changed width:50%; to width:90vh; ***/
max-width: 50%; /*** Important bit added max-width:50%; ***/
}
.square {
position: relative;
height: 0;
padding-bottom: 100%;
overflow: hidden;
}
.square-inner {
position: absolute;
background-color: #333333;
color: white;
top: 0;
bottom: 0;
left: 0;
right: 0;
padding: 1.5rem;
}
.mw {
max-width: 90vh;
}
I don't think Opera supports vh,and there are known issues. I'm wondering if this bug is affecting what you're seeing: http://code.google.com/p/chromium/issues/detail?id=124331.