I had an issue with some positioning of four divs. these four divs are in two columns. The problem that I had was that when the top div on the right side became larger (height wise) than the two divs on the left side combined, the bottom div on the right side slid over to the left side underneath the two on the left side
My solution (with the help of a wonderful stack overflow all-star) was implementing a col-sm-offset-8 on that third div.
It was worked out here bootstrap, 4 divs, 2 columns. One column not floating right
This worked.....but. The result of this was that when the top div on the right side is now smaller, it does not respond (by respond I mean it no longer is positioned right underneath the top div on the right side).
<div class="col-xs-12 col-sm-8 start">Start ride For</div>
<div class="col-xs-12 col-sm-4 pull-right open">Open ride</div>
<div class="hidden-xs hidden-sm"></div>
<div class="col-xs-12 col-sm-8 conditions ">rides We have</div>
<div class="col-xs-12 col-sm-4 col-sm-offset-8 pull-right hours">Hours of Operation</div>
.start { height: 50px; background: #fcc; }
.open { height: 250px; background: #fdb; }
.hours { height: 50px; background: #ffb; }
.hidden { height: 50px; }
.conditions { height: 150px; background: #cfc; }
I have been tinkering with this issue on a pen as well.
http://codepen.io/KDweber89/pen/LVddKK?editors=110
So...basically with my four divs, I ALWAYS want the 'hours of operation' div to be directly below the 'open ride' div, regardless of what size the 'open ride' div is. (the reality is with the real work I am doing, the 'open ride' div often changes sizes based off of the user)
Wrap the entire content inside a div and execute the media query on max-width: 768px which is the break point for the change of order currently. Inside the media query, Use a flexible box property to reorder them.
.start {
height: 50px;
background: #fcc;
}
.open {
height: 650px;
background: #fdb;
}
.hours {
height: 50px;
background: #ffb;
}
.hidden {
height: 50px;
}
.rides {
height: 150px;
background: #cfc;
}
#media (max-width: 768px) {
.flex-wrap {
display: flex;
flex-direction: column;
}
.open {
order: 3;
}
.hours {
order: 4;
}
}
#media (max-width: 480px) {
.open {
order: 2;
}
.hours {
order: 4;
}
.rides {
order: 3;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="flex-wrap">
<div class="col-xs-12 col-sm-8 start">Start Ride</div>
<div class="col-xs-12 col-sm-4 pull-right open">Open ride</div>
<div class="hidden-xs hidden-sm"></div>
<div class="col-xs-12 col-sm-8 rides ">Rides We have</div>
<div class="col-xs-12 col-sm-4 col-sm-offset-8 pull-right hours">Hours of Operation</div>
</div>
Related
I have the following html:
<div id="inner-container">
<div id="titles">
<div id="main-title">Main title here</div>
<div id="page-title">Page title here</div>
</div>
<nav id="progress-container>
<div id="page-counter">Page count here</div>
<a id="link-to-page-1"></a>
<a id="link-to-page-2"></a>
<a id="link-to-page-3"></a>
<a id="link-to-page-4"></a>
<a id="link-to-page-5"></a>
<a id="link-to-page-6"></a>
</nav>
</div>
and this css:
#inner-container {
display: flex;
}
#titles > div {
width: 100%;
}
#progress-container #page-counter {
float: left;
}
#progress-container a {
display: inline-block;
width: 30px;
height: 12px
border: 3px solid #ffffff;
}
#media (max-width: 900px) {
#progress-container #page-counter {
display: none;
}
}
I would like to add the necessary css flex rules so that #titles occupies the full remaining width, left over by #progress-container.
I have tried:
#titles {
flex-grow: 1;
}
And I've tried various rules for #page-progress, including 'flex-basis: auto' and 'flex-basis: content', but nothing has worked.
Note that I cannot set a fixed width to #progress-container as the number of 'pages' is dynamic and will vary. Also the #page-counter disappears below 900px.
If anyone has any ideas, I'd like to hear them!
try adding flex: 1; in #titles.
learn about flex properties over here
#titles {
flex: 1;
}
codepen link
Is there any way to show 2 columns layout with this html code using only css? (column1 class should be in first column, and column2 class in the second one)
<div class="container">
<div class="column1">1a</div>
<div class="column2">1b</div>
<div class="column2">2b<br>zz</div>
<div class="column1">2a</div>
<div class="column2">3b</div>
<div class="column2">4b</div>
<div class="column2">5b</div>
<div class="column1">3a</div>
<div class="column1">4a</div>
</div>
The result I expect:
1a 1b
2a 2b
3a zz
4a 3b
4b
5b
float seems the best option here if you manage the Block formatting context
example :
* {
box-sizing: border-box;
}
div div {
box-shadow: inset 0 0 0 1px;/* see me */
width: 50%;
line-height: 1.4em; /* because of that 2 lines element , to hide the gap */
background: lightblue;/* see me too */
}
.column1:first-child {
float: left; /* let another float stand aside */
}
.column2 {
float: right;
clear: right;/* pile us to the far right */
}
div div:nth-child(odd) {
background: lightgreen;/* see me different */
}
<div class="container">
<div class="column1">1a</div>
<div class="column2">1b</div>
<div class="column2">2b<br>zz</div>
<div class="column1">2a</div>
<div class="column2">3b</div>
<div class="column2">4b</div>
<div class="column2">5b</div>
<div class="column1">3a</div>
<div class="column1">4a</div>
</div>
It matches your expected layout here, but will it also with real content ?
One way you can do this would be to float the classes of .column1 and .column2 like this:
.container {
width: 100%;
}
.column1 {
float: left;
width: 50%;
}
.column2 {
float: right;
width: 50%;
}
<div class="container">
<div class="column1">1a</div>
<div class="column2">1a</div>
<div class="column1">2b</div>
<div class="column1">3a</div>
<div class="column2">2b</div>
<div class="column2">3b</div>
<div class="column2">4b</div>
<div class="column1">4a</div>
<div class="column1">5a</div>
</div>
You could also maybe simplify this by having only two inner divs representing each column like:
<div class="container">
<div class="column1">
...
</div>
<div class="column2">
...
</div>
</div>
You can also achieve the same thing using minimal code via using flex. Just add following code to your CSS.
.container {
display: flex;
flex-wrap: wrap;
}
.container div {
width: 50%;
}
Explanation :- You add display type flex to the container, so all the elements will be arranged next to each other inside this div.
Now when you give width of 50% to internal divs and give flex-wrap: wrap to the container, it ensures that only 2 divs are next to each other since there is no space left to accommodate more.
Alternatively if you want the items closer to each other you can reduce the width of the outer container.
.container {
display: flex;
flex-wrap: wrap;
width: 10%;
}
.container div {
width: 50%;
}
This would be closer to what you are expecting.
I have a flexbox and I would like my form-control (width: 100%) button to extend to the same length as the h2, not the h1. Unfortunately, because the div that both the h2 and the button are placed in does not have a width declared, the form-control class is extending the width of the button to the parent that has a width declared.
I have tried setting the parent div (.landing-header div) to position relative, and I have tried setting a min-width on it but it has not worked.
The reason I don't want to explicitly declare a width is because I don't want my h2 to wrap around, rather I want my h2 to dictate the width of the div and therefore the width of the button.
Screenshot:
screenshot
#landing-page {
.row {
height: 100vh;
}
.btn-custom {
margin-top: 50px;
}
}
.landing-header {
padding-left: 5%;
div {
min-width: 60%;
}
}
.landing-graphic {
background: $blue;
width: 40%;
}
// Extra large devices (large desktops, 1200px and up)
#media (min-width: 1200px) {
}
<div class="container-fluid">
<div class="row d-flex flex-row justify-content-between align-items-stretch">
<div class="landing-header d-flex flex-column justify-content-center">
<h1>SAMUEL COLE</h1>
<div>
<h2>Web Development and Design</h2>
<button class="btn form-control btn-custom about-nav">Get Started</button>
</div>
</div>
<div class="landing-graphic">
test
</div>
</div>
</div>
To achieve what you wanted to do I would set display: inline-block to the parent div of the h2 and the button.
With this change your snippet will look like this:
If you remove the flex characteristics from your landing-header div this is possible.
Then set the div holding the h2 and button to display:table and width:1%.
This will collapse the div to its own width.
Apply white-space:nowrap; to the h1 and h2 so the text doesn't wrap and...
#landing-page .row {
height: 100vh;
}
#landing-page .btn-custom {
margin-top: 50px;
}
.landing-header {
padding-left: 5%;
}
.landing-header div {
min-width: 60%;
}
.landing-graphic {
background: blue;
width: 40%;
}
button.about-nav {
background: limegreen;
}
.this-one {
display: table;
width: 1%;
}
.this-one h2 {
white-space: nowrap;
font-size: 14px;
}
.landing-header h1 {
white-space: nowrap;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row d-flex flex-row justify-content-between align-items-stretch">
<div class="landing-header ">
<h1>SAMUEL COLE</h1>
<div class="this-one">
<h2>Web Development and Design</h2>
<button class="btn form-control btn-custom about-nav">Get Started</button>
</div>
</div>
<div class="landing-graphic">
test
</div>
</div>
</div>
In a responsive layout, I have two columns. The left column is the sidebar and the right column is the content.
Using a media query, when the screen width is tiny, the columns turn to 100% width and stack on top of each other.
In this case, I want the sidebar (the first div) to appear beneath the content (the second div).
I tried using float: right on a small screen once it's at 100%, but at 100% width, the float apparently doesn't matter.
.left, .right {
width: 100%;
float: left;
background: green;
}
.left {
float: right;
background: red;
}
.half {
width: 50%;
}
.space {
width: 100%;
display: block;
height: 40px;
}
And on the page:
<div class="left half"> <!-- To mimic full screen size -->
Left
</div>
<div class="right half">
Right
</div>
<div class="space"></div>
<div class="left"> <!-- To mimic small screen size -->
Left
</div>
<div class="right"><!-- This should appear first -->
Right
</div>
Here is the fiddle: https://jsfiddle.net/ph09frvw/
I'm sure this is not the first time someone wanted to wrap the sidebar under the content, I just haven't been able to find a solution.
You can use display: flex and use the order property to change the order of the <div> elements. While floating can be helpful for horizontal alignment, it will be of little help for vertical alignment, Here is an example:
.flex {
display: flex;
flex-flow: row wrap;
}
.left {
order: 2;
flex: 1 0 50%;
background: red;
}
.right {
order: 1;
flex: 1 0 50%;
background: green;
}
.full {
margin-top: 20px;
}
.full > .left,
.full > .right {
flex: 1 0 100%;
}
<div class="flex">
<div class="left">
Left
</div>
<div class="right">
Right
</div>
</div>
<div class="flex full">
<div class="left">
Left
</div>
<div class="right">
Right
</div>
</div>
You could use the display:flex; property combined with flex-direction to reorder your divs. Ref: https://css-tricks.com/almanac/properties/f/flex-direction/
Remember to reference your related class-names in your HTML elements' class attribute.
Your CSS display:block should do the trick, else try something like:
float: left
When you use: display:block on a div element, you do not need to specify width:100% as it should automatically span across the width if it is not hindered by anything else.
Make sure the position of these elements are "relative", else it may not work as expected; it may be stated globally that some specific tags should be displayed "absolute" and that may break what you're trying to achieve.
Five of us have spent a day and a half working on this - got some very close solutions, but seems like it might be impossible to do without pulling in Javascript.
Scenario
We're using a responsive (media-query based), 960 grid layout. There are four divs with content. These four divs need to semantically be in the order shown in the image below. Since it is the 960 grid, we also have wrapper divs per "row" - like this:
<div id="topzone">
<div id="one">1</div>
<div id="two">2</div>
</div>
<div id="bottomzone">
<div id="three">3</div>
<div id="four">4</div>
</div>
Div one has the intro to an article, div two has an advertisement, div three has the actual article and div four has random stuff (facebook feeds, whatever).
On mobile, the divs need to display in order from one to four. On desktop they need to display the same, but in two columns, horizontally ordered first.
So far so good. Here is the kicker:
We don't know what height the divs will be - they will vary with each page (even the advertisement one).
There can't be any vertical gaps between divs.
We can't use Javascript (or really, really, really don't want to - we know we can do this easily with JS)
If you just do floats left and right you get gaps:
<div id="topzone">
<div id="one" style="float: left; height: 300px">1</div>
<div id="two" style="float: right; height: 200px">2</div>
</div>
<div id="bottomzone">
<div id="three" style="float: left; height: 100px">3</div>
<div id="four" style="float: right; height: 300px">4</div>
</div>
Attempted Solutions
CSS tables don't allow for rowspans. Workarounds either have the empty div get overlayed or leave gaps.
Masonry CSS orders the divs vertically so mobile would incorrectly drop divs two and four below one and three.
The closest we came was hijacking the overflow property to display the third div below the first one. This worked brilliantly - until we tried to add a footer to the page. Because overflow has no height according to the browser, the footer overlayed the third div.
<style type="text/css">
#one {
height: 300px;
background-color: yellow;
}
#two {
height: 200px;
background-color: brown;
}
#three {
background-color: blue; /* only shows in mobile, otherwise hidden behind #one */
}
#three-inner {
height: 100px;
border: 2px solid black;
}
#four {
height: 300px;
background-color: burlywood;
}
/* Non-mobile */
#media all and (min-width: 740px) and (min-device-width: 740px),
(max-device-width: 800px) and (min-width: 740px) {
#one {
float: left;
width: 50%;
}
#two {
float: right;
width: 50%;
}
#three {
height: 0px; /* turns into overflow */
width: 50%;
}
#three-inner {
clear: left;
}
#four {
float: right;
width: 50%;
clear: right;
}
}
</style>
<div id="topzone">
<div id="one">
<p><strong>First block.</strong></p>
</div>
<div id="two">
<strong>Second block</strong>
</div>
<div id="bottomzone">
<div id="three">
<div id="three-inner">
<p><strong>Third block.</strong></p>
</div>
</div>
<div id="four">
<p><strong>Fourth block.</strong></p>
</div>
</div>
</div>
There must be a way to do this in all CSS - tell me there is?