This is a pure html/css Question, here it goes..
I have a floating div to the right of my page layout, I need it to disappear if my screen size is too small (or if resize the browser window to a smaller setting). Let's say it's something as simple as this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Layout test</title>
<link href="simple.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<h1>Welcome</h1>
<div id="right-div">
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
<div id="main">
<p>Some content goes here!</p>
</div>
</body>
</html>
let's say it has this simple CSS too:
#main {
border: 1px solid black;
width: 800px;
height: 500px;
padding: 7px;
margin-left: auto;
margin-right: auto;
position: relative;
z-index: 10;
}
#right-div {
border: 1px solid black;
float: right;
width: 200px;
position: relative;
z-index: 9;
}
As of now, if I reduce window size, the div with id of right-div starts to overlap with the "main" div. I need to know how to make it disappear or hide if the screen size is small (or getting smaller).
How can I accomplish this?
Thank you all in advance for your help,
J.
Use Media Queries. Example:
#media all and (max-width: 300px) {
.floated_div { display: none; }
}
jsFiddle Example. Resize the output area until it is smaller then 300px to see the effect.
See this answer for IE compatibility.
Here is a solution if you have a fixed width layout. http://jsfiddle.net/uMVMG/
<div id="container">
<div class="inner">
<div id="right-div">
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</div>
<div id="main">
<p>Some content goes here!</p>
</div>
</div><!-- .inner end -->
</div><!-- #container end -->
#container {
overflow: hidden;
max-width: 500px;
min-width: 300px;
}
#container .inner {
width: 500px;
}
#right-div {
float: right;
width: 200px;
background: green;
}
#main {
width: 300px;
height: 200px;
background: red;
}
Related
I'm trying to implement a dropdown inside a scrollable container. The problem I have is that the options, when expanded, are clipped inside this container. I know this is a recognized problem with scrollable containers, but to this day I don't know a good solution for it. Here's the problem:
I need these pink options to pop out of the container. Is this possible without keeping them outside of the container?
Here's the snippet:
document.getElementById('dropdown-button').addEventListener('click', () => {
document.querySelector('.dropdown-options').classList.toggle('hidden');
});
.scrollable {
width: 200px;
height: 400px;
background: yellow;
overflow-y: auto;
}
.filler {
background: orange;
height: 350px;
}
.dropdown {
position: relative;
}
.dropdown-options {
background-color: pink;
list-style: none;
margin: 0;
padding: 0;
position: absolute;
}
.hidden {
display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Overflow</title>
</head>
<body>
<div class="scrollable">
<div class="filler"></div>
<div class="dropdown">
<button id="dropdown-button">Expand me</button>
<ul class="dropdown-options hidden">
<li>Option one</li>
<li>Option two</li>
<li>Option three</li>
</ul>
</div>
<div class="filler"></div>
</div>
</body>
</html>
Any chance for a CSS-only solution?
I am trying to put nav and img elements next to each other on top of the page.
I am putting both elements in the top_bar container.
I have tried setting the top_bar to display: inline-block;, reducing the width of nav and floating it to left and also floating the img to right. Then I have tried combinations of those, but nothing gives me the result I want. Either the elements start to jump around or the logo is put below the top_bar border.
How can I have nav and img inline and both above the top_bar bottom border?
EDIT: I also want the nav element to be positioned left-most of the page, and img right-most.
<!DOCTYPE HTML>
<html>
<head>
<style>
#top_bar {
border-bottom: 1px solid silver;
}
nav ul li {
display: inline-block;
}
img {
width: 130px;
height: 30px;
}
</style>
</head>
<body>
<div id="top_bar">
<nav>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</nav>
<img src="cs_logo.png" alt="Image not found"></img>
</div>
</body>
</html>
Add display: flex; to #top_bar
#top_bar {
border-bottom: 1px solid silver;
display: flex;
justify-content: space-between;
}
nav ul li {
display: inline-block;
}
img {
width: 130px;
height: 30px;
}
<div id="top_bar">
<nav>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</nav>
<img src="cs_logo.png" alt="Image not found"></img>
</div>
<!DOCTYPE HTML>
<html>
<head>
<style>
#top_bar {
border-bottom: 1px solid silver;
display: flex;
flex-direction: row;
align-items: center;
}
nav ul li {
display: inline-block;
}
img {
width: 130px;
height: 30px;
}
</style>
</head>
<body>
<div id="top_bar">
<nav>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</nav>
<img src="cs_logo.png" alt="Image not found"></img>
</div>
</body>
</html>
We can use CSS flex properties to achieve this. But please make sure that you are using vendor prefixes for cross browser compatability.
Your code is missing some declarations.
At first your class #top_bar should have a width declaration.
To make things easyier you could use a class instead of only declare
the image size.
Here´s a fiddle of your code that i´ve modified: https://jsfiddle.net/Thorske/nyv6mwgz/
Modified css:
* {
margin : 0:
padding : 0;
}
#top_bar {
margin : 0 auto;
width : 100%;
border-bottom: 1px solid silver;
}
.clear {
clear : both;
}
nav {
float : left;
width : 80%;
}
nav ul li {
display: inline-block;
}
.img_container {
float : right;
width : 20%;
}
.img_conatiner img {
width: 130px;
height: 30px;
}
Modifed html:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="top_bar">
<nav>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</nav>
<div class="img_container">
<img src="cs_logo.png" alt="Image not found" />
</div>
<div class="clear"></div>
</div>
</body>
</html>
There only simple changes:
the #top_bar has now a defined width.
nav has now a defined width and float.
added the class "img_container" with defindet width and float
which contains the image.
the class clear keeps everything were it is supposed to be.
Since i don´t know if you want to make your code "responsiv" you´ve
to set your values for "width" yourself.
My logo is not resizing on mobile devices and therby my menu overlaps the logo. I've hard coded the css property for my logo for desktop screen since I want my logo to appear big on desktop but i want it to resize on mobile devices and I have set setting for it but it does not work, kindly please tell me what is the best solution or what i'm doing wrong, how can i fix it.
.logo {}
#logoimg {
width:250px; height:70px; transition: all .3s ease;
}
/*============= media query max-width: 768px; =============*/
#media only screen and (max-width:768px) {
/* start query */
.header { padding-top: 5px; padding-bottom: 5px;}
.logo img {width:auto;}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
img {
width: auto ;
max-width: 100% ;
height: auto ;
}
/*============= media query max-width: 768px; =============*/
#media only screen and (max-width:768px) {
/* start query */
.header {
padding-top: 5px;
padding-bottom: 5px;
}
.img {
width:100%;
height:auto;
}
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<!-- logo -->
<div class="col-md-12">
<img src="https://upload.wikimedia.org/wikipedia/en/9/91/National_Film_and_Television_School_Logo.jpg" alt="logo"> </div>
<!-- logo -->
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="navigation"> <!-- navigation start-->
<ul>
<li class="active">Home</li>
<li>Menu 1</li>
<li>Menu 2 </li>
<li>Menu 3</li>
<li>Menu 4</li>
<li>Menu 5</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>
use class img-responsiv
It will help you
I am in process of building an AMP HTML mobile template with a CSS only menu. It has to be CSS only as the AMP HTML standards do not allow any javascript. I am sure this will help others in building an AMP HTML page with a nice menu.
I have a very basic version coded. But for some reason the menu is not appearing when I cleaned the code to be AMP. I know this is probably super simple but it is completely evading me. I put a super shortened version of my page below any help would be appreciated.
For quick reference the only errors the code below will throw from the AMP HTML debugger is the fact that I have multiple style tags, I know that is the case, I am just using them for organization purposes before combining them once in my platform.
Thanks for any help!
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel="canonical" href="">
<!-- AMP Requirements Begin -->
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style>body {opacity: 0}</style><noscript><style>body {opacity: 1}</style></noscript>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<!-- AMP Requirements End -->
<!-- Main Style Begin -->
<style>
body {
margin: 0px;
}
#main-container {
border: 1px solid #3f3f3f;
width: 100%;
}
#header-wrapper {
border: 1px solid #3f3f3f;
width: 100%;
}
#header-wrapper span {
float: right;
display: inline-block;
}
#header-wrapper span a {
display: block;
}
/* Nav Bar Begin */
#navbar-container ul {
list-style-type:none;
margin: 0px;
padding: 0px;
}
#navbar-container ul li {
margin: 0px;
padding: 0px;
display: inline;
}
/* Nav Bar End */
</style>
<!-- Main Style End -->
<!-- Menu CSS Begin -->
<style>
#main-navigation {
/* Collapsed */
width: 0;
overflow: hidden;
position: fixed;
top: 0;
left: 0;
height: 100%;
background: #12E444;
&:after {
content: "";
position: absolute;
top: 0;
right: 0;
height: 100%;
width: 34px;
}
}
#main-container {
float: right;
}
#main-navigation:target + #main-container {
width: 80%;
#main-container {
width: 80%;
left: 20%;
}
#header-wrapper {
width: 80%;
left: 20%;
}
</style>
<!-- Menu CSS End -->
</head>
<body>
<!-- Mobile Nav Chunk Begin -->
<nav id="main-navigation">
<ul>
<li>Close Menu</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
<li>Menu item Long Maybe Long</li>
</ul>
</nav>
<!-- Mobile Nav Chunk End -->
<!-- Main Wrapper Begin -->
<div id="main-container">
<!-- Header Wrapper Begin -->
<div id="header-wrapper">
<span>Call US 303-999-9999</span>
<amp-img src="test-images/mobile-logo.jpg" width="142" height="51" alt=""></amp-img>
</div>
<!-- Header Wrapper End -->
<!-- Nav Bar Begin -->
<div id="navbar-container">
<ul>
<li>Menu</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
<!-- Nav Bar End -->
<!-- Main Content Area Begin -->
<div id="main-content-area">
</div>
<!-- Main Content Area End -->
</div>
<!-- Main Wrapper End -->
</body>
</html>
#main-navigation:target + #main-container {
width: 80%;
#main-container {
width: 80%;
left: 20%;
}
#header-wrapper {
width: 80%;
left: 20%;
} /* <- MISSING THIS RIGHT CURLY BRACE */
}
</style>
Hi guys this might be a easy question but I'm struggling a bit with this one problem in my HTML and CSS page.
Problem: When i zoom int the "Section" part moves under the nav and when i zoom out the footer moves next to the section part.....
Here is the sample of my HTML page:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MyWebsite</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<header>
Header
</header>
<body>
<nav>
<ul>
<li>
Tab1
</li>
<li>
Tab2
</li>
<li>
Tab3
</li>
<li>
Tab4
</li>
<li>
Tab5
</li>
</ul>
</nav>
<section id="">
Section
</section>
<footer>
Footer
</footer>
</body>
</html>`
here is the CSS page:
header
{
width: 1325px;
height: 150px;
background-color: green;
position: relative;
}
nav
{
position: relative;
width: 400px;
height: 500px;
background-color: teal;
float: left;
overflow: hidden;
}
section
{
position: relative;
float: left;
width: 925px;
height: 500px;
background-color: blue;
}
footer
{
float: left;
width: 1325px;
height: 50px;
background-color: lime;
position: relative;
overflow: hidden;
}
thanks for your help and time in advance
Your body tag needs to be before the header. Add a wrapper with a width to your HTML/CSS.
Here is a fiddle: http://jsfiddle.net/msJLW/
<div id="wrap">
<header>
Header
</header>
<nav>
<ul>
<li>
Tab1
</li>
<li>
Tab2
</li>
<li>
Tab3
</li>
<li>
Tab4
</li>
<li>
Tab5
</li>
</ul>
</nav>
<section id="">
Section
</section>
<footer>
Footer
</footer>
</div>
#wrap{
width:1325px;
}
header
{
width: 1325px;
height: 150px;
background-color: green;
}
nav
{
width: 400px;
height: 500px;
background-color: teal;
float: left;
overflow: hidden;
}
section
{
float: left;
width: 925px;
height: 500px;
background-color: blue;
}
footer
{
float: left;
width: 1325px;
height: 50px;
background-color: lime;
overflow: hidden;
}
Jack is correct, and you might want to try adding a container to your elements. See this fiddle: JSFiddle. Essentially, they are all
position: relative
without having an absolutely positioned container to refer to. Adding a wrapping container with the CSS:
position: absolute;
height: 700px;
width: 1325px;
Kept everything in line. Let me know if this wasn't what you were trying to achieve!
I'm not entirely sure why it is moving like this, but I know a simple solution: contain it all in a wrapper. Copy/paste this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MyWebsite</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="wrapper">
<header>
Header
</header>
<nav>
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
<li>Tab 5</li>
</ul>
</nav>
<section id="">
Section
</section>
<footer>
Footer
</footer>
</div>
</body>
</html>
Keep in mind that anything not included in the <head> tags should be inside the <body> tags, except your doctype and <html> tags -- this includes your <header> tags.
Anyway, what we've done above is wrap everything in a "div", a means by which we can divide a web page up. They are easy to control using css, and you should copy/paste the following to the bottom of your css file:
.wrapper {
width: 1325px;
}
Let me know if you need any more help :D