2 divs align next to each other with 75% and 25% - html

I need to have two divs next to each other. The left div has a width of 75% and the right 25%. The content of the left div must align left and the right div aligns right.
So this is what I did:
<div class="header_menu">
<div class="links">
Home Contact
</div>
<div class="social_media">
Contact twitter linkedin
</div>
</div>
The css:
.header_menu{
border-bottom:1px solid #CCC;
}
.links{
width:75%;
display:inline;
}
.social_media{
width:25%;
display:inline;
}
I tried to add a float:left; and float:right; but then the border isn't placed at the bottom anymore.....???
M.

Take a look into this jsfiddle that should work for you:
https://jsfiddle.net/cmkgn4fg/4/
HTML-Code:
<div class="header_menu">
<div class="links">
Home Contact
</div>
<div class="social_media">
Contact twitter linkedin
</div>
<div class="clearfix"></div>
</div>
CSS-Code:
.header_menu{
border-bottom:1px solid #CCC;
width:100%;
}
.links{
width:75%;
float:left;
text-align:left;
}
.social_media{
width:25%;
float:left;
text-align:right;
}
.clearfix{
clear:both;
}

You were almost there. inline-block is the one to use.
As inline elements has a white space, which will make them slightly bigger than 75% / 25% and therefore break them into 2 lines as they would exceed 100%, margin-right: -4px; is one way to fix that and make them both stay on 1 line.
Note, the -4px is based on the set font and might need to be adjusted, so here are a more options:
How do I remove the space between inline-block elements?
Stack snippet
.header_menu{
border-bottom:1px solid #CCC;
}
.links{
display: inline-block;
margin-right: -4px;
width:75%;
text-align: left;
}
.social_media{
display: inline-block;
margin-right: -4px;
width:25%;
text-align: right;
}
<div class="header_menu">
<div class="links">
Home Contact
</div>
<div class="social_media">
Contact twitter linkedin
</div>
</div>

Inline elements don't respond to width and height styles, which is why you are running into this issue.
Remember when floating divs, you will need a clearfix. You can read about clearfixes here
<div class="header_menu clearfix">
<div class="links">
Home Contact
</div>
<div class="social_media">
Contact twitter linkedin
</div>
</div>
Then your CSS.
.header_menu{
border-bottom:1px solid #CCC;
}
.links{
width:75%;
float:left;
}
.social_media{
width:25%;
float:right;
}

The rule display: inline; ignores height and width, as the element is now an inline level element (as in it should be treated as part of text/content) but with the display properties table and table-cell you can achieve the layout you require:
<div class="header_menu">
<div class="links">
Home Contact
</div>
<div class="social_media">
Contact twitter linkedin
</div>
</div>
And the CSS:
.header_menu{
border-bottom:1px solid #CCC;
display: table;
}
.links{
width:75%;
displaY: table-cell;
}
.social_media{
width:25%;
display:table-cell;
}
This forces table-like behavior onto the elements but also keeps the styling options of the HTML elements you're using.
Example on jsFiddle.

try this :
<div class="header_menu"
<div class="links">
Home Contact
</div>
<div class="social_media clear">
Contact twitter linkedin
</div>
</div>
and in your css file :
.header_menu{
border-bottom:1px solid #CCC;
}
.links{
width:75%;
display:inline-block;
float: left;
clear: both;
background-color: red;
}
.social_media{
width:25%;
float: right;
display:inline-block;
background-color: orange;
}
the colors bck is for you to see the layout clearly ! hope this would help

Related

CSS problem with centering text when float:left is used

I would like to make my text centered, as you can see below:
I have tried several approaches, but nothing was working.
Aligning a float:left div to center?
It looks like the float:left is needed, when we want to have two divs side by side
Two divs side by side - Fluid display
This stuff is working, but I need the text on the left centered in the middle of the box show.
My code looks as follows:
<figure class="linkbox">
<a class="linkurl" href="http://www.deckchair.com/" target="blank">
<div class="links">Deckchair</div>
<div class="desc">Text content
</div>
<div style="clear:both"></div>
</a>
</figure>
CSS
.links {
display:flex;
flex-wrap:wrap;
justify-content:center;
align-items:center;
float:left;
font-weight:bold;
font-size:large;
min-width:100px;
}
.desc {
display:grid;
text-align:justify;
margin-right: 5px;
margin-left: 20%;
}
Is there any option to center the text on the left with retaining the responsiveness of the page?
First, I would recommend not to wrap multiple div into an anchor tag.
here is my code and a fiddle:
https://jsfiddle.net/nwxvzs1f/2/
<figure class="linkbox">
<a class="linkurl" href="http://www.deckchair.com/" target="blank"></a>
<div class="links">Deckchair</div>
<div class="desc">Text content
</div>
<div style="clear:both"></div>
</figure>
.links {
display:flex;
flex-wrap:wrap;
justify-content:center;
align-items:center;
float:left;
font-weight:bold;
font-size:large;
min-width:100px;
width:50%;
background-color: grey;
height:100%;
}
.desc {
display:grid;
text-align:justify;
margin-right: 5px;
margin-left: 20%;
width:50%;
}
.linkbox {
height:300px;
display:block;
background-color: red;
}
if this was not you are searching for, you are welcome to enhance your question.

Having trouble placing 2 divs side by side in wrapper

I'm having trouble putting 2 divs side by side within a wrapper. I've read existing questions and articles on how to place 2 divs side by side; it seems very simple, just define width and float:left for both divs. However, I can't get it to work!
Any help would be appreciated, thank you! :)
Here is the JSFiddle: https://jsfiddle.net/Toppoki/7pazLwLs/23/
HTML:
<div class="child1">
<div class="wrapper">
<div class="blurb">
</div>
<div class="form">
</div>
</div>
</div>
CSS:
.child1 {
background:#082a46;
margin:0;
}
.wrapper {
width:970px;
margin: 0 auto;
}
.blurb {
color: #fff;
width:200px;
height:400px;
float:left;
}
.form{
background-color:#9c0b0e;
width:100px;
height:400px;
float:left;
}
It's already working for the snippet you showed. I just put a background color on the div.form so you could see.
In your example on jsfiddle the div.blurb lacks the float:left, and there is a lot of things that can get you confused.
Start taking off some of the placeholder text and unnecessary elements and styles. Start making it very simple, indent it well, and add the styles one at a time. It will eventually work.
.child1 {
background:#082a46;
margin:0;
}
.wrapper {
border: 1px solid #ccc;
width:970px;
margin: 0 auto;
}
.blurb {
color: #fff;
width:200px;
background-color: blue;
height:400px;
float:left;
}
.form{
background-color:#9c0b0e;
width:100px;
height:400px;
float:left;
}
<div class="child1">
<div class="wrapper">
<div class="blurb">
</div>
<div class="form">
</div>
</div>
</div>
You can also place 2 divs side by side using display:inline-block on the two divs.
(If you want it responsive, define the width of the child with % and not pixels.)
.child1 {
background:#082a46;
}
.wrapper {
border: 1px solid #ccc;
}
.blurb {
color: #fff;
background-color: blue;
width:200px;
height:400px;
display:inline-block;
}
.form{
background-color:#9c0b0e;
width:100px;
height:400px;
display:inline-block;
}
<div class="child1">
<div class="wrapper">
<div class="blurb"></div>
<div class="form"></div>
</div>
</div>

inline element does not accept margin-top

I am trying to give magins to inline elements (image thumbnails for a photo gallery). But it seems margin-top is ignored for my elements.
Markup is
<div id="row1-left">
<div id="gallerypreview">
<img id="#previewImg" alt="preview for image" src="gallery/autumn1.jpg">
</div>
<div id="gallerythumbs">
<div id="gallerythumbsInner">
<div class="gallerythumb">
<img src="gallery/autumn1.jpg">
</div>
<div class="gallerythumb">
<img src="gallery/autumn2.jpg">
</div>
<div class="gallerythumb">
<img src="gallery/autumn3.jpg">
</div>
</div>
</div>
</div>
Style is:
#row1-left{
width: 460px;
height: 310px;
float: right;
margin: 15px 15px 0px;
}
#imggallery{
width:450px;
height:300px;
margin:5px;
}
#gallerypreview{
width:450px;
height:200px;
margin:2px 5px;
border-radius:20px;
background-color:#E7E7E7;
}
div#gallerypreview>img{
margin:1px 25px;
width:400px;
height:198px;
}
div#gallerythumbs{
margin:5px 5px;
width:450px;
height:90px;
background-color:#E7E7E7;
border-radius:5px;
}
#gallerythumbs .gallerythumb{
display:inline;
width:140px;
height:86px;
margin:5px 5px;
}
div.gallerythumb>img{
width:138px;
height:76px;
}
According to some old posts on SO, margin-top is not applied to inline non-replaced elements. My questions is if there is any hack to get this done, for example, for my inline image thumbnails that are to be space from their top parent element?
Inline elements and margins is a hot topic because of its unusual activity. Some people use padding to overcome this problem.
.....
Another way is to use display:table; instead of display:inline;
best way is to....
use css styling like this
div{
position:relative;
top:-2px;
}
this brings the div 2 pixels down.
display: inline; Do not respect top and bottom margins ...

How to float TWO Anchor links, align text horizontally and vertically?

Here's the image of what I'm trying to achieve for better explanation
What I'm dealing with is:
There are 2 anchor links floating with each other.
The two anchors content or number of words inside of it varies. The
anchor is the next and previous post of my wordpress blog. Please
refer to
http://codex.wordpress.org/Function_Reference/previous_post_link and
and http://codex.wordpress.org/Function_Reference/next_post_link for
reference.
Both have equal size (height and width). The height automatically adjust depending on the content of the other anchor.
There's a border line in the middle.
Text should be vertically and horizontally aligned.
You can see the live example of what I'm trying to copy for better explanation.
Here's my code and fiddle
<div class="holdmetight">
<div class="prev">TITLE OF PREVIOUS POST HERE</div>
<div class="next">TITLE OF NEXT POST HERE</div>
</div>
.holdmetight {width:100%; max-width:1023px; position:relative; display:block; }
.next, .prev { min-height:35px; height:100%; text-align:center; height:100px;}
.prev { display:block; background:#CCC; width: 100% auto; }
.next {float:left; border-right:1px solid #eceff0; background: #AAA;}
Here is a basic layout with display: table.
Benefits of display: table:
Easily vertically center the text with vertical-align: middle
Both divs maintain the same height based on the others content
Have an example!
HTML / CSS
.holdmetight {
display: table;
border: solid 1px #000;
border-top: solid 2px #000;
}
/* the > selector targets direct div children of .holdmetight */
.holdmetight > div {
display: table-cell;
vertical-align: middle;
background: #aaa;
padding: 10px;
text-align: center;
width: 50%;
}
.prev {
border-right: solid 1px #000;
}
<div class="holdmetight">
<div class="prev">
<a href="/">
TITLE OF PREVIOUS POST HERE.
This is a particulary long title
that will push both divs down.
</a>
</div>
<div class="next">
<a href="/">
TITLE OF NEXT POST HERE
</a>
</div>
</div>
DEMO
.holdmetight {width:100%; max-width:1023px; position:relative; display:block; }
.next, .prev { min-height:35px; height:100%; text-align:center; height:100px; display:inline-block;}
.prev { background:#CCC; width: 100% auto; }
.next { border-right:1px solid #eceff0; background: #AAA;}
add padding according to need

Fluid layout not fluid and one block drops down

http://jsfiddle.net/TomasRR/WuNL3/1/
Code works perfectly when there is no p's and h's in the divs. Once I put some text everything goes out of order.
<div class="cont">
<div class="left">
</div>
<div class="right">
</div>
</div> <!-- .cont -->
css
body {
width:100%;
margin:0;
padding:0;
}
.cont {
white-space:nowrap;
width:100%;
}
.left {
border:1px solid red;
width:50%;
height:200px;
display: inline-block;
}
.right {
border:1px solid black;
width:50%;
height:200px;
display: inline-block;
}
#media only screen and (max-width: 800px) {
.left, .right {
width: 400px;
}
}
when the text is added with p's and h's the html looks like that
<div class="cont">
<div class="left">
<h1>Programming and fuss</h1>
<h2><em>by Tomas R. </em></h2>
<p>MY TOP 3 PAGES:</p>
<a href="http://www.twitter.com/" target="_blank" title="it is twitter" />TWITTER</a>
WIKIPEDIA
VICE
</div>
<div class="right">
<p>"An ounce of practice is generally worth more than a ton of theory." <span>E. F. Schumacher.</span></p
</div>
It's better to look at fiddle... What I want ? I want blocks to stay in one line kind of, not to drop down.
Add vertical-align: top; to your .left style