2 divs on same line? - html

I'm trying to get 2 divs on the same line, but alligned differently. Here is a pic of what I have right now and what I want:
[pic removed by poster]
and here is the code I've got:
<div class="newsdiv">
<div class="picdiv" style="background-image: url('[...]');"></div>
<div class="titlediv">
<font size="4">', $row['title'], '</font>
</div>
<div class="textdiv">
<font size="1">This is some dummy text</font>
</div>
<div class="linksdiv">
[Read More...]
<div class="statsdiv">
Views: 0 , Comments: 0
</div>
</div>
</div>
and the stylesheet:
.newsdiv{
overflow: hidden;
height: 126px;
width: 100%;
padding: 5px;
border: 2px solid black;
display: inline-block;
margin-bottom: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.picdiv{
width: 220px;
height: 110px;
border: 1px solid #444;
background-repeat: no-repeat;
background-position: center;
background-size: 382px 120px;
float: left;
}
.titlediv{
height: 20px;
text-align: center;
}
.textdiv{
overflow: hidden;
margin-top: 8px;
height: 70px;
text-align: center;
}
.linksdiv{
font-size: 8pt;
text-align: center;
height: 10px;
}
.statsdiv{
font-size: 7pt;
text-align: right;
display: inline-block;
height: 10px;
}
Any ideas of how to do this? Thanks!

You need float here
.statsdiv {
font-size: 7pt;
text-align: right;
display: inline-block;
height: 10px;
float: right; /* Add this */
}
May be there will be more cleaner solution but you can try this for now
.linksdiv > a{
margin-left:12%;
}
Updated Demo

You can use the position: absolute; on your .statsdiv and position:relative; on .linksdiv to achieve what you want. I guess using float will be too much for this.
Updated CSS(modified classes only):
.linksdiv{
font-size: 8pt;
text-align: center;
height: 10px;
position: relative; /*added*/
}
.statsdiv{
font-size: 7pt;
/* text-align: right; not required */
display: inline-block;
height: 10px;
position: absolute; /*added*/
right: -0.5%; /*added*/
}
FIDDLE

Consider this working css. Here is Demo
.statsdiv {
font-size: 7pt;
text-align: right;
display: inline-block;
height: 10px;
float: right;/*added*/
}

Add this to your .statsdiv css
float:right;
here is the fiddle http://jsfiddle.net/cc68d/
Edit :- Although this works, as pointed out by you , it does not align Read More[..] in center. Actually the issue is with the design part. I will not go into details. You can correct this by adding style="padding-left:100px;" to your ReadMore[..] <a> tag attribute.
as shown here http://jsfiddle.net/cc68d/1/

Replace your total css code, You need float on the class .statsdiv
Your total modified code is:
.newsdiv{
overflow: hidden;
height: 126px;
width: 100%;
padding: 5px;
border: 2px solid black;
display: inline-block;
margin-bottom: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.picdiv{
width: 220px;
height: 110px;
border: 1px solid #444;
background-repeat: no-repeat;
background-position: center;
background-size: 382px 120px;
float: left;
}
.titlediv{
height: 20px;
text-align: center;
}
.textdiv{
overflow: hidden;
margin-top: 8px;
height: 70px;
text-align: center;
}
.linksdiv{
font-size: 8pt;
text-align: center;
height: 10px;
}
.statsdiv {
font-size: 7pt;
text-align: right;
display: inline-block;
height: 10px;
float: right; /* Add this */
}

You could make your div go out of the flow making its position absolute :
.statsdiv{
font-size: 7pt;
text-align: right;
display: inline-block;
height: 10px;
position: absolute;
align: right;
padding: 0 15px;
width: 96%;
left: 0;
}
Needs a little tuning to make it right in all definitions though...

Add a third div with a width of how ever wide you need it with no content between them - a place holder div ...remember to float right , text align and a margin as well.

Related

White Space Following float: left and clear: both

I am using float: left to stack two divs side by side. I am then using clear: block to clear the float, but a small white space appears between the floated divs and the next div.
I have added overflow: none to every element on the page because I saw that as the solution that worked for other people with a similar issue, but that didn't fix the issue.
#featured-container {
position: relative;
width: 100%;
text-align: center;
margin-top: -60px;
}
#featured-header {
display: inline-block;
width: 240px;
height: 30px;
}
#featured-label {
float: left;
width: 160px;
height: 30px;
line-height: 30px;
text-align: center;
background: #EEEEEE;
font-weight: 700;
}
#featured-point {
float: left;
width: 0;
height: 0;
border-bottom: 30px solid #EEEEEE;
border-right: 30px solid transparent;
}
#featured {
display: inline-block;
width: 220px;
min-height: 220px;
padding: 10px;
background: #EEEEEE;
}
.clear {
clear: left;
}
<div id="featured-container">
<div id="featured-header">
<div id="featured-label">FEATURED</div>
<div id="featured-point"></div>
</div>
<div class="clear"></div>
<div id="featured">
</div>
</div>
EDIT: I know I can add a negative margin-top to the '#featured' box, but I would really like to understand why this problem exists.
Try changing the inline-block to inline-flex
#featured-header {
display: inline-flex;
width: 240px;
height: 30px;
}
Set font-size: 0; on the parent element. The space is a character space, so setting the font-size to zero makes the size of the space zero as well. But, you'll need to set the font size of the inline-block child elements back to your desired size.
#featured-container {
position: relative;
width: 100%;
text-align: center;
margin-top: 0px;
font-size:0px;
}
#featured-header {
display: inline-block;
width: 240px;
height: 30px;
}
#featured-label {
float: left;
width: 160px;
height: 30px;
line-height: 30px;
text-align: center;
background: #EEEEEE;
font-weight: 700;
font-size:18px;
}
#featured-point {
float: left;
width: 0;
height: 0;
border-bottom: 30px solid #EEEEEE;
border-right: 30px solid transparent;
}
#featured {
display: inline-block;
width: 220px;
min-height: 220px;
padding: 10px;
background: #EEEEEE;
font-size:16px;
}
.clear {
clear: left;
}
<div id="featured-container">
<div id="featured-header">
<div id="featured-label">FEATURED</div>
<div id="featured-point"></div>
</div>
<div class="clear"></div>
<div id="featured">
</div>
</div>

How to center an image inside an anchor tag?

You can check the outcome here in this link. At the bottom of the page, on the extreme right, there is a circle with an image of a tshirt. The image is not vertically centered properly.
The css of the anchor tag is this:-
.dfa {
padding: 5px 5px;
font-size: 30px;
width: 44px;
text-align: center;
text-decoration: none;
margin: 5px 2px;
border-radius: 50%;
//line-height: 10px;
}
.dfa-tshirt {
background: #2c4762;
color: white;
}
The HTML is this:-
<a href="https://disabilityloverstshirtbuilders.com/" class="dfa dfa-tshirt">
<img src="https://png.icons8.com/color/100/t-shirt.png" style="width:35px; height:35; margin:auto; top:0; right:0; bottom:0; left:0;"/>
</a>
How can I center it? For the time being, I am using inline css for the img, which I will later remove to css file.
I would recommend to just keep it simple, let flex handle it for you. All your margins and padding will exacerbate things when your image changes sizes or other common situations
.dfa-tshirt {
background: #2c4762;
}
a {
display: flex;
justify-content: center;
align-items: center;
border-radius:50%;
width: 44px; height: 44px;
}
a img {
width: 35px; height: 35px;
}
<a href="https://disabilityloverstshirtbuilders.com/" class="dfa-tshirt">
<img src="https://png.icons8.com/color/100/t-shirt.png" />
</a>
EDIT: Non-flex solution --
I can't really plan for every scenario you may have, but to answer your question and support most browsers, I would also recommend just moving the actual styling to the image only:
a img {
width: 30px; height: 30px;
padding: 5px;
border-radius: 50%;
background: #2c4762;
}
<a href="https://disabilityloverstshirtbuilders.com/">
<img src="https://png.icons8.com/color/100/t-shirt.png" />
</a>
Use this:
img { vertical-align: middle; }
.dfa {
padding: 5px 5px;
font-size: 30px;
width: 44px;
text-align: center;
text-decoration: none;
margin: 5px 2px;
border-radius: 50%;
line-height: 10px;
}
.dfa-tshirt {
background: #2c4762;
color: white;
}
img {
vertical-align: middle;
width:35px;
height:35px;
}
<a href="https://disabilityloverstshirtbuilders.com/" class="dfa dfa-tshirt">
<img src="https://png.icons8.com/color/100/t-shirt.png">
</a>
The images parent needs to be displayed inline-block
.dfa {
padding: 5px 5px;
font-size: 30px;
width: 44px;
text-align: center;
text-decoration: none;
margin: 5px 2px;
border-radius: 50%;
display: inline-block;
}
The inline style should just be
<img src="https://png.icons8.com/color/100/t-shirt.png" style="width: 35px; height: 35px;"/>
I have just checked you site url, you can add two lines for the class as bellow.
display: table;
float: right;
.dfa {
padding: 5px 5px;
font-size: 30px;
width: 44px;
text-align: center;
text-decoration: none;
margin: 5px 2px;
border-radius: 50%;
display: table;
float: right;
}
Img tag
<img src="https://png.icons8.com/color/100/t-shirt.png" style="width: 35px; height: 35px;"/>

Vertically align div within div, and

I'm trying to create a div, and then inside the div put the date on the left, and two icons on the right. Both should be vertically centered, have some margin to the edge, and margin between other elements in the parent div. But right now when I run it, the icons end up in strange positions, often clipping out of the div.
.c-project-bar {
width: 355px;
height: 30px;
display: table;
padding: 0px 10px;
background-color: #ffffff;
}
.c-project-date {
display: table-cell;
vertical-align: middle;
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width: 25px;
float: right;
vertical-align: middle;
display: table-cell;
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
https://jsfiddle.net/enyzhxyz/
Edit:
Everyone's answers are awesome, it seemed a major issue was the fact that the was inheriting { margin: 0px; }, and it was throwing a wrench in everyone's solutions. I solved this by including a margin: initial, before using any margin style later, so it first removed { margin: 0px; } then added the one I wanted.
When you give float: right you cannot vertically centre. Instead, use the inline-block technique:
.c-project-bar {
width: 355px;
padding: 0px 10px;
background: #ccf;
}
.c-project-date, .c-project-icons {
display: inline-block;
width: 48%;
vertical-align: middle;
}
.c-project-icons {
text-align: right;
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
display: inline-block;
vertical-align: middle;
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<div class="c-project-icons">
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
</div>
I would have suggested you the transform and position centering technique, but it would be a too much of overkill for this solution. I have given some background colour to see it is perfectly vertical align middle. Although it uses slightly extra markup, this would be the right way that works on browsers that don't support flexbox too.
Here is a solution using css flexbox.
body {
background-color: #696969;
}
.c-project-bar {
display:flex;
align-items: center;
justify-content: space-between;
width: 355px;
height: 30px;
padding: 0px 10px;
background-color: #ffffff;
}
.c-project-date {
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
float: right;
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<div>
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
</div>
Another way to achieve this could be to use transform.
position: relative;
top: 50%;
transform: translateY(-50%);
.c-project-bar {
width: 355px;
height: 30px;
padding: 0px 10px;
background-color: #ccc;
}
.c-project-date {
margin: 0;
position: relative;
top: 50%;
transform: translateY(-50%);
float: left;
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
float: right;
position: relative;
top: 50%;
transform: translateY(-50%);
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
You can do it easily with display: flexbox.
.c-project-bar {
display: flex; // power
align-items: center; // aligning
width: 355px;
height: 30px;
padding: 0px 10px;
background-color: #ffffff;
}
.c-project-date {
margin-right: auto; // let's push other stuff to right side
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
}
Fixed jsfiddle

How to move image with css/html

I am trying to move an image. I Want it to be alligned with the title "Experience". Here is my code
html code
<header>
<h1>Experience</h1>
<div class="logo">
<img src="./img/exp.png">
</div>
</header>
and the css code
.logo{
width: 100px;
}
.logo img{
float: left;
margin-left: 0px 0px 0px 30px;
width: 150px;
height: 38px;
}
Just tweak the CSS a little:
.logo{
width: 100px;
display: inline-flex;
}
.logo img{
margin: auto 5px;
float: left;
width: 150px;
height: 38px;
}
Does this work?
This solution worked for me, but I have no idea for you as you didn't put your full code...
Try adding float:left to .logo and setting display: inline-block on your h1, like so:
*{
margin: 0px;
padding: 0px;
font-family: Arial, Helvetica, Sans-serif;
font-size: 34px
}
header{
background-image: url(../img/bar.png);
background-color: #00B9ED;
height: 75px;
border-bottom: 0px;
padding-left: auto;
padding-right: auto;
width: 100%;
}
.logo{
width: 100px;
float: left;
//display: inline-flex;
}
.logo img{
margin: auto 5px;
float: left;
width: 150px;
height: 38px;
}
h1 {
display: inline-block;
}
How about making margin-left: 0%; and margin-right. Depends where you want to move it.
You used shorthand for margin-left. We use shorthand for margin only. You did margin-left: top right bottom left instead. I don't think the program reads that correctly.

Align image with text

I've been running into an issue trying to vertically center an image and text together. I cannot get the image to align with the text, and even when I think it is centered, changing the browser size ruins it.
.icon-button-with-text-wrapper {
border: 1px solid #7e898a;
background-color: white;
box-sizing: border-box;
display: table;
margin-top: 10px;
padding: 10px;
font-size: 1.5em;
width: 100%;
height: 100%;
}
.icon-button-with-text-wrapper img {
margin-right: 10px;
max-height: 50px;
max-width: 50px;
vertical-align: middle;
float: left;
}
p {
vertical-align: middle;
}
}
<div class="icon-button-with-text-wrapper"><img typeof="foaf:Image" src="http://i.imgur.com/YjcaGSx.png" width="100" height="100" style="
padding-top: 10p;
" align="middle"><p>Speak to an Admissions Counselor<p></div>
I've tried vertical-align: middle, display: table-cell (and others), and different positions, along with adjusting the height, margins, etc. I've also looked at several other similar questions such as this one, this one and this one. However, none of those offered a solution that worked in this instance.
maybe so?
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.icon-button-with-text-wrapper {
border: 1px solid #7e898a;
background-color: white;
display: table;
margin-top: 10px;
padding: 10px;
font-size: 1.5em;
width: 100%;
height: 100%;
}
.icon-button-with-text-wrapper > .item{
display: table-cell;
vertical-align: middle;
}
.icon-button-with-text-wrapper > .item:nth-of-type(1){
width: 60px;
text-align: center;
}
.icon-button-with-text-wrapper img {
max-height: 50px;
max-width: 50px;
}
<div class="icon-button-with-text-wrapper">
<div class="item">
<img src="http://i.imgur.com/YjcaGSx.png" alt="" />
</div>
<div class="item">
<p>Speak to an Admissions Counselor<p>
</div>
</div>
Fiddle
Hope this will help.
<html>
<head>
<title>Flaticon WebFont</title>
<style>
.icon-button-with-text-wrapper {
border: 1px solid #444;
box-sizing: border-box;
padding: 10px;
font-size: 1.0em;
vertical-align: middle;
font-size: 1.3em;
}
.icon-button-with-text-wrapper img {
width: 50px;
height: 50px;
float: left;
vertical-align: middle;
padding: 10px;
}
</style>
</head>
<body>
<div class="icon-button-with-text-wrapper">
<img typeof="foaf:Image" src="http://i.imgur.com/YjcaGSx.png">
<p>Speak to an Admissions Counselor<p>
</div>
</body>
</html>
You don't need to float the image or put the text in its own block (p).
.icon-button-with-text-wrapper {
border: 1px solid #7e898a;
background-color: white;
box-sizing: border-box;
display: table;
margin-top: 10px;
padding: 10px;
font-size: 1.5em;
width: 100%;
height: 100%;
}
.icon-button-with-text-wrapper img {
margin-right: 10px;
max-height: 50px;
max-width: 50px;
vertical-align: middle;
}
<div class="icon-button-with-text-wrapper"><img typeof="foaf:Image" src="http://i.imgur.com/YjcaGSx.png" width="100" height="100">Speak to an Admissions Counselor</div>