Misaligned "outline" with uneven number of divs - html

My issue is that I wanted side-by-side elements with borders, but I noticed without doing some margin-hack it was difficult to use the border property and it still didn't look right. However when I use outline or box-shadow, I get this alignment issue at the end.
.inner {
outline: 1px solid black;
width: 50%;
height: 50px;
float: left;
margin: 0;
display: inline-block;
box-sizing: border-box;
position: relative;
background: #fff;
}
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
It looks alright when there's an even number of elements but when I have this last element it looks odd. Some might suggest I just make it fit to the end which would be okay but the size can be configurable sometimes so this could be a common occurrence.
What is the proper way to achieve this where the last element lines up the border(or outline) correctly?

Because you're using outline to create your border, the outlines at the center are actually overlapping one another. When you get to the bottom where there is only one div the outline is not being overlapped and therefore looks misaligned. You could solve this issues by building it as a table:
.table {
width: 100%;
display: table;
border-collapse: collapse;
}
.column {
display: table-row;
}
.inner {
display: table-cell;
border: 1px solid black;
width: 50%;
height: 50px;
background: #fff;
}
<div class="table">
<div class="column">
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="column">
<div class="inner"></div>
<div class="inner"></div>
</div>
<div class="column">
<div class="inner"></div>
</div>
</div>

Related

How to center two divs side by side?

I am using bootstrap and I have two container inside a bootstrap container. Like this:
<div class="container">
<div id="container-map">
aasdasd
</div>
<div id="container-buttons">
asdasda
</div>
</div>
What I am trying to do is center the two divs, #container-map and #container-buttons side by side, inside the main container.
This is my custom CSS for the two divs:
#container-map,
#container-buttons {
display: inline-block;
margin: 0 auto 0 auto;
width: 500px;
height: 500px;
}
Is there a reason you don't want to use the bootstraps built in gridsystem? Something like this?
<div class="container">
<div class="row">
<div class="col-md-3 col-md-offset-3">
<div class="container-map">
asdf
</div>
</div>
<div class="col-md-3">
<div class="container-buttons">
asdf
</div>
</div>
</div>
</div>
Just change your CSS to this
#container-map,
#container-buttons {
float: left;
margin-left: auto;
}
Both containers will be centered and side by side
You can try the code from this example (using text-align: center; on .container display:inline-block; for divs).
<style>
.container {
position:relative;
text-align:center;
}
#dv1, #dv2 {
display:inline-block;
width:100px;
margin:0 3px;
background:#33f;
}
</style>
<div class="container">
<div id="dv1">Div 1</div>
<div id="dv2">Div 2</div>
</div>
you make both your divs to take equal height using flex. You can refer the link to find out the browsers which support it. Have a look at this:
.container {
display: flex;
width: 400px;
background: #eee;
}
.column {
flex: 1;
background: #fff;
border: 1px solid #ccc;
margin: 1px;
}
<div class="container">
<div class="column">
<p>aasdasd</p>
</div>
<div class="column">
<p>asdasda</p>
<p>asdasda</p>
</div>
</div>

CSS: How can I show identical elements in two columns, without spacing between the elements?

I have some HTML code that looks like this:
<div class="container">
<div class="element"> </div>
<div class="element"> </div>
<div class="element"> </div>
<div class="element"> </div>
</div>
And I want to display it in a two-column layout, where each element is displayed directly underneath the one above. I've made a JSFiddle to show my current progress, but I can't figure out how to remove the white gaps between the elements. Is it at all possible, or do i need to change the HTML (I'd rather not)?
An easy way would be to wrap each column items into separate divs. Your .box and .one, .two, .three css declarations are interfering.
[http://jsfiddle.net/grLyvomy/][1]
You could use a seperate div for each column (in your case two).
.container{
border: 1px black solid;
width: 320px;
}
.clear{
clear: both;
}
.leftColumn{
width: 50%;
float: left;
}
.rightColumn{
width: 50%;
float: right;
}
.box:nth-child(2n+1){
background: green;
border-bottom: 1px solid red;
}
.box:nth-child(2n){
background: red;
border-bottom: 1px solid green;
}
.one{ height: 50px; }
.two { height: 80px; }
<div class="container">
<div class="leftColumn">
<div class="box one">first</div>
<div class="box two">second</div>
<div class="box three">third</div>
</div>
<div class="rightColumn">
<div class="box else">first</div>
<div class="box two">second</div>
<div class="box three">third</div>
<div class="box four">fourth</div>
<div class="box one">last</div>
</div>
<div class="clear"></div>
</div>
http://jsfiddle.net/nvmcxjpL/8/
Try to change one and three divs height sum same as two by changing three div height 20px to 30px
.three {
float: left;
width: 50%;
height: 30px;
}

Elastic div between two fixed height/width divs

There are some answers to a similar question already, but this one has a twist.
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-3 grey">
<div class="wrapper">
<div class="info">(i)</div>
<div class="text"><div class="labeled">This is a long text</div></div>
<div class="icon">[$]</div>
</div>
</div>
<div class="col-xs-12 col-sm-9 green">
Content
</div>
</div>
So I need three divs, aligned in one line at all conditions - info, text, icon - with two divs on the sides having fixed h/w, and one in the middle taking only as much space, as
either it needs, and not more
or is available for it, cutting the context with overflow:hidden
Here is the fiddle http://jsfiddle.net/L7tmt5w1/3/
Here are my mad skills in sketching ideas http://imgur.com/tF0HkD2
For those, who want to feel my pain, you may also try re-ordering the divs - text, icon, info - when the screen size goes mobile (bootstrap's col-xs-)
You can use the display: table-cell; method for this situation:
.wrapper {
display: table;
text-align: right;
width: 100%;
}
.info {
width: 20px;
height: 20px;
display: table-cell;
background-color: #005ea8;
color: #fff;
}
.icon {
width: 20px;
height: 20px;
display: table-cell;
background-color: #eb690b;
color: #fff;
}
.text {
display: table-cell;
background-color: #ccc;
width: auto;
}
This mimics the table display properties and keeps all the children of .wrapper inline and the middle one "elastic" as it has no defined width. You can also remove the floats.
http://jsfiddle.net/L7tmt5w1/7/
maybe this solution will help you DEMO
<aside class="panel">
...
</aside>
<div class="content">
...
</div>
.content {
overflow: hidden;
border: 1px solid;
}
.panel {
float: right;
width: 200px;
border: 1px solid;
}
You can try this http://jsfiddle.net/L7tmt5w1/3/
Remember: If you want to float an element to the right, it must be the first element. For example:
<div style="float:right"></div>
<div style="float:left"></div>
AND DIV's are already block elements, so you don't have to add display:block to a DIV-element
I don't know if this is what you want: jsfiddle
if not content on "text" no div... if too much content it's hidden
(but you can add
overflow:auto
to the text div for scroll bars

Create div with two divs inside that need to stay centered

I'm making a web site responsive, and on the home page I should insert two "containers" that should be centered and aligned. (containers in this case are two divs with inside images and text)
I wish they would behave in this way
and when the page is "restricted", the two divs should position itself in this way
I tried like this, but it is not exactly what I would get
<div style="">
<div style="width: 300px;float: left;">
div 1
</div>
<div style="width: 300px;float: left;">
div 2
</div>
</div>
I'd try to use display: inline-block property. In this way you don't have to apply 'overflow' for parent and it's pretty easy to make blocks centered.
HTML:
<div class="wrapper">
<div class="box">Div 1</div>
<div class="box">Div 2</div>
</div>
CSS:
.wrapper {
text-align: center;
/* Just decoration */
border: 1px solid blue;
padding: 20px;
}
.wrapper .box {
display: inline-block;
width: 300px;
height: 50px;
/* Just decoration */
border: 1px solid green;
}
Take a look at the fiddle http://jsfiddle.net/caprella/y4BQ3/
I put something quick together for you. You will have to use media queries to find the size of the page when you want the style to switch. Mess around with my example and you should be able to figure something out to your liking.
<div id="box">
<div class="innerBox">
div 1
</div>
<div class="innerBox">
div 2
</div>
<div class="clear"></div>
</div>
And the CSS...
#box {
width:88%;
background:red;
padding:20px 6%;
}
.clear{clear:both}
.innerBox {
width:41%;
float:left;
background:blue;
display:block;
}
.innerBox:first-child {
margin-right:18%;
}
#media screen and (max-width: 400px) {
#box .innerBox {
float:none;
width:100%;
margin:20px 0 0 0;
}
#box .innerBox:first-child {
margin-top:0;
}
}
}
JsFIddle link: http://jsfiddle.net/x3JLX/
Check out this Fiddle. There's only a few simple changes to your existing code, which I included below.
http://jsfiddle.net/ArKKG/
<div style="overflow:auto; height: 100% text-align: center;">
<div style="width: 300px; height: 50px;float: left;">
div 1
</div>
<div style="width: 300px;height: 50px;float: left;">
div 2
</div>
</div>
And some CSS to make them visible, and keep the borders separated.
div{
border: 1px solid black;
margin: 4px;
}

Fluid Layout Pure CSS

I am trying to create a fluid layout.
Everything is working FF, Chrome, Safari and IE8
This just doesn't work in IE7. I am sure it's a problem with the floated containers.
Tried to do a couple clear fixes, but that didn't seem to work. Just not sure what I am missing, any thoughts or suggestions are appreciated.
If you compare how it renders in IE7 to IE8 a few things I noticed:
The background-color for the first row is the background color of the
container
The bottom border and margin of the container are missing
Here is a live example on jsFiddle
Here's the HTML
<div class="container layout">
<div class="containerContent row">
<div class="group">
<div class="column">
<div class="component">
Player 1:
</div>
</div>
<div class="column">
<div class="component">
<input class="text" type="text"/>
</div>
</div>
</div>
<div class="groupByTwo group">
<div class="column">
<div class="component">
Player 2:
</div>
</div>
<div class="column">
<div class="component">
<input class="text" type="text"/>
</div>
</div>
</div>
<div class="clearFix"></div>
</div>
</div>
Here is the CSS
.container{
margin: 5px;
border: 1px solid #fff;
background-color: #aaa;
}
.containerContent{
margin: 1px;
background-color: #f5f5f5;
}
.group{
float: left;
width: 50%;
overflow: hidden;
}
.column{
float: left;
width: 50%;
overflow: hidden;
}
.component{
padding: 5px;
}
.clearFix{
clear: both;
}
It's definitely a hasLayout issue, like skybondsor said.
The best way I've seen for clearing floats is as such:
.floatParent {
zoom: 1; <-- this is for IE7
}
.floatParent:after { <-- this is for all the good browsers
content: "\0020";
clear: both;
display: block;
}
Then you can remove that anti-semantic clearfix div from the markup, and just have something clean like this:
<div class="floatParent">
<div class="floating">I FLOAT!</div>
<div class="floating">WHOA ME TOO!</div>
</div>
Then it's just a matter of fixing up those pesky form fields, which for some reason always cause layout issues (by dropping on to the next line).