width = 100% not properly working in IE, Microsoft edge browser - html

enter image description hereI am facing some strange problem in edge and all other IE versions.The problem is that I declare a div and set some property on that div.Initially I set width: 1140px but when the screen resize to its max-width: 1200px I set the div width:100%.All this changes are working perfectly in chrome,firefox,opera,safari but when I test this code in edge or any IE versions it takes too much space on the right side of the device screen.
header{
width: 100%;
height: 500px;
position: relative;
}
.beast-text-box {
position: absolute;
width: 1140px;
height: auto;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
#media only screen and (max-width: 1200px) {
.beast-text-box {
width: 100%;
}
}
<header>
<div class="beast-text-box">
<h1 class="heading-h1">TRAIN LIKE A BEAST</h1>
BEAST MODE
SHOW ME MORE
</div>
</header>

transform is causing issue in IE. Try below CSS to .beast-text-box in media.
position: relative;
left: 0;
transform: translate(0);
header {
width: 100%;
height: 500px;
position: relative;
overflow: hidden;
}
.beast-text-box {
position: absolute;
max-width: 1140px;
height: auto;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
#media only screen and (max-width: 1200px) {
.beast-text-box {
width: 100%;
/*Added this CSS*/
position: relative;
left: 0;
transform: translate(0);
/*Added this CSS*/
}
}
<header>
<div class="beast-text-box">
<h1 class="heading-h1">TRAIN LIKE A BEAST</h1>
BEAST MODE
SHOW ME MORE
</div>
</header>

I made the following changes and it started working! There a semi-colon missing in header CSS and instead of width: 1140px; i changed to max-width: 1140px;. What i guess happend is, left: 50% and width: 1140px together are adding to the total width and forcing the horizontal scrollbar to show up in IE11.
header{
width: 100%;
height: 500px;
position: relative;
overflow:hidden;
}
.beast-text-box {
position: absolute;
max-width: 1140px;
height: auto;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
#media only screen and (max-width: 1200px) {
.beast-text-box {
width: 100%;
}
}
<header>
<div class="beast-text-box">
<h1 class="heading-h1">TRAIN LIKE A BEAST</h1>
BEAST MODE
SHOW ME MORE
</div>
</header>

Related

100vh going over viewport only on laptop

I'd like to use #conA as a hero section and have #conA's height to fill the entire viewport (so #conA height:100vh). It's working correctly on tablet, but on laptop, it looks like #conA's height takes up much more than viewport.
Son on tablet it looks correctly:
enter image description here
But on laptop, it looks like:
enter image description here
(note that I tried putting a blue border to #conA, and it's way taller than viewport)
What I'd like it to look like instead is this:
enter image description here
Is there a way to fix this?
<section id="conA">
<div class="container">
<div id="heroText">
<div id="text-fixed">lorem ipsum</div>
<div id="text"></div>
</div>
<div id="images"></div>
</div>
</section>
#conA {
height: 100vh;
}
#conA .container {
margin-left: auto;
margin-right: auto;
display: block;
}
#heroText {
color: #56525E;
}
#conA #text {
display: inline;
}
#media (min-width: 600px) and (max-width: 959px) {
#conA .container {
width: 70%;
}
#conA {
min-height: 800px;
position: relative;
}
#heroText {
line-height: 1.7;
font-size: 28px;
text-align: center;
position: absolute;
width: 70%;
top: 20%;
left: 50%;
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
#conA #images {
text-align: center;
position: absolute;
top: 35%;
left: 50%;
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
width: 90%;
}
#conA #images img{
width: 13em;
height: auto;
}
}
It feels like there is more to it than you have provided. However, my observations are as such
You have #media queries defined for tablet view, so your height:100vh is not affecting it on tablets.
Instead of defining height:100vh try using max-height: 100vh. It would limit your #conA to 100vh only.
Make sure you have <meta name="viewport" content="width=device-width, initial-scale=1.0"> added inside your tag.
Finally, it very much depends on type of screen. The Pixel DPI (density per inch) for your Tablet may be something very different from your system. Read more about Pixel density here: https://material.io/design/layout/pixel-density.html
do you really need a #media?
here is your simplified code with flexBox.
is the rendering as expected?
#conA {
height: 100vh;
border:blue 5px solid;
}
#conA .container {
display:flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#heroText {
background: red;
line-height: 1.7;
font-size: 28px;
text-align: center;
width: 70%;
margin-top:20vh;
}
#conA #images {
background: yellow;
text-align: center;
margin-top: 1em;
width: 100%;
}
#conA #images img{
width: 13em;
height: auto;
}

How do I make a flexible block that will make have the same width and height as a flexible photo and will be on top of the photo?

look, i have a photo (1000px). This photo is flexible. It means when the user's window is less than 1000px photo starts reducing in proportion. So, i want to make a block, that will do the same as photo. That block should have width and height like a photo.
.img_random{
margin-left: 50%;
transform: translate(-50%);
position: absolute;
max-width: 1057px;
width: 95%;
}
.img_random img{
width: 100%;
}
.block-that-should-be-like-a-photo{
position: absolute;
max-width: 1057px;
height: 621px;
width: 95%;
background-color: red;
opacity: 0.5;
margin-left: 50%;
transform: translate(-50%);
}
<div class="img_random">
<img src="https://sun9-14.userapi.com/c857536/v857536576/10ef5e/WDQSe10CrOs.jpg" alt="A random photo">
</div>
<div class="block-that-should-be-like-a-photo"></div>
Considering the fact that the ratio will always be the same you can consider the following code:
.img_random {
margin-left: 50%;
transform: translate(-50%);
position: absolute;
max-width: 1057px;
width: 95%;
}
.img_random img {
width: 100%;
}
.block-that-should-be-like-a-photo {
position: absolute;
max-width: 1057px;
width: 95%;
background-color: red;
opacity: 0.5;
margin-left: 50%;
transform: translate(-50%);
}
.block-that-should-be-like-a-photo:before {
content:"";
display:block;
padding-top:48%; /* The ratio of the image*/
}
<div class="img_random">
<img src="https://www.tanianault.ca/wp-content/uploads/2017/02/Nault-SunsetOverBaldButte-1000px.jpg" alt="A random photo">
</div>
<div class="block-that-should-be-like-a-photo"></div>
You just need to put your overlay div inside the same parent as the image and make the overlay take up 100% of the parents space to match the space taken by the image.
.img_random{
left: 50%;
transform: translate(-50%);
position: absolute;
max-width: 1057px;
width: 95%;
}
.img_random img{
width: 100%;
}
.block-that-should-be-like-a-photo{
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: red;
opacity: 0.5;
}
<div class="img_random">
<img src="https://www.tanianault.ca/wp-content/uploads/2017/02/Nault-SunsetOverBaldButte-1000px.jpg" alt="A random photo">
<div class="block-that-should-be-like-a-photo"></div>
</div>

How to absolute position DIV of unknown width

My layout consists of 3 DIVs
The first DIVis a wrapper.
The second DIV is centered and uses max-width:980px; Otherwise it defaults to 100% width.
The third DIV is 200px wide and uses absolute position. right:-200pxand top:0px position it next to the first DIV
This layout works perfect but only because the last DIVhas a width of 200px. If that DIV had a variable width I couldn't use right:-200px and it wouldn't place correctly.
So my question is what would I do if the DIV with absolute position had a variable width? How would I place it next to the main DIV?
Here is my code.
<div class="outer_container">
<div class="internal_alignment">
<div class="main_container"></div>
<div class="column_outside"></div>
</div>
</div>
CSS
.outer_container {
overflow: hidden;
position: relative;
}
.internal_alignment {
position: relative;
max-width: 980px;
margin: 0px auto;
}
.main_container {
height: 500px;
background-color: bisque;
}
.column_outside {
position: absolute;
top: 0px;
right: -200px;
height: 500px;
width: 200px;
background-color: black;
}
FYI: the outer_container DIV allows column_outside to sit outside the screen if the browser is smaller than 980px wide.
Make it a child of the main and give it left: 100%;
.outer_container {
overflow: hidden;
position: relative;
}
.internal_alignment {
position: relative;
max-width: 980px;
margin: 0px auto;
}
.main_container {
height: 500px;
background-color: bisque;
}
.column_outside {
position: absolute;
top: 0px;
left: 100%;
height: 500px;
width: 200px;
background-color: black;
}
<div class="outer_container">
<div class="internal_alignment">
<div class="main_container">
<div class="column_outside"></div>
</div>
</div>
</div>
After a second thought, simply use left: 100% instead of right: -200px;
.outer_container {
overflow: hidden;
position: relative;
}
.internal_alignment {
position: relative;
max-width: 980px;
margin: 0px auto;
}
.main_container {
height: 500px;
background-color: bisque;
}
.column_outside {
position: absolute;
top: 0px;
left: 100%;
height: 500px;
width: 200px;
background-color: black;
}
<div class="outer_container">
<div class="internal_alignment">
<div class="main_container"></div>
<div class="column_outside"></div>
</div>
</div>
Very simple:
.column_outside {
right: 0px;
-webkit-transform: translateX(100%);
-moz-transform: translateX(100%);
transform: translateX(100%);
}
Demo https://jsfiddle.net/n4nq6Lxt/
No need to change your HTML structure.
You can use transform: translateX(100%); what it does is to move the element to the right of the amount of the width of the element itself.
right: 0;
transform: translateX(100%);

Centre an image within a div when filling 100% width and height

I am trying to centre an img within a containing div, where the img fills (minimum) 100% of the width and height of that containing div, meaning thta the image automatically scales to maintain image ratio. It is easy for me to align that img to the top, bottom, left or right, but I am hoping to centre the img both vertically and horizontally. I have been unable to locate the solution thus far, so any help greatly appreciated.
HTML
<section id="hero-image">
<img src="https://s-media-cache-ak0.pinimg.com/originals/ae/1d/6e/ae1d6ef744320d237a95fc1e7d6ee98b.jpg">
</section>
CSS
#hero-image {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
background: red;
}
#hero-image img {
position: absolute;
min-height: 100%;
height: auto;
min-width: 100%;
width: auto;
margin: auto;
right: 0;
left: 0;
top: 0;
z-index: 0;
}
Fiddle
Use transform:translateX(-50) to manage this (but CSS3), large screen or small screen the image will always stay center and keep his ratio aspect.
Here the fiddle
Otherwise if you want something more cross browser you will probably need a bit of javascript, but I may be wrong.
Could you not set the hero image as a background? This will allow for more flexibilty both in terms of positioning and image size.
<section class="hero-image" style="background-image:url('https://s-media-cache-ak0.pinimg.com/originals/ae/1d/6e/ae1d6ef744320d237a95fc1e7d6ee98b.jpg');">
</section>
.hero-image {
width: 100%;
height: 400px;
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
}
This achieves what you've set out to do exactly. There's other benefits to this method too, for instance, responsive images.
The CSS above sets the properties for any background image within a div class of hero-image. All you need to do then, is inline the background-image itself.
NOTE: If this doesn't have to be CMS driven, you can simply apply the image to the class rather than have it inline.
If you're happy with CSS3 (not supported in some older browsers) you could do this:
#hero-image img {
position: absolute;
min-height: 100%;
height: auto;
min-width: 100%;
width: auto;
margin: auto;
left: 50%;
top: 50%;
z-index: 0;
-webkit-transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
}
#hero-image {
position: relative;
width: 600px;
height: 400px;
overflow: hidden;
background: red;
}
#hero-image img {
position: absolute;
min-height: 100%;
height: auto;
min-width: 100%;
width: auto;
margin: auto;
left: 50%;
top: 50%;
z-index: 0;
-webkit-transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
}
<section id="hero-image">
<img src="https://s-media-cache-ak0.pinimg.com/originals/ae/1d/6e/ae1d6ef744320d237a95fc1e7d6ee98b.jpg">
</section>
You can try this:
CSS
#hero-image {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
background: red;
}
#hero-image img {
position: absolute;
display:block;
height: auto;
margin: 0 auto;
top: 0;
z-index: 0;
min-height:100%;
width:100%;
left: -50%;
-webkit-transform: translateX(50%);
transform: translateX(50%);
}
HTML
<section id="hero-image">
<img src="https://s-media-cache-ak0.pinimg.com/originals/ae/1d/6e/ae1d6ef744320d237a95fc1e7d6ee98b.jpg">
</section>
DEMO HERE
You could also just set it as a background with background-size: cover. Like this: https://jsfiddle.net/wzjzjsdp/2/
.img1, .img2 {
height: 400px;
width: 300px;
background-image:url(http://placehold.it/350x150);
background-repeat: no-repeat;
background-position: center center;
background-size:cover;
display:inline-block;
}
.img2 {
width:500px;
height:400px;
}
<div class="img1"></div>
<div class="img2" style="background-image:url(http://placehold.it/350x250"></div>
EDIT: You can use inline style.

Vertical centering image in unknown height div does not properly position image on mobile Safari/iOS

I'm using the vertical centering technique explained here on CSS-Tricks: http://css-tricks.com/centering-in-the-unknown
I have an image that needs to be vertically centered in a div. It seems to work just fine on every platform except mobile Safari/iOS, where the image is placed out of view. I can't seem to work out what the quirk or compliance issue is on mobile Safari that is causing this.
Here's the issue in a CodePen: http://codepen.io/anon/pen/iDalc
Here's my stripped down HTML and CSS
<div class="headline">
<div class="wrapper">
<a>
<div class="background">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Durga%2C_Burdwan%2C_2011.JPG/1920px-Durga%2C_Burdwan%2C_2011.JPG">
</div>
</a>
</div>
</div>
<div class="headline">
<div class="wrapper">
<a>
<div class="background">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Upper_Antelope_Canyon_Heart_Formation_2013.jpg/640px-Upper_Antelope_Canyon_Heart_Formation_2013.jpg">
</div>
</a>
</div>
</div>
<div class="headline">
<div class="wrapper">
<a>
<div class="background">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Maly_Krashokholmsky_bridge_4exp_Oloneo.jpg/1920px-Maly_Krashokholmsky_bridge_4exp_Oloneo.jpg">
</div>
</a>
</div>
</div>
And my CSS:
.headline {
background: gray;
padding: 0 3.125%;
width: 93.75%;
margin-left: auto;
margin-right: auto;
max-width: 768px;
}
.wrapper {
position: relative;
overflow: hidden;
min-height: 190px;
width: 100%;
margin-bottom: 2px;
}
.background {
position: absolute;
top: 0;
height: 100%; width: 100%;
z-index: 1;
background-color: #000;
text-align: center;
}
.background img {
width: 100%;
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.background:before {
content: "";
position: absolute;
z-index: 2;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
}
Can anyone advise what CSS issue is happening in mobile Safari? So far, I've diagnosed it may have something to do with the transform: translate-Y property. All mobile Safari testing done on device as well as iOS simulator.
Thanks in advance!
For anyone who comes across this in any searches, I managed to fix this on my own. Changed .background img to position: absolute; and left: 0;
I don't entirely understand what's going on but whatever it fixed it. I'd also like to add that I was mistaken in my original post. Somewhere I credited CSS Tricks for the vertical centering technique I implemented, which wasn't the actual source. I had referenced the following URL: http://davidwalsh.name/css-vertical-center
Updated CodePen: http://codepen.io/anon/pen/zJugd
.headline {
background: gray;
padding: 0 3.125%;
width: 93.75%;
margin-left: auto;
margin-right: auto;
max-width: 768px;
}
.wrapper {
position: relative;
overflow: hidden;
min-height: 190px;
width: 100%;
margin-bottom: 2px;
}
.background {
position: absolute;
top: 0;
height: 100%; width: 100%;
z-index: 1;
background-color: #000;
text-align: center;
}
.background img {
width: 100%;
position: absolute;
left: 0;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
.background:before {
content: "";
position: absolute;
z-index: 2;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
}