Create one div with text and image separate? - css

I am currently coding my first website, and one of the elements I have are "story blocks" that are a fixed width and split between images and text, but all in one div. Here is an image for reference: http://i.imgur.com/FAbi4xF.jpg?1.
Let me explain the different parts going on here: an image with a slight black overlay as well as text on the left, and then text describing the story on the right. Currently, I have an inefficient way of creating this element, involving two separate divs and disparate headers for the story titles. Here is the HTML:
<div class="story-image">
<h2 class="story-head">STORY TITLE</h2>
<img src="http://gearpatrol.com/wp-content/uploads/2012/06/microsoft-surface-gear-patrol1.jpg"/>
</div>
<!--this ends the image and title part, begins the text part-->
<div class="story-text">
<h5>Story description.</h5>
</div>
And the CSS:
.story-image {
position: inherit;
background-color: rgba(0,0,0,.39);
margin-left: 58px;
margin-bottom: 4px;
float: left;
overflow: hidden;
width: 220px;
height: 100px;}
.story-head {
position: absolute;
text-align: center;
vertical-align: middle;
color: white;
font-weight: 800;
width:220px;
line-height:100px;}
.story-text {
background-color: white;
width: 200px;
line-height: 100px;
height:100px;
float: left;}
.story-text h5 {
padding-left:8px;
padding-top: 20px;
vertical-align: middle; }
As you can see, it isn't the most efficient way of doing this sort of thing and can cause issues where the two elements break away from eachother (example here: http://i.imgur.com/eBF8Rwa.png). I cannot figure out a way to have one single div that is divided between image and text. Does anyone know of a possible way to do this? Thank you and happy holidays!

Try this:
CSS
.container {
width:400px;
display:inline-block;
height:80px;
margin-right:10px;
padding:0;
}
.left {
display:table-cell;
vertical-align:middle;
width:120px;
margin:0;
padding:0;
height:80px;
background-image:url(URL);
background-size:100%;
}
.right {
display:table-cell;
vertical-align:middle;
width:280px;
padding:0 10px;
}
HTML
<div class="container">
<div class="left">tjen<br>a</div>
<div class="right">tjen<br>a</div>
</div>
JSFiddle demo.
The problem with the two elements becoming break appart in some situations should be gone, and you can change vertical-align:middle to top or bottom if you want the text to have a different position in the div. Hope this helps!

Related

Why can't I position my image and search bar on the right side of the nav bar?

Inside my nav bar I had a search field positioned exactly where I wanted in the right corner using float: right;
The problem is that I tried to add a profile picture to the left of the search bar (also using float:right), but not only did that not position the profile picture in the upper right corner, but it also knocked my search bar visually outside of the nav bar (below it).
I'm guessing I can't use float: right; for both of these elements, so my question is how what can I do with div nav (profile pic is nested inside) and div head-search (search bar) in order to position both of these in the top right corner?
http://codepen.io/donnaloia/pen/hlEjs
nav {
float: right;
width:13em;
margin:10px auto;
background:#f6f6f6;
border-radius: 50px;
box-shadow: 0 0 4px rgba(0,0,0,.15);
}
.user-avatar img{
border-radius: 50px;
float:right;
margin-left:200px;
margin-top:1px;
width: 30px;
height: auto;
}
#head-search {float:right; width:350px;}
#head-search span {float:right; color: #008000; font:normal 12px/46px 'Strait',sans-serif;}
If I get you, you want your nav and search bar to be aligned right? Simply create a container div and float the container.
div id="navsb-container">
div id="nav"></div>
div id="sb"></div>
</div>
#navsb-container{
clear:both;
float:right;
width:80%;
display:block;
}
#nav{
float:left;
display:inline-block;
position:relative;
width:39%;
}
#sb{
float:right;
display:inline-block;
position:relative;
width:49%;
}
Your search bar is pushed out because the width of the container is not wide enough to contain it.
Let's break this down to a simple example.
Here is a simple (broken) layout. "Run code snippet" to see the results:
header {
height: 100px;
background: #000;
padding: 50px;
font-size: 2em;
}
nav,
.search {
height: 100px;
width: 220px;
background: #F00;
float: right;
margin: 0 5px;
}
<header>
<img src="http://www.placehold.it/100" />
<nav>Nav</nav>
<div class="search">Search</div>
</header>
2 problems with this example:
The search should be on the far right, but here is shown before the nav
When the header gets to small, the search breaks its layout and is displayed underneath the nav
The simplest solution:
Switch the order of the search and nav in your HTML to get the correct position
Place a min-width on the header to prevent it from getting too small
In your Codepen example you have #media. Use this to control what happens when the viewport goes past its min-width.
Looks better:
header {
height: 100px;
background: #000;
padding: 50px;
font-size: 2em;
min-width: 600px;
}
nav,
.search {
height: 100px;
width: 220px;
background: #F00;
float: right;
margin: 0 5px;
}
<header>
<img src="http://www.placehold.it/100" />
<div class="search">Search</div>
<nav>Nav</nav>
</header>

Make div size as per contents and horizontally center inside it's parent

I have a div message which basically has a text and a link. I want its size to be changing based on the string inside it. Also I want this message div to be centered inside its container.
I have been playing with it for a while without much luck. Here is the link to JSfiddle: http://jsfiddle.net/pDYJ8/
Also I don't know how make that text and link appear one after other ( not on the new line )
Here is the code:
<div class="container">
<div class="message">
<p>do you want to </p>
<a href="somewhere" target="_blank">
buy this product
</a>
</div>
</div>
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
}
.message {
margin-right: auto;
margin-left: auto;
background: #ddd;
width:100px;
}
Tried display inline block to fit its content but then it wouldn't center it inside its parent.
Keeping width 100px for now just to mock my requirements
Just Tweak Some CSS
See the demo fiddle.
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
text-align: center; /*added*/
}
.container .message {
display: inline-block; /*added*/
text-align: left; /*added*/
background: #ddd;
}
.message p { /*added*/
display: inline-block;
}
Explanation
The text-align center causes the now inline-block display of .message to center, which is then reset to have its own text-align back at left (this is not necessary). To get the a on the same line, the p also needs to be some type of inline display, here I chose inline-block as well..
I think you are over complicating things. All you need is a text-align: centeron the container and a display: inline-block on the message:
.container {
background: #666;
font-size: 15px;
color: #e47911;
text-align: center;
}
.container .message {
background: #ddd;
display: inline-block;
}
http://jsfiddle.net/Pevara/pDYJ8/9/
The inline block makes the div act as a word inside text, and the text-align center makes the 'word' align to the center...
Here is a simplified approach to a couple of the answers given. It reduces the amount of HTML and CSS needed.
CSS
.container {
color: #e47911;
text-align: center;
}
.message {
display: inline;
background: #DDDDDD;
}
HTML
<div class="container">
<p class="message">
Do you want to buy this product?
</p>
</div>
I would definitely put your anchor tag, <a> inside the paragraph tag, <p>.
You could even remove display: inline; from .message if you made it a <span> rather than a <p>.
Check this out:
http://jsfiddle.net/pDYJ8/10/
Changes made to above link
.container .message {
margin: 0 auto;
width:auto;
}
span{
background: #ddd;
display:inline;
}
You can simplify it with display: table; and margin: 0 auto;
.container {
display: table;
margin: 0 auto;
background-color: #DDD;
}
<div class="container">
do you want to buy this product
</div>

Centering two divs in a div: one of fixed width and the other of variable width/height

I have a situation where I have one div of fixed width, containing an image pulled from Twitter, and another div of variable width containing user text of variable length. What I want to achieve is something like the following:
I can do this well enough with a single div that has background-image and padding-left. But I want to be able to apply border-radius to the img element, which simply won't be possible with a background-image.
If I do text-align: center on the outer div, it gets me halfway there. Here's a DEMO and a screenshot:
But this obviously isn't fully what I want.
How can I accomplish this?
Ask and you shall receive — a simplified jsFiddle example:
As an added bonus, the text is vertically centered too!
HTML:
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png" />
</div>
<div class="logo-name">
AppSumo is a really really long title that continues down the page
</div>
</div>
CSS:
.logo {
background-color: #eee;
display: table-cell;
height: 100px;
position: relative;
text-align: center;
vertical-align: middle;
width: 600px;
}
.logo-container {
background-color: #fff;
border-radius: 10px;
left: 10px;
position: absolute;
top: 10px;
width: 75px;
}
.logo-name {
font: bold 28px/115% Helvetica, Arial, sans-serif;
padding-left: 85px;
}
Would it be something like this?
http://jsfiddle.net/uPPTM/6/
.logo {
width:80%;
margin:auto;
background-color: red;
}
.logo-container {
border: 1px solid gold;
width:73px;
height: 73px;
display: inline-block;
vertical-align:middle;
}
.logo-name {
display: inline-block;
}
You can float the image container (or image itself without the container) to the left, clearing anything the left... and then float the text to the left, clearing anything to the right.
.logo-container{
float:left;
clear:left;
}
.logo-name{
float:left;
clear:right;
}
You can adjust the distance of the text using margins.
.logo-name{
float:left;
clear:right;
margin-top:10px;
margin-left:5px;
}
Use absolute positioning with a left position to push the title text past the image.
http://jsfiddle.net/uPPTM/9/
.logo { width: 50px; }
.title {
position: absolute;
top: 0;
left: 50px;
font-size: 32px;
text-align: center;
}
img {
border: 1px solid gray;
border-radius: 15px;
}
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png">
</div>
<div class="logo-name">AppSumo</div>
</div>

CSS: problem with Float property

Here is my problem with CSS when I use the Float property
My picture: http://www.sourimage.com/show-image.php?id=fb748238bf7e4ab12001e64cb543066b
It does not look good because having many blank space among the block.
My CSS code:
.listcol{
width:180px;
float:left;
margin-right:5px;
background-color:#eceff1;
margin-top: 1px;
min-height:200px;
background-image: url(../images/colbg_btm.gif);
background-repeat: no-repeat;
background-position: bottom;
margin-bottom: 0px;
}
.listcol ul{
margin-left: 0px;
padding-left: 0px;
padding-top: 0px;
margin-top: 0px;
list-style-type: none;
}
Please take a look to review and help me to correct no any blank space on the screen!
Thanks so much!
Can't see the picture, but maybe if you express the width as a percentage of the width of the page...Instead of 180px, maybe
width: 80%;
..using whatever percentage looks best to you.
I think we need to see your html markup to be sure, but what you need to do is have a structure where block1 and block3 are in a container block. The container can float, and block2 can float. Block1 and block3 do not need to float.
can you post some of the html? Or fix the image? It looks like you took care of the left margin for your UL, but there may some extra margin lying around in some of your other block elements since they tend to come with built in margin.
i also agree with Robert Harvey. Try to use percentages where reasonable instead of pixels. it'll be more consistent across any changes the user or browser makes to screen size, font size, etc.
are block1, 2, and 3 all UL's in the listcol div?
I think u can solve your problem by giving line-height to li as "0px".
ul li {
float:left;
line-height:0px;
}
ul {
width:124px;
}
<ul>
<li><img src="images.jpeg" /></li>
<li><img src="images.jpeg" /></li>
</ul>
The space can't be avoided in current solution because it's the height of the block2 that's "pushing" down block3 when it's floated to to left.
So you will probably need a completelly different html layout to solve your request...
Please show us som more samples of how you want the page to look with more li elements of different heights.
Huynh:
You need to wrap a div around each of your columns. That should allow the boxes to flow from top to bottom within their own div, and then you can float each of the div columns. Give each div a width of 25%. You might also want to wrap the whole thing in a div, and give it a width of 100%.
I try to work around on your ideas but It's not fine.
Also, I put my HTML as below:
#content02{
width:740px;
float:left;
margin-left: 10px;
}
.wrapper{
width:760px;
margin-left:auto;
margin-right:auto;
}
.main02{
width:760px;
float:left;
background-color:#FFFFFF;
padding-bottom:20px;
padding-top: 10px;
}
.listcol{
width:180px;
float:left;
--position: absolute;
margin-right:5px;
background-color:#eceff1;
margin-top: 1px;
min-height:200px;
margin-bottom: 0px;
}
.listcol ul{
margin-left: 0px;
padding-left: 0px;
padding-top: 0px;
margin-top: 0px;
list-style-type: none;
}
.listheader{
display: block;
margin-top: 0px;
background-color: #2F404A;
color: #FFFFFF;
font-weight: bold;
font-size: 10px;
width: 180px;
padding-top: 5px;
padding-bottom: 5px;
background-position: top;
text-align: center;
}
<div class="wrapper">
<div class="main02">
<div id="content02">
<div class="listcol">
<ul>
</ul>
</div>
<div class="listcol">
<ul>
</ul>
.....
.....
</div>
Please to note that the number of block increasing dynamically and use the same style "listcol". You can see the picture depicting this issue here: http://www.sourimage.com/show-image.php?id=fb748238bf7e4ab12001e64cb543066b
Please try getting rid of "min-height" in the '.listcol'. Also, get rid og the "position: absolute" in the '.listcol'.
Let me know if that worked.
If it doesn't, you can try to modify your HTML to be in columns, so for example:
<div class="column">
<div class="box"></div>
<div class="box"></div>
</div>
<div class="column">
<div class="box"></div>
<div class="box"></div>
</div>
And the CSS:
.column{
float: left;
width: 200px;
margin-right: 5px;
}
.box{
margin: 0px;
}

css and div tag layout problems

I have a header bar that spans horizontally across my web page, which is comprised of one div tag and three nested div tags.
HTML:
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="rightTop">
RIGHT
</div>
<div id="centerTop">
CENTER
</div>
</div>
CSS:
#top-bar
{
margin: 0;
padding: 1px 4px;
font-size: x-small;
background-color: #005555;
font-family: Arial;
}
#top-bar .separator
{
padding: 0 7px;
border-right: 0px solid #fff;
border-left: 0px solid #fff;
}
#leftTop
{
display: inline;
float: left;
}
#rightTop
{
display: inline;
float: right;
}
#centerTop
{
color: #ffffff;
text-align: center;
}
And it works just great, except for the fact that the div tags are out of order in the HTML code, which I don't like. If I order the div tags by placing them Left, Center, and Right, in the HTML, then the Right div just disappears from the webpage! I'm guessing that it has something to do with the float and text-align attributes having a conflict.
Anyone have any ideas on what is going on here, or is there an easier way to do this in CSS?
Try float: left; on #centerTop or display: inline on all three without any floats.
This works fine, but it depends on what you need. If you dont know the height of the content and you want it to expand dynamicly, then this is not enough:
#leftTop
{
float: left;
}
#rightTop
{
float: right;
}
#centerTop
{
float:left;
text-align: center;
}
I just tested the code from the original post in Firefox 3.0.10, Opera 9.64, IE8 and Google Chrome 2.0.181.1
All browsers showed all 3 divs, not a single div fell off the screen... Are you perhaps using IE6?
I am running your HTML and CSS of FF 3.0.10.
When you re-arrange the CENTERTOP div to be between the LEFTOP and RIGHTTOP divs, the RIGHTTOP div doesn't fall 'off the page' but the "RIGHT" text just falls off onto the next line.
My solution is proposed below (you'll notice I have some additions and some best-practice techniques).
HTML CODE:
<html>
<head>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
</div>
<div class="clearer">
</div>
<div id="randomContent">
RANDOM CONTENT
</div>
</body>
CSS CODE:
#top-bar {
margin: 0;
font-family: Arial;
}
#leftTop {
float: left;
width: 20%;
border: 1px solid red;
}
#centerTop {
float: left;
width: 20%;
border: 1px solid blue;
}
#rightTop {
border: 1px solid green;
}
.clearer {
clear: both;
}
#randomContent {
background-color: yellow;
}
So you'll notice in the HTML that the divs are arranged in order from LEFT to CENTRE to RIGHT. In this CSS, this has been reflected by floating the LEFTTOP and CENTRETOP divs left. You will also notice that I have specified a width property on the LEFTTOP and the CENTERTOP divs, to enable you to space out your divs as wide as you want. (You'll be able to visually see your width modifications as I've added in a border on the divs). No width percentage property has been applied on the RIGHTTOP div as it will consume the remaining 60% of the width (after the LEFTTOP and CENTRETOP have consumed the 40%).
I have also added a CLEARER div. Think of the CLEARER div is a horizontal line break. Essentially it acts as a line of demarcations to separate the floated divs from the content below.
You can then add whatever content you want in the RANDOMCONTENT div.
Hope this helps :)
I don't know that it disappears, but it would drop down a line. Lot's of websites put it out of order for that reason (I know I do).
Another alternative:
#top-bar
{
margin: 0;
padding: 1px 4px;
font-size: x-small;
background-color: #005555;
font-family: Arial;
}
#top-bar .separator
{
padding: 0 7px;
border-right: 0px solid #fff;
border-left: 0px solid #fff;
}
#top-bar>div
{
float: left;
width: 33%;
}
#rightTop
{
text-align: right;
}
#centerTop
{
color: #ffffff;
text-align: center;
width: 34%;
}
And then put <br style="clear:both"/> right before you close your top-bar div.
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
<br style="clear:both"/>
</div>
Not sure if you want the width's defined like this, however.
Another solution:
Set the leftTop, centerTop, and rightTop to display:table-cell,
Set the top-bar to display:table-row,
Set a container to display:table
Set the width of the container and row (#table-bar) to 100%;
Set the width of the columns to the desired ratios (e.g., 25% for left and right, 50% for center)
caveat: table, table-row, and table-cell css display values do not work in IE 5.5 or 6 (and maybe Opera 8); but they do work nicely in all contemporary browsers. IE conditionals can be used to split code for IE > 5 and IE < 7.
TEST:
<html>
<head>
<title>3 Column Header Test</title>
<style type="text/css">
body#abod {
background-color:#F5ECBD;
color:#000;
}
#hdrrow {
margin:0;
padding:0;
width:100%;
border:1px solid #0C5E8D;
display:table;
}
#top-bar {
margin:0;
padding:1px 4px;
width:100%;
font-size:100%;
background-color:orange;/*#005555;*/
font-family: Arial;
border:1px solid #000;
display:table-row;
}
#leftTop {
margin:0;
padding:0 16px;
width:24%;
text-align:left;
color:#000;
background-color:#F0DD80;
border:1px dashed #f00;
display:table-cell;
}
#centerTop {
margin:0;
padding:0 16px;
width:40%;
margin:0 auto;
text-align:center;
color:#000;
background-color:#F5ECBD;
border:1px dashed #f00;
display:table-cell;
}
#rightTop {
margin:0;
padding:0 16px;
width:24%;
text-align:right;
color:#000;
background-color:/*#F0DD80;*/transparent;
/*shows the orange row color*/
border:1px dashed #f00;
display:table-cell;
}
#footer {
padding:25px;
color:#000;
background-color:#F5ECBD;
}
</style>
</head>
<body id="abod">
<div id="hdrrow">
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
</div>
</div>
<h4 id="footer">Footer Lorem ipsum dolor sit amet</h4>
</body>
</html>
Use relative positioning to swap the positions of the divs after they have been floated:
The HTML
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
</div>
The CSS
#leftTop {
width:33%;
float:left;
}
#centerTop {
width:33%;
float:right;
position:relative;
right:33%;
}
#rightTop {
width:33%;
float:right;
position:relative;
left:33%;
}
I use the same process in my Perfect Liquid Layouts to change the column source ordering.

Resources