I would like to place to DIVS (grey & red) inside a DIV (black) under the first DIV (black) when you resize the window and the screen is less than 1024 px. Take a look at the example under. You can also see the image attached.
I would really like som advice here, im totally lost here at the moment.
This is how I want it to be on screens more than 1024px:
<div id="black">
<div id="grey"></div>
<div id="red"></div>
</div>
This is how I want it to be on screens less than 1024 px:
<div id="black"></div>
<div id="grey"></div>
<div id="red"></div>
There is no need to duplicate the content.
#black{background:black;overflow:hidden;}
#grey, #red{
min-height:100px;
margin:2%;
float:left;
width:47%;
}
#grey{background:gray;margin-right:1%}
#red{background:red;margin-left:1%}
#media (min-width:1024px){
#black{padding-top:100px;}
#grey, #red{
float:none;
width:auto;
margin:0;
}
}
<div id="black">
<div id="grey"></div>
<div id="red"></div>
</div>
Sorry, but that is not possible as written.
You cannot move items outside their containing structures using CSS. You can only reformat them within their present structure.
Optionally, you can duplicate the existing black div and show/hide one or the other based on media queries (screen size).
Or, you can use jQuery/javascript to move the DOM items. But CSS alone cannot do this.
Using just CSS (with media queries) and two container <div>s to separate logic:
#media (max-width: 1024px) {
.large { display: none; }
.small { display: block; }
.black { height: 100px; }
}
#media (min-width: 1025px) {
.large { display: block; }
.small { display: none; }
.red, .grey { float: left; }
.black:after { content: ""; display: table; clear: both; }
.red { width: calc(50% - 5px); margin-left: 10px; }
.grey { width: calc(50% - 5px); }
}
.large {
height: 200px;
}
.small {
height: 200px;
}
.black {
background-color: black;=
height: 100px;
padding: 10px;
box-sizing: border-box;
}
.red {
background-color: red;
height: 100px;
}
.grey {
background-color: grey;
height: 100px;
}
<div class="large">
<div class="black">
<div class="grey"></div>
<div class="red"></div>
</div>
</div>
<div class="small">
<div class="black"></div>
<div class="grey"></div>
<div class="red"></div>
</div>
Above snippet better viewed in full page or this codepen
Related
i'm having big issues trying to convert 3 divs from horizontal to vertical.
The code below shows that for a screen bigger than 1200px, everything works fine.
What i want is a responsive behaviour: by reducing the browser's width, the 3 divs are positioned one below the other. Currently, the code will adjust so that 2 divs are in the first row if there is room for it.
HTML:
<div class="example">
<div class="example-item example-item-1">abcabcabcabcabcabcabcabcabcabcabcabcabc</div>
<div class="example-item example-item-2">123456789012345678901234567890123456789012345678901234567890</div>
<div class="example-item example-item-3">xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz</div>
</div>
and CSS:
.example {
background-color: black;
padding: 10px 20px;
text-align: center;
}
.example-item {
font-size: 0.7em;
display: inline-block;
padding: 10px 10px;
}
.example-item-1 {
background-color: blue;
}
.example-item-2 {
background-color: red;
}
.example-item-3 {
background-color: green;
}
Actual result, what i don't want
Desire result, what i want
You can use this css code and change the "max-width" property with your own decision:
#media screen and (max-width: 900px) {
.example-item {
display: block;
}
}
jsfiddle:
https://jsfiddle.net/mr_seven/1ktcqay4/2/
you can change result area width to see what will be happened if screen width changes.
In all honesty I would look at using a grid system e.g. bootstraps grid system or even better checkout flex box.
However to simply answer the question you could do this:
.container{
text-align:center;
}
.box{
height:20px;
width:150px;
border:1px solid black;
display: block;
margin:0 auto;
}
#media only screen and (min-width: 768px) {
.box {
display: inline-block;
}
}
.box1{
background:steelblue;
}
.box2{
background:indianred;
}
.box3{
background:coral;
}
<div class="container">
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
</div>
https://jsfiddle.net/y7c96sp8/
I have a responsive fluid width website. For one section I have a title, text and an image.
For larger displays I need the title and text to sit to the right of the image. For smaller displays I want a single column with the title first. (see image)
<div class="cont">
<h1>Here is Title</h1>
<div class="img"></div>
<p>Some text</p>
</div>
.cont {
background: grey;
width: 30%;
margin: auto;
}
.img {
height: 200px;
width: 200px;
background: blue;
}
Is this layout possible? For support reasons I cant use flexbox.
http://codepen.io/anon/pen/JoYMoX
I would use your current CSS as default for smaller screens and then use media queries to adjust the layout for larger ones. You may have to use absolute positioning.
For your example:
#media only screen and (min-width: 720px) {
.img {
position: absolute;
top: 0;
left: -200px;
}
.cont {
margin-left: 200px;
position: relative;
}
}
Edit - Alternative Without Absolute Positioning:
As I mentioned in the comments, you could also place the image in the content twice and then simply hide one as needed with media queries. Not ideal, but at least the browser should only download the image once.
So for example:
.cont-wrap {
background: grey;
width: 60%;
margin: auto;
overflow: hidden;
}
.cont {
float: left;
}
.img {
height: 200px;
width: 200px;
background: blue;
}
.left {
display: none;
float: left;
}
#media only screen and (min-width: 720px) {
.img {
display: none;
}
.img.left {
display: block;
}
}
<div class="cont-wrap">
<div class="img left"></div>
<div class="cont">
<h1>Here is Title</h1>
<div class="img"></div>
<p>Some text</p>
</div>
</div>
with bootstrap you can use the offset parametrics, and the pull or push orders.
with the normal css, you can use media query setting position absolute...
#media all and (max-width:768px){
.title{position: absolute;
top:0; left: 0;
OR
width:100%; float:left; ...}
.bluebox{width:100%;
margin-top:20px;}}
I was wondering if someone can help me to work out how to create the below layout using bootstrap.
Large screens the layout is:
and then for small screens the layout would be:
Basically the red will have an image (full height of red box) with and box (yellow) overlapping it. The yellow box will have the same height as the blue box. Then on mobile all four boxes will stack on top of one another.
I've had a play around with the code here:
http://www.bootply.com/6N3RHao0CI
If anyone can help me out it would be appreciated! :-)
Thanks
Well, took me a bit of time, but then I figured out my media queries were smaller than the screensize I was looking at. Turns out I had the solution all along. Of course media sizes are set to what I was tampering with, so do change those to bootstrap sizes.
Settings will have to be changed a bit, but the idea is there.
This is the code used: Bootply link
I gave the div the name floater and gave it the following CSS:
#media(min-width: 1000px) {
.floater {
right: 34%;
position: absolute;
}
}
#media(max-width: 999px) {
.floater {
position: relative;
right: 0px !important;
}
}
Using pure bootstrap. Added some CSS for the sake of demo.
DEMO: http://jsfiddle.net/arshadmuhammed/bkogj3wn/6/
(please don't forget to resize the output screen in fiddle)
HTML
<div class="col-md-10">
<div class="col-md-8 col-md-offset-4 hidden-sm hidden-xs"></div>
</div>
<div class="col-md-12 hai2 hidden-md hidden-lg"></div>
<div class="col-md-2 col-xs-6"></div>
<div class="col-md-2 col-xs-6 hai"></div>
CSS
.col-md-10{
background:url('http://images.all-free-download.com/images/graphiclarge/blue_abstract_background_310971.jpg');
background-size:cover;
height:200px;
padding-right:0px!important;
}
.col-md-8{
background:yellow;
height:100px;
}
.col-md-2{
background:blue;
height:100px;
}
.hai{
background:green;
height:100px;
}
.hai2{
background:yellow;
height:100px;
}
The Bootstrap grid is not really made for this design pattern (equal heights and all). If you want something fluid with equal height text boxes on the right (for the larger viewports), I would roll my own using display:table and table-cell.
DEMO: https://jsbin.com/hufasa
HTML
<div class="hero-feature">
<div class="hero-image">
<div class="intro-text">Text box</div>
</div>
<!-- /.hero-image -->
<div class="feature-box-wrapper">
<div class="feature-box blue">
<div>text</div>
</div><!-- /.feature -->
<div class="feature-box green">
<div>text</div>
</div><!-- /.feature-box -->
</div><!-- /.feature-box-wrapper -->
</div><!-- /.hero-feature -->
CSS
.hero-feature .hero-image {
background-image: url(http://placekitten.com/g/500/500);
background-size: cover;
background-position: center top;
padding-top: 50%;
}
.hero-feature .intro-text {
background: gold;
padding: 5%;
}
.hero-feature .feature-box {
padding: 5%;
color: #fff;
float: left;
width: 50%;
}
.hero-feature .blue {
background-color: blue
}
.hero-feature .green {
background-color: green
}
#media (min-width:768px) {
.hero-feature {
display: table;
width: 100%;
}
.hero-feature .intro-text {
position: absolute;
top: 0;
right: 0;
padding: 5%;
width: 70%;
}
.hero-feature .hero-image {
display: table-cell;
background-image: url(http://placekitten.com/g/500/500);
background-size: 100.5%;
/*disquise the rounding errors in many browsers */
background-position: center center;
height: 400px; /*height of the image*/
padding: 0;
position: relative;
}
.hero-feature .feature-box-wrapper {
display: table-cell;
width: 30%;
}
.hero-feature .feature-box {
display: table;
height: 50%; /* half the height of the .hero-image */
width: 100%;
vertical-align: middle;
float: none;
}
.hero-feature .feature-box > div {
display: table-cell;
vertical-align: middle;
text-align: center;
}
}
Everything not shared by all viewport sizes goes outside the media query.
I am trying to achieve the following in a two column float css design
My css for the two is this:
.div1 {
width: 25%;
float:left;
}
.div2 {
width: 75%;
float: right;
}
.container {
width:960px;
}
#media screen and (max-width: 320px) {
.div1, .div2 {
width: 100%;
display: block;
float: none;
clear: both;
}
.container {
width: 100%;
}
}
my html is this:
...
<div class="container">
<div class="div1">
... content inside
</div>
<div class="div2">
<img src="photo_loc"/>
</div>
</div>
I have Div I and Div II. Div I is 25% width and Div II is 75% width. When I go to 320px (iphone portrait) using responsive design Div II goes below Div I, which I assume is the normal process.
What I am trying to do is have Div II above Div I using floats, how can this be achieved through css?
Working Fiddle
.div1 {
width: 25%;
float:left;
background:orange;
}
.div2 {
width:75%;
float: right;
background:red;
}
#media screen and (max-width: 320px) {
.div1, .div2 {
width: 100%;
display: block;
float: none;
clear: both;
}
.div1{
position:relative;
top:100px;
}
.div2{
position:absolute;
top:0;
height:100px;
}
}
Swap the HTML positions of div1 and div2 around.
It may not be semantically correct in terms of how the page should be layed out but it will still work.
Keep the CSS the same and have your html like this
<div class="div2">Text for box 2</div>
<div class="div1">Text for box 1</div>
Demo: http://jsfiddle.net/u3Ng3/1/
I've been working on a two row and 1 column layout with flexbox, I'm using flexbox because I don't think css2.1 can fill the remainding space for box-B. In my example of my jsFiddle, I can't get box-C to shift up on the right hand side and also I can't get box-B to flex vertically and fill the contents can someone please help me with this layout
jsFiddle here
#container {
background-color:red;
width:100%; height:100%
}
#three-box-layout {
display:flex;
display:-ms-flex;
display:-webkit-flexbox;
display:-moz-flex;
height:100%;
-ms-flex-direction:column;
-webkit-flex-direction:column
}
.shuffle-box {
}
#box-a {
background-color:#f601ff; -ms-flex-order:1; -webkit-flex-order:1;
margin-right:30%;
}
#box-b {
-ms-flex:3;
-webkit-flex:3;
-moz-flex:3;
flex:3;
background-color:#37fe02;
margin-right:30%;
}
#three-box-layout #box-c {
-ms-flex:3;
-webkit-flex:3;
-moz-flex:3;
flex:3;
background-color:#02effe;
margin-left:70%; float:right;
}
<div id="container">
<div id="three-box-layout">
<div id="box-a" class="shuffle-box">
<div style="height:425px; background-color:pink">A</div>
</div>
<div id="box-b">B</div>
<div id="box-c">C</div>
</div>
</div>
You can do this with CSS tables (Flexbox isn't necessary)
Resize the browser to see the media queries in action!
FIDDLE1 (little content) / FIDDLE2 (lots of content)
Markup
<div class="container">
<div class="row1">
<div>A</div>
<div></div> /* give this div table cell 50% width on wide screens */
</div>
<div class="row2">
<div>B</div>
<div>C</div>
</div>
</div>
CSS
--
.container {
width: 800px;
height: 500px;
display:table;
text-align: center;
position: relative;
}
.row1 {
display:table-row;
max-height: 425px;
background: pink;
}
.row1 div {
display:table-cell;
width:50%;
}
.row2 {
display:table-row;
height: 100%;
}
.row2 div {
width: 100%;
height: 100%;
float:left;
background: green;
}
.row2 div + div {
background: aqua;
width: 50%;
height: 100%;
position: absolute;
top:0;
right:0;
}
#media (max-width: 1024px) {
.row1 {
width: 100%;
}
.row1 div + div {
display: none;
}
.row2 div {
width: 50%;
}
.row2 div + div {
position: static;
}
}