I'm fairly new to coding, have been at it for a few hours for a month now. For the past few hours I've been stuck with the problem that can be seen in the two pictures I attached. I've tried searching for answers and various methods such as min-width, display:flex, adjusting the font-size from autoscaling with vw and using rem. I just want my text to stay inside of the laptop screen no matter what screen size I'm viewing it on. I know I could use the easy way and just photoshop the photo with the text but I want to learn how to do it with coding for future projects as well. I do want the picture to scale a bit so it can be viewed on for example a phone in an ok size. Can you help me please?
body {
margin: 0;
text-align: center;
}
h1 {
color: #66BFBF;
font-family: "Dancing Script", Verdana, Geneva, Tahoma, sans-serif;
font-size: 5vw;
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%, -50%);
}
.top-container {
background-color: #ccf2f4;
padding-top: 50px;
position: relative;
}
.laptop {
width: 50%;
height: 50%;
}
<section>
<div class="top-container">
<img class="laptop" src="img/laptop.png" alt="cloud-img">
<img class="top-cloud" src="img/cloud.png" alt="cloud-img">
<h1>I'm Nhien</h1>
<h2 class="dreamer">just a gamer with big dreams.</h2>
</div>
</section>
You can also access the website from www.nhienweb.com
You should add the image to the background of the section or the div.
For this answer, I am adding it to the section background.
<section>
<div class="top-container">
<img class="top-cloud" src="img/cloud.png" alt="cloud-img">
<h1>I'm Nhien</h1>
<h2 class="dreamer">just a gamer with big dreams.</h2>
</div>
</section>
In the HTML, I have removed the laptop img from html and will add it in the css. I will position the cloud as absolute.
And here is the css for this code
body {
margin: 0;
text-align: center;
}
h1 {
color: #66BFBF;
font-family: "Dancing Script", Verdana, Geneva, Tahoma, sans-serif;
font-size: 5vw;
position: absolute;
top: 45%;
left: 50%;
transform: translate(-50%, -50%);
}
.top-container {
background-color: #ccf2f4;
padding-top: 50px;
position: relative;
}
.top-cloud {
position: absolute;
top: 0;
left: 0;
/* Positioned it relative to the top-container */
}
section {
background-image: url([your path to img relative to the css file]);
background-size: cover;
/* bg size to cover makes it what you want gives it full width at any screen */
}
If this doesn't help comment and ask me.
I applied this code to the CSS so it is working now. Don't think that this is the correct way to solve the problem but it seems fine when I tested my website on my mobile. Thanks for your trouble and time!
#media (max-width: 591px){
h1{
top:30%;
}
}
Related
I'm having trouble getting a h2 header to appaer within an absolutely positioned element. I've ran through the list of possible errors, but still can't seem to find the answer. The h2 element, after inspecting the screen, has a height of 0 for some reason.
Here's a codepen link
HTML:
<div class="gallery">
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<a class='call-action' href='#'>
<h2>Shop Now.</h2>
</a>
</div>
CSS:
.gallery {
position: absolute;
z-index: 2;
width: 100%;
display: flex;
flex-wrap: wrap;
font-size: 0;
img {
display: block;
width: 25%;
height: 50%;
#media only screen and (max-width: 500px) {
width: 50%;
height: 100%;
}
}
}
$call-action-width: 150px;
$call-action-height: 50px;
.call-action {
width: $call-action-width;
height: $call-action-height;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 0;
background-color: white;
opacity: 0.5;
border-radius: 5%;
h2 {
position: relative;
color: black;
font-size: 2em;
}
}
Sorry if the answer is obvious - I've just returned to practicing coding and I'm extremely rough.
Thanks for your help!
The problem lies in your font-size. You can't have an em font within a position: absolute <div>, as em is relative to the parent element.
Simply swapping to a fixed-size font (such as px) fixes the problem. I've created a new pen showcasing this here.
Unfortunately this means that your font can't be responsive. If you want responsive font, you'll either have to use a few media queries, or restructure your HTML so that the <h2> element has a position: relative parent (so you can use em).
Hope this helps! :)
I have been working on a new homepage for my website, but
I can't figure out why text moves up and down when I resize
my browser.
My CSS Code:
.welcome {
width: 100%;
height: 60px;
background-color: #4CAF50;
margin-top: -4px;
}
.welcome h1 {
font-family: 'Lato', sans-serif;
color: white;
font-size: 40px;
margin-bottom: 20px;
margin-top: 5px;
margin-left: 15px;
}
.welcome p {
color: white;
overflow: hidden;
float: left;
position: relative;
top: -50em;
}
#welcome-background {
width: 100%;
height: auto;
max-height: 1000px;
margin-top: -16px;
min-height: 500px;
}
If you see any other CSS error's please let me know
My HTML:
<div class="welcome">
<h1 style="float:left;">About Us</h1>
<img id="welcome-background" style="" src="/new_homepage/img/black-bg.png">
<p style="color: white; position: relative; top: -50em;">Hardwire Studios' is a gaming community that has servers am a variety of games, such as Minecraft, Garry's Mod, Counter-Strike: Global Offensive, Rust, and many more coming in the future. We try to provide the best "Lag-Free" experience on all of our server, yet also make them as fun and enjoyable as they can be, by only using the best of the best host companies. You can also see our future plan's by simply scrolling down a little more, until you find the "Future Plan's" Section.</p>
</div>
Your paragraph uses relative positioning, which means it is still in the flow of the document. Because it comes after an image, its vertical position changes as the height of the image changes.
Instead. put the image and paragraph inside of a wrapper element that is positioned relatively, then position the paragraph with absolute positioning.
This could look something like this:
HTML:
<div id="welcome-wrapper">
<img id="welcome-background" src="...">
<p>Hardwire Studios' is...</p>
</div>
CSS:
#welcome-wrapper {
position: relative;
}
#welcome-wrapper p {
position: absolute;
top: 10em;
}
I have an image with width 100% so that it enlarges with the window's size. How can I place texts over it so that they enlarge in the same proportion as the image does?
HTML:
<div class=instructables>
<h1>
I N S T R U C T A B L E S
</h1>
<img src="http://i.imgur.com/QvCRbHp.jpg" width="100%"/>
<div class=projects>
9
</div>
</div>
CSS:
.instructables {
margin-top: 150px;
margin-bottom: 200px;
margin-left: 150px;
margin-right: 150px;
letter-spacing: 2px;
line-height: 2;
font-size: 15px;
position: relative;
}
.projects{
position: absolute;
top: 140px;
left: 285px;
font-size: 350%;
font-family: Georgia, Serif;
color: #424242;
}
I tried using position: absolute; and obviously it doesn't work as the text just stay in the same place even if the image is enlarged.
Thank you!
Update: So basically I have this section on my website
http://i.imgur.com/q5kaxMM.png
with a jpg (robot and words) and numbers. When I enlarge the window, the jpg enlarges, but the numbers stay the same (in terms of size and position)
http://i.imgur.com/4nhdnl2.png
My goal is to make the entire section looks exactly the same as the 1st image, no matter how I enlarge the window.
Definitely need some media query love here. I'd suggest starting with some basic styles and add as needed. These can get you started, you can add as many as you like and adjust as you like.
#media screen and (max-width: 480px) {
.projects {font-size: 15px;}
}
#media screen and (min-width: 800px) {
.projects {font-size: 25px;}
}
#media screen and (min-width: 1200px) {
.projects {font-size: 40px;}
}
You were quite close with the position: absolute;. I think the best you can do, is position the text (.projects) with percents. That's way it's not locked on a fixed, pixel position.
Have a look: https://jsfiddle.net/v1u01md3/1/
It's not foolproof, for example when you make the screen really small. But that could be fixed with some media queries.
Have you tried using mediaqueries to adjust your text position/size?
Here is a good overview: https://developer.mozilla.org/de/docs/Web/CSS/Media_Queries/Using_media_queries
You can use background-image instead img and put a text over without position: absoulte like this:
#wrapper {
position: relative;
width: 100%;
height: 300px;
background: url('path/to/img') no-repeat top;
background-size: cover;
text-align: center;
}
jsFiddle
The idea to make that work is that you have to include inside an element with position relative all elements with position absolute.
Here is a working example modifying your code. HTML here:
<div class="instructables">
<div class="projects">
<h1>
I N S T R U C T A B L E S
</h1>
<img src="http://i.imgur.com/QvCRbHp.jpg" width="100%"/>
<span>9</span>
</div>
</div>
and the CSS here:
.instructables {
letter-spacing: 2px;
line-height: 2;
font-size: 15px;
position: relative;
}
.projects{
position: absolute;
top: 140px;
left: 285px;
font-size: 15px;
font-family: Georgia, Serif;
color: #424242;
}
h1
{
position: absolute;
}
span
{
position: absolute;
}
so I placed some links (text) over an image and in desktop browsers everything is working just fine, however, once you open it in a mobile browser, the text is not showing up. Here's my css:
#header {
width: 100%;
position: relative;
}
#dm {
top: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
}
#logo {
position: absolute;
top: 5%;
left: 18.8%;
width: 13.5%;
height: 64.8%;
background-color: transparent;
border: none;
}
h2 {
position: absolute;
top: 59%;
left: 21.7%;
width: 78%;
}
h2>a {
color: #f6f6f6;
font-size: 1.3vw;
font-variant: small-caps;
font-weight: lighter;
font-family: "Times New Roman", Times, serif;
text-decoration: none;
text-shadow: #000000 10% 10% 20%;
margin-right: 1%;
}
And HTML code:
<div id="header">
<img src="header.png" border="0" id="dm">
<a id="logo" href="index.html"></a>
<h2>Domů O nás Web & Grafický design Digitální kresba Pro firmy Ostatní služby Portfolio Faq Kontakt</h2>
</div>
Does anyone know how to solve this issue?
The font size you have set for the links is font-size: 1.3vw;, which means they will appear at 1.3% of the viewport width.
For desktop browsers with screen width of at least 1000px, it will show up as at least 13px which is totally fine.
For a mobile browser, if your device screen is 640px wide, it will only appear as ~8px which is way too small, which is probably why you couldn't see it. Besides, most mobile devices have width of between 320px and 400+px if viewed at portrait mode, so that makes the text even smaller.
When I put it at the fiddle, I thought there was no text until I placed a grey placeholder image. You should have used em instead of vw for the font size if you want to scale it.
I'm trying to make this page but as soon as the screen is smaller it wont make me scroll down to see more of my text.
Here is the link fiddle
<html>
<div class="container_12 container clearfix">
<div class="grid_12 clearfix main_content">
<div class="content">
<div><img src="http://placehold.it/780x150"></div>
<div class="text">
<h1>title</h1>
<p> text<p>
<p> text<p>
<p> text<p>
<p> text<p>
</p>
</div>
</div>
</div>
</div>
</html>
<style>
.content{
width: 780px;
padding: 20px;
position: fixed;
left: 50%;
top: 50%;
color: #000000;
font-family: Lora BoldItalic, Lora;
margin-top: -312px;
margin-left: -390px;
}
.text {
background: gray;
opacity: 0.6;
padding: 20px;
}
</style>
Thank you so much in advance!
I removed a few things from your CSS:
position: fixed;
left: 50%;
top: 50%;
margin-top: -312px;
margin-left: -390px;
In general (unless you really know what are you doing) using margin (especially negative values), position fixed, left and top is a bad idea because you are forcing elements to stay in a fixed position so your page will not work in all screen sizes.
I saw in your code that you are trying something with a background (is not visible in the demo) try to add the background to body not to html. I don't know what you are trying but I have a feeling that you want the page content to scroll without moving the background you will need to check this out http://jsfiddle.net/gF7Af/31/
I have the following:
.myimg{
margin: auto;
}
.content{
margin: auto;
width: 780px;
color: #000000;
font-family: Lora BoldItalic, Lora;
}
i dont see any of your text.. i dont understand why you put fixed position to the content nor those negative margins, so i removed those odd rules and at least i can see the text now.
Modify .content like this:
.content{
width: 780px;
padding: 20px;
/*margin: auto;*/
color: #000;
font-family: Lora BoldItalic, Lora;
}