CSS Div Postioning - html

The styles in the following code are working fine on large devices (Desktops and tablets). But on mobile devices most of the divs are overlapping because of the margin-top values.
I know this is not a propery way of designing website responsively.
Could you please give me a solution?
#welcome {
background: yellow;
background-size: 100% 100%;
width: 100%;
height: 600px;
}
#inquiry {
margin-top: 600px;
width: 100%;
height: 500px;
background: red);
}
#products {
margin-top: 1100px; /*(margin-top of inquiry + height of inquiry) */
width: 100%;
height: 500px;
background: green;
}
#footer {
margin-top: 1600px; /* (margin-top of products + height of products) */
width: 100%;
height: 500px;
background: blue;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div id="welcome">
Welcome Message
</div>
<div id="Inquiry">
Inquiry Form
</div>
<div id="products">
Products
</div>
<div id="footer">
footer
</div>

Although it is better to use Mediaqueries, You can also use:
margin-top: 10vh;
height: 20vh;
this will place the div 10% of the screen height down, and will give it a height that is 20% of the screen size. The problem is that it is CSS3 so old browsers won't support it. (I think everything below android 4.0 won't support it. you have to test this though) The amount of people using outdated browser is getting less and less.

You can use margin-top: % instead of pixels.
Or just use #media queries to control your margin on different screens.
For example: #media (max-width: 991px) { #products{ margin-top: 100px; } }

Try with:
position: absolute;
margin-top: 150px;
margin-left: 100px;
//inside of a box you can use padding-top......

You can either use percentages for width and height properties (a width of 80% will allways be 80% of the current size of the browser window, and thus your element will scale responsively when the window is resized).
However, I'd strongly recomend you learn to use media queries instead.
Lets say you have a left navigation menu of class left-nav, that you'd like to occupy 20% of the page width, but to be hidden at page widths of 800px and less.
.left-nav{width:20%}
#media screen and (max-width 800;){
.left-nav{display:none}
}
It should hopefully be really easy for you to figure out how media queries work from this example - you specify a maximum and/or minimum page width or height at which to apply given rules. Should be fairly straight forward to make your page layout behave properly using these.

Based on your comments, normal static/relative div elements are by default as wide as the screen and will stack themselves vertically after each other.
By setting position: relative on them, you can move them from that normal flow and if you set position: absolute you take them out from that normal flow and place them on an exact position/size using left, top, width and height properties.
Here is a sample showing that:
#welcome {
background: yellow;
height: 600px;
position: relative;
left: 50px;
}
#inquiry {
height: 500px;
background: #f99;
}
#products {
height: 500px;
background: #9f9;
}
#footer {
height: 500px;
background: #99f;
}
#extra {
position: absolute;
top: 100px;
left: 100px;
width: 200px;
height: 200px;
background: #f9f;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div id="welcome">
Welcome Message, positioned relative, moved out of flow 50px to the left
</div>
<div id="inquiry">
Inquiry Form
</div>
<div id="products">
Products
</div>
<div id="footer">
footer
</div>
<div id="extra">
extra, positioned absolute
</div>

Related

How to set default page dimension in CSS?

I'm trying converting my Photoshop design into a web site, manually writing HTML and CSS. It's my first time doing this type of exercise, so I'm having a little problem from the get-go with page dimensions.
I did my PS design using a 1920px page width, this is the fullscreen result. Writing CSS, I set header width to 1920px and logo width to 150px (as in the PS file). But I obtain this (don't worry about logo position).
As you can see, the page is very "zoomed in" and the scrollbar appears down below. I want to display the whole page without a scrollbar, just as in PS, keeping the same ratio between elements.
This is my HTML & CSS code for the header:
#logo img {
float: left;
width: 150px;
height: 150px;
}
#header {
position: absolute;
top: 0px;
left: 0px;
width: 1920px;
/* I also tried width: 100% */
height: 100px;
background: #000000;
}
<div id="header">
<div id="logo">
<img src="..\codice\export\logo.png" alt="logo">
</div>
</div>
As shown in the code, I also tried setting the header's width to 100% but this way the logo proportion (150 px / 1920 px) was not respected.
How can I write in CSS: "1920 should be your 100% when visualizing the page with the browser"?
I'm sorry if this is a silly question but it's my first time working with these tools.
I made this jsfiddle
You can check with a fluid width: 100% you should not have this horizontally scrollbar
Then i added a header_content div with a fixed width of 520px (then you can see it is centered and well placed. but you will need to change that value according to your photoshop header width.
Note : css margin:0 auto makes your div centered horizontally.
Some additional HTML and CSS may solve the problem for you! And I'm considering you have to add the menu which you not yet done. Here is my solution. I put some helpful comment that you help you to understand the code properly. You can have same code at my codepen example.
body {
padding-top: 150px;
/* if you don't add this your code will be hidden under the #heade */
}
#logo img {
float: left;
width: 150px;
height: 150px;
}
#header {
position: absolute;
top: 0;
/*No need to add 'px' when the value is 0 */
left: 0;
/* I also tried width: 100% */
right: 0;
/*Thsi will cover the right side. So no need to declear a width*/
/* logo has some space at to so we are adding a padding at top*/
padding-top: 25px;
height: 75px;
/* reduce to 75px so header will be just half of the logo image*/
background: #000000;
}
.container {
width: 1170px;
/* hae to make this responsive for smallar devices*/
box-sizing: border-box;
margin: 0 auto;
}
.navigation {
float: right
}
/*Eacaping the proper code for the navigation so here is some face code */
.navigation {
color: #fff;
}
<div id="header">
<div class="container">
<div id="logo">
<img src="http://www.logospng.com/images/22/itunes-12-logopng-wikimedia-commons-22786.png" alt="logo">
</div>
<nav class="navigation">
Home Link1 link2 ecc
<!-- I escaping the coding of nav here -->
</nav>
</div>
</div>

50% height divs stacked as rows

I have 2 divs that have a height of 50% stacked as a row, on a page that is split into 2 colours.
However on smaller screens the user needs to scroll, which breaks the design. I want the two 50% divs to fill the entire webpage.
.first-50 {
height: auto;
width: 100%;
background: red;
position: relative;
color: #fff;
min-height: 50%;
}
.second-50 {
min-height: 50%;
height: auto;
width: 100%;
background: blue;
position: relative;
display: none;
}
<div class="height-50 first-50">
text
</div>
<div class="height-50 second-50">
text
</div>
Thank you
Here is a quick example that works in the limited scenario I tested it for.
With relative positioning, it is only going to fill as much space as it needs. You probably need absolute positioning/sizing to force it to fill the regions you require. However, note the other elements on your page (typically relative or the default static positioned) will not be aware of them at all, and will simply overlap them or whatever.
So if you are looking for just a background thing where it always fills 50%/50% regardless of the content on top, this absolute positioning approach could suffice. If you need these two areas to follow the flow of the page and respect elements around them, then it will not.
body {
height: 100%;
min-height: 100%;
}
.first50 {
background: red;
color: #fff;
}
.second50 {
top: 50%;
background: blue;
color: #fff;
}
.height50 {
height: 50%;
min-height: 50%;
width: 100%;
position: absolute;
}
<div class="height50 first50">
text
</div>
<div class="height50 second50">
text
</div>
To expound upon a previous user's answer, using vh is a way of getting the height of the device with near perfect support in modern browsers. You can split the two divs into 50vh and it will resize accordingly. CCS-Tricks has some more info on this topic!
.first-50 {
height: 50vh;
width: 100%;
background: red;
position: relative;
color: #fff;
}
.second-50 {
height: 50vh;
width: 100%;
background: blue;
position: relative;
}
<div class="height-50 first-50">
text
</div>
<div class="height-50 second-50">
test
</div>
You can use the vh to adjust the height when the viewport resizes
quote from w3schools.com
vw Relative to 1% of the width of the viewport
vh Relative to 1% of the height of the viewport
div {
height: 50vh;
width: 100%; /* or width : 100vw */
}
and add what ever you want to it

Incorrect positioning of divs when using height in percentage

I'm not sure if this problem has been posted before, but I don't know how to ask this question effectively.
In my website I've created two large sections one after the other (not referring to the tag), one's height is set to 100% and the other is set to 90%. I've added a div directly underneath the second section. To keep the div stuck I've set "top" to 190% in order to simulate the length of the two sections. Although I've set a minimum height to each section, which makes the div crawl underneath the sections when they've stopped resizing.
How can I avoid this whilst using "position: absolute" for the elements?
html example (using one larger section):
<html>
<body>
<div class="section1"></div>
<div class="box"></div>
</body>
</html>
css example:
.section1 {
display: inline-block; width: 100%; height: 100%; min-height: 500px;
position: absolute;
}
.box {
width: 100%; height: 200px;
position: absolute; top: 100%; margin-top: 50px;
}
Thanks,
Jonathan
Just don't use position:absolute.
I'm assuming the reason you had it is because you needed height 100% of the viewport, without using JS. You could use the vh unit, but it doesn't have the best support/reliability.
The easiest way is to simply set html and body to height:100%;:
body,
html {
margin: 0;
height: 100%;
}
.full {
height: 100%;
background: teal;
}
.shorter {
height: 90%;
background: #fbfbfb;
}
footer {
background: #222021;
color: white;
text-align: center;
padding: 10px;
}
<section class="full"></section>
<section class="shorter"></section>
<footer>Made with love by a kitten</footer>
Note that I did add extra CSS for styling purposes.

Is it possible to make a fixed position div responsive?

I have been attempting to make some SVG data charts responsive but seem to be unable to do so with the current CSS 'position:fixed' applied to the elements.
I'm looking for, if possible, a solution that doesn't rely on media queries as I have multiple elements that I would need to apply this to. If this isn't possible, then any suggestions on what to do in order to keep all the data matched up with the SVG as the browser is resized, would be great!
Ideally I would like the SVG to scale up and down in size, whilst remaining central, no matter what size the browser is.
This is one of the SVGs that i'm looking to make responsive (right hand side)
http://datahealthcheck.databarracks.com/2016/#backup-section-3
I've created a codepen and added just one percentage on the SVG as an example of the problem http://codepen.io/anon/pen/YGvXkq
<div id="Backup-3"></div>
<p id="percentage" class="backup3-percentage">3%</p>
#Backup-3 {
position: fixed;
width: 550px;
margin-left: 73px;
margin-top: 31px;
}
.backup3-percentage {
position: fixed;
color: #000;
margin-left: 478px;
margin-top: 96px;
font-size: 1em;
transform: rotate(6deg);
}
I'd go with viewport units
.responsive-div {
position: fixed;
width: 70vw; // vw being viewport-width, so 70% of the width of the viewport
height: 50vh; // vh being viewport-height, so 50% of the height of the viewport
}
This article is going more in-depth about it
body{ margin:0; padding:0;
}
.mydiv {
max-width:1800px;
width:100%;
position:fixed; background:red; height:100px; border:5px solid green; box-sizing: border-box;}
<div class="mydiv">
</div>
I think media queries would be the best approach for make the div responsive.
If not you can use with:100% and max-width to your position fixed div
max-width dont work with position: fixed. This my alternative:
`.nav{
width: 1280px;
position: fixed;
}
#media all and (max-width: 1280px) {
.nav{
width: 100%;
}
}`
if you used width without #media 100% your block

CSS width and margin as percent not behaving as I want

<html>
<head>
<style>
html, body {
background-color: red;
height: 100%;
width: 100%;
overflow: hidden;
}
* { margin: 0; padding:0; }
.topbar {
min-width: 100%;
min-height: 50%;
background-color: green;
}
.fill {
min-width: 100%;
min-height: 50%;
background-color: white;
}
.container {
min-width: 100%;
min-height: 50%;
background-color: blue;
}
</style>
</head>
<body>
<div class="topbar">
"topbar
<div class="fill">
"fill"
<div class="container">
"container"
</div>
</div>
</div>
</body>
</html>
Why "fill" is not taking the complete 50% of its containing element "topbar" on the screen?
Why "container" is not taking complete 50% of its containing element "fill" on the screen?
Separate Question:
My goal is to create a layout which can fit almost all screens desktops/laptops. I am not focusing on phone screen layouts for now. I am trying to use width and height as percentages for my layout. Please suggest if that is the right approach or point me to alternatives.
Because the body's height is uncertain.You need a parent dom
When you use percentage for height and width, it's important that the parent element has specific size in pixel...
in this case "topbar" class should have size in pixel