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;
}
Related
I'm trying to center text over an image; however, whenever I resize it, the text does not stay vertically centered.
.hero-image {
margin: 0 auto;
}
.hero-image img {
width: 100%;
}
.hero-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
<div class="hero-image">
<img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg">
<div class="hero-text">
Here is the hero text
</div>
</div>
Here's what's happening:
Working properly:
Vertical centering is off when resizing:
Your css for your hero text looks good – If I'm understanding your question correctly, I think the image should be modified to use 100 viewport width / height rather than 100%.
body {
margin: 0;
}
.hero-image img {
width: 100vw;
height: 100vh;
object-fit: cover;
}
.hero-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
You need to set the boundary of the container for positioned child. Add position: relative to .hero-image class. Otherwise it's using the next relative parent for it.
.hero-image {
margin: 0 auto;
position: relative;
}
.hero-image img {
width: 100%;
}
.hero-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
color: red;
}
<div class="hero-image">
<img src="https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg">
<div class="hero-text">
Here is the hero text
</div>
</div>
You can try this translate-free solution:
.hero-text {
position: absolute;
top: 33vw;
text-align: center;
width: 100%;
}
If you plan to keep the image stretched over the whole width.
You can do it with "display: flex".
.hero-image {
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg);
background-size: cover;
background-position: center;
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.hero-image img {
width: 100%;
}
.hero-text {
}
What is the best way to do that?
If full screen then show "FULL SCREEN MEMBERSHIP"
If mobile or tablet size then show "Please use Full Screen" and hide textA
Thanks !!!
#media only screen and (min-width: 301px) {
html,body{
height: 100vh;
width: 100%;
background: #356aa0;
}
.fullScreen{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50% , -50%);
background: #fff;
width: 400px;
border-radius: 12px;
text-align: center;
}
p {
display:none
}
#media only screen and (max-width: 300px) {
body {
background-color: red;
}
p {
display:block
}
}
<div class="fullScreen">
FULL SCREEN MEMBERSHIP
</div>
<p id="mobile">Please use Full Screen</p>
Try this:
html,
body {
height: 100vh;
width: 100%;
background: #356aa0;
}
.fullScreen {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #fff;
width: 400px;
border-radius: 12px;
text-align: center;
}
p {
display: none
}
#media only screen and (max-width: 300px) {
p {
display: block !important;
}
.fullScreen{
display:none;
}
}
<div class="fullScreen">
FULL SCREEN MEMBERSHIP
</div>
<p id="mobile">Please use Full Screen</p>
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>
I have the following fiddle
https://jsfiddle.net/7q8nejo3/1/
I'm going to explain what I have know, and what I intent to achieve.
NOW
I have a fixed position modal in the middle of the page in a min-width and min-height ... that is for mobiles devices... all works ok... if you resize the page it's all fine.
What I need
Due to the min-width and min-height, the modal keeps its size, so I need that when the screen is bigger, the tourImage also increase its size. I try to use
width: 80%; min-width:260px;
This in order to resize the picture but didn't work.
Any advice?
Yop,
I think you need to add a width on your fixed modal, and on your image, like this :
.tourImage{
position: relative;
width: 100%;
min-width: 260px;
min-height: 340px;
margin: 0 auto;
display: none;
background: #55acee;
}
.popover{
background:#fff;
width: 80%;
transform: translate(-50%, -50%);
position: fixed;;
top: 50% !important;
left: 50% !important;
padding: 0 20px;
}
https://jsfiddle.net/7q8nejo3/4/
You can adjust the widths as you need.
You could try this way. What you will get is that the width of the image won't overflow the container div.
#media screen and (max-width:800px){
.tourImage{
position: relative;
width: 80%;
min-width: 260px;
min-height: 340px;
margin: 0 auto;
background: #55acee;
}
.popover{
background:#fff;
width: 80%;
transform: translate(-50%, -50%);
position: fixed;;
top: 50% !important;
left: 50% !important;
padding: 0 20px;
}
}
#media screen and (max-width:400px){
.popover{
width: auto;
}
}
My requirement:
I'd like to create a vertical left menu with SQUARE menu options with centered text like so
What I've got so far:
Text Not vertically aligned!
HTML:
<div id="LeftMenu">
<div class="LeftMenuItem">Invoices</div>
<div class="LeftMenuItem">Expenses</div>
</div>
CSS:
html, body {
height: 100% !important;
width: 100% !important;
margin: 0 !important;
font-size:12px;
}
#LeftMenu {
background-color: #0583c0;
height: 100%;
width: 8%;
}
.LeftMenuItem {
border-bottom:1px;
border-bottom-color:#00619e;
border-bottom-style:solid;
width: 100%;
height: 0;
color:White;
text-align:center;
vertical-align:middle;/* of course this doesnt work */
padding-bottom:100%;
}
.LeftMenuItem:hover {
background-color:#0095de;
}
JSFIDDLE:
http://jsfiddle.net/7U53M/
The Problem:
The problem is that its not vertically aligning the text :(
Other Solutions I've tried:
http://jsfiddle.net/XpL5U/
I tried to use displays of table, table-row,table-cell. This did center-align the text but now makes the cells as long as the page.
CONSTRAINT:
I'd like to not have to use hard-coded pixels. I would like to use percentages.
Certainly you can use line height, but that only works when you have 1 line of text. Here is a better solution:
.element {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
You can use whatever class name you would like for .element and then just add it to the appropriate items. You can read a full article on it here: http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/
Try this:
CSS
html, body {
height: 100% !important;
width: 100% !important;
margin: 0 !important;
font-size:12px;
}
#LeftMenu {
background-color: #0583c0;
height: 100%;
width: 8%;
}
.LeftMenu_content {
display: inline-block;
vertical-align: middle;
}
.LeftMenuItem {
border-bottom:1px;
border-bottom-color:#00619e;
border-bottom-style:solid;
width: 100%;
height:0;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
padding-bottom:100%;
color:White;
text-align:center;
}
.LeftMenuItem:before {
content:"";
display: inline-block;
height: 100%;
padding-bottom:100%;
vertical-align: middle;
}
.LeftMenuItem:hover {
background-color:#0095de;
}
HTML
<div id="LeftMenu">
<div class="LeftMenuItem">
<div class="LeftMenu_content">Invoices</div>
</div>
<div class="LeftMenuItem">
<div class="LeftMenu_content">Expense</div>
</div>
</div>
Fiddle
try to chnage css
From
.LeftMenuItem {
border-bottom: 1px solid #00619E;
color: #FFFFFF;
height: 0;
padding-bottom: 100%;
text-align: center;
vertical-align: middle;
width: 100%;
}
TO
.LeftMenuItem {
border-bottom: 1px solid #00619E;
color: #FFFFFF;
padding: 20px 0;
text-align: center;
vertical-align: middle;
width: 100%;
}
may be it will correctly working
The square requirement forces you to add an extra element. Partial credit goes to Kyle Shevlin for the use of transform.
http://codepen.io/cimmanon/pen/rHbge
<div id="LeftMenu">
<div class="LeftMenuItem">
<div class="foo">Invoices</div>
</div>
<div class="LeftMenuItem">
<div class="foo">Expenses
<img src="http://placekitten.com/50/50" /></div>
</div>
</div>
CSS:
html, body {
height: 100% !important;
width: 100% !important;
margin: 0 !important;
font-size: 12px;
}
#LeftMenu {
background-color: #0583c0;
height: 100%;
width: 8%;
}
.LeftMenuItem {
border-bottom: 1px;
border-bottom-color: #00619e;
border-bottom-style: solid;
width: 100%;
color: white;
position: relative;
text-align: center;
}
.LeftMenuItem:hover {
background-color: #0095de;
}
.LeftMenuItem:before {
display: block;
padding-bottom: 100%;
content: '';
}
.foo {
position: absolute;
top: 50%;
width: 100%;
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
text-align: center;
}
From what I see on your design, the metrics are pretty much fixed, so why not giving a massive padding-top (something like 70%, or 65px) to push the text exactly where you want it to be.When it comes to the image, assign that as a background image to .LeftMenuItem with positioning center 20% and size 40px 40px
http://jsfiddle.net/AFLdc/