I have this table-style DIV code for a used vehicle sales platform:
.mainwrapper {
border: 2px solid;
display: table;
}
.itemwrapper {
display: table-row;
width: 706px;
}
.mainwrapper {
margin-bottom: 20px;
}
.item {
width: 700px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
.item:nth-child(2) {
float: left;
margin: -2px;
}
.item1 {
display: table-cell;
text-align: left;
margin-left: -30px;
}
.item1 p {
margin-top: -30px;
}
.item-price {
width: 300px;
background-color: blue;
padding: 1em;
color: white;
text-align: center;
}
.picture, .item {
display: table-cell;
border: 1px solid;
}
.picture {
width: 90px;
margin: 1px;
border: 2px solid;
}
.picture img {
height: 185px;
}
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">1992 ELDDIS PAMPEROS XLi</div>
<div class="item-price">£1,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>2 berth, good condition</p></div>
</div>
</div>
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">2008 SWIFT CHALLENGER 540</div>
<div class="item-price">£13,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>4 berth end bedroom</p></div>
</div>
</div>
What I am trying to do is ensure the class item1 is opposite the image, with the text like this if you didn't have the £ per month div and list as table:
Basically, what I am trying to fix is the text that's in class item1 opposite the image (not with all the description or colored DIV there); see the image below.
I tried margin-left and margin-top, but it won't quite put the image opposite.
This is the result of my code:
I can't quite get it to work as I'd expected, text opposite image and size of DIV in the CSS; if anyone can help, I'd much welcome this.
It works OK - no major coding errors, but isn't quite esthetically working out, and that's the basic problem.
I'm trying this as basic HTML first before attempting anything with Javascript, just to ensure it works as a standalone design.
Edit: I tried vertical-align for text, that worked, but it's fixing the gap between image div and text that's the issue. There's a large amount of space I don't know how to fix.
As the answer for the text is solved. You can change the column width by changing the css property of item. you can do it as follows. The width was 700px in your code you can reduce to get a smaller width. I changed it to 400px.
.item {
width: 400px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
Related
I'm currently making a personal webpage using simple HTML and CSS codes. I'm currently stuck in a problem in the CSS part where I have to make 3 pictures appear in their 3 respective columns. It worked earlier before I added the div class intro. I did try removing it, undid changes, but it didn't go back to how it used to go. I'm actually self-taught and haven't been taking proper classes, so I may not understand too complicated explanation and I'm really sorry for that. Here's how my code looks.
HTML:
<div class="me">
<div class="smallcontainer">
<div class="row">
<div class="col-3">
<img src ="me/me.jpg"></img>
</div>
<div class="col-3">
<img src ="me/me3.jpg"></img>
</div>
<div class="col-3">
<img src ="me/me4.jpg"></img>
</div>
</div>
</div>
<div class="intro">
<h1>My name's Shinji</h1>
</div>
</div>
CSS:
.me {
background-color: black;
padding: 50px 50px;
}
.row {
display: flex;
}
.col-3 {
flex-basis:30%;
max-width: 250px;
margin-bottom: 30px;
}
.smallcontainer {
margin: auto;
max-width: 1080px;
margin-left:100px;
margin-right: 100px;
}
.smallcontainer img{
width: 250px;
margin-left: 20px;
}
.intro {
color:white;
font-size: 100px;
}
Here's an image on how it looked liked before it stopped working (without the black background)
Here's how it looked life AFTER. It seems like .smallcontainer img is targeting the img properly, but when I tell it to margin-left, it literally gives a margin only to the left-most picture, and not the other images. I've been frustrated about this for hours and I don't know what's going on.
.me {
background-color: black;
padding: 50px 50px;
}
.row {
display: flex;
}
.col-3 {
flex-basis:30%;
max-width: 250px;
margin-bottom: 30px;
margin: 20px; /* add this line */
}
.smallcontainer {
margin: auto;
max-width: 1080px;
margin-left:100px;
margin-right: 100px;
}
.smallcontainer img{
width: 250px;
margin-left: 20px;
}
.intro {
color:white;
font-size: 100px;
}
try this css
So I'm questioning myself why the div with the class champ_info isn't placed next to the image because the image is an inline-block element. So the Text in my div element lies under the image instead of next to the image. My code is below.
.champ_info {
background: #0b2e33;
color: white;
}
.champ_container {
background: #10474e;
}
.champ_img {
border: 3px solid #1ba9bd;
border-radius: 50%;
margin: 5px;
height: 5rem;
width: auto;
}
<div class="champ_container">
<img class="champ_img" src="https://ddragon.leagueoflegends.com/cdn/9.5.1/img/champion/Pyke.png">
<div class="champ_info">
Some Text
</div>
</div>
Thank you in advance.
I personally find making inherently block level elements inline counter intuitive. Flex box is the perfect solution to your problem.
.champ_container {
width: 40%;
margin: 0 auto;
display: flex;
/* justify-content: center; */
align-items: center;
background: #10474e;
}
.champ_info {
background: #0b2e33;
color: white;
width: 100%;
height: 100%;
}
.champ_img {
border: 3px solid #1ba9bd;
border-radius: 50%;
margin: 5px;
height: 5rem;
width: auto;
}
<div class="champ_container">
<img class="champ_img" src="https://ddragon.leagueoflegends.com/cdn/9.5.1/img/champion/Pyke.png">
<div class="champ_info">
Some Text
</div>
</div>
<div> is a block element, which means it takes up the whole line. Put display: inline; inside the css for the <div> and it places it next to the image like you wanted.
Add vertical-align: top; if you want the text to align to the top. Since the image and the text align to the bottom of the parent, you need to manually set them to align to the top.
.champ_info {
background: #0b2e33;
color: white;
display: inline;
vertical-align: top;
}
.champ_container {
background: #10474e;
}
.champ_img {
border: 3px solid #1ba9bd;
border-radius: 50%;
margin: 5px;
height: 5rem;
width: auto;
}
<div class="champ_container">
<img class="champ_img" src="https://ddragon.leagueoflegends.com/cdn/9.5.1/img/champion/Pyke.png">
<div class="champ_info">
Some Text
</div>
</div>
I'm having some issues with styling the top bar for a website. I want all of the anchor tags to be equally distributed throughout the entirety of the screen. There are four anchor tags, so I thought that by making the width of each tag 25%, they would each take up a quarter of the block. They should theoretically all be in one line, but the very last one gets moved down. I have no clue what's causing this to happen and hope I'll be able to get some help. Thank you!
html code:
<div class="navbar">
Home
Lessons
Contact
Login
</div>
CSS code:
* {
margin-left:0;
margin-right:0;
margin-bottom:0;
margin-top:0;
}
html, body {
height:100%;
width:100%;
}
.navbar {
background-color: #555;
overflow: auto;
}
.navbar a {
float: left;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
width: 25%; /* Four equal-width links. If you have two links, use 50%, and 33.33% for three links, etc.. */
text-align: center; /* If you want the text to be centered */
}
It is the padding defined in .navbar a that causes the offset. You can add a box-sizing: border-box to the rule to avoid this effect (content will shrink instead of container expanding to fit the padding). Or, better i think, use flexbox :
.navbar {
background-color: #555;
overflow: auto;
display: flex
}
.navbar a {
flex: 1;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
text-align: center; /* If you want the text to be centered */
}
The display: flex will initialize flexbox on navbar container and the flex: 1 in anchor will tell browser to give equal width to all elements.
A quick fiddle to explain (case 1 & 2 can be easily fixed with box-sizing) :
.container {
width: 200px;
border: 1px solid #000;
}
.item-no-flex {
width: 25%;
padding: 3px;
border: 1px dashed #000;
float: left;
}
.item-ib {
width: 25%;
padding: 3px;
border: 1px dashed #000;
display: inline-block
}
.container-flex {
width: 200px;
display: flex;
border: 1px solid #000
}
.item-flex {
width: 25%;
padding: 3px;
flex: 1;
border: 1px dashed #000;
}
<p>Float</p>
<div class="container">
<div class="item-no-flex">1</div>
<div class="item-no-flex">2</div>
<div class="item-no-flex">3</div>
<div class="item-no-flex">4</div>
</div>
<p style="clear:both">Inline-block</p>
<div class="container">
<div class="item-ib">1</div>
<div class="item-ib">2</div>
<div class="item-ib">3</div>
<div class="item-ib">4</div>
</div>
<p style="clear:both">Flexbox</p>
<div class="container-flex">
<div class="item-flex">1</div>
<div class="item-flex">2</div>
<div class="item-flex">3</div>
<div class="item-flex">4</div>
</div>
Try simplifying your code and using flexbox.
HTML:
<div class="navbar">
Home
Lessons
Contact
Login
</div>
CSS:
.navbar {
display: flex;
justify-content: space-around;
}
This should distribute the links equally across the page with space around each link, regardless of screen width.
First of all, thank you for taking your time answering my maybe stupid question. I have a problem with my span not wanting to stay where mummy told him to stay.
HTML
<section class="row" id="white_background">
<div class='col-lg-6 col-md-6 col-sm-6 col-xs-12'>
<article class="news_post">
<span class='news_head'>
<h4>BLA BLA BLA BLA BLA BLA BLA</h4>
<p><?php echo $NewsItem->getValueEncoded( "summary" ) ?></p>
<p><?php echo $NewsItem->getValueEncoded( "uploadDate" ) ?></p>
</span>
</article>
</div>
</section>
Sass
#white_background {
background: $light;
height:100%;
padding: 20px;
text-align: center;
#inlcude clearfix;
h2 {
text-align: center;
color: $purple;
}
}
.news_post {
#include article_back (
'../img/picture_2.png');
padding: 20px;
margin-top: 30px;
margin-bottom: 50px;
}
.news_head {
max-width: 330px;
background-color: $purple;
padding: 6px 12px;
display: block;
color: $white;
margin: auto 50px auto 0px;
transform: translateY(120px);
text-align: left;
font-size: 12px;
h4 {
font-family: $heading;
font-size:13px;
}
}
Now it does this:
http://imgur.com/a/HQ46N
So it seems like the span does not see the div as a boundary.. any ideas?^^
EDIT: Sorry for making a slight mess here.
What I want to achieve is make the article-span overflow the article in a controlled way. Meaning: If I manually resize the window, the span should not go outside of the parent article.
The problem is that the sections I used, do not render the movement of the span properly and that is why the sections overlaps, like shown here: http://imgur.com/a/fZyux (Note: the left item in the purple box overflows the light grey edges of the article. It should stay inside this grey box.
The dealbreaker now is, when i resize the window to smaller window sizes, the span starts to move down, but i expected it to move to the edge of it's parten article or div.
The span does not get recognize that it sits in a div and does not get restricted by its boundaries. Why is that the case? It's super hard to explain but I tried.
Take a look at the full mockup to get an idea. http://imgur.com/a/5ZQn9
I hope this makes it more clear what i want to achieve^^
Use display flex;.
article {
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid red;
}
article span {
width: 30px;
height: 30px;
border: 1px solid green;
}
<article>
<span></span>
</article>
The sections overlap because using:
transform: translateY(120px);
makes the element behave like it was:
position: relative;
top: 120px;
The element has moved on the screen, but the rest of the layout renders as if it was still in the original place. To push the borders you can add margin-bottom to your span.
.container {
background-color: yellow;
height: auto;
}
.element-transformed {
display: block;
width: 50%;
height: 100px;
background-color: red;
transform: translateY(70px);
}
.element-positioned {
display: block;
width: 50%;
height: 100px;
background-color: red;
position: relative;
top: 70px;
}
.make-some-space {
height: 100px;
}
.element-transformed-with-margin {
display: block;
width: 50%;
height: 100px;
background-color: red;
transform: translateY(70px);
margin-bottom: 70px;
}
<div class="container">
<span class="element-transformed">
<p>transform: translateY(70px);</p>
</span>
</div>
<div class="container">
<span class="element-positioned">
<p>position: relative; top: 70px;</p>
</span>
</div>
<div class="make-some-space"></div>
<div class="container">
<span class="element-transformed-with-margin">
<p>transform: translateY(70px); margin-bottom: 70px;</p>
</span>
</div>
<div class="container">
<span class="element-positioned">
<p>position: relative; top: 70px;</p>
</span>
</div>
On to the real question. I started learning HTML and I ran into a circumstance that I don't understand why it happens and was hoping someone could explain this to me.
Above is my code and what I don't get is why the introduction of the word text in box1 causes the whole box to be displayed on a new line.
Once the text is removed it realigns. any help is welcome.
I'm going to end this off by saying that I tried pasting in code (using the code blocks button and surrounding my code with <code> and <pre> tags but the editor would not display the code or would break the code block by every second)
Try vertical-align:top; with inline-block.
When we use inline-block we can align blocks in 3 different way's, like if 2 block have large height and one has small then using vertical-align:top; make all three block aligned at top using vertical-align:middle; make these three block aligned middle and using vertical-align: bottom; make these blocks aligned at the bottom side
div {
display: inline-block;
vertical-align:top;
margin: 25px;
}
p {
padding: 10px;
text-align: center;
}
#box1 {
width: 100px;
height: 100px;
background-color: red;
}
#box2 {
width: 100px;
height: 100px;
background-color: blue;
}
#box3 {
width: 100px;
height: 100px;
background-color: yellow;
}
<div id="container">
<div id="box1"><p>text</p></div>
<div id="box2"></div>
<div id="box3"></div>
</div>
It is because you have a pixel value for the width and height of all 3 of your divs. The text "increases" the width and pixel value of your divs.
UPDATE
Why not use the float property?
div {
float: left;
display: block;
margin: 25px;
}
p {
padding: 10px;
text-align: center;
}
#box1 {
width: 100px;
height: 100px;
background-color: red;
}
#box2 {
width: 100px;
height: 100px;
background-color: blue;
}
#box3 {
width: 100px;
height: 100px;
background-color: yellow;
}
<div id="container">
<div id="box1"><p>text</p></div>
<div id="box2"></div>
<div id="box3"></div>
</div>