Text not aligning next to image inside box - html

This is the html for my box with the title text and image inside it.
<div id="about">
<div id="title"> <h3><b>About</b></h3></div>
<div id="text"><p>Text</p></div>
<div id="img"><img src="img/3.jpg" height="300" width= "400" alt="?">
</div>
and this is the css
#about {
color: white;
padding: 10px;
position: relative;
width: 90%;
height: 325px;
background: lightgrey;
top: 30px;
margin: 0 auto;
overflow: auto;
color: white;
background: #262626;
box-sizing: border-box;
}
#text {
width: 720px;
position: relative;
top: 30px;
float: right;
}
#title {
float: right;
position: relative;
right: 725px;
top: 0px;
}
#img {
}
My problem is that because my title is always 725px to the right, if I had a title larger than 5 letters it isnt right next to the picture or else I'll have to position it again, is there an easier way around this? Because doing it manually is frustrating.
Thanks.

You can solve this much easier using flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Put the title and text in a own wrapper .text-wrapper and place the img before the wrapper.
On your parent about use follow property, to place them each to other:
.about {
display: flex;
}
Some refactoring: You don't really ned a div around the image for this case. And I recommend you to use class in case of id. Check out the snippet:
.about {
display: flex;
}
.text-wrapper {
padding-left: 20px;
}
<div class="about">
<img src="https://www.telegraph.co.uk/content/dam/news/2016/08/23/106598324PandawaveNEWS_trans_NvBQzQNjv4Bqeo_i_u9APj8RuoebjoAHt0k9u7HhRJvuo-ZLenGRumA.jpg?imwidth=450" height="300" width="400" alt="?">
<div class="text-wrapper">
<div class="title">
<h3><b>Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Proin eget tortor risu</b></h3>
</div>
<div class="text">
<p>Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Proin eget tortor risus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Pellentesque in ipsum id orci porta dapibus. Vestibulum ac diam sit amet quam vehicula
elementum sed sit amet dui.
suscipit tortor eget felis p
Sed porttitor lectus nibh.</p>
</div>
</div>
</div>

Try to remove floating from title and text and 'right' property from .title, then add 'float: left' to the picture.

Related

CSS for sophisticated two columns layout (text and images)

I have been trying to write CSS for responsive two columns layout (text and images), but I am having hard time with that. Layout looks like this:
two column layout
Requirements:
Content within columns is generated dynamically based on the data from database (so the height of the columns is dynamic)
Columns must have equal width and height (height depends on content)
Content within columns must be horizontally and vertically aligned
Layout must be responsive, so when screen size is changed columns and content must resize accordingly without loosing the ratio and layout of images. In mobile screen the second column goes below the first one (width of the column is 90% of the screen)
Content position in layout could change, so text could be on the right column and images on the left column (configured in CMS). Such change must not affect layout at all
There may be more similar sections on the page, so the distance between them must not be too long and the content of one section cannot cover any other section
It must be displayed correctly on IE11
I have been playing with it for a while and it does not seem to work correctly for me. I have tried to play with position absolute and relative. Unfortunately, the problem is always with images. When I change the screen size two smaller images do not hold orignal position (they move to the left or right). I am not sure if my approach is good or not, I do not even know if something like this is possible in CSS (well, I could write a lot of media rules for different screen sizes, but I am looking for nicer solutions), I have never seen this images layout before. I would be very keen to know what the best approach is.
I created some code snippet in order to demonstrate the issue:
.section {
margin: 0;
display: block;
width: 100%;
height: auto;
padding: 0;
text-align: center;
}
.content-left {
position: relative;
display: inline-block;
width: 45%;
vertical-align: middle;
text-align: center;
float: left;
}
.content-right {
position: relative;
display: inline-block;
width: 45%;
vertical-align: middle;
text-align: center;
float: right;
}
.first-image-wrapper {
position: relative;
z-index: 1;
height: 10rem;
width: 10rem;
text-align: center;
margin: auto;
vertical-align: middle;
right: -2rem;
top: 7rem;
}
.content-image-1 {
vertical-align: middle;
border-radius: 0.2rem;
width: inherit;
height: inherit;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.second-image-wrapper {
position: relative;
right: -1rem;
bottom: 7rem;
border-radius: 0.2rem;
height: 8.4rem;
width: 8.4rem;
z-index: 4;
}
.content-image-2 {
max-width: 100%;
height: auto;
display: inline-block;
vertical-align: middle;
}
.third-image-wrapper {
position: relative;
bottom: 18rem;
right: -10rem;
border-radius: 0.2rem;
height: 5rem;
width: 5rem;
background-size: cover;
z-index: 5;
}
.content-image-3 {
max-width: 100%;
height: auto;
display: inline-block;
vertical-align: middle;
}
#media (max-width: 500px) {
.content-left, .content-right {
width: 100%;
display: block;
}
}
<div class="section">
<div class="content-left">
<div class="content-text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa
</p>
</div>
</div>
<div class="content-right">
<div class="first-image-wrapper">
<img class="content-image-1" src="https://i.postimg.cc/BtB3G5dz/P1000003.jpg" alt="Image 1">
</div>
<div class="second-image-wrapper">
<img class="content-image-2 " src="https://i.postimg.cc/SnJb8BJ0/P1000064.jpg" alt="Image 2">
</div>
<div class="third-image-wrapper">
<img class="content-image-3" src="https://i.postimg.cc/ykfzD16X/P1000071.jpg" alt="Image 3">
</div>
</div>
</div>
Any help would be appreciated.
display:flex works for a genuine IE11 (I run one) :
You could do :
.section {
align-items: center;
display: flex;
width: 100%;
}
.section>div {
width: 50%;
}
.content-right {
margin: auto;
}
img {
display: block;
margin: auto;
}
img:nth-child(1) {
width: 20%;
margin-left: 70%;
margin-bottom: -2rem;
}
img:nth-child(2) {
width: 45%;
margin-left: 10%;
position:relative;
}
img:nth-child(3) {
width: 50%;
margin: -2rem 0 0 40%;
;
}
#media (max-width: 500px) {
.section {
display: block;
}
.section>div {
width: auto;
}
}
<div class="section">
<div class="content-left">
<div class="content-text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.
Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id
imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa
</p>
</div>
</div>
<div class="content-right">
<img class="content-image-1" src="https://i.postimg.cc/BtB3G5dz/P1000003.jpg" alt="Image 1">
<img class="content-image-2 " src="https://i.postimg.cc/SnJb8BJ0/P1000064.jpg" alt="Image 2">
<img class="content-image-3" src="https://i.postimg.cc/ykfzD16X/P1000071.jpg" alt="Image 3">
</div>
</div>
here is a jsbin that works and was made in IE11 https://jsbin.com/lemonopica/1/edit?html,css,output
If you don't fill okay with flex, you can use display:table and display:table-cell which allows vertical centering too , it is understood by every browsers. here is a table/table-cell demo https://jsbin.com/gecokurebe/1/edit?html,css,output

CSS - positioning images centrally within a container

I'm trying to line up some images within a container and can't seem to move them to make them central. This is how they currently look
I need to sit completely centrally and as is evident they are too far over to the right. I've tried lots of different things but can't get it right. I don't think I'm identifying the correct element.
Here's my code for the section on the photo
body {
margin: 0 auto 0 auto;
}
.container {
margin: auto;
max-width: 100%;
padding-left: 10px;
padding-right: 10px;
}
section#welcome {
height: 500px;
max-width: 100%;
}
section#welcome div.row {
height: 250px;
text-align: center;
position: relative;
}
#welcome h4 {
color: #000000;
font-size: 20px;
padding-top: 50px;
line-height: 5px;
}
section#welcome p {
font-size: 10px;
color: #bdc3c7;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
/* centering an image within a column */
section#welcome .four {
position: relative;
display: inline-block;
bottom: 50px;
}
.four h3 {
position: absolute;
color: #FFF;
font-size: 20px;
margin: 0;
top: 50%;
left: 55%;
transform: translate(-50%, -50%);
}
section#welcome img {
display: block;
margin-bottom: 30px;
}
.images,
.four {
margin-right: 100px;
}
<section id="welcome">
<div class="container">
<div class="row">
<div class="twelve columns">
<h4>WELCOME TO FEATURE MEDIA</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vel ex nisl. Vestibulum vitae ultricies nisl. Praesent sodales, leo at pellentesque pellentesque, nunc erat dapibus nunc, ut congue libero lorem in orci. Suspendisse potenti. Quisque
facilisis mauris in vestibulum tempor. Suspendisse nec venenatis nisi. Phasellus sodales viverra ante quis efficitur. Pellentesque quis orci mi. Phasellus tempus, sapien ut luctus pellentesque, lacus risus accumsan lorem, in porta urna tellus
ac nibh. Nunc varius elit non diam vehicula aliquet. In eget urna id orci molestie pulvinar. Integer quis risus eu erat iaculis aliquet ut at eros. Etiam feugiat, ante vel molestie finibus, lacus urna pharetra leo, ut lobortis massa lectus quis
lorem. Vestibulum rhoncus turpis sagittis sapien vulputate sagittis. Nunc ac velit sollicitudin, consequat arcu ac, tincidunt risus.</p>
</div>
</div>
<hr class="hrindeximages">
<div class="images row">
<div class="four columns">
<div id="video">
<h3>VIDEO</h3>
<img src="images/VIDEO.jpg" alt="Video" style="width:300px;height:150px;">
</div>
</div>
<div class="four columns">
<div id="blog">
<h3>BLOG</h3>
<img src="images/blog.jpg" alt="blog" style="width:300px;height:150px;">
</div>
</div>
<div class="four columns">
<div id="faq">
<h3>FAQ</h3>
<img src="images/faq.jpg" alt="FAQ" style="width:300px;height:150px;">
</div>
</div>
</div>
</div>
</section>
The issue is caused by the margin-right: 100px attribute in the .images, .four class. If you get rid of that, your images should be centered:
.images, .four {
margin-right: 100px; // this line causes your images to offset from center
}
If you still want to have that space in between your images, you can add a margin-left field to balance it out:
.images, .four {
margin-right: 50px;
margin-left: 50px;
}
Use the padding attribute to manually center the images.
This may be help you.
.images { text-align: center; width: 100%; }
Like #yelq said, get rid of the margin-right:100px.
And to make it even more flexible, I would use the display: flex property and use the margin short notation allowing you to change the separation between pictures in only one place.
I would remove the class row from <div class="images row"> since it is not necessary.
In your css, change it to:
.images, .four {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 0 50px;
/*margin-right: 100px;*/
}
I would also remove the styling on the img elements and add it to the css, again to allow easier development incase of changes.
.images img {
width:300px;
height:150px;
}

background color of parent matching height of child with negative top and bottom margins

Attached is an image of the desired affect I am going for. I am trying to do this with negative margins. I need the child element to expand past the parent element on the top and bottom. The issue is the background color of the parent element is matching the height of the child element. This is a responsive site and I will not be able to use absolute positioning of the child element as the height of the child (and therefore parent element) will be variable. Any help would be greatly appreciated
Following is my code...
body {
background-color:#DBD9DB;
}
#headerImage {
margin: 0;
}
#headerImage img {
display: block;
margin: 0 auto;
}
#parent {
background-color: #fff4d7;
}
#child {
background:none #FFF;
margin:-46px auto -38px;
position:relative;
text-align:center;
width:80%;
}
<body>
<div id="headerImage">
<img src="http://fpoimg.com/300x250?text=Preview" width="300" height="250" />
</div>
<div id="parent">
<div id="child">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum dapibus sapien a nulla finibus mattis id quis est. Proin malesuada magna vitae orci maximus, ac viverra felis iaculis. Curabitur lacinia commodo est et auctor. Suspendisse potenti. Nulla scelerisque metus et leo cursus, non auctor turpis fermentum.</p>
</div>
</div>
</body>
body {
background-color: #DBD9DB;
}
#parent {
background-color: #fff4d7;
}
#parent:before,
#parent:after {
content: " ";
display: block;
height: 50px;
background-color: blue;
}
#child {
background: none #FFF;
position: relative;
text-align: center;
width: 80%;
margin: -50px auto;
}
.parent {
width: 100%;
height: auto;
margin: 0 auto 0 auto;
background-color: #b5b5b5;
overflow: hidden;
position: relative;
}
img {
width: 100%;
height: auto;
}
.clearfix {
overflow: auto;
zoom: 1;
}
<body>
<div class="parent clearfix">
<div class="css-slideshow">
<div id="parent">
<img class="clearfix" src="http://unilaboralgirona.com/wp-content/uploads/2015/03/ZContact.jpg" />
<div id="child">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum dapibus sapien a nulla finibus mattis id quis est. Proin malesuada magna vitae orci maximus, ac viverra felis iaculis. Curabitur lacinia commodo est et auctor. Suspendisse potenti.
Nulla scelerisque metus et leo cursus, non auctor turpis fermentum.</p>
</div>
</div>
</div>
</div>
</body>
You can use :after and :before pseudo-elements in the #parent id to change the background-color and then use negative margin in #child.
Check this snippet out:
body {
background-color:#DBD9DB;
}
#headerImage {
margin: 0;
}
#headerImage img {
display: block;
margin: 0 auto;
}
#parent {
background-color: #fff4d7;
}
#parent:before, #parent:after{
content: " ";
display: block;
height: 50px;
background-color: blue;
}
#child {
background:none #FFF;
position:relative;
text-align:center;
width:80%;
margin: -50px auto;
}
<body>
<div id="headerImage">
<img src="http://fpoimg.com/300x250?text=Preview" width="300" height="250" />
</div>
<div id="parent">
<div id="child">
<h1>Title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum dapibus sapien a nulla finibus mattis id quis est. Proin malesuada magna vitae orci maximus, ac viverra felis iaculis. Curabitur lacinia commodo est et auctor. Suspendisse potenti. Nulla scelerisque metus et leo cursus, non auctor turpis fermentum.</p>
</div>
</div>
</body>

Html <p> tag expand dynamically to parent that is next to another div

How could to put the right box next to the left box (inline-block) with precise sizes? I can put it up, but the size will not be the same in different browsers. Then how could I put the left box near to the right box without that whitespace? Thank you.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: block;
position: relative;
}
.entry .img-cont,.body-cont {
vertical-align: top;
}
.entry .img-cont {
width: 50px;
display: inline-block;
}
.entry .body-cont {
width: 508px;
display: inline-block;
}
.entry .body-cont p {
display: table;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk"/>
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar, turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>
see this
http://jsfiddle.net/leandroparrar/6omjqefj
Here is an example of what the problem looks like:
Your answer is pretty close to what you need.
If you zero-our the margins on the p element in .body-cont, that will get rid of the extra whitespace that appears on top of the paragraph.
If you try to use inline-block's, it is easy to get some extra whitespace between elements due to carriage-returns (newlines) in the HTML file.
If you use display: table-cell on .img-cont and .body-cont, then the
two elements will rest side-by-side and you can control the horizontal spacing using left/right padding as needed.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: table;
}
.entry .img-cont, .entry .body-cont {
display: table-cell;
vertical-align: top;
}
.entry .img-cont img {
display: block;
}
.entry .body-cont p {
margin: 0;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk" />
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar,
turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>

CSS center site with floated elements and show full container background-color

I have simple site, with the classic elements: Container, Header, Content and Footer. The container has a background-color, which covers the whole content of the site (including header, content and footer). For some reason this won't work with floated elements within the container.
I have found a solution at StackOverflow, but it doesn't feel right. Solution is to set { display: table; } onto the container id.
The page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style>
body
{
background-color: #999;
font-family: Tahoma;
font-size: 12px;
font-style:normal;
font-variant:normal;
font-weight:normal;
margin: 0px;
padding: 0px;
}
h1
{
color: green;
font-family:Tahoma;
font-size: 26px;
font-weight: bold;
margin: 5px 0;
text-indent: 10px;
width: 100%;
}
#container
{
background: #FFF;
margin: 0 auto;
width: 950px;
position: relative;
}
#header
{
position: relative;
height: 100px;
width: 950px;
}
#content, #content-ext
{
float: left;
margin: 0;
width: 950px;
}
#nav
{
float: left;
padding: 10px 10px 10px 0;
width: 200px;
}
ul#menu
{
cursor: pointer;
display: block;
list-style: none outside none;
margin: 0 auto;
padding: 0;
text-align: left;
width: 200px;
}
ul#menu li
{
margin: 0;
padding: 0;
}
ul#menu li a
{
color: #111;
display: inline-block;
height: 50px;
font-size: 18px;
line-height: 60px;
margin: 0 auto;
padding: 0 0 0 10px;
text-decoration: none;
width: 100%;
}
#mainImg
{
background: #111;
height: 150px;
float: right;
margin: 0 0 20px 0;
width: 710px;
}
#main-content
{
float: right;
width: 710px;
}
#extra
{
float: left;
width: 500px;
}
#contact
{
float: left;
width: 450px;
}
#footer
{
color:#999;
height:20px;
width:950px;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>test</h1>
</div>
<div id="content">
<div id="nav">
<h1>Menu</h1>
<ul id="menu">
<li>Home</li>
<li>Contact</li>
</ul>
</div>
<div id="mainImg"></div>
<div id="main-content">
<h1>Welkom</h1>
<p><strong>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sed arcu arcu, a interdum metus. Aenean vel libero nulla. Nulla facilisi. Maecenas malesuada libero a ante vulputate vestibulum. Cras id neque vitae lectus luctus tempor non non risus. Morbi aliquam porttitor facilisis. Sed pulvinar erat sit amet est auctor tincidunt. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget arcu lorem, non accumsan ipsum. Donec venenatis adipiscing massa, sed molestie augue ullamcorper et.</strong>
<br/><br/>
Morbi id eros vitae risus tristique bibendum. Quisque nec metus sit amet nunc tincidunt vehicula sit amet non nibh. Nullam risus orci, porttitor ut malesuada vel, volutpat eget sapien. Proin tempus nunc sit amet ligula viverra hendrerit. Donec tempus tristique risus. Fusce at semper est. Etiam ligula est, varius ut tempus at, laoreet bibendum eros. In hac habitasse platea dictumst.</p>
</div>
</div>
<div id="content-ext">
<div id="extra">
<h1>Extra</h1>
<p>Quisque nec metus sit amet nunc tincidunt vehicula sit amet non nibh. Nullam risus orci, porttitor ut malesuada vel..</p>
</div>
<div id="contact">
<h1>Contact</h1>
<p>Quisque nec metus sit amet nunc tincidunt vehicula sit amet non nibh. Nullam risus orci, porttitor ut malesuada vel..</p>
</div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
Does any has a decent solution for using floated elements within a container? And showing the container as overall background?
Well, there is two classic solutions:
Set "overflow: hidden;" to #container. It will clear floats but have drawback — if you have elements with "position: absolute;" inside container that must be positioned partially outside it, they will be cut by overflow.
Use clearfix hack on #container: http://www.webtoolkit.info/css-clearfix.html it will fix it for you and don't have this drawbacks but is more complex.
Not sure is it better then "display: table;" or not, through.
Leave Display:table, it's not a good solution for a simple problem.
You are starting with a simple layout, and it's a good practice to start with a clean and tested layout.
My favorite resources are:
Little boxes
Free-css
code sucks
good luck