I have created a site using HTML and CSS. The site is currently not responsive and I need to change into a responsive one. I have used media queries but it doesn't seem to be working. Are there any mistakes in my code? I have applied media queries to the entire CSS. Should I apply media query to the entire CSS or only a particular part of the CSS?
I tried using the media query #media screen and (max-width: 300px) { } and it works when resizing the browser but after that when the browser is maximized again the desktop style is not getting applied.
#media screen and (max-width: 300px) {
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
background: #fff;
margin: 0;
/* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
padding: 0;
text-align: center;
}
.thrColElsHdr #container {
width: 800px;
background: #FFFFFF;
margin: 0 auto;
/* the auto margins (in conjunction with a width) center the page */
border: 1px solid #000000;
text-align: left;
/* this overrides the text-align:
center on the body element. */
margin-bottom: 0px;
}
.thrColElsHdr #header {
background: #DDDDDD;
padding: 0 10px;
/* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead
of text, you may want to remove the padding. */
background-image: url(images/top.png);
height: 160px;
}
#top_menu {
color: #e5e491;
font-size: 15px;
text-decoration: none;
height: -20px;
width: auto;
float: right;
margin: 90px 0 -50px 0;
}
#top_login {
color: #e5e491;
font-size: 10px;
text-decoration: none;
float: right;
text-align: right;
margin: 0px 0 0px 0;
width: 600px;
height: 30px;
position: relative;
top: -160px;
}
.thrColElsHdr #header h1 {
margin: 0;
/* zeroing the margin of the last element in
the #header div will avoid margin collapse - an unexplainable space between divs. If the div has a border around it, this is not necessary as that also avoids the margin collapse */
padding: 0px 0;
/* using padding instead of margin will allow you to
keep the element away from the edges of the div */
color: #FFF;
font-family: "Times New Roman", Times, serif;
height: 160px;
}
.thrColElsHdr #sidebar1 {
float: left;
width: 16em;
/* since this element is floated, a width must be given */
background: #75b808;
/* top and bottom padding create visual space within this div */
margin: 15px 10px 15px 15px;
background-image: url(images/news_top.png);
background-repeat: no-repeat;
padding: 15px 2px 2px 2px;
font-size: 10px;
}
.thrColElsHdr #sidebar2 {
float: right;
width: 17em;
/* since this element is floated, a width must be given */
background: #EBEBEB;
/* the background color will be displayed for the length of the content in the column, but no further */
padding: 10px 0;
/* top and bottom padding create visual
space within this div */
border: #0a4b67;
border-width: thick;
margin: 5px;
font-size: 10px;
}
.thrColElsHdr #sidebar1 h3,
.thrColElsHdr #sidebar1 p,
.thrColElsHdr #sidebar2 p,
.thrColElsHdr #sidebar2 h3 {
margin-left: 10px;
/* the left and right margin
should be given to every element that will be placed in the side columns */
margin-right: 10px;
}
.thrColElsHdr #mainContent {
margin: 0 12em 0 1em;
/* the right margin can be given in ems or pixels. It creates the space down the right side of the page.
*/
color: #0a4b67;
}
.thrColElsHdr #mainContent h2 h3 h4 {
color: #0a4b67;
}
.thrColElsHdr #footer {
padding: 0 10px;
/* this padding matches the left alignment of the elements in the divs that appear above it. */
background: #E1E994;
}
.thrColElsHdr #line {
padding: 0 10px;
/* this padding matches the left alignment of the elements in the divs that appear above it. */
background: #E1E994;
margin-top: 0px;
}
.thrColElsHdr #footer p {
margin: 0;
/* zeroing the margins of the first element in the footer will
avoid the possibility of margin collapse - a space between divs */
padding: 10px 0;
/* padding on this element will create space, just as the the margin would have, without the margin collapse issue */
}
/* Miscellaneous classes for reuse */
.fltrt {
/* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
float: right;
margin-left: 8px;
}
.fltlft {
/* this class can be used to float an element left in your
page */
float: left;
margin-right: 8px;
}
.clearfloat {
/* this class should be placed on a div or break element and should be the final element before the close of a container that should fully contain a float */
clear: both;
height: 0;
font-size: 1px;
line-height: 0px;
}
.thrColElsHdr #container #header #top_menu_logo {
margin-top: 0em;
margin-right: 0em;
margin-bottom: 0em;
margin-left: 0em;
float: left;
}
.thrColElsHdr #container #header #top_menu a {
color: #e5e491;
}
.thrColElsHdr #container #subheading {
font-size: 14px;
height: 211px;
}
div#nifty {
font-size: 12px;
background: #2d6482;
width: 300px;
}
div.rounded div {
height: 1px;
overflow: hidden;
}
#radiusx,
#radiusy {
text-align: right;
width: 20px;
}
div#nifty p {
color: #dfe791;
padding: 2px;
margin: 2px;
}
#thetext {
float: right;
width: 380px;
padding: 10px;
font-size: 12px;
font-weight: bold;
}
#theimg {
float: left;
width: 400px;
}
}
</style>
please any idea any mistake please comment my question ?
The way to do media queries is this:
/* rules that apply regardless of resolution */
.some > .css > .selector {
display: block;
background-color: rgb(255, 255, 255);
}
#media screen and (min-width: 1440px) {
/* rules that apply for screens >= 1440px */
.some > .css > .selector {
width: 1280px;
color: rgb(144, 144, 144);
}
}
#media screen and (min-width: 1024px) and (max-width: 1439px) {
/* rules that apply for screens 1024px - 1439px */
.some > .css > .selector {
width: 920px;
color: rgb(102, 102, 102);
}
}
#media screen and (min-width: 640px) and (max-width: 1023px) {
/* rules that apply for screens 640px - 1023px */
.some > .css > .selector {
width: 500px;
color: rgb(64, 64, 64);
}
}
#media screen and (max-width: 639px) {
/* rules that apply for screens 639px and below */
.some > .css > .selector {
width: 300px;
color: rgb(0, 0, 0);
}
}
Alter the conditions with your desired breakpoints, of course.
As a guideline, always apply your media queries after your style for that element / logical section of elements. This makes it convenient later on to find and edit your styles for a particular element across all resolutions.
A few things to note for first-timers:
ensure that you use px values that don't overlap in the media queries.
the rules within media queries will "overwrite" those outside of it.
You should do a link tag for the css and have this meta tag
and your
#media screen and (max-width: 300px) {}
does not make sense because then you will choose a screen that's smaller than 300px width
#media screen and (min-width: 300px) and (max-width: 460px) {}
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
Related
I am creating a responsive header. I got two columns and want the button in the right column to be vertical centered.
I know I can center items using top: 50%; margin-top: -xy px, and my button although got a fixed height: 40px.
To use this method I wrapped my button inside a div {position: relative}. This does not work, as the div does not stretch its own height.
How can I solve this with css? First I thought about Flexbox, but it has quite some lack of browser compatibility.
JSFIDDLE DEMO
You can greatly simplify your code - remove floats, (use display: inline-block instead), remove the .relative div, etc.
Working Fiddle
html, body {
margin: 0;
padding: 0;
font-size: 16px;
}
header {
background: red;
color: #fff;
padding: 0.5em;
}
header h1 {
font-size: 2em;
margin: 0 0 0.2em;
}
header p {
margin: 0;
}
header button {
height: 40px;
width: 70px;
}
.column-left {
display:inline-block;
width: 70%;
vertical-align: middle;
}
.column-right {
width: 29%;
text-align: right;
display: inline-block;
vertical-align: middle;
height: 100%;
}
/* responsive */
#media (min-width: 200px) {
header {
padding: 1em;
}
}
#media (min-width: 300px) {
header {
padding: 1.5em;
}
}
#media (min-width: 400px) {
header {
padding: 2em;
}
}
#media (min-width: 500px) {
header {
padding: 2.5em;
}
}
#media (min-width: 600px) {
header {
padding: 3em;
}
}
/* helpers */
.clearfix:after {
content: ".";
clear: both;
display: block;
visibility: hidden;
height: 0px;
}
Remove this div with position: relative and add position: relative to your header tag. You can even delete your column-right div.
Another solution:
header button {
top: 50%;
transform: translateY(-50%); // instead of negative margin-top (it is useful when your button has dynamic height, supports IE9+ with -ms- prefix)
}
JSFIDDLE
i'm starting with responsive design and i'm having the next issue:
when i rezise my div, in a design of two column and the right one is going down.
Ex before rezise:
and after rezise:
I dont jnow why is going down f the design is in %. Maybe somebody can help'me, here the css code that i'm using.
* { margin: 0px; padding: 0px; }
h1 {font: bold 20px verdana, sans-serif; }
h2 {font: bold 14px verdana, sans-serif; }
header, section, footer, aside, nav, article, figure, figcaption, hgroup { display: block; }
body { width: 100%; text-align: center; }
#envoltura { margin: 15px auto; text-align: left; padding: 0px 2% 0px 2%}
#cabecera { background: #FFFBB9; border: 1px solid #999999; padding: 20px; text-align: center; }
#menu { background: #CCCCCC; padding: 5px 15px; text-align: center; }
#menu li { display: inline-block; list-style: none; padding: 5px; font: bold 14px verdana, sans-serif; }
#seccion { float: left; width: 65%; margin: 20px 0px 0px 0px; border: 2px solid #999999; padding: 2%; text-align: center; }
#lateral { float: left; width: 26.4%; margin: 20px 0px 0px 0px; padding: 2%; background: #CCCCCC; border: 2px solid #999999; text-align: center; }
#pie { clear: both; text-align: center; padding: 20px; border-top: 2px solid #999999; }
Because you haven't allowed enough room for the two columns at that window size!
► Declare different layout per media queries:
(method below based on a min and max screen resolution range)
#media only screen and (min-width: 480px) and (max-width: 979px) {
/* Your styles for between these two resolutions here */
}
OR! depending on what you want to do, complete end goal, you may not even need to use media queries
► Define width of each columns in a percentage relevant to either the browser, or a wrapper div.
#wrapper {
position: relative; // the below is relative to this
margin: 0 auto; // center
max-width: 1024px; // max size of container
width: 100%; // 100% between min and max ;)
min-width: 960px; // lowest toggle point
}
.left {
width: 80%; // 80% of above
float:left;
min-width: 500px; // add a min-width to declare stopping point
}
.right {
width: 20%; // 20% of above
float:right;
min-width: 100px; // add a min-width to declare stopping point
}
It is because your divs are equaling more than 100% of your parent try this and edit it as you see fit
If you post your html I can explain it furthur
set your body to an absolute value example body{width:1200px} then set your first media query to #media screen and (max-width: 1199px){body{width: 100%}} This way you know that anything over 1199px the page will break
I know there are lots of people asking the div align center problem. But I tried most solution and it seems won't work in my case. Sorry that I have to ask here. I created the floating ad at my site http://tacfeed.com using the following css,
/* Default Stylesheet */
#close {
margin-left: auto;
margin-right: auto;
float:left;
display:inline-block;
padding:2px 5px;
color:black;
background: #fff;
}
#responsive-adsense {
display: none;
}
#responsive-adsense{
display: none;
}
/*
GENERAL MEDIA QUERY FOR SMALL SCREEN SIZES - COVERS COMMON MOBILE DEVICES, PHONES/TABLETS...
*/
#media only screen and (max-width: 1023px) {
.adcontainer {
display: none;
}
#responsive-adsense{
display: none;
}
}
#media only screen and (max-width: 899px) {
.adcontainer {
width: 100%;
height: auto;
position: fixed;
bottom: 0px;
display: block;
background: #fff;
color: #fff;
margin: 0 auto;
padding: 0px;
}
#responsive-adsense {
margin-left: auto;
margin-right: auto;
padding: 0px !important;
width: 728px !important;
display: block !important;
margin:0;
}
}
#media only screen and (max-width: 767px) {
.adcontainer {
width: 100%;
height: auto;
position: fixed;
bottom: 0px;
display: block;
background: #fff;
color: #fff;
margin: 0 auto;
padding: 0px;
}
#responsive-adsense {
margin-left: auto;
margin-right: auto;
padding: 0px !important;
width: 728px !important;
display: block !important;
margin:0;
}
}
#media only screen and (max-width: 599px) {
.adcontainer {
width: 100%;
height: auto;
position: fixed;
bottom: 0px;
display: block;
background: #fff;
color: #fff;
margin: 0 auto;
padding: 0px;
}
#responsive-adsense {
margin-left: auto;
margin-right: auto;
padding: 0px !important;
width: 468px !important;
display: block !important;
margin:0px;
}
}
#media only screen and (max-width: 479px) {
.adcontainer {
width: 100%;
height: auto;
position: fixed;
bottom: 0px;
display: block;
background: #fff;
color: #fff;
margin: 0 auto;
padding: 0px;
}
#responsive-adsense {
margin-left: auto;
margin-right: auto;
padding: 0px !important;
width: 320px !important;
display: block !important;
margin:0px;
}
}
/* Here's the css for mobile devices */
#media only screen and (max-width: 320px) {
.adcontainer {
width: auto !important;
padding: 0px !important;
height: 50px !important;
}
#responsive-adsense {
margin-left: auto;
margin-right: auto;
padding: 0px !important;
width: 320px !important;
display: block !important;
margin:0px;
}
}
/*
Add your custom styles in this file instead of style.css so it
is easier to update the theme. Simply copy an existing style
from style.css to this file, and modify it to your liking.
When you update your theme, backup this file and re-add it after.
*/
/* Global */
.mystyle {}
/* Tablet - 800px, 768px & 720px */
#media only screen and (min-width: 720px) and (max-width: 800px) {
.mystyle {}
}
/* Mobile - 480px & 320px */
#media only screen and (max-width: 719px) {
.mystyle {}
}
/* Mobile - 320px */
#media only screen and (max-width: 479px) {
.mystyle {}
}
And here is the HTML code at footer, before the end of .
<div class="adcontainer" style="background-color: rgba(255, 0, 0, 0)">
<div id="responsive-adsense">
<span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>[X]</span>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- mobileadfor2013 -->
<ins class="adsbygoogle"
style="display:inline-block;width:320px;height:50px"
data-ad-client="ca-pub-2658894403753596"
data-ad-slot="4801520732"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</div>
</body>
Now, I got two problems. First, the ad align center in portrait mode, but it won't align center if I rotate my iPhone or Android phone to landscape. I tried to use "vertical-align", "margin", "float" and all seems no luck.
Second, the ad does not align to the very bottom of the screen. You can try to visit our site tacfeed.com on mobile. You can see there is at least 1 px left in the bottom area.
Any help or advise would be appreciated.
It seems people don't know this solution to position things to center...you always think the solution needs to be complicated in order to work but you can do this with CSS and it has support on all browsers...
To align verticaly to center a div you only need to add:
position: absolute;
left:0;
right:0;
margin: auto;
Horizontal:
position: absolute;
top:0;
bottom:0;
margin: auto;
And now, absolute center:
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
margin: auto;
+Update
AND, here is a jsfiddle to show that/how it works
++Update
Also, this comes as an update, since now your ad is position:absolute, you can add bottom:0 to it and it will stick to the bottom, no pixels in between.
+++Update
And another jsfiddle to show the exact way of aligning it to center and make it stick to the bottom
I'm using the dashboard template example that comes with Bootstrap. Sidebar disappears after a certain screen size and that's fine. But I want to set a specific width until that point. Now it's kind of responsive and stretches in width on bigger screens (it's a bit nasty on large monitors.) A good width would be 250px.
CSS
/*
* Base structure
*/
/* Move down content because we have a fixed navbar that is 50px tall */
body {
padding-top: 50px;
}
/*
* Global add-ons
*/
.sub-header {
padding-bottom: 10px;
border-bottom: 1px solid #eee;
}
/*
* Sidebar
*/
/* Hide for mobile, show later */
.sidebar {
display: none;
}
#media (min-width: 768px) {
.sidebar {
position: fixed;
top: 51px;
bottom: 0;
left: 0;
z-index: 1000;
display: block;
padding: 20px;
overflow-x: hidden;
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
background-color: #31373d;
border-right: 1px solid #eee;
}
}
/* Sidebar navigation */
.nav-sidebar {
margin-right: -21px; /* 20px padding + 1px border */
margin-bottom: 20px;
margin-left: -20px;
}
.nav-sidebar > li > a {
padding-right: 20px;
padding-left: 20px;
color: #98a7b5;
}
.nav-sidebar > li > a:hover {
background: transparent;
color: #fff;
}
.nav-sidebar > .active > a, .nav-sidebar > li > a:hover {
color: #fff;
background-color: #272c30;
}
/*
* Main content
*/
.main {
padding: 20px;
}
#media (min-width: 768px) {
.main {
padding-right: 40px;
padding-left: 40px;
}
}
.main .page-header {
margin-top: 0;
}
/*
* Placeholder dashboard ideas
*/
.placeholders {
margin-bottom: 30px;
text-align: center;
}
.placeholders h4 {
margin-bottom: 0;
}
.placeholder {
margin-bottom: 20px;
}
.placeholder img {
display: inline-block;
border-radius: 50%;
}
Any ideas? Thanks.
Source code is avaliable at getbootstrap.com
How about a max-width: 250px in your .sidebar CSS?
You could use a CSS media query to set a max-width on larger screens..
#media (min-width: 992px){
.sidebar {
max-width:250px;
}
}
Demo: http://www.bootply.com/126485
I just ran into the same issue. For me I was happy for the sidebar to remain fluid for the small and medium viewports. At a large viewport of 1200px or more I want to have a fixed width of 250px and then have the main content area cover the rest.
In the HTML give your main content div a class name or id. This will make it easy to select it in your css media query. Here i've used a class called 'content'.
<div class="col-sm-3 sidebar"> ... </div>
<div class="col-sm-9 col-sm-offset-3 content"> ... </div>
Then add your css:
#media(min-width:1200px){ // breakpoint where I set a fixed sidebar width. You can change this to whatever you want.
.sidebar {width:250px}
.content {margin-left:0;padding-left:265px;width:100%;} // reset the width and margin, overwriting the bootstrap grid. Add padding to the left to equal the width of your fixed width sidebar + half your defined grid gutter width. For me that is the default 15px.
}
I'm using Bootstrap and I have a carousel under my navbar.
It works OK on normal computers, check this link.
However, I'm having trouble on smaller screens, e.g. iPhone. Just resize your browser screen to see what I mean.
I'm figuring maybe it isn't necessary the responsive CSS but something else I' doing wrong. Maybe their are better ways to get the carousel image with resized on every screen.
Also, I would like the carousel to have a 100% height of the screen, so the carousel spans the entire screen, and the rest of the content shows only when you scroll.
CSS I'm using:
/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
/* Carousel base class */
.carousel {
margin-top: -80px;
position: fixed;
width: 100%;
}
.carousel .container {
position:relative;
z-index: 9;
}
.carousel-control {
height: 80px;
margin-top: 0;
font-size: 120px;
text-shadow: 0 1px 1px rgba(0,0,0,.4);
background-color: transparent;
border: 0;
z-index: 10;
}
.carousel .item {
min-height: 800px;
}
.carousel img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: auto;
margin-top: -200px;
}
.carousel-caption {
background-color: transparent;
position: static;
max-width: 550px;
padding: 0 20px;
margin-top: 200px;
}
.carousel-caption2 {
background-color: transparent;
position: static;
max-width: 380px;
padding: 200px 20px;
}
.carousel-caption h1,
.carousel-caption .lead,
.carousel-caption2 h1,
.carousel-caption2 .lead {
margin: 0;
line-height: 1.25;
color: #fff;
text-shadow: 0 1px 1px rgba(0,0,0,.4);
}
.carousel-caption .btn,
.carousel-caption2 .btn {
margin-top: 10px;
}
#wrapper-container {
margin-bottom: -80px;
padding-bottom: 80px;
position: relative;
background: inherit;
top: 60%;
}
/* Featurettes
------------------------- */
.featurette-divider {
margin: 80px 0; /* Space out the Bootstrap <hr> more */
}
.featurette {
padding-top: 120px; /* Vertically center images part 1: add padding above and below text. */
overflow: hidden; /* Vertically center images part 2: clear their floats. */
}
.featurette-image {
margin-top: -120px; /* Vertically center images part 3: negative margin up the image the same amount of the padding to center it. */
}
/* Give some space on the sides of the floated elements so text doesn't run right into it. */
.featurette-image.pull-left {
margin-right: 40px;
}
.featurette-image.pull-right {
margin-left: 40px;
}
/* Thin out the marketing headings */
.featurette-heading {
font-size: 50px;
font-weight: 300;
line-height: 1;
letter-spacing: -1px;
}
/* RESPONSIVE CSS
-------------------------------------------------- */
#media (max-width: 979px) {
.container.navbar-wrapper {
margin-bottom: 0;
width: auto;
}
.navbar-inner {
border-radius: 0;
}
.carousel .item {
min-height: 500px;
}
.carousel img {
min-width: 100%;
height: auto;
}
.featurette {
height: auto;
padding: 0;
}
.featurette-image.pull-left,
.featurette-image.pull-right {
display: block;
float: none;
max-width: 40%;
margin: 0 auto 20px;
}
}
#media (max-width: 767px) {
.navbar-inner {
margin: -20px;
}
.carousel {
margin-left: -20px;
margin-right: -20px;
}
.carousel .container {
}
.carousel .item {
height: 300px;
}
.carousel img {
height: 300px;
}
.carousel-caption {
width: 65%;
padding: 0 70px;
margin-top: 100px;
}
.carousel-caption h1 {
font-size: 30px;
}
.carousel-caption .lead,
.carousel-caption .btn {
font-size: 18px;
}
.marketing .span4 + .span4 {
margin-top: 40px;
}
.featurette-heading {
font-size: 30px;
}
.featurette .lead {
font-size: 18px;
line-height: 1.5;
}
}
There's a lot you would need to do to clean it up... The following will get you started, but there would definitely be a bit more tweaking to do.
I didn't look at the CSS to fill the screen with an image as per your last request. I think you will have to look at adding a different carousel with other cropped images with a portrait aspect ratio if you want that, so you show the specific part of the image you want.
Firstly under #media (max-width: 767px), remove:
.navbar-inner {
margin: -20px;
}
It's causing your menu bar at the top to shift up out of sight.
From #media... .carousel, remove:
margin-left: -20px;
margin-right: -20px;
This is messy, and is there because of the padding added to body (see below).
Add the following to #media (max-width... .carousel:
position: relative;
margin-top: 0px;
Because you want the carousel to sit neatly under the navbar.
Remove the following from #media... body
padding-right: 20px;
padding-left: 20px;
This is causing problems for the carousel, and you can add this padding for specific divs like wrapper-container if you want.
From .carousel img, remove:
margin-top: -200px;
Next, you have to fix the fact that the text under the carousel is moved way down:
Add the following to #media... #wrapper-container
top: 0;
Remove the following from #media (max-width: 979px)
.carousel .item {
min-height: 500px;
}
and the following from #media (max-width: 767px)
.carousel img {
height: 300px;
}
because the carousel is nowhere near that height at smartphone sizes.
You will also have to play around with the positioning of the caption text in the #media CSS. You may want to decide to lose some caption text as the carousel shrinks.
This will get you started, and you can go from there...
For starters, get rid of the margin-top: -200px; on your .corousel img style.
With a small screen, your image height is less than 200px and this causes it to go off of the screen.