How apply different media queries rules for a specific div? - html

I'm working on AB Testing variation for a website that use tons of CSS rules and I'm wondering if there is a possibility to apply their classical 767px view media queries for a unique div (something like an iframe behavior)
Indeed, we try to add a second div to give more information and we reduced the size of the original div that used to live with width:100%
I would like to do something like this :
<main>
<section>
<div class="Original_Div"> //Apply CSS like browser is 767px
</div>
<div class="New_Div"> //Apply normal CSS
</div>
</section>
</main>
Perhaps this will help better understanding
Actual Version
Variation variation
Massive thanks !!

You can add any extra styling you like by adding more style blocks to your html. The example below has a media query that will give you the red div only at screen widths of up to 768px. For sizes bigger than that (e.g. a laptop or desktop) then the green div appears. To ensure the div gets no larger than 768px (e.g. a typical tablet size) then use the max-width css rule. If you want to control the height also to make it look like it's a tablet screen then you can use the aspect-ratio css rule (see here for info)
If you're modifying someone else's html then make sure you use a unique class name or id so you control your specific elements. The inspector on the browser's web developer tools (on firefox it's Ctrl + Shift + I) is useful to find out what's actually being rendered.
body {
background-color: #282C34;
}
section {
display:flex;
gap: 1rem;
padding: 1rem;
height:80vh;
}
section > div {
padding: 1rem 2rem;
}
.Original_Div {
flex-grow:1;
max-width:767px;
border: 0.25rem solid red;
color:red;
}
.New_Div {
flex-grow:1;
border: 0.25rem solid #00ff00;
color: #00ff00;
}
#media only screen and (max-width:767px) {
.New_Div {
display:none;
}
}
<main>
<section>
<div class="Original_Div">
Original with media queries that fit this width
</div>
<div class="New_Div">
New block
</div>
</section>
</main>

I think I understand what you have asked. You want to behave a div in tablet media query while at the same time the other div should behave on media queries of normal Document size.
You can apply tablet media queries to a specific div and add !important so it will override other media queries that may have applied to its parent div/body element.
You can do the same for other div where you want to apply regular media queries.
#media screen and (min-width: 600px) {
.Original_Div {
max-width: 80% !important;
display: inline !important;
}
.New_Div{
max-width: 20% !important;
display: inline !important;
}
}
If I misunderstood your requirements, do let me know so I can provide you with right solution.

Related

How to 4 side by side image using html to Mobile view vertical and Desktop view Horizontal

I have 4 images on my website that I want to display horizontally on desktop and vertically on mobile. (I just started learning HTML so I basically know nothing about coding)
The only thing I could think of was to find a way to set that a number of images will align themselves next to each other as long as there's room for them.
Please tell me if there is any simple way to set this up.
Simply use flexbox. Then add #media-queries to address screens with a limited width and change the flex-direction there to column while the default is row.
div {
display: flex;
}
img {
width: 25%;
}
#media only screen
and (max-width: 568px) {
div {
flex-direction: column;
}
img {
width: 100%;
}
}
/* for visualisation only */
img {display: block;}
<div>
<img src="https://via.placeholder.com/300x200.jpg/FF0000">
<img src="https://via.placeholder.com/300x200.jpg/00FF00">
<img src="https://via.placeholder.com/300x200.jpg/0000FF">
<img src="https://via.placeholder.com/300x200.jpg/FFFF00">
</div>
You can use the tranform css method to solve this,
"transform: rotate(90deg);". Here is a link to help you more: https://www.w3schools.com/cssref/css3_pr_transform.asp
and then add a media query code in the css where the transform property for mobile would be 90deg. Here is a link to help you with the media query: https://www.w3schools.com/css/css_rwd_mediaqueries.asp

Flexbox compatibility with horizontal pre / code scrolling

I have some code inside pre and code tags in a bootstrap container that I'd like to scroll horizontally. This normally works fine, until I add a flexbox to my page's body in order to accomplish a sticky footer. After this, the code no longer scrolls horizontally when the page is narrow (such as for mobile viewing).
Here's my code (note that horizontal scrollbars for the code go away as you narrow the window):
html, body {
height: 100%;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
code {
max-height: 200px;
background-color: #eeeeee;
word-break: normal !important;
word-wrap: normal !important;
white-space: pre !important;
}
.flexer {
flex: 1;
}
footer {
background-color: #CCC;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12 docs">
<p>Some sample code</p>
<pre><code>Loading mirror speeds from cached hostfilebase: mirrors.arpnetworks.com * centosplus: mirrors.arpnetworks.com* extras:mirrors.arpnetworks.com*rpmforge: mirror.hmc.eduupdates: mirrors.arpnetworks.comExcluding Packages in global exclude list</code></pre>
</div>
</div>
</div>
<div class="flexer"></div>
<footer>
<div class="container">
<div class="row">
<div class="col-sm-12 text-center">
footer
</div>
</div>
</div>
</footer>
http://jsfiddle.net/nturor46/1/
Any idea how to use flexbox for sticky footers while still maintaining scrolling pre / code?
a simple
.container { width:100%; }
resizes the website correctly. BUT then, Chrome doesn't let you actually use the scrollbar. This is caused by it overflowing the dimension of all of its containers (apart from BODY).
Hence we gotta tell the browser to correctly recognize the pre node:
.container {
max-width: 100%;
}
pre {
position: relative;
}
This tells Chrome to correctly handle mouse events again AND fixes the layout
Please note that the margin-bottom of the pre-node is lost in overflow-country, which could cause your layout to look weird. max-width was used in the final version to make sure it doesn't overwrite fixed width statements made in bootstrap
PS: tested in current Chrome and Firefox http://jsfiddle.net/nturor46/32/
Those bootstrap styles just wreak havoc on natural CSS!
The problem seems to come from a conflict between your column-direction flex container and bootstrap rules. It's basically resulting in the horizontal scrollbar shifting from the pre / code content box to the browser window, when the content box overflows the screen.
With these adjustments, your desired layout seems to work:
make the primary .container div the primary flex container (in your code this role is played by the body element)
move the footer element into this new container
use flex auto margins to stick the footer to the bottom
override bootstrap margin, padding and width wherever necessary
HTML
<div class="container">
<div class="row">
<div class="col-md-12 docs">
<p>Some sample code</p>
<pre><code>Loading mirror speeds from ... cached hostfilebase</code></pre>
</div>
</div>
<footer>
<div class="container">
<div class="row">
<div class="col-sm-12 text-center">
footer
</div>
</div>
</div>
</footer>
</div>
CSS
html, body { height: 100%; }
body > .container {
display: flex;
flex-direction: column;
height: 100%;
width: 100%; /* override bootstrap styles */
padding: 0; /* override bootstrap styles */
}
body > .container > .row {
margin: 0; /* override bootstrap styles */
display: flex; /* nested flex container */
justify-content: center; /* center content box on page */
}
body > .container > .row > .docs {
width: 75%; /* adjust width of the content box here */
}
code {
max-height: 200px;
background-color: #eeeeee;
word-break: normal !important;
word-wrap: normal !important;
white-space: pre !important;
}
footer {
margin-top: auto; /* stick footer to bottom of container */
background-color: #CCC;
}
Revised Fiddle
Tested in Chrome and Firefox.
What happens here is most definitely a bug in Chrome.
After playing around with your Fiddle, and looking at it with other browsers, I can conclude that this is a Chrome-specific problem. And a curious one.
For some reason, <div class="col-md-12 docs"> grows to the size it should have (the height of p and pre together), but doesn't account for the horizontal scrollbar inside the pre tag.
Here's an image to demonstrate the problem. The part with the red background is the container.
Since pre has a border of 1px wide at the bottom, the result leaves a 1px gap for you to actually use the scrollbar. You can try it yourself. Just try to grab the most upper 1px line of the scrollbar.
Removing the flex properties does fix your problem, but we're not going to accept that.
Now, I would've thought that adding a padding of 0.1px to the bottom of the parent would fix the problem, but it didn't. I then tried wrapping the pre tag in a div with class chromefix, and then added the following CSS
.chromefix{
overflow: hidden;
}
But that created an even weirder situation where the container grew with the scrollbar for about 50%
So I tried combining the two, but not a lot of difference there.
This is where I started looking at the pre tag and its properties. It has overflow: auto by Bootstrap default. So what I tried was adding
pre{
overflow-x: scroll !important;
}
And guess what? It worked!
So all you have to do is add overflow-x: scroll !imporant to pre, and you're good to go! Here's a working Fiddle.
Hope this helps
As a sidenote. I think you want to move max-height: 200px to pre as well. It won't work when you've applied it to code.
Problem seems to be with the width of <pre>.
When width of your screen goes below 768px no specific width is applied to the .container by bootstrap hence the issue is occurring.
When width of your screen is above 768px following classes from bootstrap.css come in picture.
#media (min-width: 1200px)
.container {
width: 1170px;
}
#media (min-width: 992px)
.container {
width: 970px;
}
#media (min-width: 768px)
.container {
width: 750px;
}
As you can see once the width goes below 768px there is no specific width given.
To counter this issue, you have to write your css, something like this.
#media (min-width: 480px) {
.container {
width: calc(100% - 40px);
}
}
Basically, you have to specify width for .container when screen width goes below 768px. Once you do, it will fix your issue.
Wrap the prev tag and its content with div like below.
<div class="code">{your code goes here}</div>
css :
.code{
width:92vw; /*you can change this in media query to specific device width for better results*/
overflow-x:auto;
}
Working jsfiddle link

floating, left aligned, centered div

I would like to have a div, that is centered in its parent, but children inside of it would be left aligned. Wanting this, I obtained the following:
.centered {
text-align: center;
}
.container {
background: red;
padding: 10px;
display: inline-block;
}
.child {
width: 100px;
height: 100px;
float: left;
}
.child:nth-child(even) {
background: green;
}
.child:nth-child(odd) {
background: blue;
}
<div class="centered">
<div class="container">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>
http://jsfiddle.net/SbPRg/ (source: Having the floating children elements determine parent width).
The problem is that when you resize the page, there still is a problem whereas the main "red" div is wider than what I want, and shows a big part of red instead of resizing to a smaller container that 'fits' its children.
Would you have any idea of how to obtain something like this, without javascript ?
The main idea is to display a "gallery" of images, in a responsive way (if the screen is too small, display one image per row, up until three per row if the screen is wide).
Just add CSS Media Query rules. Add the following rule to your CSS.
CSS
#media screen and (max-width: 337px) {
.child
{
float:none;
text-align:center;
display:block;
}
}
Working : Demo
Note: CSS Media Query is use to effect the styling to your HTML based on screen width's and height's
Here in your example i have added media query rule which gives style to .child class when the screen width reaches the 337px .
There could be max-width or min-width for width's similar for heights.
What is max-width: Here i have given it 337px, so it says that the css inside the rule should apply only when screen width is <=337px. If screen width is >337px then it would not apply the css within that rule.
Just set the Container width to the exact width you want (in the JSFidlle: you need to add width:300px;)
and when you resize the window nothing happen.
to make it responsive use:
#media screen and (max-width:480px)
and set the width of the container for every screen size(or for the three block and for two then for just one)
You could try a media query like this:
#media screen and (max-width: 350px) {
.container {width: 100px;}
}
Check the demo
The reason that the parent container "shows a big part of red instead of resizing to a smaller container that 'fits' its children" is that you've explicitly given it ten pixels of padding on all sides. Remove that padding from .container, and the div will shrink to the width of its children.
I guess you want something like this.
Use media query to apply relevant css to the elements based on the various screen sizes.
Here is the css you need to add:
#media only screen and (max-width: 350px) {
.child {
clear:both;
}
}
One alternative would be to use a framework like Bootstrap, which is good enough for making responsive webpages.
Here are the links you can refer to:
Media query reference.
Bootstrap.

Different style for same div in mobile and desktop

I’ve got a <div> section in content body with a <table> inside, like below:
<div class="hub_specification">
example html table
</div>
For Desktop view, I’d like to show this <div> with 50% width of content area, floated left.
For Mobile view, I’d like to show this <div> with 100% width of content area, without any float.
So what will be the CSS code to decorate the hub_specification <div>?
It would be better if both ways were present: CSS for a file called style.css in WordPress and inline.
If I am not wrong do you want this? click
HTML
<div class="hub_specification">
example html table
</div>
CSS
.hub_specification {
width:100%;
background-color:red;
height:20px;
}
#media only screen and (max-width:350px) {
.hub_specification {
width:50%;
background-color:oink;
height:20px;
}
}
Use CSS media queries
.hub_specification{
background-color: lightgreen;
}
#media screen and (max-width: 480px) {
.hub_specification {
background-color: lightblue;
}
}
Reference: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp

What is the correct way to do a CSS Wrapper?

I have heard a lot of my friends talk about using wrappers in CSS to center the "main" part of a website.
Is this the best way to accomplish this? What is best practice? Are there other ways?
Most basic example (live example here):
CSS:
#wrapper {
width: 500px;
margin: 0 auto;
}
HTML:
<body>
<div id="wrapper">
Piece of text inside a 500px width div centered on the page
</div>
</body>
How the principle works:
Create your wrapper and assign it a certain width. Then apply an automatic horizontal margin to it by using margin: 0 auto; or margin-left: auto; margin-right: auto;. The automatic margins make sure your element is centered.
The best way to do it depends on your specific use-case.
However, if we speak for the general best practices for implementing a CSS Wrapper, here is my proposal: introduce an additional <div> element with the following class:
/**
* 1. Center the content. Yes, that's a bit opinionated.
* 2. Use `max-width` instead `width`
* 3. Add padding on the sides.
*/
.wrapper {
margin-right: auto; /* 1 */
margin-left: auto; /* 1 */
max-width: 960px; /* 2 */
padding-right: 10px; /* 3 */
padding-left: 10px; /* 3 */
}
... for those of you, who want to understand why, here are the 4 big reasons I see:
1. Use max-width instead width
In the answer currently accepted Aron says width. I disagree and I propose max-width instead.
Setting the width of a block-level element will prevent it from stretching out to the edges of its container. Therefore, the Wrapper element will take up the specified width. The problem occurs when the browser window is smaller than the width of the element. The browser then adds a horizontal scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling of small windows. This is important when making a site usable on small devices. Here’s a good example showcasing the problem:
/**
* The problem with this one occurs
* when the browser window is smaller than 960px.
* The browser then adds a horizontal scrollbar to the page.
*/
.width {
width: 960px;
margin-left: auto;
margin-right: auto;
border: 3px solid #73AD21;
}
/**
* Using max-width instead, in this situation,
* will improve the browser's handling of small windows.
* This is important when making a site usable on small devices.
*/
.max-width {
max-width: 960px;
margin-left: auto;
margin-right: auto;
border: 3px solid #73AD21;
}
/**
* Credits for the tip: W3Schools
* https://www.w3schools.com/css/css_max-width.asp
*/
<div class="width">This div element has width: 960px;</div>
<br />
<div class="max-width">This div element has max-width: 960px;</div>
So in terms of Responsiveness, is seems like max-width is the better choice!-
2. Add Padding on the Sides
I’ve seen a lot of developers still forget one edge case. Let’s say we have a Wrapper with max-width set to 980px. The edge case appears when the user’s device screen width is exactly 980px. The content then will exactly glue to the edges of the screen with not any breathing space left.
Generally, we’d want to have a bit of padding on the sides. That’s why if I need to implement a Wrapper with a total width of 980px, I’d do it like so:
.wrapper {
max-width: 960px; /** 20px smaller, to fit the paddings on the sides */
padding-right: 10px;
padding-left: 10px;
/** ... omitted for brevity */
}
Therefore, that’s why adding padding-left and padding-right to your Wrapper might be a good idea, especially on mobile.
Or, consider using box-sizing so that the padding doesn’t change the overall width at all.
3. Use a <div> Instead of a <section>
By definition, the Wrapper has no semantic meaning. It simply holds all visual elements and content on the page. It’s just a generic container. Therefore, in terms of semantics, <div> is the best choice.
One might wonder if maybe a <section> element could fit this purpose. However, here’s what the W3C spec says:
The element is not a generic container element. When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the div element instead. A general rule is that the section element is appropriate only if the element's contents would be listed explicitly in the document's outline.
The <section> element carries it’s own semantics. It represents a thematic grouping of content. The theme of each section should be identified, typically by including a heading (h1-h6 element) as a child of the section element.
Examples of sections would be chapters, the various tabbed pages in a tabbed dialog box, or the numbered sections of a thesis. A Web site's home page could be split into sections for an introduction, news items, and contact information.
It might not seem very obvious at first sight, but yes! The plain old <div> fits best for a Wrapper!
4. Using the <body> Tag vs. Using an Additional <div>
Here's a related question. Yes, there are some instances where you could simply use the <body> element as a wrapper. However, I wouldn’t recommend you to do so, simply due to flexibility and resilience to changes.
Here's an use-case that illustrates a possible issue: Imagine if on a later stage of the project you need to enforce a footer to "stick" to the end of the document (bottom of the viewport when the document is short). Even if you can use the most modern way to do it - with Flexbox, I guess you need an additional Wrapper <div>.
I would conclude it is still best practice to have an additional <div> for implementing a CSS Wrapper. This way if spec requirements change later on you don't have to add the Wrapper later and deal with moving the styles around a lot. After all, we're only talking about 1 extra DOM element.
You don't need a wrapper, just use the body as the wrapper.
CSS:
body {
margin:0 auto;
width:200px;
}
HTML:
<body>
<p>some content</p>
<body>
<div class="wrapper">test test test</div>
.wrapper{
width:100px;
height:100px;
margin:0 auto;
}
Check working example at http://jsfiddle.net/8wpYV/
The easiest way is to have a "wrapper" div element with a width set, and a left and right margin of auto.
Sample markup:
<!doctype html>
<html>
<head>
<title></title>
<style type="text/css">
.wrapper { width: 960px; margin: 0 auto; background-color: #cccccc; }
body { margin: 0; padding: 0 }
</style>
</head>
<body>
<div class="wrapper">
your content...
</div>
</body>
</html>
a "wrapper" is just a term for some element that encapsulates all other visual elements on the page. The body tag seems to fit the bill, but you would be at the mercy of the browser to determine what displays beneath that if you adjust the max-width.
Instead, we use div because it acts as a simple container that does not break. the main, header, footer, and section tags in HTML5 are just div elements named appropriately. It seems that there could (or should) be a wrapper tag because of this trend, but you may use whichever method of wrapping you find most suitable for your situation. through classes, ids and css, you can use a span tag in a very similar way.
There are a lot of HTML element tags that we do not use often or possibly even know about. Doing some research would show you what can be done with pure HTML.
Are there other ways?
Negative margins were also used for horizontal (and vertical!) centering but there are quite a few drawbacks when you resize the window browser: no window slider; the content can't be seen anymore if the size of the window browser is too small.
No surprise as it uses absolute positioning, a beast never completely tamed!
Example: http://bluerobot.com/web/css/center2.html
So that was only FYI as you asked for it, margin: 0 auto; is a better solution.
Centering content has so many avenues that it can't really be explored in a single answer. If you would like to explore them, CSS Zen Garden is an enjoyable-if-old resource exploring the many, many ways to layout content in a way even old browsers will tolerate.
The correct way, if you don't have any mitigating requirements, is to just apply margin: auto to the sides, and a width. If your page has no content that needs to go outside those margins, just apply it to the body:
body {
padding: 0;
margin: 15px auto;
width: 500px;
}
https://jsfiddle.net/b9chris/62wgq8nk/
So here we've got a 500px wide set of content centered at all* sizes. The padding 0 is to deal with some browsers that like to apply some default padding and throw us off a bit. In the example I do wrap the content in an article tag to be nice to Screen Readers, Pocket, etc so for example the blind can jump past the nav you likely have (which should be in nav) and straight to the content.
I say all* because below 500px this will mess up - we're not being Responsive. To get Responsive, you could just use Bootstrap etc, but building it yourself you use a Media Query like:
body {
padding: 0;
margin: 15px;
#media (min-width: 500px) {
margin: 15px auto;
width: 500px;
}
}
Note that this is SCSS/SASS syntax - if you're using plain CSS, it's inverted:
body {
padding: 0;
margin: 15px;
}
#media (min-width: 500px) {
body {
margin: 15px auto;
width: 500px;
}
}
https://jsfiddle.net/b9chris/62wgq8nk/6/
It's common however to want to center just one chunk of a page, so let's apply this to only the article tag in a final example.
body {
padding: 0;
margin: 0;
}
nav {
width: 100%;
box-sizing: border-box;
padding: 15px;
}
article {
margin: 15px;
#media (min-width: 500px) {
margin: 15px auto;
width: 500px;
}
}
https://jsfiddle.net/b9chris/62wgq8nk/17/
Note that this final example also uses CSS Flexbox in the nav, which is also one of the newer ways you could center things. So, that's fun.
But, there are special circumstances where you need to use other approaches to center content, and each of those is probably worth its own question (many of them already asked and answered here on this site).
/******************
Fit the body to the edges of the screen
******************/
body {
margin:0;
padding:0;
}
header {
background:black;
width:100%;
}
.header {
height:200px;
}
nav {
width:100%;
background:lightseagreen;
}
.nav {
padding:0;
margin:0;
}
.nav a {
padding:10px;
font-family:tahoma;
font-size:12pt;
color:white;
}
/******************
Centered wrapper, all other content divs will go inside this and will never exceed the width of 960px.
******************/
.wrapper {
width:960px;
max-width:100%;
margin:0 auto;
}
<!-------- Start HTML ---------->
<body>
<header>
<div id="header" class="wrapper">
</div>
</header>
<nav>
<div id="nav" class="wrapper">
</div>
</nav>
</body>