Issues in vertical hr line in html and css - html

I have been created the web page using html and css.
I have been created two products in one column.
I need to add vertical hr line between these two products.
For that i did,
Html:
<div class="headerDivider"></div>
Css:
.headerDivider {
border-right:1px solid #16222c;
height:400px;
margin-right: 458px;
overflow:hidden;
}
It doesn't work correctly, Here is my jsfiddle: http://jsfiddle.net/22z6vjrx/
Can anyone please help me to fix this,
Thanks in advance .....

You have to float all of divs (or inline-block display). But I prefer to add vertical line as border on right for elements:
.product {
float: left;
width: 100px;
height: 200px;
margin-bottom: 5px;
}
.clear {
clear: both;
}
.first {
background-color: #eee;
}
.product:not(:last-child) {
border-right: 1px solid black;
}
.second {
background-color: #ddd;
}
.third {
background-color: #dfd;
}
.fourth {
background-color: #ddf;
}
<div class='row'>
<div class='clear'></div>
<div class="product first"></div>
<div class="product second"></div>
</div>
<div class='row'>
<div class='clear'></div>
<div class="product third"></div>
<div class="product fourth"></div>
</div>
Updated version
Add box-sizing: border-box; to include border and padding to element width (otherwise with will be too big as it will be width + border-width * 2 + padding * 2). Also make sure you have floated products divs as well as vertical hr:
.product,
.vhr {
box-sizing: border-box;
}
.product {
float: left;
width: 49%;
height: 200px;
margin-bottom: 5px;
}
.first {
background-color: #eee;
}
.second {
background-color: #ddd;
}
.vhr {
float: left;
border-left: 1px solid black;
height: 200px;
width: 1%;
margin-left: 1%;
}
<div class="product first"></div>
<div class="vhr"></div>
<div class="product second"></div>

Related

Unexpected result from margin-top next to a floated element

I'm having issues with a layout like this:
.wrapper {
clear: both;
background-color: #ccc;
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
margin-top: 20px;
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
width: 100px;
float: left;
background: lightblue;
}
.main {
margin-left: 100px;
background: lightgreen;
}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
<div class="wrapper">
<div class="top">
<div class="box">top</div>
</div>
<div class="side">side</div>
<div class="main">main</div>
</div>
The .main and .side elements need to be aligned. As you can see in the above snippet, everything is fine unless the .top element has no height in which case the margin-top rule causes them to be skewed. All of the following "fix" the issue but each has a drawback:
adding border to .wrapper (I might be able to live with a transparent border but I really don't like this since it feels like a dirty hack and I'd rather not add a border. For some reason the border needs to have a width of at least 1px or this doesn't work)
.wrapper {
clear: both;
background-color: #ccc;
border: 1px solid #000;
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
margin-top: 20px;
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
width: 100px;
float: left;
background: lightblue;
}
.main {
margin-left: 100px;
background: lightgreen;
}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
adding overflow: hidden to .wrapper (this hides parts of some elements and causes others to fall in the wrong place)
adding overflow: auto to .wrapper (this adds scroll bars in some scenarios)
Those last two are not apparent in my snippet but in the real world application they cause problems as mentioned here.
I have a strong suspicion the issue is related to Why doesn't the height of a container element increase if it contains floated elements? and CSS container doesn't stretch to accommodate floats but I've tried many of those suggestions and none seem to quite solve the issue - perhaps because one of my divs is floated and the other is not.
Since this is part of a large application, I don't want to drastically change the layout, just have some css that will keep .main and .side aligned regardless of the content before those elements.
You can make the main element to be inline-block and use calc to set the width. This shouldn't affect your layout a lot and you will get the correct output:
.main {
width:calc(100% - 100px);
display:inline-block;
background: lightgreen;
}
Full code:
.wrapper {
background-color: #ccc;
clear: both;
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
margin-top: 20px;
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
width: 100px;
float: left;
background: lightblue;
}
.main {
width:calc(100% - 100px);
display:inline-block;
background: lightgreen;
}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
<div class="wrapper">
<div class="top">
<div class="box">top</div>
</div>
<div class="side">side</div>
<div class="main">main</div>
</div>
Another hacky idea is to make sure your top element is never empty:
.top:empty {
font-size:0;
}
.top:empty::before {
content: "\80"; /* a random character */
}
Full code
.wrapper {
background-color: #ccc;
clear: both;
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
margin-top: 20px;
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
width: 100px;
float: left;
background: lightblue;
}
.main {
margin-left: 100px;
background: lightgreen;
}
.top:empty {
font-size:0;
}
.top:empty::before {
content: "\80"; /* a random character */
}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
<div class="wrapper">
<div class="top">
<div class="box">top</div>
</div>
<div class="side">side</div>
<div class="main">main</div>
</div>
You can also consider the same trick but using a pseudo element on the main wrapper:
.wrapper::before {
content: "\80"; /* a random character */
display:block;
font-size:0;
}
Full code
.wrapper {
background-color: #ccc;
clear: both;
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
margin-top: 20px;
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
width: 100px;
float: left;
background: lightblue;
}
.main {
margin-left: 100px;
background: lightgreen;
}
.wrapper::before {
content: "\80"; /* a random character */
display:block;
font-size:0;
}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
<div class="wrapper">
<div class="top">
<div class="box">top</div>
</div>
<div class="side">side</div>
<div class="main">main</div>
</div>
You can also make the wrapper inline-block with a width equal to 100% and it will behave almost the same as a block element:
.wrapper {
background-color: #ccc;
display:inline-block;
width:100%;
vertical-align:top; /* avoid some unwanted white space issue*/
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
margin-top: 20px;
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
width: 100px;
float: left;
background: lightblue;
}
.main {
margin-left: 100px;
background: lightgreen;
}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
<div class="wrapper">
<div class="top">
<div class="box">top</div>
</div>
<div class="side">side</div>
<div class="main">main</div>
</div>
For the explanation, you are facing a margin collpasing issue like described in the specification:
Two margins are adjoining if and only if:
both belong to in-flow block-level boxes that participate in the same block formatting context
no line boxes, no clearance, no padding and no border separate them (Note that certain zero-height line boxes (see 9.4.2) are ignored for this purpose.)
both belong to vertically-adjacent box edges, i.e. form one of:
top margin of a box and top margin of its first in-flow child
You can do this much more elegantly with grid. Here is the grid code:
.wrapper {
display: grid;
grid-template-areas:
"top top"
"side main";
grid-template-columns: 100px 1fr;
}
.top{grid-area:top}
.side{grid-area:side}
.main{grid-area:main}
Notice how many other elements I was able to comment out and still keep the desired layout.
.wrapper {
/*clear: both;*/
background-color: #ccc;
}
.wrapper+.wrapper {
margin-top: 50px;
}
.side,
.main {
height: 100px;
padding: 10px;
box-sizing: border-box;
/*margin-top: 20px;*/
}
.box {
padding: 10px;
}
.top {
background: yellow;
}
.side {
/*width: 100px;
float: left;*/
background: lightblue;
}
.main {
/*margin-left: 100px;*/
background: lightgreen;
}
.wrapper {
display: grid;
grid-template-areas:
"top top"
"side main";
grid-template-columns: 100px 1fr;
}
.top{grid-area:top}
.side{grid-area:side}
.main{grid-area:main}
<div class="wrapper">
<div class="top"></div>
<div class="side">side</div>
<div class="main">main</div>
</div>
<div class="wrapper">
<div class="top">
<div class="box">top</div>
</div>
<div class="side">side</div>
<div class="main">main</div>
</div>

Swap divs(Column Children) in Different Columns in Responsive

I have bootstrap column arrangement as following and have few divs with different height inside the columns.
HERE IS THE CODEPEN
.column>div {
border: 1px solid #333;
background: #ddd;
color: white;
padding: 15px;
margin: 5px 0;
text-align: center;
font-size: 18px;
}
div#child-1 {
height: 150px;
}
div#child-2 {
height: 50px;
}
div#child-3 {
height: 50px
}
div#child-4 {
height: 100px;
}
div#child-5 {
height: 100px;
}
div#child-6 {
height: 150px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12 column">
<div id="child-1">1</div>
<div id="child-3">3</div>
<div id="child-5">5</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 column">
<div id="child-2">2</div>
<div id="child-4">4</div>
<div id="child-6">6</div>
</div>
</div>
</div>
When it comes to mobile devices, I need to use col-xs-12 for each columns.
Now I need to have the following structure formed in the responsive #child-1, #child-2, #child-3, #child-4, #child-5, #child-6.
But for now I have the pattern mixed as #child-1, #child-3, #child-5, #child-2, #child-4, #child-6.
How can I swap these elements to the alignment that I wanted?
I think float can be helpful for such layout then on small device you can switch to flexbox (or even CSS-grid) and adjust the order. The main trick is to have all the elements inside one container so that you can easily handle them:
.container {
max-width:1124px;
margin:auto;
}
.column>div {
border: 1px solid #333;
background: #ddd;
color: white;
padding: 15px;
margin: 5px;
text-align: center;
font-size: 18px;
box-sizing:border-box;
width:calc(50% - 10px);
}
div#child-1 {
height: 150px;
float:left;
}
div#child-2 {
height: 50px;
float:right;
}
div#child-3 {
height: 50px;
float:left;
}
div#child-4 {
height: 100px;
float:right;
}
div#child-5 {
height: 100px;
float:left;
}
div#child-6 {
height: 150px;
float:right;
}
#media (max-width:767px) {
.container {
display:flex;
flex-direction:column;
}
.column > div {
width:auto;
}
#child-1 {order:1}
#child-2 {order:2}
#child-3 {order:3}
#child-4 {order:4}
#child-5 {order:5}
#child-6 {order:6}
}
<div class="container column">
<div id="child-1">1</div>
<div id="child-2">2</div>
<div id="child-4">4</div>
<div id="child-3">3</div>
<div id="child-6">6</div>
<div id="child-5">5</div>
</div>

Responsive two column grid into one alternating rows

I would like to create the following responsive grid structure, taking in mind that element 3 and 5 are not always present and they have variable height.:
grid image
I've tried with floating elements, but element 2 get's into the second column:
https://jsfiddle.net/a2tbbs2b/2/
CSS:
.panel{
background: white;
box-sizing: border-box;
overflow: auto;
}
.panel::after {
content: "";
clear: both;
display: table;
}
.section{
width: 60%;
float: right;
text-align: center;
}
.section.left{
float: left;
width: 40%;
}
.one{
background: green;
height: 80px;
}
.two{
background: blue;
height: 70px;
}
.three{
background: red;
height: 20px;
}
.four{
background: brown;
height: 20px;
}
.five{
background: yellow;
height: 50px;
}
HTML:
<div class="panel">
<div class="section three">3</div>
<div class="section left one">1</div>
<div class="section four">4</div>
<div class="section left two">2</div>
<div class="section five">5</div>
</div>
I'm trying not to duplicate HTML to create this layout.
Any help is welcome. Thank you for your time!
#main {
width: 98vw;
height: 300px;
border: 1px solid black;
display: -webkit-flex; /* Safari */
display: flex;
}
#main div{ margin:5px;}
.flex-1{flex:1;}
.flex-2{flex:2;}
.flex-3{flex:3;}
<div id="main">
<div class='flex-2' style="background-color:coral;">RED</div>
<div class='flex-3' style="background-color:lightblue;">BLUE</div>
<div class='flex-1'></div>
<div class='flex-1' style="background-color:lightgreen;">Green div with more content.
</div>
you can add the content in their specific containers .I am unable to understand your numbering scheme , but you can add content likewise.
Read More

CSS style div3 differently if div1 and div2 don't exist

If a user is signed up to my site, in their login area I have 3 divs as follows:
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
These divs all have a width of 32% and sit inline with each other.
#psts-cancel-link {
background: white;
border-left: 3px solid #ccc;
padding: 1em;
width: 32%;
min-height: 270px;
float: left;
}
.psts-receipt-link {
background: white;
border-left: 3px solid #ccc;
min-height: 270px;
float: left;
width: 32%;
margin: 0 10px;
padding: 20px;
}
#psts-signup-another {
background: white;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
width: 32%;
min-height: 270px;
float: left;
}
When a user is not signed up, only one of the divs displays:
<div id="psts-signup-another"></div>
Is it possible to change the styling of this so that it's width is 100% when div1 and div2 aren't displayed?
So far I have tried this, but with no success:
#psts-cancel-link ~ .psts-receipt-link ~ #psts_existing_info #psts-signup-another {
width:100%;
}
Table Layout Implementation
Use a table layout. Specify display: table on the parent and display: table-cell on the child elements.
#psts-cancel-link {
background: tomato;
border-left: 3px solid #ccc;
padding: 1em;
min-height: 270px;
display: table-cell;
word-wrap: break-word;
}
.psts-receipt-link {
background: lightblue;
border-left: 3px solid #ccc;
min-height: 270px;
margin: 0 10px;
padding: 20px;
display: table-cell;
word-wrap: break-word;
}
#psts-signup-another {
background: tomato;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
min-height: 270px;
display: table-cell;
word-wrap: break-word;
}
.container {
display: table;
width: 100%;
table-layout: fixed;
}
Logged in
<div class="container">
<div id="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logged out
<div class="container">
<div id="psts-signup-another"></div>
</div>
Flexbox Layout Implementation
You can also use flexbox which expands and shrinks the child items according to the parent container.
#psts-cancel-link {
background: tomato;
border-left: 3px solid #ccc;
padding: 1em;
min-height: 270px;
flex: 1;
}
.psts-receipt-link {
background: lightblue;
border-left: 3px solid #ccc;
min-height: 270px;
margin: 0 10px;
padding: 20px;
flex: 1;
}
#psts-signup-another {
background: tomato;
padding: 1em;
border-left: 3px solid #ccc;
margin-bottom: 30px;
min-height: 270px;
flex: 1;
}
.container {
display: flex;
}
Logged in
<div class="container">
<div id="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logged out
<div class="container">
<div id="psts-signup-another"></div>
</div>
You could simply use :first-child if it's indeed the only child in the second case.
#psts-signup-another:first-child {}
You can use the adjacent selector. Have a look at the following snippet:
#psts-signup-another {padding: 5px; background: #f99;}
div + div + #psts-signup-another {padding: 5px; background: #99f;}
<h2>Div when three divs are present</h2>
<div class="theDivs">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
<h2>Div when three divs are not present</h2>
<div class="theDivs">
<div id="psts-signup-another"></div>
</div>
i think you should use another container div with a new class when user logout.
Logged:
<div class="container">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
Logout:
<div class="container logout">
<div id="psts-cancel-link" class="psts-cancel-link"></div>
<div class="psts-receipt-link"></div>
<div id="psts-signup-another"></div>
</div>
CSS:
.container.logout > div {
display:none;
}
.container.logout > .psts-signup-another {
display:block;
}

Move elements to the right but keep their order in the HTML?

I need a number of elements to line up horizontally in the order in which they appear in the HTML. I need them to move to the right of their container.
If I float the items to the right then the order changes.
If I display as inline-block and make the container's text aligned to the right then there are spaces between them.
I can change the HTML however I cant remove all the white space (which may fix the issue with the inline-blocks). Can this be solved?
http://codepen.io/anon/pen/xbLbLE
<div class="cont">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
.itemA,
.itemB {
width: 50px;
height: 50px;
}
.itemA {
background: green;
float: right;
}
.contB {
text-align: right;
}
.itemB {
background: red;
display: inline-block;
}
Float the elements that need to be in the correct order to the left and float their container to the right.
HTML
<div class="cont">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
CSS
.itemA,
.itemB {
width: 50px;
height: 50px;
}
.itemA {
background: green;
}
.itemB {
background: red;
}
.cont {
float: right;
}
.cont div {
float: left;
}
and the jsFiddle: http://jsfiddle.net/enaeLr60/
The downside, as you have noticed, with using display: inline-block is that any space between each inline-block will be rendered as a single space. I prefer to float items to fix this versus the options like the following as they a not preferred.
<div>1</div><div>2</div> - no spaced between elements
<div>1</div><!-- comment block --><div>2</div> - comment between
elements
use a negative margin
UPDATE
My original answer forgot to include a wrapper around the .cont DIVs. As a result the DIVs with .itemA appear after the .itemB DIVs. See updated code to correct this.
HTML
<div class="list-container">
<div class="cont">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
</div>
CSS
.itemA,
.itemB {
width: 50px;
height: 50px;
}
.itemA {
background: green;
}
.itemB {
background: red;
}
.list-container {
float: right;
}
.cont,
.cont div {
float: left;
}
updated jsFiddle: http://jsfiddle.net/enaeLr60/1
I am not sure if I am understanding your problem correctly, but can you give this CSS and try and see if it is what you are attempting to do.
.cont{
display:inline;
float:right
}
.itemA,
.itemB {
width: 50px;
height: 50px;
display: inline-block;
margin-right:-4px;
}
.itemA:last-child, .itemB:last-child{
margin-right:0;
}
.itemA {
background: green;
}
.contB {
text-align: right;
}
.itemB {
background: red;
}
The margin-right:4px is a hack around the display:inline-block; so, this may not be desirable.
My answer involves adding a wrapper around it all: http://codepen.io/anon/pen/KwvwyL
<div class="wrapper">
<div class="cont contA">
<div class='itemA'>1</div>
<div class='itemA'>2</div>
<div class='itemA'>3</div>
<div class='itemA'>4</div>
</div>
<div class="cont contB">
<div class='itemB'>1</div>
<div class='itemB'>2</div>
<div class='itemB'>3</div>
<div class='itemB'>4</div>
</div>
</div>
Then in the CSS:
.wrapper {
float: right;
}
.contA {
float: left;
background: green;
}
.contB {
float: left;
background: red;
}
.itemA, .itemB {
float: left;
}
.itemA,
.itemB {
width: 50px;
height: 50px;
display:inline-block;
}
.itemA { background: green; }
.cont { display:inline-block; word-spacing: -100%; }
.contB { float:right; }
.itemB { background: red; }
CodeOpen