Okay, I'm very new to CSS and only minimally familiar with HTML so I'm still kind of fumbling around with both of them. I'm building a practice site and I can't figure out what I'm doing wrong here. My goal is to have the image box to the left of the header and paragraph, but have the title on the same line as the top of the image. Here's what I have:
<img src="" />
<div class="bios">
<h4>First Last</h4>
<p>This is my bio</p>
</div>
Paired with this CSS:
.bios {
height: 100px;
width: auto;
display: inline;
background-color: #a78ffc;
clear: left;
display: inline;
/** top, right, bottom, left **/
margin: 1px 1px 1px 10px;
/** top, right, bottom, left **/
padding: 1px 1px 1px 10px
}
img {
height: 100px;
width: 100px;
float: left;
clear: left;
display: inline;
}
I added the background color to really see what's going on in the preview and I'm more confused than ever. This is how it's displaying:
http://i1126.photobucket.com/albums/l618/spenciecakes/Screen%20Shot%202016-05-13%20at%2010.41.45%20AM_zps50dajzko.png
EDIT
Okay, I've added the additional code as requested and I've added the display: inline to both elements but honestly it all appears the same..
I can't solve your problem with only the code you provided (what's the code for the images?), but I can tell you what's wrong with the current code. First, in order for the width and height property to work, the display property needs to be set to either inline-block or block.
Secondly, the float property does not have a value center. It can only take the values left and right (you need to the first one in this case).
The negative margin trick works like a charm (Explanation in code comments)
.bio {
overflow: hidden; /* Prevent negative margin from leaking out */
}
.bio-inner {
overflow: hidden; /* Clearfix */
margin-left: -1em; /* A) Remove spacing between items when they are against the left side (Must be negative B) */
}
.bio-thumbnail {
height: 3em;
width: auto;
background-color: #a78ffc;
}
.bio-thumbnail,
.bio-info {
float: left;
margin-left: 1em; /* B) Add spacing between items (Must be positive A) */
}
.bio-info-heading {
margin: 0em; /* Just removing default margins */
}
.bio-info-text {
margin-top: 0.5em;
}
<div class="bio">
<div class="bio-inner">
<img class="bio-thumbnail" src="http://i.stack.imgur.com/7bI1Y.jpg">
<div class="bio-info">
<h4 class="bio-info-heading">First Last</h4>
<p class="bio-info-text">This is my bio</p>
</div>
</div>
</div>
This, I have found, works best in cases where screens may be too small to fit the image and text side-by-side.
You can use display: inline-block.
.inline {
display: inline-block;
}
.title {
font-weight: bold;
}
<div>
<div class="inline">
<img src="http://placehold.it/50x50" />
</div>
<div class="inline">
<div class="title">Item 1</div>
<p>Item 1 description</p>
</div>
</div>
<div>
<div class="inline">
<img src="http://placehold.it/50x50" />
</div>
<div class="inline">
<div class="title">Item 2</div>
<p>Item 2 description</p>
</div>
</div>
Related
For my class I have to have 3 divs floated left in a row with the outer two half the size of the middle one. It's driving me crazy that the rows aren't centered on the page. Is there a way to center them without getting rid of the float?
I tried creating a container div with text-align just as a shot in the dark but that didn't work. All other research I've seen is to change the float to display but I have to use float so I can't do that.
div.container {
width: 100%;
text-align: center;
border: none;
background-color: pink;
height: 100%;
}
div.cover {
width: 20%;
}
div.author {
width: 50%;
font-family: calibri;
}
div.links {
width: 20%;
}
<div class="cover">
<p class="inner">
<img src="Images/Divergent.jpg"><br>
</p>
</div>
<div class="author">
<p class="inner" style="margin-top: 10px;">
<b>Divergent<br>Veronica Roth</b><br>
</p>
</div>
<div class="links">
<ul>
<li>
<p class="link" onclick="parent.open('https://www.britannica.com/biography/Veronica-Roth')">
https://www.britannica.com/biography/Veronica-Roth
</p>
</li>
</ul>
</div>
Your instinct to wrap the 3 "columns" in a container div is correct. This allows you to use what is commonly referred to as the "clearfix" trick. Items that are "floated" are ignored by the normal box flow of the page which is why the container seems to collapse and ignore your floating contents.
Frustrating indeed!
This is the "clearfix":
div.container:after {
content: ''; /* no content in this pseudo element */
display: table; /* be 100% wide */
clear: both; /* clear the previous floats */
}
The :after pseudo selector on the container is the same thing as putting an empty div as the last item in the container. By clearing the floats, the container will wrap around the floating items.
This is a hack... but it works! The entire web development community has used this to "fix" the difficulties inherent with using floats for years to create layouts before the advent of real layout systems like Flexbox and CSS Grid.
After the container clears the floating items inside, just set the widths so that they add up to 100% and you are good.
div.container {
width: 100%;
text-align: center;
border: none;
background-color: pink;
height: 100%;
}
div.container:after {
content: '';
display: table;
clear: both;
}
div.cover {
width: 25%;
float: left;
}
div.author {
width: 50%;
font-family: calibri;
float: left;
}
div.links {
width: 25%;
float: left;
}
<div class="container">
<div class="cover">
<p class="inner">
<img src="Images/Divergent.jpg"><br>
</p>
</div>
<div class="author">
<p class="inner" style="margin-top: 10px;">
<b>Divergent<br>Veronica Roth</b><br>
</p>
</div>
<div class="links">
<ul>
<li>
<a href="https://www.britannica.com/biography/Veronica-Roth">
Veronica-Roth
</a>
</li>
</ul>
</div>
</div>
div.container position:relative;
div.cover float:left;
div.autor put inside tag align="center"
div.links float:right;
or
div.autor margin:5%;
or
display:inline-block;
or you can use text-align:center; in css on the parent div and then display:inline-block; on each div inside the parent div
The problem is that I have an image on the left side of my screen and I have a paragraph on the right side of my screen and I have an hr line below the paragraph and the image. So whenever I resize the browser window, the paragraph just goes below the hr line because it gets compressed(squashed) on resizing. I don't want the paragraph to go below the hr line even if I resize the browser window. I want the paragraph to go below the image but not exceeding the hr line. If you want to see what is the problem please check out my website and resize the browser window to see the problem.
Link to website: http://www.greenfield-school.com/AboutUs.html
Thanks
This is the HTML:
<h3><b>About Us</b></h3>
<hr style="border: 2px solid green; width: 88%">
<div class="row2">
<div class="column2" style="background-color:;">
<img class="pic img-responsive" src="Icon.jpeg" style="height: 200px; height: 230px">
</div>
<div class="column" style="background-color:;">
<h5 class="welcome"><b>Mission</b></h5>
<p class="paragraph" style="font-family: book antiqua"><li class="vision1">To care for, respect and encourage all children equally, regardless of gender or ability.</li>
<li class="vision1">To encourage our pupils to develop a sense of self worth, self discipline, personal responsibility and consideration for others.</li>
<li class="vision1">To provide an enjoyable, challenging and purposeful atmosphere for our pupils.</li>
<li class="vision1">To value and encourage the special talents and strengths of pupils.</li></p>
</div>
</div>
And this is the CSS:
.vision1 {
list-style-type: square;
font-family: book-antiqua;
}
.row2{
display: flex;
padding: 2rem 6rem;
}
.column {
flex: 60%;
height: 200px; /* Should be removed. Only for demonstration */
}
.column2{
flex: 0%;
height: 0px; /* Should be removed. Only for demonstration */
}
h3{
padding: 5rem 5rem 0rem;
color: green;
}
Let me offer you an alternative - I can't seem to trigger that auto-adjust on the container height. But this works seamlessly:
This is your HTML:
<div class="container">
<div class="column one"> Column 1 - this is where your picture is</div>
<div class="column two"> Column 2 - this is where your paragraph is</div>
</div>
<hr />
This is CSS:
.container{
width: 100%;
overflow: auto;
height: auto;
clear: both;
display: block;
}
.column{
float: left;
}
.column.one{
//style for column one here
}
.column.two{
//style for column two here
}
hr{
clear: both;
}
This will adjust the height of the container based on on the biggest height of either column one or column two
I have checked this code here CodePen
I have the classic two divs side-by-side problem, which usually I have no problem with (float: left both divs and add a clear:both div after them).
My requirements are making this more complicated to solve...
I would like the left-hand div to occupy, as a column, the left hand side of the containing div (the left hand div will hold a number, ie '1.')
I would like the right-hand div to occupy the remaining space to the right of the left div - and most importantly I would like it NOT to drop below the left-hand div when there is insufficient 'space' for it to fit. Instead, I would like the right-hand div to remain in position and for the text within to WRAP, staying to the right of the left-hand div. Surely this is simple!
I do NOT want to set arbitrary width values because the length of the number in the left-hand div will vary, affecting the distance between the number and the right-hand text.
Here is some example html:
<div class="popup-container"> // set to width: 300px; in css
<div class="popup-text">
<div class="float-left">
<h3>2.<.h3>
</div>
<div class="float-left">
<h3>Example text here, long enough to wrap around<.h3>
</div>
<div class="clear"></div>
</div>
</div>
And the css:
.popup-container {
width: 300px;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.float-left {
float: left;
}
.clear {
clear: both;
}
OK, I think that's about it. If anyone knows how to have the left div operate as a column, against which the text in the right-hand div remains justified left (instead of dropping 'below' the left hand div), that would be swell.
EDIT
Thanks for all the answers. I should have mentioned (!!) it has to work in IE8. I know. But it really does. Big organisation, not updating its machines, unfortunately.
Flexbox and CSS Tables can both do that.
Support
Flexbox is IE10+
CSS Tables are IE8+
FLEXBOX
.popup-container {
width: 300px;
border:1px solid grey;
}
.popup-text {
display: flex;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.left {
flex: 0 0 auto;
background: #c0ffee;
}
.right {
flex:1;
background: yellow;
}
<div class="popup-container">
<div class="popup-text">
<div class="left">
<h3>2.</h3>
</div>
<div class="right">
<h3>Example text here, long enough to wrap around</h3>
</div>
</div>
</div>
CSS Tables
.popup-container {
width: 300px;
border:1px solid grey;
}
.popup-text {
display: table
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.left {
background: #c0ffee;
display: table-cell;
}
.right {
background: yellow;
display: table-cell;
}
<div class="popup-container">
<div class="popup-text">
<div class="left">
<h3>2.</h3>
</div>
<div class="right">
<h3>Example text here, long enough to wrap around</h3>
</div>
</div>
</div>
Use display:flex;
.popup-container {
width: 300px;
}
.popup-container .popup-text {
display: flex;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
.float-left {
float: left;
}
.clear {
clear: both;
}
<div class="popup-container">
<!-- set to width: 300px; in css -->
<div class="popup-text">
<div class="float-left">
<h3>2.</h3>
</div>
<div class="float-left">
<h3>Example text here, long enough to scroll</h3>
</div>
<div class="clear"></div>
</div>
</div>
Here is a solution using display: flex
.popup-container {
width: 300px;
background-color: coral;
}
.popup-text {
display: flex;
}
.popup-text div.two {
flex: 1;
background-color: cornflowerblue;
}
.popup-text h3 {
line-height: 1.25;
padding: 0px 8px 0px 0px;
}
<div class="popup-container">
<!-- set to width: 300px; in css -->
<div class="popup-text">
<div class="one">
<h3>2.</h3>
</div>
<div class="two">
<h3>Example text here, long enough to scroll</h3>
</div>
</div>
</div>
I have a page with 4 <div>'s that have float: left applied to them inside of a container element. The container has background: #ffffff applied to it, but it is not working as I would expect in IE (8, specifically). It works fine in Chrome and FireFox.
I know that if I remove my slider from the container, everything displays as I would expect it. So it has something to do with that, I'm just not sure what it is.
Here is how it should look:
Here is how it is displaying in IE:
CSS:
/* Container */
.main-content {
margin-bottom: 15px;
background: #ffffff;
border: 1px solid #e1e1e1;
}
/* Columns (Wrecked Vehicles, Welcome, Inv Search) */
.col {
float: left;
width: 289px;
padding: 10px;
margin: 5px;
}
/* Slider */
.slider {
float: left;
padding: 10px;
margin: 5px;
}
HTML:
<div class="main-content">
<div class="col n1">
...
</div>
<div class="slider">
...
</div>
<div class="col n2">
...
</div>
<div class="col n3">
...
</div>
<div class="clear"></div>
</div>
Try adding overflow:hidden to the .main_content. The reason the backgorund isn't showing is because, due to all the children floating, the container has zero "real" height.
I am aiming for a setup similar to this:
Unfortunately I end up with this:
Here are my specs:
I'm trying to get divs with an image to be set up without borders, and divs with text to have a 1 px border.
Here are the divs I set up:
<section id="row2">
<div id="textBox1" class="column left">
<p> TEXT BOX 1 </p>
</div> <!--#textBox1 .column.left-->
<div class="column right">
<img src="assets/top-right-image.png"/>
</div>
</section> <!--#row2-->
<section id="row3">
<div class="column left"><img src="assets/bottom-left-image.png"/></div>
<div id="textBox2" class="column right">
<p> TEXT BOX 2 </p>
</div>
</section> <!--#row3-->
As you can see, I set up the text divs with an id "textBox1" and "textBox2". Unfortunately, this blows them up and makes the div.column.left in #row3 to align to the right.
here is the CSS:
.column {
float: left;
position: relative;
margin: 20px 11px;
}
.left {
width: 408px;
}
.right {
width: 449px;
}
#bannerPic {
padding: 0px 15px;
}
#row2 div {
height: 352px;
}
#row3 div {
height: 598px;
}
#textBox1 {
border: 1px solid #BCBCBC;
margin-bottom: 20px;
}
#textBox2 {
border: 1px solid #BCBCBC;
}
Where am I going wrong?
Chances are the top two items are not the exact same height, so the 3rd item, the taller photo, is "hanging" on the first. This happens because of the way float behavior works. Make sure the parts of each row (the divs) are rendered to the exact same height, including all borders, margin, padding, etc.
The other option is to "clear" the section tags. Since part of your content is text, this may be a lot easier. It's probably easier anyway. :)
section { clear: both }
Try adding a style cascade for:
section {
clear: both;
}
to clear out the floats and reset each row to the margin.