如何使图像居中并以整个宽度显示

时间:2015-09-20 11:48:36

标签: html css

昨天我发布了一个问题,有些人回复得非常好。但是我已经尝试了你所说的一切,似乎没有任何工作。

如果我给图像一个宽度,但只有当宽度小于实际的一个(500px)时,它才有用。我的形象比那个大。我试图改变图像的宽度(在图像中添加一个类,然后在css中指定图像的宽度,仍然无法正常工作,甚至在分号前写作!重要)

我要粘贴整个""网站" (这是一个简单的单页简历)代码,这样你就可以更清楚地看到可能出错的地方。

我希望图像居中并保持原始尺寸:

<!DOCTYPE html>

<html>

<head>

<title>My Resume</title>

<link href="http://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Playfair+Display:900" rel="stylesheet" type="text/css">

<style type="text/css">

body, h1, h2, h3, p { 
margin: 0; 
}

header {
padding-top: 120px;
padding-bottom: 120px;
background-image: url('http://bit.ly/1FV66V8');
background-size: cover;
font-family: 'Playfair Display';
text-align: center;
color: #DB5175;
}


h1 {
font-size: 72px;
}


h2 {
margin-bottom: 40px;
font-size: 24px;
}


h3 {
margin-top: 70px;
margin-bottom: 70px;

border: 2px solid #ffffff;
display: inline-block;
padding-right: 30px;
padding-left: 30px;
padding-top: 15px;
padding-bottom: 15px;

text-transform: uppercase;
letter-spacing: 2px;
}


main {
padding-bottom: 100px;
background-color: #DB5175;
font-family: 'Roboto';
color: white;
text-align: center;
}


p {
padding-bottom: 20px;
width: 500px;

margin-left: auto;
margin-right: auto;

line-height: 160%;
}


</style>

</head>


<body>

<header>
<h1>Name Surname</h1>
<h2>Blogger</h2>

<p>
<a href="http://facebook.com"><img src="https://lh4.googleusercontent.com/-AhzOzjiqgqg/UMIP9ONtHsI/AAAAAAAACzM/GzwcSFumocs/s65/facebook.png" alt="Facebook Icon" /></a>
<a href="http://twitter.com"><img src="https://lh5.googleusercontent.com/-E4DPgG0jOhk/UMIP81ijQ1I/AAAAAAAACzI/5jFodl9F9N4/s65/twitter_bird.png" alt="Twitter Icon" /></a>
<a href="https://instagram.com"><img src="https://lh6.googleusercontent.com/-mswXpGilY98/Ugp9MeFO_pI/AAAAAAAAER0/nhReers_OJg/s64/instagram.png" alt="Instagram Icon" /></a>
<a href="http://pinterest.com"><img src="https://lh5.googleusercontent.com/-DMXeFEjNoV8/UMIP9F6SnJI/AAAAAAAACzQ/itYWZIxvOuw/s65/pintrest.png" alt="Pinterest Icon" /></a>
</p>
</header>

<main>
<h3>Background</h3>

<p>I've been rolling solo since 2014, but previously jammed with a bunch of tech startups like Dropbox, Codecademy, and Treehouse. My recent work is a departure from my product-centric past, focusing on 3D illustration, animation, and motion design.</p>

<p><img src="http://los40tuxtla.com/wp-content/uploads/2015/05/nrm_1410437342-blake-lively-gucci-hp.jpg" alt="Foto Blanca"/></p>

<p>That's kind of what it's all about, y'know? Feeling out our path, taking creative risks, and knocking it out of the park without taking it too seriously. I get into specific tactics and proven strategies, but it's also an ongoing conversation about growth, meaning, and happiness.</p>

<p>I've met lots of creative and curious people through my newsletter, where we talk shop and share experiences. I'd love to meet you, too!</p>

<h3>Philosophy</h3>

<p>I'm a lifelong learner and love to gather new skills and study extraordinary people. I believe 1) being exceptional is often just putting in more effort than anyone expects, 2) releasing our ego is a prerequisite for growth, and 3) life is too important to take seriously. Party on!</p>
</main>

</body>

</html>

谢谢!

3 个答案:

答案 0 :(得分:0)

只需在图像中设置100%的宽度即可。它将自动适应父宽度。

&#13;
&#13;
body, h1, h2, h3, p { 
margin: 0; 
}

header {
padding-top: 120px;
padding-bottom: 120px;
background-image: url('http://bit.ly/1FV66V8');
background-size: cover;
font-family: 'Playfair Display';
text-align: center;
color: #DB5175;
}


h1 {
font-size: 72px;
}


h2 {
margin-bottom: 40px;
font-size: 24px;
}


h3 {
margin-top: 70px;
margin-bottom: 70px;

border: 2px solid #ffffff;
display: inline-block;
padding-right: 30px;
padding-left: 30px;
padding-top: 15px;
padding-bottom: 15px;

text-transform: uppercase;
letter-spacing: 2px;
}


main {
padding-bottom: 100px;
background-color: #DB5175;
font-family: 'Roboto';
color: white;
text-align: center;
}


p {
padding-bottom: 20px;
width: 500px;

margin-left: auto;
margin-right: auto;

line-height: 160%;
}
&#13;
<header>
<h1>Name Surname</h1>
<h2>Blogger</h2>

<p>
<a href="http://facebook.com"><img src="https://lh4.googleusercontent.com/-AhzOzjiqgqg/UMIP9ONtHsI/AAAAAAAACzM/GzwcSFumocs/s65/facebook.png" alt="Facebook Icon" /></a>
<a href="http://twitter.com"><img src="https://lh5.googleusercontent.com/-E4DPgG0jOhk/UMIP81ijQ1I/AAAAAAAACzI/5jFodl9F9N4/s65/twitter_bird.png" alt="Twitter Icon" /></a>
<a href="https://instagram.com"><img src="https://lh6.googleusercontent.com/-mswXpGilY98/Ugp9MeFO_pI/AAAAAAAAER0/nhReers_OJg/s64/instagram.png" alt="Instagram Icon" /></a>
<a href="http://pinterest.com"><img src="https://lh5.googleusercontent.com/-DMXeFEjNoV8/UMIP9F6SnJI/AAAAAAAACzQ/itYWZIxvOuw/s65/pintrest.png" alt="Pinterest Icon" /></a>
</p>
</header>

<main>
<h3>Background</h3>

<p>I've been rolling solo since 2014, but previously jammed with a bunch of tech startups like Dropbox, Codecademy, and Treehouse. My recent work is a departure from my product-centric past, focusing on 3D illustration, animation, and motion design.</p>

<p><img src="http://los40tuxtla.com/wp-content/uploads/2015/05/nrm_1410437342-blake-lively-gucci-hp.jpg" width="100%" alt="Foto Blanca"/></p>

<p>That's kind of what it's all about, y'know? Feeling out our path, taking creative risks, and knocking it out of the park without taking it too seriously. I get into specific tactics and proven strategies, but it's also an ongoing conversation about growth, meaning, and happiness.</p>

<p>I've met lots of creative and curious people through my newsletter, where we talk shop and share experiences. I'd love to meet you, too!</p>

<h3>Philosophy</h3>

<p>I'm a lifelong learner and love to gather new skills and study extraordinary people. I believe 1) being exceptional is often just putting in more effort than anyone expects, 2) releasing our ego is a prerequisite for growth, and 3) life is too important to take seriously. Party on!</p>
</main>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

感谢所有人。

我想回答一下我最后做了什么,以防将来帮助别人。

老师/班级说&#34;包装&#34; p中的图像跟随@Fabian Lurtz的adice改变了p为div。

然后在css中,关注@Alvin Pascoe的建议:http://bit.ly/1OniMMl

我补充说:

.div {
display: block;
margin-left: auto;
margin-right: auto;
}

如果我的网站&#34;会有更多的div我可以设置一个类或id

希望它有所帮助。再次感谢所有人!

答案 2 :(得分:0)

如果您想将宽度设置为100%,只需将宽度设置为 - max-width: 100%