For larger screens I have a scenario where I can display an image next to some text like this:
<div class="wrapper">
<div>
<img>
</div>
<div>
<div> text</div>
<div> text</div>
<div> text</div>
</div>
</div>
For smaller screens, I want to eliminate the wrapper so that the image stacks on top of the text; but I can't figure out how to eliminate the div with the wrapper class to let the image stack on top of the image. My issue isn't using media queries, it's eliminating the outer wrapper when I apply the media queries.
Any suggestions?
You can use display:contents for this...
These elements don't produce a specific box by themselves. They are replaced by their pseudo-box and their child boxes.
.wrapper {
display: flex;
}
#media (min-width: 601px) {
.wrapper {
display: contents;
}
}
<div class="wrapper">
<div>
<img src="https://s3.amazonaws.com/baconmockup/img/baconmockup-200-130.jpg" alt="">
</div>
<div>
<div> text</div>
<div> text</div>
<div> text</div>
</div>
</div>
you can use JavaScript to remove the selector nullifying the wrapper.
var wrapper=document.querySelector('.wrapper')
if(window.innerWidth < 500){
wrapper.classList=""
}
UPDATE:
My answer is incorrect-- you apparently can do this with media queries. See Paulie_D's answer to this post for more details.
Original Answer:
You cannot use media queries to eliminate a container while leaving its children in place. The way to do this would be to duplicate the content without the wrapper for smaller screens:
#media (max-width: 800px) {
.showOnLargeScreens {
display: none;
}
}
#media (min-width: 801px) {
.showOnSmallScreens {
display: none;
}
}
<div class="wrapper">
<div class="showOnLargeScreens">
<p>Large screen!</p>
</div>
<p class="showOnSmallScreens">Small screen!</p>
<div>
<div> text</div>
<div> text</div>
<div> text</div>
</div>
</div>
Related
I have a dynamic audio player with artist and title.
It works fine but on mobile view, artist and title stick out my div.
Here is a picture:
I'd like artist - title (when longer that user's screen) display just a line below.
I already tested by changing display, overflow-text, word-breakā¦
Here is my code:
#media screen and (orientation: portrait) and (max-width: 750px) {
.titrage { display: inline-grid; }
}
<div class='titrage'>
<div class='now'>Your are listening <img class='cover' src="cover.png" alt='audio-cover'> : </div>
<div class='artist'>Loading </div>
<div class='separator'>-</div>
<div class='title'>...</div>
</div>
Use flex maybe? This should work fine without media queries. Or can with media queries too.
.titrage {
display: flex;
flex-direction: row;
align-items: center;
}
.pad {
padding: 10px;
}
<div class='titrage'>
<div class='now'>
<img class='cover' src="https://via.placeholder.com/75" alt='audio-cover'>
</div>
<div class="pad">
<span>You are Listening :</span>
<span class='artist'>Bee Gees </span>
<span class='separator'>-</span>
<span class='title'>Stayin' Alive (1977)</span>
</div>
</div>
Changing the divs to use display: inline-block; instead of inline grid will change the divs layout behavior.
Here is a link from MDN with more details on block and inline layout flow.
Right now with inline-grid each div is taking up a full row, regardless of its content's size, changing to inline-block will make each div take up roughly the amount of size that its content requires, resulting in them displaying on the same line if they can fit, demonstrated in code snippet below:
.titrage, .now, .artist, .separator, .title { display: inline-block; }
<div class='titrage'>
<div class='now'>Your are listening <img class='cover' src="cover.png" alt='audio-cover'> : </div>
<div class='artist'>Loading </div>
<div class='separator'>-</div>
<div class='title'>...</div>
</div>
This question about making divs layout on the same line is directly related.
Also this question about preferred line break points seems tangentially related.
Thanks for answering ;)
I finally found a solution.
I just put artist - title a line below 'You are listening'.
On .titrage a display: block. On .now a display: fixed. And on a new div .now (including .artist, .separator and .title) a display: flex
It's working for me :p
I have styled the #screen-xs-max and #screen-sm-max. Both of those queries worked. Now I am trying to style #screen-md-max and the styles wont apply to a certain div. Not all styles - just to a particular div. They dont even show up at all when you inspect the element?
HTML:
<div class="screenshot-projects bottom-border wrap" style="background-color:#fff;">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="projects-img">
<img src="~/Images/img-lgipad.png" />
</div>
</div>
</div>
Less:
#media (max-width:#screen-md-max) {
.screenshot-projects {
projects-img {
max-width: 300px!important;
}
}
}
The only style not applying is the one to the image size - it's instead taking up the entire screen, and this only happens once i resize to medium. At xs and s it's max:100%; and it applies.
You forgot the dot
#media (max-width:#screen-md-max) {
.screenshot-projects {
.projects-img {
max-width: 300px!important;
}
}
}
I have 3 HTML div elements that I'd like to see sit side-by-side on a wide screen but, as the screen width collapses, I'd like to see them become vertical.
For example...
Wide screen:
-----------
abc def ghi
-----------
Narrow screen:
abc
def
ghi
Currently, having three HTML divs that look as follows (in another div block) only lays them out vertically...
<div id="Master_Div">
<div id="Div_1">
<p>abc</p>
</div>
<div id="Div_2">
<p>def</p>
</div>
<div id="Div_3">
<p>ghi</p>
</div>
</div>
Maybe, the answer is tied to media queries and/or responsive design but I'm not sure.
Thanks for any help you can offer.
I would do something like this unless you actually need to apply an ID, classes are easier for multiple divs with one assignment in css, plus they take up less space.
html---
<div class="wrapper">
<div class="third">Something here</div>
<div class="third">Something here</div>
<div class="third">Something here</div>
</div>
css---
.third {
width: 33.333333%
float:left;
}
#media screen and (max-width: 767px) {
.third {
width: 100%;
float:none;
}
}
Yes, there are media queries that allow you to control layout for mobile screens, ex:
/* Portrait and Landscape */
#media only screen
and (min-device-width: 768px)
and (max-device-width: 1024px)
and (-webkit-min-device-pixel-ratio: 1) {
/* stack vertically */
}
A simple media query can do this.
/* Apply inner styles if the viewport's width is at least 500px */
#media (min-width: 500px) {
.responsive {
width:33.3333%;
float:left;
}
}
<div id="Master_Div">
<div class="responsive" id="Div_1">
<p>abc</p>
</div>
<div class="responsive" id="Div_2">
<p>def</p>
</div>
<div class="responsive" id="Div_3">
<p>ghi</p>
</div>
</div>
Your problem is best resolved by natural design patterns without resorting to media queries. Think about it this way , if you lay out your columns side by side using css float:left; and you define some min-width value per column. then as soon as the screen width becomes smaller than the width value you defined X 3 , your columns will be laid out vertically
I am trying to figure out why the not selector is not working. Here is my code:
http://jsfiddle.net/8CKJa/15/
CSS:
#full-content, #mobile-content {
display: none;
}
.collapsed .make #mobile-content {
display: block;
}
.content:not(.collapsed) .make #full-content {
display: block;
}
HTML:
<div class="content">
<div class="content collapsed">
<div id="car">
<div class="make">
<div id="full-content">
full content
</div>
<div id="mobile-content">
mobile content
</div>
</div>
</div>
</div>
</div>
I am trying to hide the full-content div. As you can see .content:not(.collapsed) is not suppose to match any of the divs but it is matching the full-content div. How can I hide the full-content div. I am not sure how many .content parents there will be. The collapsed class can disappear if the menu is expanded.
The :not() selector is working as expected. The issue is that your wrapper div has the class of content without collapsed and then you have one with the class collapsed. Removing the first div makes it work as expected.
http://jsfiddle.net/3L7ym/
<div class="content collapsed">
<div id="car">
<div class="make">
<div id="full-content">
full content
</div>
<div id="mobile-content">
mobile content
</div>
</div>
</div>
</div>
Presuming you are looking to key off of .collapsed and you can't know ahead of time how many .content containers you'll have, you may be able to simplify the whole thing by removing the :not selector:
.collapsed #mobile-content {
display: block;
}
.collapsed #full-content, #mobile-content {
display: none;
}
Fiddle here.
I have simple 3-column layout based on Twitter Bootstrap. The only problem is, that each column is assembled from block with different heights.
<div class="container">
<div id="blocks" class="row">
<div class="span4">
<div class="block" id="block1">
<div class="block" id="block4">
<div class="block" id="block7">
</div>
<div class="span4">
<div class="block" id="block2">
<div class="block" id="block5">
<div class="block" id="block8">
</div>
<div class="span4">
<div class="block" id="block3">
<div class="block" id="block5">
<div class="block" id="block9">
</div>
</div>
</div>
.
It works quite fine, except for small displays. Then the order of blocks is not sorted.
Is there some way to achieve sorted blocks without any JavaScript?
There is no way to achieve this effect with the 3 column structure. If the blocks were all the same height then you could float:left all the blocks without the columns then they would wrap in order. Since they are not the same size you would have to use a JavaScript such as masonry:
http://masonry.desandro.com/
Its gonna be quite difficult to achieve this, you might wanna play around with single list with float: left; or display: inline-block;
or
you can have 2 set of list with #media inside the css, so depend on the screen size you can show or hide the selected div
css example
#media screen and (min-width: 768px) and (max-width: 1024px) {
.displayFullScreen {display: block;}
.displayMobileScreen {display: none;}
}
#media screen and (max-width: 767px) {
.displayFullScreen {display: none;}
.displayMobileScreen {display: block;}
}
Other way is to use column-count: 3; and column-gap: 10px;. But order is different on wide screen.
Fiddle: https://jsfiddle.net/ksvx2txb/102/